Skip to content

Road Elevation Smoothing - #1220

Merged
karussell merged 13 commits into
graphhopper:masterfrom
boldtrn:smooth_elevation
Dec 29, 2017
Merged

karussell merged 13 commits into
graphhopper:masterfrom
boldtrn:smooth_elevation

Conversation

@boldtrn

@boldtrn boldtrn commented Nov 15, 2017 •

Copy link
Copy Markdown
Member

This PR fixes/approaches #517.

The problem with GH currently is that the elevation data contains spikes and the overall calculated ascend and descend are sometimes wrong by a factor of two or more.

Because of the increased calculation of ascend and descend, the distance can be wrong as well, because GH calculates the distance of a road using lat, lon, and elevation.

This PR proposes a rather simple approach. Before adding an edge to the graph, we smooth it's elevation, removing most spikes. Take the geometry of the edge and for every "pillar" but not "tower" node of the geometry, look at the surrounding points and calculate the average of these points.

TODO:
I am not sure if it would be better to rate points that are farther away with a lower weight? I tried this briefly and this led to worse results. Probably because outliers will have a greater impact.

Here are some comparisons:

Schauinsland
GH Maps 29.46km ↗1891m ↘1515m
1220schauinsland-gh

PR 28.96km ↗1208m ↘828m
1220schauinsland-pr

Google Maps 28.0km ↗902m ↘542m
1220schauinsland-gm

From Grabenstetten to St. Johan.

GH Maps 15.72km ↗907m ↘863m
1220johann-gh

PR 15.37km ↗491m ↘446m
1220johann-pr

Google Maps 15.3 km ↗328m ↘277m
1220johann-gm

Neuffen.

GH Maps 5.15km ↗613m ↘332m
1220neuffen-gh

PR 4.86km ↗360m ↘82m
1220neuffen-pr

Google Maps 4.8km ↗293m ↘14m
1220neuffen-gm

I also tried routes #330 and #533. It seems like both issues are already fixed because there has been a CGIAR data update recently. However I showed how the smoothing works in these cases with a screenshot below.

Routes from #330.
Herrnstein
1220herrnstein-unsmoothed
1220herrnstein-smoothed

Routes from #553.

Bad Hindelang
1220badhindelang-unsmoothed
1220badhindelang-smoothed

@boldtrn

boldtrn commented Nov 20, 2017 •

Copy link
Copy Markdown
Member Author

I just tried to apply a LOESS regression instead of a "simple average" approach. For most cases this produced comparable or slightly better results than the average function, but sometimes it completely failed resulting in elevation values of 0, for example for this route, I just commited it so that interested users could try this approach or maybe there is some room for tuning the LoessInterpolator? Unless we find a way to fix these issues, I would remove the Loess approach for the final PR again.

BTW: I removed the LOESS approach in 0d0c685. If anyone wants to try it out, have a look at cdab456.

@boldtrn

boldtrn commented Nov 22, 2017

Copy link
Copy Markdown
Member Author

I updated this PR to also smooth the Tower Nodes, I am not 100% sure yet, if this is a good thing to do or not. Most cases improved due to that, but a few became worse. Maybe it might be better to use a high precision elevation data source and only smooth pillar nodes?

@boldtrn

boldtrn commented Nov 27, 2017

Copy link
Copy Markdown
Member Author

I updated this PR. The smoothing happens now in the OSMReader.

For two reasons:

  1. If we change the elevation, we have to update the FlagEncoders like Bike2Weight after the elevation was updated.
  2. The elevation spikes actually lead to a wrong distance. One example would be this route. GraphHopper Maps states it is 4.91km, after the smoothing it is 4.65km. Google Maps 4.6km. Here 4.6km.

Currently it is designed as an "opt-in" feature, so you have to specify graph.elevation.smooth_elevation=true.

@karussell

Copy link
Copy Markdown
Member

Cool, thanks! So it is no longer a WIP?

@boldtrn

boldtrn commented Nov 27, 2017

Copy link
Copy Markdown
Member Author

Cool, thanks! So it is no longer a WIP?

