Can only snap to the nodes of the road?

Hello! As the problem shows, can only snap to the tower nodes or pillar nodes of the edge. Edges can have as few as two points, which leads to very imprecise snap points. I want to be able to interpolate and snap to more accurate points based on the line. By the way, I did it with Geotools. But I still want to ask if it’s possible to do this now, even without Geotools.

Snapping in GraphHopper is rather advanced and allows to snap to any position on the edge. Not sure what you had to do with Geotools, but this is an inbuilt functionality. You can see it in action on GraphHopper Maps.

        hopper.setEncodedValuesString(“osm_way_id“);
        hopper.importOrLoad();
        IntEncodedValue OSMWayIDEnc = hopper.getEncodingManager().getIntEncodedValue(OSMWayID.KEY);
        EdgeFilter fromIdFilter = (edgeIteratorState) -> {
            return edgeIteratorState.get(OSMWayIDEnc) == Integer.parseInt(startOSMId.toString());
        };
        Snap from = hopper.getLocationIndex().findClosest(Double.parseDouble(params.getLon1()), Double.parseDouble(params.getLat1()), fromIdFilter);

How to snap to any position? Above is my current code. I used an edge filter to snap to edge with osmid equal to a specific value.

Thank you very much for your answer. It was very useful and told me that I had done repetitive work.

@ karussell One mistake I made, without any exception, was to reverse snap’s lat and lon, which resulted in snap positon always being a tower node.By the way, after spending a few hours looking through a few hundred lines of source code for the findcloset method, it does exactly what you say it does.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.