Search in sources :

Example 1 with DefaultSnapFilter

use of com.graphhopper.routing.util.DefaultSnapFilter 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();
}
Also used : ProfileResolver(com.graphhopper.routing.ProfileResolver) LoggerFactory(org.slf4j.LoggerFactory) Subnetwork(com.graphhopper.routing.ev.Subnetwork) HashMap(java.util.HashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) Range(org.hibernate.validator.constraints.Range) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) ShortestPathTree(com.graphhopper.isochrone.algorithm.ShortestPathTree) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) IntParam(io.dropwizard.jersey.params.IntParam) Profile(com.graphhopper.config.Profile) TraversalMode(com.graphhopper.routing.util.TraversalMode) Graph(com.graphhopper.storage.Graph) NODE_BASED(com.graphhopper.routing.util.TraversalMode.NODE_BASED) GraphHopper(com.graphhopper.GraphHopper) org.locationtech.jts.geom(org.locationtech.jts.geom) com.graphhopper.util(com.graphhopper.util) Logger(org.slf4j.Logger) Context(javax.ws.rs.core.Context) ResponsePathSerializer(com.graphhopper.jackson.ResponsePathSerializer) LocationIndex(com.graphhopper.storage.index.LocationIndex) RouteResource.errorIfLegacyParameters(com.graphhopper.resources.RouteResource.errorIfLegacyParameters) LongParam(io.dropwizard.jersey.params.LongParam) BooleanEncodedValue(com.graphhopper.routing.ev.BooleanEncodedValue) NotNull(javax.validation.constraints.NotNull) ResponseType.geojson(com.graphhopper.resources.IsochroneResource.ResponseType.geojson) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) GHPointParam(com.graphhopper.http.GHPointParam) GraphEdgeIdFinder(com.graphhopper.storage.GraphEdgeIdFinder) Triangulator(com.graphhopper.isochrone.algorithm.Triangulator) ContourBuilder(com.graphhopper.isochrone.algorithm.ContourBuilder) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Weighting(com.graphhopper.routing.weighting.Weighting) ToDoubleFunction(java.util.function.ToDoubleFunction) FiniteWeightFilter(com.graphhopper.routing.util.FiniteWeightFilter) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) Snap(com.graphhopper.storage.index.Snap) UriInfo(javax.ws.rs.core.UriInfo) EDGE_BASED(com.graphhopper.routing.util.TraversalMode.EDGE_BASED) Collections(java.util.Collections) RouteResource.removeLegacyParameters(com.graphhopper.resources.RouteResource.removeLegacyParameters) GraphEdgeIdFinder(com.graphhopper.storage.GraphEdgeIdFinder) Triangulator(com.graphhopper.isochrone.algorithm.Triangulator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) TraversalMode(com.graphhopper.routing.util.TraversalMode) Snap(com.graphhopper.storage.index.Snap) Profile(com.graphhopper.config.Profile) FiniteWeightFilter(com.graphhopper.routing.util.FiniteWeightFilter) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) LocationIndex(com.graphhopper.storage.index.LocationIndex) Graph(com.graphhopper.storage.Graph) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) BooleanEncodedValue(com.graphhopper.routing.ev.BooleanEncodedValue) ContourBuilder(com.graphhopper.isochrone.algorithm.ContourBuilder) ShortestPathTree(com.graphhopper.isochrone.algorithm.ShortestPathTree) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph)

Example 2 with DefaultSnapFilter

use of com.graphhopper.routing.util.DefaultSnapFilter 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);
        }
    }
}
Also used : ConstraintVertex(org.locationtech.jts.triangulate.ConstraintVertex) Vertex(org.locationtech.jts.triangulate.quadedge.Vertex) NodeAccess(com.graphhopper.storage.NodeAccess) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) Stop(com.conveyal.gtfs.model.Stop) ConstraintVertex(org.locationtech.jts.triangulate.ConstraintVertex) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) QuadEdge(org.locationtech.jts.triangulate.quadedge.QuadEdge) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) Instant(java.time.Instant) ReadableTriangulation(com.graphhopper.isochrone.algorithm.ReadableTriangulation) ConformingDelaunayTriangulator(org.locationtech.jts.triangulate.ConformingDelaunayTriangulator) QuadEdgeSubdivision(org.locationtech.jts.triangulate.quadedge.QuadEdgeSubdivision) JsonFeature(com.graphhopper.util.JsonFeature) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) ContourBuilder(com.graphhopper.isochrone.algorithm.ContourBuilder)

Example 3 with DefaultSnapFilter

use of com.graphhopper.routing.util.DefaultSnapFilter in project graphhopper by graphhopper.

the class GtfsReader method connectStopsToStreetNetwork.