I need to go through the code one more time. It is working, I just want to clean up a couple of minor things and need to fix some tests, etc. Feel free to try it though :). I think it should be ready for review tomorrow 👍.

@boldtrn boldtrn changed the title [WIP] Road Elevation Smoothing Road Elevation Smoothing Nov 28, 2017
@boldtrn
boldtrn requested a review from karussell November 28, 2017 07:13
@boldtrn

boldtrn commented Nov 28, 2017

Copy link
Copy Markdown
Member Author

Cool, thanks! So it is no longer a WIP?

I updated and cleaned up this PR. I think this should be good to merge now.

public DataReader setSmoothElevation(boolean smoothElevation) {
// TODO implement elevation smoothing for shape files
return this;
}

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.

Do we need elevation smoothing for shape files?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can be done later

@karussell karussell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

private final static int MAX_SEARCH_DISTANCE = 180;

public static PointList smoothElevation(PointList geometry) {
if (geometry.size() >= MIN_GEOMETRY_SIZE) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we do not need check this IMO due to i=1 and i<size-1

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.

Yes true, I had it there because I was experimenting with different min geometry sizes to see if there is a difference. Also when I played with the LOESS regression, this required at least 7? points to work.

for (int i = 1; i < geometry.size() - 1; i++) {
int start = i - MAX_SEARCH_RADIUS < 0 ? 0 : i - MAX_SEARCH_RADIUS;
// +1 because we check for "< end"
int end = i + MAX_SEARCH_RADIUS + 1 >= geometry.size() ? geometry.size() : i + MAX_SEARCH_RADIUS + 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this MAX_SEARCH_RADIUS necessary additionally to MAX_SEARCH_DISTANCE?

(also naming should be like MAX_NODES or something)

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.

Points are not evenly scattered. For highways one point might be far away. You are right about the naming, I will improve this, thanks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

But isn't the search distance the only thing we want?

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.

But isn't the search distance the only thing we want?

Good point :). I will have to try and compare the results 👍.

@boldtrn boldtrn Dec 27, 2017 •

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.

I did that. I think this makes indeed more sense. Overall the differences weren't huge in my tests. I think we might need to improve this over time if we find cases where the smoothing is not ideal.

for (int j = start; j < end; j++) {
// We skip points that are too far away, important for motorways
if (MAX_SEARCH_DISTANCE > Helper.DIST_PLANE.calcDist(geometry.getLat(i), geometry.getLon(i), geometry.getLat(j), geometry.getLon(j))) {
sum += geometry.getEle(j);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

What I tried so far was to weight closer points as more important. This made the results worse, see my comment (copied it below). What we could try is to weight closer points with a lower weight. But this sounds wrong to me, but I could try it if you think this would make sense?

I am not sure if it would be better to rate points that are farther away with a lower weight? I tried this briefly and this led to worse results. Probably because outliers will have a greater impact.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, ok. Did you apply the changed formula ala (x*ele1+y*ele2)/(x+y)?

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.

Yes, but I didn't use the actual distance from the points but onĺy the array index. I can do more experiments using the weighted arithmetic mean if you want?

@karussell karussell added this to the 0.10 milestone Dec 4, 2017

// elevation
String eleProviderStr = args.get("graph.elevation.provider", "noop").toLowerCase();
this.smoothElevation = args.getBool("graph.elevation.smooth_elevation", false);

@karussell karussell Dec 4, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd slightly prefer to avoid "elevation" wording duplication and instead use graph.elevation.smoothing?

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.

Yes makes sense, I will change this :).

@karussell

Copy link
Copy Markdown
Member

There seems to be a test failure (or is this unrelated?)

GraphHopperWebIT.testRetrieveOnlyStreetname:123 expected:<[Turn right onto B 24]6> but was:<[Continue onto B 9]6>

@boldtrn

boldtrn commented Dec 29, 2017 •

Copy link
Copy Markdown
Member Author

There seems to be a test failure (or is this unrelated?)

This seems to be unrelated, but I fixed it anyway. The routing on the server changed (maybe some OSM data changed?), not sure why, but the route looks OK to me.

@karussell

Copy link
Copy Markdown
Member

Ah, ok. Didn't notice we are testing here against the server - thanks!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants