Skip to content

Commit

Permalink
make conversion from JTS Polygon to GH public and use .create
Browse files Browse the repository at this point in the history
  • Loading branch information
karussell committed Feb 3, 2018
1 parent 2475844 commit 144da90
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
Expand Up @@ -72,6 +72,19 @@ public Polygon(double[] lat, double[] lon) {
epsilon = (maxLat - minLat) / 10; epsilon = (maxLat - minLat) / 10;
} }


/**
* Lossy conversion to a GraphHopper Polygon.
*/
public static Polygon create(com.vividsolutions.jts.geom.Polygon polygon) {
double[] lats = new double[polygon.getNumPoints()];
double[] lons = new double[polygon.getNumPoints()];
for (int i = 0; i < polygon.getNumPoints(); i++) {
lats[i] = polygon.getCoordinates()[i].y;
lons[i] = polygon.getCoordinates()[i].x;
}
return new Polygon(lats, lons);
}

/** /**
* Wrapper method for {@link Polygon#contains(double, double)}. * Wrapper method for {@link Polygon#contains(double, double)}.
*/ */
Expand Down
Expand Up @@ -51,7 +51,7 @@ public static SpatialRuleLookup buildIndex(JsonFeatureCollection jsonFeatureColl
for (int i = 0; i < jsonFeature.getGeometry().getNumGeometries(); i++) { for (int i = 0; i < jsonFeature.getGeometry().getNumGeometries(); i++) {
Geometry poly = jsonFeature.getGeometry().getGeometryN(i); Geometry poly = jsonFeature.getGeometry().getGeometryN(i);
if (poly instanceof com.vividsolutions.jts.geom.Polygon) if (poly instanceof com.vividsolutions.jts.geom.Polygon)
borders.add(ghPolygonFromJTS((com.vividsolutions.jts.geom.Polygon) poly)); borders.add(Polygon.create((com.vividsolutions.jts.geom.Polygon) poly));
else else
throw new IllegalArgumentException("Geometry for " + id + " (" + i + ") not supported " + poly.getClass().getSimpleName()); throw new IllegalArgumentException("Geometry for " + id + " (" + i + ") not supported " + poly.getClass().getSimpleName());
} }
Expand Down Expand Up @@ -87,15 +87,4 @@ public static SpatialRuleLookup buildIndex(JsonFeatureCollection jsonFeatureColl
public static SpatialRuleLookup buildIndex(JsonFeatureCollection jsonFeatureCollection, String jsonIdField, SpatialRuleFactory spatialRuleFactory) { public static SpatialRuleLookup buildIndex(JsonFeatureCollection jsonFeatureCollection, String jsonIdField, SpatialRuleFactory spatialRuleFactory) {
return buildIndex(jsonFeatureCollection, jsonIdField, spatialRuleFactory, .1, new BBox(-180, 180, -90, 90)); return buildIndex(jsonFeatureCollection, jsonIdField, spatialRuleFactory, .1, new BBox(-180, 180, -90, 90));
} }

private static Polygon ghPolygonFromJTS(com.vividsolutions.jts.geom.Polygon polygon) {
double[] lats = new double[polygon.getNumPoints()];
double[] lons = new double[polygon.getNumPoints()];
for (int i = 0; i < polygon.getNumPoints(); i++) {
lats[i] = polygon.getCoordinates()[i].y;
lons[i] = polygon.getCoordinates()[i].x;
}
return new Polygon(lats, lons);
}

} }
Expand Up @@ -103,7 +103,7 @@ protected boolean checkAdjacent(EdgeIteratorState edge) {
*/ */
public void fillEdgeIDs(GHIntHashSet edgeIds, Geometry geometry, EdgeFilter filter) { public void fillEdgeIDs(GHIntHashSet edgeIds, Geometry geometry, EdgeFilter filter) {
if (geometry instanceof Point) { if (geometry instanceof Point) {
GHPoint point = GHPoint.from((Point) geometry); GHPoint point = GHPoint.create((Point) geometry);
findClosestEdgeToPoint(edgeIds, point, filter); findClosestEdgeToPoint(edgeIds, point, filter);
} else if (geometry instanceof LineString) { } else if (geometry instanceof LineString) {
PointList pl = PointList.from((LineString) geometry); PointList pl = PointList.from((LineString) geometry);
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/com/graphhopper/util/shapes/GHPoint.java
Expand Up @@ -35,6 +35,10 @@ public GHPoint(double lat, double lon) {
this.lon = lon; this.lon = lon;
} }


public static GHPoint create(Point point) {
return new GHPoint(point.getY(), point.getX());
}

public static GHPoint parse(String str) { public static GHPoint parse(String str) {
return parse(str, false); return parse(str, false);
} }
Expand Down Expand Up @@ -100,8 +104,4 @@ public String toString() {
public Double[] toGeoJson() { public Double[] toGeoJson() {
return new Double[]{lon, lat}; return new Double[]{lon, lat};
} }

public static GHPoint from(Point point) {
return new GHPoint(point.getY(), point.getX());
}
} }

0 comments on commit 144da90

Please sign in to comment.