void connectStopsToStreetNetwork() {
    EncodingManager em = graph.getEncodingManager();
    FlagEncoder footEncoder = em.getEncoder("foot");
    final EdgeFilter filter = new DefaultSnapFilter(new FastestWeighting(footEncoder), em.getBooleanEncodedValue(Subnetwork.key("foot")));
    for (Stop stop : feed.stops.values()) {
        if (stop.location_type == 0) {
            // Only stops. Not interested in parent stations for now.
            Snap locationSnap = walkNetworkIndex.findClosest(stop.stop_lat, stop.stop_lon, filter);
            Integer stopNode;
            if (locationSnap.isValid()) {
                stopNode = gtfsStorage.getStreetToPt().get(locationSnap.getClosestNode());
                if (stopNode == null) {
                    stopNode = out.createNode();
                    indexBuilder.addToAllTilesOnLine(stopNode, stop.stop_lat, stop.stop_lon, stop.stop_lat, stop.stop_lon);
                    gtfsStorage.getPtToStreet().put(stopNode, locationSnap.getClosestNode());
                    gtfsStorage.getStreetToPt().put(locationSnap.getClosestNode(), stopNode);
                }
            } else {
                stopNode = out.createNode();
                indexBuilder.addToAllTilesOnLine(stopNode, stop.stop_lat, stop.stop_lon, stop.stop_lat, stop.stop_lon);
            }
            gtfsStorage.getStationNodes().put(new GtfsStorage.FeedIdWithStopId(id, stop.stop_id), stopNode);
        }
    }
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) EdgeFilter(com.graphhopper.routing.util.EdgeFilter) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) Snap(com.graphhopper.storage.index.Snap)

Example 4 with DefaultSnapFilter

use of com.graphhopper.routing.util.DefaultSnapFilter in project graphhopper by graphhopper.

the class SPTResource method doGet.

