How to get the speed of a specific edge with Graphhopper 6.0

Hi,

I used this code to get the closest edge from a location:

LocationIndex index = graphHopper.getLocationIndex();
Snap snap = index.findClosest(50.42811631606215, 4.926120400569366, EdgeFilter.ALL_EDGES);

 if (snap.isValid()) {
            EdgeIteratorState edge = snap.getClosestEdge();
            log.info("{}", edge.getName());
} else {
            log.warn("query invalid");
}

How to get the speed of this closest edge ?

Thank you

Regards

1 Like

There are multiple speeds depending on your profiles etc.

For example you could try

DecimalEncodedValue speedEnc = graphHopper.getEncodingManager().getDecimalEncodedValue(VehicleSpeed.key("car"));
double speedFwd = edge.get(speedEnc);
double speedBwd = edge.getReverse(speedEnc);

Thank you, it’s working
I also succeed to retrieve the max_speed based on your code example;

Regards

1 Like

Thanks for the hints. I increased the code to add the max_speed as a function of the roadType when the maxspeed info is not present for that segment:

   public void pointDirectSnap(TrackPoint trackPoint){
        GraphHopper graphHopper= MapHandler.getMapHandler().getHopper();
        LocationIndex index = graphHopper.getLocationIndex();
        QueryResult snap = index.findClosest(trackPoint.getLatitude(), trackPoint.getLongitude(), EdgeFilter.ALL_EDGES);
        if (snap.isValid()) {
            EdgeIteratorState edge = snap.getClosestEdge();
            trackPoint.setRoad_name(edge.getName());
            flagEncoder = (CarFlagEncoder) graphHopper.getEncodingManager().getEncoder("car");
            IntsRef intsRef = edge.getFlags();
            double max_speed = flagEncoder.getDecimalEncodedValue(MaxSpeed.KEY).getDecimal(true, intsRef);
            EnumEncodedValue<RoadClass> roadType = flagEncoder.getEnumEncodedValue(RoadClass.KEY, RoadClass.class);
            trackPoint.setType(String.valueOf(roadType.getEnum(false, intsRef)));
            if ((max_speed != Double.POSITIVE_INFINITY) && (max_speed < 140) && (max_speed > 10)) {
                trackPoint.setMax_speed(max_speed);
                trackPoint.setMaxSpeedinMap(true);
            } else   //If max_Speed= Infinity, ie no speed limit has been found in OSM Maps
            {
                trackPoint.setMaxSpeedinMap(false);
                switch (roadType.getEnum(false, intsRef)) {
                    case MOTORWAY:
                        trackPoint.setMax_speed(120);
                        break;
                    case TRUNK:
                    case PRIMARY:
                    case SECONDARY:
                    case TERTIARY:
                        trackPoint.setMax_speed(90);
                        break;
                    case SERVICE:
                    case TRACK:
                        trackPoint.setMax_speed(40);
                        break;
                    case RESIDENTIAL:
                    case BRIDLEWAY:
                    case STEPS:
                    case CYCLEWAY:
                    case PATH:
                    case LIVING_STREET:
                    case FOOTWAY:
                    case PEDESTRIAN:
                    case PLATFORM:
                    case CORRIDOR:
                        trackPoint.setMax_speed(20);
                        break;
                    case ROAD:
                    case UNCLASSIFIED:
                    case OTHER:
                    default:
                        trackPoint.setMax_speed(80);
                        break;
                }
            }
        }
        else { trackPoint.setRoad_name("Road Not found"); }
    }

In order to complete the info, how could I get the Municipality of that road/edge? (the city the car is in? place/municipality feature)

Thanks,

By default GraphHopper does not contain this information. If you have the city boundary coordinates + names you could use the custom areas feature during import (you would do this just the way GraphHopper determines the country encoded value). Other options might be obtaining this information from the corresponding OSM relations or using something like the addr tag from nearby buildings, but you would need to add Java code that does this.

Or actually an easier option could be using a Geocoder like Nominatim.

:pray: Thanks

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