Skip to content

Commit

Permalink
pick the isochrone component that contains the origin, fixes #1686
Browse files Browse the repository at this point in the history
  • Loading branch information
michaz committed Oct 11, 2019
1 parent 6f0ef2a commit 87a9e03
Showing 1 changed file with 19 additions and 18 deletions.
Expand Up @@ -15,10 +15,7 @@
import com.graphhopper.storage.index.QueryResult;
import com.graphhopper.util.StopWatch;
import com.graphhopper.util.shapes.GHPoint;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.triangulate.ConformingDelaunayTriangulator;
import org.locationtech.jts.triangulate.ConstraintVertex;
import org.locationtech.jts.triangulate.quadedge.QuadEdgeSubdivision;
Expand Down Expand Up @@ -137,20 +134,8 @@ public Response doGet(
ContourBuilder contourBuilder = new ContourBuilder(tin);
for (int i = 0; i < buckets.size() - 1; i++) {
MultiPolygon multiPolygon = contourBuilder.computeIsoline((double) i + 0.5);
int maxPoints = 0;
Polygon maxPolygon = null;
for (int j = 0; j < multiPolygon.getNumGeometries(); j++) {
Polygon polygon = (Polygon) multiPolygon.getGeometryN(j);
if (polygon.getNumPoints() > maxPoints) {
maxPoints = polygon.getNumPoints();
maxPolygon = polygon;
}
}
if (maxPolygon == null) {
throw new IllegalStateException("no maximum polygon was found?");
} else {
polygonShells.add(maxPolygon.getExteriorRing().getCoordinates());
}
Polygon maxPolygon = heuristicallyFindMainConnectedComponent(multiPolygon, geometryFactory.createPoint(new Coordinate(point.lon, point.lat)));
polygonShells.add(maxPolygon.getExteriorRing().getCoordinates());
}
for (Coordinate[] polygonShell : polygonShells) {
JsonFeature feature = new JsonFeature();
Expand Down Expand Up @@ -181,4 +166,20 @@ public Response doGet(
build();
}

private Polygon heuristicallyFindMainConnectedComponent(MultiPolygon multiPolygon, Point point) {
int maxPoints = 0;
Polygon maxPolygon = null;
for (int j = 0; j < multiPolygon.getNumGeometries(); j++) {
Polygon polygon = (Polygon) multiPolygon.getGeometryN(j);
if (polygon.contains(point)) {
return polygon;
}
if (polygon.getNumPoints() > maxPoints) {
maxPoints = polygon.getNumPoints();
maxPolygon = polygon;
}
}
return maxPolygon;
}

}

0 comments on commit 87a9e03

Please sign in to comment.