use of com.graphhopper.isochrone.algorithm.ContourBuilder in project graphhopper by graphhopper.
the class IsochroneResource method doGet.
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response doGet(@Context UriInfo uriInfo, @QueryParam("profile") String profileName, @QueryParam("buckets") @Range(min = 1, max = 20) @DefaultValue("1") IntParam nBuckets, @QueryParam("reverse_flow") @DefaultValue("false") boolean reverseFlow, @QueryParam("point") @NotNull GHPointParam point, @QueryParam("time_limit") @DefaultValue("600") LongParam timeLimitInSeconds, @QueryParam("distance_limit") @DefaultValue("-1") LongParam distanceLimitInMeter, @QueryParam("weight_limit") @DefaultValue("-1") LongParam weightLimit, @QueryParam("type") @DefaultValue("json") ResponseType respType, @QueryParam("tolerance") @DefaultValue("0") double toleranceInMeter, @QueryParam("full_geometry") @DefaultValue("false") boolean fullGeometry) {
StopWatch sw = new StopWatch().start();
PMap hintsMap = new PMap();
RouteResource.initHints(hintsMap, uriInfo.getQueryParameters());
hintsMap.putObject(Parameters.CH.DISABLE, true);
hintsMap.putObject(Parameters.Landmark.DISABLE, true);
if (Helper.isEmpty(profileName)) {
profileName = profileResolver.resolveProfile(hintsMap).getName();
removeLegacyParameters(hintsMap);
}
errorIfLegacyParameters(hintsMap);
Profile profile = graphHopper.getProfile(profileName);
if (profile == null)
throw new IllegalArgumentException("The requested profile '" + profileName + "' does not exist");
LocationIndex locationIndex = graphHopper.getLocationIndex();
Graph graph = graphHopper.getGraphHopperStorage();
Weighting weighting = graphHopper.createWeighting(profile, hintsMap);
BooleanEncodedValue inSubnetworkEnc = graphHopper.getEncodingManager().getBooleanEncodedValue(Subnetwork.key(profileName));
if (hintsMap.has(Parameters.Routing.BLOCK_AREA)) {
GraphEdgeIdFinder.BlockArea blockArea = GraphEdgeIdFinder.createBlockArea(graph, locationIndex, Collections.singletonList(point.get()), hintsMap, new FiniteWeightFilter(weighting));
weighting = new BlockAreaWeighting(weighting, blockArea);
}
Snap snap = locationIndex.findClosest(point.get().lat, point.get().lon, new DefaultSnapFilter(weighting, inSubnetworkEnc));
if (!snap.isValid())
throw new IllegalArgumentException("Point not found:" + point);
QueryGraph queryGraph = QueryGraph.create(graph, snap);
TraversalMode traversalMode = profile.isTurnCosts() ? EDGE_BASED : NODE_BASED;
ShortestPathTree shortestPathTree = new ShortestPathTree(queryGraph, queryGraph.wrapWeighting(weighting), reverseFlow, traversalMode);
double limit;
if (weightLimit.get() > 0) {
limit = weightLimit.get();
shortestPathTree.setWeightLimit(limit + Math.max(limit * 0.14, 2_000));
} else if (distanceLimitInMeter.get() > 0) {
limit = distanceLimitInMeter.get();
shortestPathTree.setDistanceLimit(limit + Math.max(limit * 0.14, 2_000));
} else {
limit = timeLimitInSeconds.get() * 1000;
shortestPathTree.setTimeLimit(limit + Math.max(limit * 0.14, 200_000));
}
ArrayList<Double> zs = new ArrayList<>();
double delta = limit / nBuckets.get();
for (int i = 0; i < nBuckets.get(); i++) {
zs.add((i + 1) * delta);
}
ToDoubleFunction<ShortestPathTree.IsoLabel> fz;
if (weightLimit.get() > 0) {
fz = l -> l.weight;
} else if (distanceLimitInMeter.get() > 0) {
fz = l -> l.distance;
} else {
fz = l -> l.time;
}
Triangulator.Result result = triangulator.triangulate(snap, queryGraph, shortestPathTree, fz, degreesFromMeters(toleranceInMeter));
ContourBuilder contourBuilder = new ContourBuilder(result.triangulation);
ArrayList<Geometry> isochrones = new ArrayList<>();
for (Double z : zs) {
logger.info("Building contour z={}", z);
MultiPolygon isochrone = contourBuilder.computeIsoline(z, result.seedEdges);
if (fullGeometry) {
isochrones.add(isochrone);
} else {
Polygon maxPolygon = heuristicallyFindMainConnectedComponent(isochrone, isochrone.getFactory().createPoint(new Coordinate(point.get().lon, point.get().lat)));
isochrones.add(isochrone.getFactory().createPolygon(((LinearRing) maxPolygon.getExteriorRing())));
}
}
ArrayList<JsonFeature> features = new ArrayList<>();
for (Geometry isochrone : isochrones) {
JsonFeature feature = new JsonFeature();
HashMap<String, Object> properties = new HashMap<>();
properties.put("bucket", features.size());
if (respType == geojson) {
properties.put("copyrights", ResponsePathSerializer.COPYRIGHTS);
}
feature.setProperties(properties);
feature.setGeometry(isochrone);
features.add(feature);
}
ObjectNode json = JsonNodeFactory.instance.objectNode();
sw.stop();
ObjectNode finalJson = null;
if (respType == geojson) {
json.put("type", "FeatureCollection");
json.putPOJO("features", features);
finalJson = json;
} else {
json.putPOJO("polygons", features);
final ObjectNode info = json.putObject("info");
info.putPOJO("copyrights", ResponsePathSerializer.COPYRIGHTS);
info.put("took", Math.round((float) sw.getMillis()));
finalJson = json;
}
logger.info("took: " + sw.getSeconds() + ", visited nodes:" + shortestPathTree.getVisitedNodes());
return Response.ok(finalJson).header("X-GH-Took", "" + sw.getSeconds() * 1000).build();
}
use of com.graphhopper.isochrone.algorithm.ContourBuilder in project graphhopper by graphhopper.
the class PtIsochroneResource method doGet.
@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response doGet(@QueryParam("point") GHLocationParam sourceParam, @QueryParam("time_limit") @DefaultValue("600") long seconds, @QueryParam("reverse_flow") @DefaultValue("false") boolean reverseFlow, @QueryParam("pt.earliest_departure_time") @NotNull OffsetDateTimeParam departureTimeParam, @QueryParam("pt.blocked_route_types") @DefaultValue("0") int blockedRouteTypes, @QueryParam("result") @DefaultValue("multipolygon") String format) {
Instant initialTime = departureTimeParam.get().toInstant();
GHLocation location = sourceParam.get();
double targetZ = seconds * 1000;
GeometryFactory geometryFactory = new GeometryFactory();
final FlagEncoder footEncoder = encodingManager.getEncoder("foot");
final Weighting weighting = new FastestWeighting(footEncoder);
DefaultSnapFilter snapFilter = new DefaultSnapFilter(weighting, graphHopperStorage.getEncodingManager().getBooleanEncodedValue(Subnetwork.key("foot")));
PtLocationSnapper.Result snapResult = new PtLocationSnapper(graphHopperStorage, locationIndex, gtfsStorage).snapAll(Arrays.asList(location), Arrays.asList(snapFilter));
GraphExplorer graphExplorer = new GraphExplorer(snapResult.queryGraph, gtfsStorage.getPtGraph(), weighting, gtfsStorage, RealtimeFeed.empty(), reverseFlow, false, false, 5.0, reverseFlow, blockedRouteTypes);
MultiCriteriaLabelSetting router = new MultiCriteriaLabelSetting(graphExplorer, reverseFlow, false, false, 0, Collections.emptyList());
Map<Coordinate, Double> z1 = new HashMap<>();
NodeAccess nodeAccess = snapResult.queryGraph.getNodeAccess();
for (Label label : router.calcLabels(snapResult.nodes.get(0), initialTime)) {
if (!((label.currentTime - initialTime.toEpochMilli()) * (reverseFlow ? -1 : 1) <= targetZ)) {
break;
}
if (label.node.streetNode != -1) {
Coordinate nodeCoordinate = new Coordinate(nodeAccess.getLon(label.node.streetNode), nodeAccess.getLat(label.node.streetNode));
z1.merge(nodeCoordinate, (double) (label.currentTime - initialTime.toEpochMilli()) * (reverseFlow ? -1 : 1), Math::min);
} else if (label.edge != null && (label.edge.getType() == GtfsStorage.EdgeType.EXIT_PT || label.edge.getType() == GtfsStorage.EdgeType.ENTER_PT)) {
GtfsStorage.PlatformDescriptor platformDescriptor = label.edge.getPlatformDescriptor();
Stop stop = gtfsStorage.getGtfsFeeds().get(platformDescriptor.feed_id).stops.get(platformDescriptor.stop_id);
Coordinate nodeCoordinate = new Coordinate(stop.stop_lon, stop.stop_lat);
z1.merge(nodeCoordinate, (double) (label.currentTime - initialTime.toEpochMilli()) * (reverseFlow ? -1 : 1), Math::min);
}
}
if (format.equals("multipoint")) {
MultiPoint exploredPoints = geometryFactory.createMultiPointFromCoords(z1.keySet().toArray(new Coordinate[0]));
return wrap(exploredPoints);
} else {
MultiPoint exploredPoints = geometryFactory.createMultiPointFromCoords(z1.keySet().toArray(new Coordinate[0]));
// Get at least all nodes within our bounding box (I think convex hull would be enough.)
// I think then we should have all possible encroaching points. (Proof needed.)
locationIndex.query(BBox.fromEnvelope(exploredPoints.getEnvelopeInternal()), edgeId -> {
EdgeIteratorState edge = snapResult.queryGraph.getEdgeIteratorStateForKey(edgeId * 2);
z1.merge(new Coordinate(nodeAccess.getLon(edge.getBaseNode()), nodeAccess.getLat(edge.getBaseNode())), Double.MAX_VALUE, Math::min);
z1.merge(new Coordinate(nodeAccess.getLon(edge.getAdjNode()), nodeAccess.getLat(edge.getAdjNode())), Double.MAX_VALUE, Math::min);
});
exploredPoints = geometryFactory.createMultiPointFromCoords(z1.keySet().toArray(new Coordinate[0]));
CoordinateList siteCoords = DelaunayTriangulationBuilder.extractUniqueCoordinates(exploredPoints);
List<ConstraintVertex> constraintVertices = new ArrayList<>();
for (Object siteCoord : siteCoords) {
Coordinate coord = (Coordinate) siteCoord;
constraintVertices.add(new ConstraintVertex(coord));
}
ConformingDelaunayTriangulator cdt = new ConformingDelaunayTriangulator(constraintVertices, JTS_TOLERANCE);
cdt.setConstraints(new ArrayList(), new ArrayList());
cdt.formInitialDelaunay();
QuadEdgeSubdivision tin = cdt.getSubdivision();
for (Vertex vertex : (Collection<Vertex>) tin.getVertices(true)) {
if (tin.isFrameVertex(vertex)) {
vertex.setZ(Double.MAX_VALUE);
} else {
Double aDouble = z1.get(vertex.getCoordinate());
if (aDouble != null) {
vertex.setZ(aDouble);
} else {
vertex.setZ(Double.MAX_VALUE);
}
}
}
ReadableTriangulation triangulation = ReadableTriangulation.wrap(tin);
ContourBuilder contourBuilder = new ContourBuilder(triangulation);
MultiPolygon isoline = contourBuilder.computeIsoline(targetZ, triangulation.getEdges());
// debugging tool
if (format.equals("triangulation")) {
Response response = new Response();
for (Vertex vertex : (Collection<Vertex>) tin.getVertices(true)) {
JsonFeature feature = new JsonFeature();
feature.setGeometry(geometryFactory.createPoint(vertex.getCoordinate()));
HashMap<String, Object> properties = new HashMap<>();
properties.put("z", vertex.getZ());
feature.setProperties(properties);
response.polygons.add(feature);
}
for (QuadEdge edge : (Collection<QuadEdge>) tin.getPrimaryEdges(false)) {
JsonFeature feature = new JsonFeature();
feature.setGeometry(edge.toLineSegment().toGeometry(geometryFactory));
HashMap<String, Object> properties = new HashMap<>();
feature.setProperties(properties);
response.polygons.add(feature);
}
JsonFeature feature = new JsonFeature();
feature.setGeometry(isoline);
HashMap<String, Object> properties = new HashMap<>();
properties.put("z", targetZ);
feature.setProperties(properties);
response.polygons.add(feature);
response.info.copyrights.addAll(ResponsePathSerializer.COPYRIGHTS);
return response;
} else {
return wrap(isoline);
}
}
}
Aggregations