# Speed values returned by getSpeed method

Hi

Does getSpeed (ReaderWay way) returns average speed of an edge during routing?
What does getSpeed (IntsRef edgeFlags) return?
Is FlagEncoder.getMaxSpeed() only used to calculate weight of a road in weighting (e.g., fastest weighting)?
when do we use edgeIteratorState speedEnc and avSpeedEnc method?

How I should get the initial edge speed values before calculating a route?
How I should get the edge speed during routing?

in my experiment I found for osm id 26524519:
edge length from edgeEState:32.205
speed from edgeEState.get(avSpeedEnc) : 80.0
initial speed from ((CarFlagEncoder) encoder).getSpeed(allEdges.getFlags()) is: 155.0
maxspeed from ((CarFlagEncoder) encoder).getMaxSpeed(): 140.0;
140/ 3.6 = 38.888888888888886 m/s

Thank you so much

((CarFlagEncoder) encoder).getMaxSpeed()

This is just a global, fixed value that is defined for each encoder. It is used to truncate all speeds to this maximum value and to estimate the remaining weight for the AStar routing algorithm (basically the routing goes wrong if it is smaller than the actual speeds used in the graph, but when it is too large the routing becomes slow)

What do you mean by ‘initial speed’ here? The speed that is set before reading OSM?

edgeEState.get(avSpeedEnc) : 80.0

Yes, this is exactly how you retrive the speed (in km/h) from an edge: You pass the average speed encoded value to the edge state.

Note that the GraphHopper graph usually stores two different kinds of speeds : One is the ‘average_speed’, which is the one used for routing and an estimate of the speed that is actually realized when driving on this route. There is one such speed for each vehicle/encoder, like `car_average_speed` and `bike_average_speed` (in older GH versions they were called `car\$average_speed` and `bike\$average_speed`). And then there is the `max_speed` encoded value which is not directly related to the route calculation, but can be used for path details (or in custom models where it can indeed affect the routing). It holds the legal maximum speed of a road (as it is written on traffic signs), typically larger than the average speed.

I want to calculate the maximum capacity (max density) of an edge. Simplified form could be edge length /car size * # of lane or 1000 * freeflow speed / car Spacing * # of lane . That is why I need the free flow or initial speed.

I calculated it from graphhopper storage graph. I am not sure if I did it correctly. Does the graph store speed before reading OSM or after? Actually I am kinda confused. (I am using the old version of GH)

``````Graph graph = graphHopper.getGraphHopperStorage();
AllEdgesIterator allEdges = graph.getAllEdges();

while (allEdges.next()){
int alledgeId = allEdges.getEdge();
double dist = allEdges.getDistance();  // in meter
double init_speed = ((CarFlagEncoder) encoder).getSpeed(allEdges.getFlags()); //km/h
edge_speed_init.put( alledgeId, init_speed );    // map
``````

So, the average speed encoded value is the speed that we use for actual routing? Should I consider this value as free flow speed and use to calculate capacity? For that specific edge OSM has speed limit of 88 km/h or 55 mile/h but GH has 80 km/h. Why it is less than OSM?

I want to see if an edge has high density (e.g., max capacity=12,000 and current density=6000), how the speed changes and traffic looks like. How I can do that?

Does the graph store speed before reading OSM or after?

No all speeds are zero before.

Yes the average speed encoded value is the one used for the actual routing (when you use the ‘fastest’ weighting). I think you can consider it as free flow speed yes. And yes a default assumption is that the average speed is about 90% of the maximum speed (0.9*88=~80), because normally you need to slow down sometimes.

Thanks. Still did not understand what ((CarFlagEncoder) encoder).getSpeed(allEdges.getFlags()) returns. 155 km/h is which speed?

In my experiment, I want to reduce the speed of an edge if it has more than half of its capacity and after some time may be return to original speed. Could you please tell me if I am doing it correctly (in carFlagEncoder)?

``````public double getSpeed(IntsRef edgeFlags) {
return super.getSpeed(edgeFlags);
}

public IntsRef setSpeed(IntsRef edgeFlags, double speed) {
super.setSpeed(false,edgeFlags,speed);
boolean reverse = false;

if (speed < 0 || Double.isNaN(speed))
throw new IllegalArgumentException("Speed cannot be negative or NaN: " + speed + ", flags:" + BitUtil.LITTLE.toBitString(edgeFlags));

if (speed < speedFactor / 2) {
speedEncoder.setDecimal(reverse, edgeFlags, 0);
accessEnc.setBool(reverse, edgeFlags, false);
return;
}

if (speed > getMaxSpeed())
speed = getMaxSpeed();

speedEncoder.setDecimal(reverse, edgeFlags, speed);
}
``````

and then in my main code to set my new speed, I use it as…

`````` EdgeIteratorState edge.setFlags(((CarFlagEncoder) encoder).setSpeed(edge.getFlags(), newSpeed));
``````

What is the right way to do it?

Thanks. Still did not understand what ((CarFlagEncoder) encoder).getSpeed(allEdges.getFlags()) returns. 155 km/h is which speed?

You haven’t mentioned what version you are using and what you are doing such that 155 is returned, so I don’t know what you mean.

Oh sorry… I am using version 0.13. And ((CarFlagEncoder) encoder).getSpeed(allEdges.getFlags()) returns this 155 km/h.

Ok, I can barely remember version 0.13. Did you consider using a more recent version?
And you mean the speed is 155km/h after you created the graph, without doing anything else? That seems unexpected to me, can you provide a full runnable example for this?

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