MATSIM
GeometryUtils.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * GeometryUtils.java
5  * *
6  * *********************************************************************** *
7  * *
8  * copyright : (C) 2019 by the members listed in the COPYING, *
9  * LICENSE and WARRANTY file. *
10  * email : info at matsim dot org *
11  * *
12  * *********************************************************************** *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * See also COPYING, LICENSE and WARRANTY file *
19  * *
20  * *********************************************************************** */
21 
22 package org.matsim.core.utils.geometry;
23 
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.Random;
30 
31 import org.geotools.api.feature.simple.SimpleFeature;
32 import org.geotools.geometry.jts.GeometryBuilder;
33 import org.locationtech.jts.geom.Coordinate;
34 import org.locationtech.jts.geom.Geometry;
35 import org.locationtech.jts.geom.GeometryFactory;
36 import org.locationtech.jts.geom.LineString;
37 import org.locationtech.jts.geom.Point;
38 import org.locationtech.jts.geom.Polygon;
39 import org.matsim.api.core.v01.Coord;
42 import org.matsim.core.gbl.Gbl;
44 
49 public class GeometryUtils {
50 
51  private static final GeometryFactory GEOMETRY_FACTORY = new GeometryFactory();
52 
53  private GeometryUtils() {} // do not instantiate
54 
63  public static List<Link> findIntersectingLinks( Link link, final Network network ) {
64  LineString segment = GeometryUtils.createGeotoolsLineString(link) ;
65  return GeometryUtils.findIntersectingLinks(segment, network) ;
66  }
67 
77  public static List<Link> findIntersectingLinks(LineString lineString, final Network network) {
78  // yy One could probably improve this method by using the (already existing) link quadtree to look only at
79  // those links that are in the bounding box. kai, oct'17
80 
81  // convert matsim links into geotools line strings:
82  Map<Link,LineString> segments = new LinkedHashMap<>() ;
83  for ( Link link : network.getLinks().values() ) {
84  LineString theSegment = GeometryUtils.createGeotoolsLineString(link);
85  segments.put( link, theSegment ) ;
86  }
87 
88  // find the intersecting segments:
89  List<Link> resultList = new ArrayList<>();
90  for ( Entry<Link, LineString> entry : segments.entrySet() ) {
91  if( lineString.intersects( entry.getValue() ) ){
92  resultList.add( entry.getKey() ) ;
93  }
94  }
95  return resultList;
96  }
97 
104  public static LineString createGeotoolsLineString(Link link) {
105  Coordinate fromCoord = MGC.coord2Coordinate( link.getFromNode().getCoord() ) ;
106  Coordinate toCoord = MGC.coord2Coordinate( link.getToNode().getCoord() ) ;
107  LineString theSegment = GEOMETRY_FACTORY.createLineString(new Coordinate[] { fromCoord, toCoord });
108  return theSegment;
109  }
110 
111  public static Point createGeotoolsPoint(Coord coord ) {
112  Coordinate coordinate = MGC.coord2Coordinate(coord) ;
113  Point point = GEOMETRY_FACTORY.createPoint(coordinate);
114  return point ;
115  }
116 
117  public static Polygon createGeotoolsPolygon(List<Coord> coords ) {
118 
119  // better way to do this is welcome. kai, dec'17
120  double [] flatArray = new double[coords.size()*2] ;
121  int ii=0 ;
122  for ( Coord coord : coords ) {
123  flatArray[ii] = coord.getX() ;
124  ii++ ;
125  flatArray[ii] = coord.getY() ;
126  ii++ ;
127  }
128  return new GeometryBuilder().polygon( flatArray ) ;
129 
130  // the following yields some failing tests in the minibus contrib. ihab, feb'19
131 
132 // Coordinate[] coordinates = new Coordinate[coords.size()] ;
133 // int ii=0 ;
134 // for ( Coord coord : coords ) {
135 // coordinates[ii] = new Coordinate(coord.getX(), coord.getY()); ;
136 // ii++ ;
137 // }
138 // return new GeometryFactory().createPolygon(coordinates);
139 
140  }
141 
142  public static Point getRandomPointInFeature( Random rnd, SimpleFeature ft ) {
143  Gbl.assertNotNull(ft );
144  Point p = null;
145  double x, y;
146  // generate a random point until a point inside the feature geometry is found
147  do {
148  x = ft.getBounds().getMinX() + rnd.nextDouble() * (ft.getBounds().getMaxX() - ft.getBounds().getMinX());
149  y = ft.getBounds().getMinY() + rnd.nextDouble() * (ft.getBounds().getMaxY() - ft.getBounds().getMinY());
150  p = MGC.xy2Point(x, y);
151  } while ( ! (((Geometry) ft.getDefaultGeometry()).contains(p)) );
152  return p;
153  }
154 
155 
156 }
static Polygon createGeotoolsPolygon(List< Coord > coords)
static Point xy2Point(final double x, final double y)
Definition: MGC.java:158
static final GeometryFactory GEOMETRY_FACTORY
static void assertNotNull(Object obj)
Definition: Gbl.java:212
Map< Id< Link >, ? extends Link > getLinks()
static LineString createGeotoolsLineString(Link link)
static List< Link > findIntersectingLinks(LineString lineString, final Network network)
static Point getRandomPointInFeature(Random rnd, SimpleFeature ft)
static List< Link > findIntersectingLinks(Link link, final Network network)
static Coordinate coord2Coordinate(final Coord coord)
Definition: MGC.java:113