Repository navigation
Conversation
| public interface GeometryEncodedValue extends DecimalEncodedValue { | ||
| double calculateDecimal(boolean reverse, IntsRef ref, PointList geometry); | ||
| } |
There was a problem hiding this comment.
This is the basic idea. GeometryEncodedValues can calculate a double value for a given geometry. Not sure if the ref parameter is actually needed, but it also does not seem to hurt (so far). There are two cases where this is done: 1) during import (similar to tag parsers, but for geometry), and 2) for virtual edges (the geometry is only known at query time).
| public class ElevationGain { | ||
| public static final String KEY = "elevation_gain"; | ||
|
|
||
| public static GeometryEncodedValue create() { | ||
| return new ElevationGainEncodedValue(); | ||
| } |
There was a problem hiding this comment.
Just a quick sketch of an encoded value that holds the total elevation gain (or loss if evaluated in reverse) of an edge.
|
I'm not sure if this is actually needed yet and if we should actually try to replace Also average slope is interesting, because this is just Another minor thing: For slope we probably need a signed decimal EV. |
| // todonow: how should we manage geometry encoded values / where do they come from, probably EncodingManager somehow? | ||
| // here they are very similar to tag parsers. (handleWayTags -> handleGeometry) | ||
| GeometryEncodedValue distanceEnc = Distance.create(); | ||
| double towerNodeDistance = distanceEnc.calculateDecimal(false, flags, pointList); |
There was a problem hiding this comment.
If we ever replaced distance with the distance EV this would be the place to calculate the distance (and same for elevation gain etc.) in OSMReader.
| import com.graphhopper.storage.IntsRef; | ||
| import com.graphhopper.util.PointList; | ||
|
|
||
| public interface GeometryEncodedValue extends DecimalEncodedValue { |
There was a problem hiding this comment.
The name might be confusing. It is not meant as an EncodedValue that stores the 'geometry/pillar nodes/point list', but rather a road attribute that depends on this geometry.
|
Thank you! Interesting to think about this topic having this code sketch :)
I'm not sure :) and not sure if it is worth the effort in general :) I would view this "geo-dependent calculation" also more as an "external procedure" where we do not need to adapt getting the value. And only adapt setting the value when we create the edge 1. in OSMImport and 2. for VirtualEdgeIteratorState. The advantage would be that there is no performance implication at query time and no need to add a new API for this to custom models etc. |
But at query time we have to calculate distance, elevation gain etc. for the virtual edges. There is no way around this? And where else would you put the code that does this calculation. It is the same code we use for the real edges during import.
Can you explain what you mean here? What needs to be done for custom models? And what was the original idea of 'length-dependent' encoded values? Did you have something different in mind? |
Yes. But this calculation has to be done only once and then we store the value in the virtual edge. Then, for fetching this value, it does not matter if it is a relative or absolute decimal value.
If we would introduce a new edge.get() or edge.getElevationGain() we would need to adapt the current code in custom models.
The idea for this was not really concrete. But I also do not think that the code is far away from this idea. I think that it would be nice to avoid a new edge.get method and instead have e.g. code in VirtualEdgeIteratorState (and in OSMReader/TagParser) that sets the final value: for(encVal : encodingManager.getGeoDependentEncodedValues()) {
encVal.setDecimal(false, intsRef, pointsList)
}
new VirtualEdgeIteratorState(..., intsRef, ...)And then, for reading, the "geo-dependent" EncodedValues are identical like the other DecimalEncodedValues |
Ok. I thought the getter would not be called many times (or even only once) for virtual edges.
Ok, I see. I thought the problem with this would be that the virtual edges would have to be aware of which geometry values exist. Currently
I already do this for import: // todonow: how should we manage geometry encoded values / where do they come from, probably EncodingManager somehow?
// here they are very similar to tag parsers. (handleWayTags -> handleGeometry)
GeometryEncodedValue distanceEnc = Distance.create();
double towerNodeDistance = distanceEnc.calculateDecimal(false, flags, pointList);
// ...
EdgeIteratorState iter = graph.edge(fromIndex, toIndex)
// this is just the standard setter for `DecimalEncodedValue` that is currently inherited by `GeometryEncodedValue`
.set(distanceEnc, towerNodeDistance)
For
Ok, yes. I can see the benefits of this. Or... we do not do any of this and just add The |
|
Yes, indeed. We should try to avoid the knowledge of the geometry values and could indeed do it in the getter for virtual edges (as you say: it shouldn't matter performance-wise). But I would still try to avoid a different getter method. We could do the mechanics under the hood like (?) double getDecimal(EncodedValue enc) {
if(enc instance GeoEncValue) return ((GeoEncValue)enc).calculateValue(reverse, edgeFlags, pointList)
enc.getDecimal(reverse, edgeFlags);
}
Sorry, you already did this how meant it (I think), but I did not fully understand the "todonow" (i.e. wasn't sure which of this code is a sketch)
Yes, this would be a possibility.
Another option would be to use "elevation gain per meter" :) but of course the precision would be lost. |
|
I removed
Once we know how to get/set values for geometry EVs the next question is where we keep the geometry EVs. The code where I put the todonow simply creates the distance EV but that won't cut it, because we also need the EV to read the values later.
What kind of elevation EVs we actually use or provide is another question I guess. The good news is that once we established the framework for this we can use any implementation that calculates some double values (but only unsigned so far!) from the point list. The idea of the |
# Conflicts: # core/src/main/java/com/graphhopper/reader/osm/OSMReader.java
|
Nice!
Yes. My point was more that there is a possibility to consider elevation in an encoded value (and in custom models) and still avoiding establishing a new framework but this surely comes with your mentioned drawbacks. Still, at the moment, we have to focus on improving the elevation data or precision first IMO as otherwise we won't get the intended benefits. See e.g. this route close a mountain which is a smooth slightly downward route in reality. In Bike2FlagEncoder I explicitly used the "average slope" (elevation difference of tower nodes only) as the more detailed "elevation gain" produced worse results. What just came to my mind is that with this new framework one could block a very specific location at an edge without introducing a new barrier edge. I.e. assume tower nodes A and B with pillar nodes in-between |
But how? You mean using
Ok. But maybe
Not sure if I understand this yet. Do you mean blocking a specific location during import or at query time? |
Using a DecimalEncodedValue like As this slope is "smeared" over the entire edge (it is "per meter") we can split the edge anywhere, i.e. there is no special handling required for this average slope to create virtual edges. (We could even have gain and loss as separate EV) And this average_slope is different to what we currently do in Bike2FlagEncoder: (and in my memories I only compared rough_average_slope with average_slope and I picked the rough values but this might have changed.)
Currently I think that the "average_slope" will even give better results (as it hides these artifacts). So I would play with this first in custom models and make it easy there (something like
Not really related to this issue. But yes, I meant at import time without creating a separate edge. And this showed me a generic version of store "something per pillar node". |
Ok, I see, yes this is true and because of this we would not need any special handling for split edges.
I think this entirely depends on the data and the use-case. For good mountainbike profiles (at least some I can imagine), or wheelchair profiles that need to prevent steep sections I don't think the average slope would be sufficient, but for car even not considering the elevation can give useful results. This is kind of the idea of this draft: It does not matter and everybody can just implement whatever encoded values they like.
So far I did not (only) intend to use this for custom models, because one could still implement some weighting in Java that makes use of this. But let's see: Just off the top of my head a few ideas. I would find the maximum slope interesting (can be used to prevent or prefer steep sections): "priority": [
{
"if": "max_slope > 0.1"
"multiply_by": 0.1
}
]or maybe the average upward/downward slope to find/exclude steep roads "priority": [
{
"if": "average_upward_slope > 0.05"
"multiply_by": 0.1
}
]or just count how many meters of an edge are above a certain slope threshold: "priority": [
{
"if": "steep_sections > 30"
"multiply_by": 0.1
}
]The simple
Hm, but this requires "slope" as a right-hand value and it would be hard to tell how this influences the weight, because the absolute value of "slope" might be unknown or hard to compare to the factors used in other rules.
Not really related to this issue, but I think the two formulae are equivalent. The difference difference between the elevation gain and the elevation loss is equal to the difference between the elevation at A and B?! |
Yes, but IMO we need this. Otherwise introducing and tweaking "avoid or prefer hills" might be too complicated. We could allow a factor in-between
Why? Couldn't we define the slope like it is usually defined in meters of elevation gain or loss per 100m?
Ah, yes, indeed. Probably I tried something else with involving pillar nodes too basing the speed changes on the total elevation gain + loss.
Probably. But before we come to this challenge we need to tune the elevation data. |
Using
Yes, if we allowed right-hand encoded values I also think we need at least the ability to scale the values with a custom factor, or even some functions, as you proposed. For example we would have to be able to deal with negative slope values.
Sure, using right-hand values would allow a continuous weighting based on the right hand side encoded value. But not sure we definitely need this for elevation. For some use cases it might very well be enough to use a few categories like I have to say I am mostly speculating here though. My idea was just providing the tools so GraphHopper users can deal with elevation in a flexible way. Which of the different options is most important and which ones are not as critical is probably impossible to tell without actually trying to build a realistic elevation-aware profile, and even then it probably depends on the exact use-case. So the real question is how to build elevation-aware routing and because I think this depends on the use-case so much I thought the best option is to introduce the tools that are needed (namely these geometry EVs) to use the elevation for route calculations.
Sure we could define some convention. What I meant was that this will be hard to put in relation to other rules like
I think quite the opposite: We need to be able to include the elevation in the modeling to find out in which way we need to tune the elevation data. |
Probably my comments were a bit too intense ... I'm not against this draft. I do not want to hinder something you would like to push :)
With "avoiding hills" I also mean to prefer less hilly roads. With this simple "max_slope>0.1" condition you won't optimize the elevation changes on a route and only avoid or exclude too big elevation changes.
Yes. I meant that we do not necessarily need this PR and could feed an ordinary average_slope EV. |
Sure, I did not feel like you wanted to hinder something :) It's certainly good to make sure something is actually needed before we introduce it.
Yes, maybe this is the best thing to start with. I was just about to write an issue just for average_slope. I'm sure we will learn something along the way and everything we need for this we probably need for more advanced elevation use-cases as well. |
I was trying to make some progress towards #1898. See comments inline.