Skip to content

Add geometry encoded value - #2451

Draft
easbar wants to merge 5 commits into
masterfrom
geometry_ev
Draft

easbar wants to merge 5 commits into
masterfrom
geometry_ev

Conversation

@easbar

@easbar easbar commented Oct 29, 2021

Copy link
Copy Markdown
Member

I was trying to make some progress towards #1898. See comments inline.

Comment on lines +24 to +26
public interface GeometryEncodedValue extends DecimalEncodedValue {
double calculateDecimal(boolean reverse, IntsRef ref, PointList geometry);
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment on lines +24 to +29
public class ElevationGain {
public static final String KEY = "elevation_gain";

public static GeometryEncodedValue create() {
return new ElevationGainEncodedValue();
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick sketch of an encoded value that holds the total elevation gain (or loss if evaluated in reverse) of an edge.

@easbar

easbar commented Oct 29, 2021 •

Copy link
Copy Markdown
Member Author

I'm not sure if this is actually needed yet and if we should actually try to replace edge.getDistance() with edge.get(distanceEnc)? The obvious alternative would be adding edge.getElevationGain(reverse) instead... How many of such geometry encoded values can we come up with and are there any other 'length-dependent' EVs? Maybe something like maximum slope?

Also average slope is interesting, because this is just (elevation_gain(forward)-elevation_gain(backward))/distance, so basically a geometry encoded value that does not store anything but is calculated based on other geometry encoded values...

Another minor thing: For slope we probably need a signed decimal EV.

Comment on lines +658 to +661
// 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);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@karussell

Copy link
Copy Markdown
Member

Thank you! Interesting to think about this topic having this code sketch :)

I'm not sure if this is actually needed yet and if we should actually try to replace edge.getDistance() with edge.get(distanceEnc)? The obvious alternative would be adding edge.getElevationGain(reverse) instead... How many of such geometry encoded values can we come up with and are there any other 'length-dependent' EVs? Maybe something like maximum slope?

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.

@easbar

easbar commented Nov 1, 2021

Copy link
Copy Markdown
Member Author

The advantage would be that there is no performance implication at query time

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.

and no need to add a new API for this to custom models etc.

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?

@karussell

karussell commented Nov 1, 2021 •

Copy link
Copy Markdown
Member

But at query time we have to calculate distance, elevation gain etc. for the virtual edges. There is no way around this?

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.

Can you explain what you mean here? What needs to be done for custom models?

If we would introduce a new edge.get() or edge.getElevationGain() we would need to adapt the current code in custom models.

And what was the original idea of 'length-dependent' encoded values? Did you have something different in mind?

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

@easbar

easbar commented Nov 1, 2021 •

Copy link
Copy Markdown
Member Author

Yes. But this calculation has to be done only once and then we store the value in the virtual edge.

Ok. I thought the getter would not be called many times (or even only once) for virtual edges.

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.

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 VirtualEdgeIteratorState has a distance field and then it would also need an elevationGain field etc. and they would have to be (statically) associated with the according GeometryEncodedValues somehow. But maybe we could just use a dynamic Map<String, Double> that maps encoded values names to double values instead?

and instead have e.g. code in VirtualEdgeIteratorState (and in OSMReader/TagParser) that sets the final value:

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 VirtualEdgeIteratorState we could do the same, but like I said then we need this Map<String, Double> or similar.

And then, for reading, the "geo-dependent" EncodedValues are identical like the other DecimalEncodedValues

Ok, yes. I can see the benefits of this.

Or... we do not do any of this and just add EdgeIteratorState.get/setElevationGain... and keep EdgeIteratorState.get/setDistance as well. But especially EdgeIteratorState.getElevationGain would not work well with custom models or at least also require code changes there.

The Map<String, Double> approach does not sound so bad to me right now?!

@karussell

Copy link
Copy Markdown
Member

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);
}

I already do this for import

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)

But maybe we could just use a dynamic Map<String, Double> that maps encoded values names to double values instead?

Yes, this would be a possibility.

Or... we do not do any of this and just add EdgeIteratorState.get/setElevationGain...

Another option would be to use "elevation gain per meter" :) but of course the precision would be lost.

@easbar

easbar commented Nov 1, 2021

Copy link
Copy Markdown
Member Author

I removed get(GeometryEncodedValue) and do the instanceof check that you proposed in VirtualEdgeIteratorState now. This should work the same way as my first version, but we don't need to add another get method. We could still somehow store or cache the calculated values in VirtualEdgeIteratorState to replace the calculateDecimal call.

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)

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.

Another option would be to use "elevation gain per meter" :) but of course the precision would be lost.

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 ElevationGain I added here was that it is not just the average slope of the entire edge but rather the sum of all positive elevation changes so an edge that starts/ends on the same elevation, but represents a road that still goes up and down between the start and end can be distinguished from a road that is just flat. Note that we can get the ElevationLoss by simply reading the ElevationGain in reverse. See also my comment about the average slope here: #2451 (comment)

# Conflicts:
#	core/src/main/java/com/graphhopper/reader/osm/OSMReader.java
@karussell

karussell commented Nov 1, 2021 •

Copy link
Copy Markdown
Member

Nice!

The good news is that once we established the framework for this we can use any implementation that calculates some double values from the point list.

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 A-x-y-z-B and the elevation is 0 0 BIG 0 0. And now if a destination is at x then the routes goes via A. But if a destination is at z then the route goes via B. Or to put it differently: elevation is just one of the many possible per-pillar-node data we could store and make available like construction data, pedestrian crossing, traffic light or even speed.

@easbar

easbar commented Nov 1, 2021

Copy link
Copy Markdown
Member Author

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.

But how? You mean using edgeState.getSlope() or something? I don't really see how this would be easier even.

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.

Ok. But maybe graph.elevation.long_edge_sampling_distance and graph.elevation.way_point_max_distance already help here? Hm or maybe not your example looks like we rather need some kind of smoothing. But anyway, I'm not sure if such artifacts are a good reason not to allow such geometry-dependent EVs already, because once we have this at least we can start experimenting with elevation EVs. But sure there is also no need to rush with this issue.

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.

Not sure if I understand this yet. Do you mean blocking a specific location during import or at query time?

@karussell

Copy link
Copy Markdown
Member

But how? You mean using edgeState.getSlope() or something? I don't really see how this would be easier even.

Using a DecimalEncodedValue like edgeState.get(averageSlope). We would have to feed this value at import time from what you wrote above:

average_slope := (elevation_gain(forward) - elevation_gain(backward)) / distance

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:

rough_average_slope := (elevation at A - elevation at B) / distance

(and in my memories I only compared rough_average_slope with average_slope and I picked the rough values but this might have changed.)

But anyway, I'm not sure if such artifacts are a good reason not to allow such geometry-dependent EVs already,

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 "multiply_by": "slope" to avoid hills?). And I would see this here as a later step. btw: how would we use it in custom models?

Not sure if I understand this yet. Do you mean blocking a specific location during import or at query time?

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".

@easbar

easbar commented Nov 2, 2021

Copy link
Copy Markdown
Member Author

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.

Ok, I see, yes this is true and because of this we would not need any special handling for split edges.

Currently I think that the "average_slope" will even give better results (as it hides these artifacts)

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.

btw: how would we use it in custom models?

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 average_slope could be used in a similar way (average_slope > 0.05), but would be less useful.

something like "multiply_by": "slope" to avoid hills

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.

And this average_slope is different to what we currently do in Bike2FlagEncoder:

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?!

@karussell

Copy link
Copy Markdown
Member

but this requires "slope" as a right-hand value and it would be hard to tell how this influences the weight

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 "multiply_by": 0.5 * slope or even a math function "multiply_by": sqrt(slope) and have an exact definition. We need this to not only avoid steep sections, but pick one of many possible routes with different elevation changes e.g. assume you have a route with mild incline and then one with sharper incline. (The alternative would be to use some kind of a "step function" with many if's)

because the absolute value of "slope" might be unknown or hard to compare to the factors used in other rules

Why? Couldn't we define the slope like it is usually defined in meters of elevation gain or loss per 100m?

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?!

Ah, yes, indeed. Probably I tried something else with involving pillar nodes too basing the speed changes on the total elevation gain + loss.

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

Probably. But before we come to this challenge we need to tune the elevation data.

@easbar

easbar commented Nov 3, 2021 •

Copy link
Copy Markdown
Member Author

Yes, but IMO we need this. Otherwise introducing and tweaking "avoid or prefer hills" might be too complicated.

Using "if": "max_slope > 0.1" would be a very simple approach to avoiding hills and not require any changes to the custom model language. What is and what isn't "too complicated" probably strongly depends on the exact use case. I don't see how this draft is any more complicated than introducing right-hand encoded values. I'm not saying we don't need right hand values for some use-cases and this issue should also not be about whether we need right-hand values or geometry encoded values. They are just two different tools and maybe we even need both.

We could allow a factor in-between "multiply_by": 0.5 * slope or even a math function "multiply_by": sqrt(slope) and have an exact definition.

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.

We need this to not only avoid steep sections, but pick one of many possible routes with different elevation changes e.g. assume you have a route with mild incline and then one with sharper incline. (The alternative would be to use some kind of a "step function" with many if's)

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 too_steep,steep,flat`. But as long as we only look at the average slope, which might completely hide the elevation changes along the way, I'm not sure using a continuous function over a step function is the most critical improvement. And again, the question is not really whether we need right hand values or geometry encoded values, because they do not exclude each other at all.

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.

Why? Couldn't we define the slope like it is usually defined in meters of elevation gain or loss per 100m?

Sure we could define some convention. What I meant was that this will be hard to put in relation to other rules like if tunnel multiply_by: 0.1. But if we had the ability to scale the right hand side with constant factors or mathematical functions we could deal with this, yes.

Probably. But before we come to this challenge we need to tune the elevation data.

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.

@karussell

Copy link
Copy Markdown
Member

I don't see how this draft is any more complicated than introducing right-hand encoded values.

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 :)

Using "if": "max_slope > 0.1" would be a very simple approach to avoiding hills and not require any changes to the custom model language

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.

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.

Yes. I meant that we do not necessarily need this PR and could feed an ordinary average_slope EV.

@easbar

easbar commented Nov 3, 2021

Copy link
Copy Markdown
Member Author

I do not want to hinder something you would like to push :)

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. I meant that we do not necessarily need this PR and could feed an ordinary average_slope EV

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants