MATSIM
ShpGeometryUtils.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2019 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.utils.gis.shp2matsim;
21 
22 import java.net.URL;
23 import java.util.List;
24 import java.util.stream.Collectors;
25 
26 import org.locationtech.jts.geom.Geometry;
27 import org.locationtech.jts.geom.Point;
28 import org.locationtech.jts.geom.Polygonal;
29 import org.locationtech.jts.geom.prep.PreparedGeometry;
30 import org.locationtech.jts.geom.prep.PreparedGeometryFactory;
31 import org.locationtech.jts.geom.prep.PreparedPolygon;
32 import org.matsim.api.core.v01.Coord;
35 
36 public class ShpGeometryUtils {
37  public static List<Geometry> loadGeometries(URL url) {
38  return GeoFileReader.getAllFeatures(url)
39  .stream()
40  .map(sf -> (Geometry)sf.getDefaultGeometry())
41  .collect(Collectors.toList());
42  }
43 
44  public static List<PreparedGeometry> loadPreparedGeometries(URL url) {
45  PreparedGeometryFactory factory = new PreparedGeometryFactory();
46  return GeoFileReader.getAllFeatures(url)
47  .stream()
48  .map(sf -> factory.create((Geometry)sf.getDefaultGeometry()))
49  .collect(Collectors.toList());
50  }
51 
52  public static List<PreparedPolygon> loadPreparedPolygons(URL url) {
53  return GeoFileReader.getAllFeatures(url)
54  .stream()
55  .map(sf -> new PreparedPolygon((Polygonal)sf.getDefaultGeometry()))
56  .collect(Collectors.toList());
57  }
58 
59  public static boolean isCoordInGeometries(Coord coord, List<Geometry> geometries) {
60  Point point = MGC.coord2Point(coord);
61  return geometries.stream().anyMatch(g -> g.contains(point));
62  }
63 
64  public static boolean isCoordInPreparedGeometries(Coord coord, List<PreparedGeometry> geometries) {
65  Point point = MGC.coord2Point(coord);
66  return geometries.stream().anyMatch(g -> g.contains(point));
67  }
68 }
static List< PreparedGeometry > loadPreparedGeometries(URL url)
static boolean isCoordInPreparedGeometries(Coord coord, List< PreparedGeometry > geometries)
static Point coord2Point(final Coord coord)
Definition: MGC.java:131
static boolean isCoordInGeometries(Coord coord, List< Geometry > geometries)
static Collection< SimpleFeature > getAllFeatures(final String filename)
static List< PreparedPolygon > loadPreparedPolygons(URL url)
static List< Geometry > loadGeometries(URL url)