// Annotating this as application/json because errors come out as json, and
// IllegalArgumentExceptions are not mapped to a fixed mediatype, because in RouteResource, it could be GPX.
@GET
@Produces({ "text/csv", "application/json" })
public Response doGet(@Context UriInfo uriInfo, @QueryParam("profile") String profileName, @QueryParam("reverse_flow") @DefaultValue("false") boolean reverseFlow, @QueryParam("point") @NotNull GHPointParam point, @QueryParam("columns") String columnsParam, @QueryParam("time_limit") @DefaultValue("600") LongParam timeLimitInSeconds, @QueryParam("distance_limit") @DefaultValue("-1") LongParam distanceInMeter) {
    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);
    NodeAccess nodeAccess = queryGraph.getNodeAccess();
    TraversalMode traversalMode = profile.isTurnCosts() ? EDGE_BASED : NODE_BASED;
    ShortestPathTree shortestPathTree = new ShortestPathTree(queryGraph, queryGraph.wrapWeighting(weighting), reverseFlow, traversalMode);
    if (distanceInMeter.get() > 0) {
        shortestPathTree.setDistanceLimit(distanceInMeter.get());
    } else {
        double limit = timeLimitInSeconds.get() * 1000;
        shortestPathTree.setTimeLimit(limit);
    }
    final String COL_SEP = ",", LINE_SEP = "\n";
    List<String> columns;
    if (!Helper.isEmpty(columnsParam))
        columns = Arrays.asList(columnsParam.split(","));
    else
        columns = Arrays.asList("longitude", "latitude", "time", "distance");
    if (columns.isEmpty())
        throw new IllegalArgumentException("Either omit the columns parameter or specify the columns via comma separated values");
    Map<String, EncodedValue> pathDetails = new HashMap<>();
    for (String col : columns) {
        if (encodingManager.hasEncodedValue(col))
            pathDetails.put(col, encodingManager.getEncodedValue(col, EncodedValue.class));
    }
    StreamingOutput out = output -> {
        try (Writer writer = new BufferedWriter(new OutputStreamWriter(output, Helper.UTF_CS))) {
            StringBuilder sb = new StringBuilder();
            for (String col : columns) {
                if (sb.length() > 0)
                    sb.append(COL_SEP);
                sb.append(col);
            }
            sb.append(LINE_SEP);
            writer.write(sb.toString());
            shortestPathTree.search(snap.getClosestNode(), l -> {
                IsoLabelWithCoordinates label = isoLabelWithCoordinates(nodeAccess, l);
                sb.setLength(0);
                for (int colIndex = 0; colIndex < columns.size(); colIndex++) {
                    String col = columns.get(colIndex);
                    if (colIndex > 0)
                        sb.append(COL_SEP);
                    switch(col) {
                        case "node_id":
                            sb.append(label.nodeId);
                            continue;
                        case "prev_node_id":
                            sb.append(label.prevNodeId);
                            continue;
                        case "edge_id":
                            sb.append(label.edgeId);
                            continue;
                        case "prev_edge_id":
                            sb.append(label.prevEdgeId);
                            continue;
                        case "distance":
                            sb.append(label.distance);
                            continue;
                        case "prev_distance":
                            sb.append(label.prevCoordinate == null ? 0 : label.prevDistance);
                            continue;
                        case "time":
                            sb.append(label.timeMillis);
                            continue;
                        case "prev_time":
                            sb.append(label.prevCoordinate == null ? 0 : label.prevTimeMillis);
                            continue;
                        case "longitude":
                            sb.append(Helper.round6(label.coordinate.lon));
                            continue;
                        case "prev_longitude":
                            sb.append(label.prevCoordinate == null ? null : Helper.round6(label.prevCoordinate.lon));
                            continue;
                        case "latitude":
                            sb.append(Helper.round6(label.coordinate.lat));
                            continue;
                        case "prev_latitude":
                            sb.append(label.prevCoordinate == null ? null : Helper.round6(label.prevCoordinate.lat));
                            continue;
                    }
                    if (!EdgeIterator.Edge.isValid(label.edgeId))
                        continue;
                    EdgeIteratorState edge = queryGraph.getEdgeIteratorState(label.edgeId, label.nodeId);
                    if (edge == null)
                        continue;
                    if (col.equals(Parameters.Details.STREET_NAME)) {
                        sb.append(edge.getName().replaceAll(",", ""));
                        continue;
                    }
                    EncodedValue ev = pathDetails.get(col);
                    if (ev instanceof DecimalEncodedValue) {
                        DecimalEncodedValue dev = (DecimalEncodedValue) ev;
                        sb.append(reverseFlow ? edge.getReverse(dev) : edge.get(dev));
                    } else if (ev instanceof EnumEncodedValue) {
                        EnumEncodedValue eev = (EnumEncodedValue) ev;
                        sb.append(reverseFlow ? edge.getReverse(eev) : edge.get(eev));
                    } else if (ev instanceof BooleanEncodedValue) {
                        BooleanEncodedValue eev = (BooleanEncodedValue) ev;
                        sb.append(reverseFlow ? edge.getReverse(eev) : edge.get(eev));
                    } else if (ev instanceof IntEncodedValue) {
                        IntEncodedValue eev = (IntEncodedValue) ev;
                        sb.append(reverseFlow ? edge.getReverse(eev) : edge.get(eev));
                    } else {
                        throw new IllegalArgumentException("Unknown property " + col);
                    }
                }
                sb.append(LINE_SEP);
                try {
                    writer.write(sb.toString());
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            logger.info("took: " + sw.stop().getSeconds() + ", visited nodes:" + shortestPathTree.getVisitedNodes() + ", " + uriInfo.getQueryParameters());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    };
    // Give media type explicitly since we are annotating CSV and JSON, because error messages are JSON.
    return Response.ok(out).type("text/csv").build();
}
Also used : com.graphhopper.routing.ev(com.graphhopper.routing.ev) java.util(java.util) ProfileResolver(com.graphhopper.routing.ProfileResolver) LoggerFactory(org.slf4j.LoggerFactory) EncodingManager(com.graphhopper.routing.util.EncodingManager) Inject(javax.inject.Inject) ShortestPathTree(com.graphhopper.isochrone.algorithm.ShortestPathTree) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) Profile(com.graphhopper.config.Profile) TraversalMode(com.graphhopper.routing.util.TraversalMode) Graph(com.graphhopper.storage.Graph) OutputStreamWriter(java.io.OutputStreamWriter) NODE_BASED(com.graphhopper.routing.util.TraversalMode.NODE_BASED) GraphHopper(com.graphhopper.GraphHopper) com.graphhopper.util(com.graphhopper.util) Logger(org.slf4j.Logger) Context(javax.ws.rs.core.Context) LocationIndex(com.graphhopper.storage.index.LocationIndex) RouteResource.errorIfLegacyParameters(com.graphhopper.resources.RouteResource.errorIfLegacyParameters) BufferedWriter(java.io.BufferedWriter) LongParam(io.dropwizard.jersey.params.LongParam) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) NotNull(javax.validation.constraints.NotNull) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) GHPointParam(com.graphhopper.http.GHPointParam) GraphEdgeIdFinder(com.graphhopper.storage.GraphEdgeIdFinder) NodeAccess(com.graphhopper.storage.NodeAccess) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) Weighting(com.graphhopper.routing.weighting.Weighting) FiniteWeightFilter(com.graphhopper.routing.util.FiniteWeightFilter) Writer(java.io.Writer) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) Snap(com.graphhopper.storage.index.Snap) UriInfo(javax.ws.rs.core.UriInfo) EDGE_BASED(com.graphhopper.routing.util.TraversalMode.EDGE_BASED) GHPoint(com.graphhopper.util.shapes.GHPoint) RouteResource.removeLegacyParameters(com.graphhopper.resources.RouteResource.removeLegacyParameters) GraphEdgeIdFinder(com.graphhopper.storage.GraphEdgeIdFinder) NodeAccess(com.graphhopper.storage.NodeAccess) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) StreamingOutput(javax.ws.rs.core.StreamingOutput) TraversalMode(com.graphhopper.routing.util.TraversalMode) Snap(com.graphhopper.storage.index.Snap) Profile(com.graphhopper.config.Profile) BufferedWriter(java.io.BufferedWriter) FiniteWeightFilter(com.graphhopper.routing.util.FiniteWeightFilter) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) IOException(java.io.IOException) LocationIndex(com.graphhopper.storage.index.LocationIndex) Graph(com.graphhopper.storage.Graph) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) ShortestPathTree(com.graphhopper.isochrone.algorithm.ShortestPathTree) OutputStreamWriter(java.io.OutputStreamWriter) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Example 5 with DefaultSnapFilter

use of com.graphhopper.routing.util.DefaultSnapFilter in project graphhopper by graphhopper.

the class MapMatching method findCandidateSnapsInBBox.

private List<Snap> findCandidateSnapsInBBox(double queryLat, double queryLon, BBox queryShape) {
    EdgeFilter edgeFilter = new DefaultSnapFilter(unwrappedWeighting, inSubnetworkEnc);
    List<Snap> snaps = new ArrayList<>();
    IntHashSet seenEdges = new IntHashSet();
    IntHashSet seenNodes = new IntHashSet();
    locationIndex.query(queryShape, edgeId -> {
        EdgeIteratorState edge = graph.getEdgeIteratorStateForKey(edgeId * 2);
        if (seenEdges.add(edgeId) && edgeFilter.accept(edge)) {
            Snap snap = new Snap(queryLat, queryLon);
            locationIndex.traverseEdge(queryLat, queryLon, edge, (node, normedDist, wayIndex, pos) -> {
                if (normedDist < snap.getQueryDistance()) {
                    snap.setQueryDistance(normedDist);
                    snap.setClosestNode(node);
                    snap.setWayIndex(wayIndex);
                    snap.setSnappedPosition(pos);
                }
            });
            double dist = DIST_PLANE.calcDenormalizedDist(snap.getQueryDistance());
            snap.setClosestEdge(edge);
            snap.setQueryDistance(dist);
            if (snap.isValid() && (snap.getSnappedPosition() != Snap.Position.TOWER || seenNodes.add(snap.getClosestNode()))) {
                snap.calcSnappedPoint(DistanceCalcEarth.DIST_EARTH);
                if (queryShape.contains(snap.getSnappedPoint().lat, snap.getSnappedPoint().lon)) {
                    snaps.add(snap);
                }
            }
        }
    });
    return snaps;
}
Also used : DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) VirtualEdgeIteratorState(com.graphhopper.routing.querygraph.VirtualEdgeIteratorState) EdgeFilter(com.graphhopper.routing.util.EdgeFilter) IntHashSet(com.carrotsearch.hppc.IntHashSet) Snap(com.graphhopper.storage.index.Snap)

Aggregations

DefaultSnapFilter (com.graphhopper.routing.util.DefaultSnapFilter)5 Snap (com.graphhopper.storage.index.Snap)4 Weighting (com.graphhopper.routing.weighting.Weighting)3 GraphHopper (com.graphhopper.GraphHopper)2 Profile (com.graphhopper.config.Profile)2 GHPointParam (com.graphhopper.http.GHPointParam)2 ContourBuilder (com.graphhopper.isochrone.algorithm.ContourBuilder)2 ShortestPathTree (com.graphhopper.isochrone.algorithm.ShortestPathTree)2 RouteResource.errorIfLegacyParameters (com.graphhopper.resources.RouteResource.errorIfLegacyParameters)2 RouteResource.removeLegacyParameters (com.graphhopper.resources.RouteResource.removeLegacyParameters)2 ProfileResolver (com.graphhopper.routing.ProfileResolver)2 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)2 EdgeFilter (com.graphhopper.routing.util.EdgeFilter)2 EncodingManager (com.graphhopper.routing.util.EncodingManager)2 FiniteWeightFilter (com.graphhopper.routing.util.FiniteWeightFilter)2 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)2 TraversalMode (com.graphhopper.routing.util.TraversalMode)2 EDGE_BASED (com.graphhopper.routing.util.TraversalMode.EDGE_BASED)2 NODE_BASED (com.graphhopper.routing.util.TraversalMode.NODE_BASED)2 BlockAreaWeighting (com.graphhopper.routing.weighting.BlockAreaWeighting)2