Repository navigation
Interpolates elevation for tunnels and bridges, fixes issue #713 - #798
highsource wants to merge 16 commits into
Conversation
otherway it was failing with "Graph not prepared for highway=forestry" error.
Thanks! The DataFlagEncoder is a bit unpolished at the moment.
This is correct and will continue to grow probably to 64bit. E.g. we store the maximum speed for cars currently forward&backward leading already to 2*5bits - we could probably do a bit better if we do not store multiples of 5 but use e.g. 0,1,2,3,4, ... for common speeds like 5, 10, 20, 30, 50? Also the intention is to fully move everything to this encoder (as often "raw data" is necessary) and potentially breaking the
You mean interpolation here? I would not make this configurable. I would enable interpolation as soon as elevation is enabled and the DataFlagEncoder is there. |
|
I understood the intention of the
Yes, interpolation. I've added these configuration options for a backward compatibility path. I'm OK to drop it. Should I do it or could you do it upon merge? |
What is even worse: we need two speed values for hgv and potentially others too. Highly likely a dynamic storage size per edge should be introduced but then we cannot easily use it like an array and would need a binary search for every index. Not sure yet how to solve this efficiently.
Currently no one uses DataFlagEncoder so we should be save :)
I can do this |
| // throw new RuntimeException("do not call flagsDefault"); | ||
| // TODO This is called on each of the encoders so I had to replace the runtime exception with something, | ||
| // but I'm not sure this is correct. | ||
| return setAccess(0, forward, backward); |
There was a problem hiding this comment.
Will have a look why this is called and what to do, normally it is:
long flags = speedEncoder.setDefaultValue(0);
return setAccess(flags, forward, backward);There was a problem hiding this comment.
I think speedEncoder was null, but I'll check again. I did not want to change the DataFlagEncoder a lot, that did not feel completely finished yet and it was not in the focus.
| @Test | ||
| public void testHighwaySpeed() { | ||
| Map<String, Double> map = new HashMap<>(); | ||
| Map<String, Double> map = new LinkedHashMap<>(); |
| final FlagEncoder genericFlagEncoder = ghStorage.getEncodingManager().getEncoder("generic"); | ||
| if (!(genericFlagEncoder instanceof DataFlagEncoder)) { | ||
| throw new IllegalStateException("Generic flag encoder equired for elevation interpolation of bridges and tunnels is enabled but does not habe the expected type " + DataFlagEncoder.class.getName() + "."); | ||
| } else { |
There was a problem hiding this comment.
I know this is a bit of taste: but I prefer to avoid nested if-else and here one could avoid the else as the if stops in every case.
There was a problem hiding this comment.
Ok, that's really a matter of taste. I prefer this type if/else blocks, make me think less (like I don't have to check if the if block stops or not). I'd like to leave it as is if it's not that important for you.
| import gnu.trove.set.TIntSet; | ||
| import gnu.trove.set.hash.TIntHashSet; | ||
|
|
||
| public abstract class AbstractEdgeElevationInterpolator { |
There was a problem hiding this comment.
Would you mind to add one sentence description to every new class. I know the current state is not always this way but we should improve inline documentation a bit :)
Also the license header is missing in the new classes, plus feel free to add the @author tag here.
There was a problem hiding this comment.
ps. We could also use License Maven Plugin, it can automatically add or check license headers. I used it for a number of GPL projects and it worked very well.
| int firstNodeId = edge.getBaseNode(); | ||
| int secondNodeId = edge.getAdjNode(); | ||
|
|
||
| double lat0 = storage.getNodeAccess().getLat(firstNodeId); |
There was a problem hiding this comment.
I would assign nodeAccess to a local variable outside of the loop. Potentially the JVM is already doing this but it also reads a bit faster :)
| this.nodeElevationInterpolator = new NodeElevationInterpolator(storage); | ||
| } | ||
|
|
||
| protected abstract boolean isStructureEdge(EdgeIteratorState edge); |
There was a problem hiding this comment.
Can we rename this method? E.g. isInterpolableEdge or something similar?
| public static final double EPSILON = 0.00001; | ||
| public static final double EPSILON2 = EPSILON * EPSILON; | ||
|
|
||
| public double calculateElevation(double lat, double lon, double lat0, double lon0, double ele0, double lat1, |
There was a problem hiding this comment.
It should be more clear what this method does. Especially as the parameters are somehow unexpected. Add a comment here?
There was a problem hiding this comment.
Ah, now I understood. This is the case for 2 outer points, the other calculateElevation methods or for 3 outer points and 4+
There was a problem hiding this comment.
I'll give these methods more elaborate names.
karussell
left a comment
There was a problem hiding this comment.
Thanks!
Would you mind also to check the code formatting, see #770 (comment)
| // throw new RuntimeException("do not call flagsDefault"); | ||
| // TODO This is called on each of the encoders so I had to replace the runtime exception with something, | ||
| // but I'm not sure this is correct. | ||
| return setAccess(0, forward, backward); |
There was a problem hiding this comment.
I'll check why this got called. But we could just remove this method and the super method will be called:
long flags = speedEncoder.setDefaultValue(0);
return setAccess(flags, forward, backward);There was a problem hiding this comment.
I think speedEncoder was null, but I'll check.
There was a problem hiding this comment.
Yes, speedEncoder is null. I'll leave return setAccess(0, forward, backward); for now, I've also filed #800 for this.
There was a problem hiding this comment.
Ah, yes. One would need the carFwdFlagEncoder here ... but we also have backward, hmmh. I would like to get rid of this.
| public class BridgeElevationInterpolatorTest { | ||
|
|
||
| private static final double EPSILON= 0.0000000001d; | ||
| private static final ReaderWay BRIDGE_WAY; |
There was a problem hiding this comment.
I do not like mutable static variables that much - you do not write to it but it is allowed and could then introduce problems for multithreaded unit test execution. In test we also do not need to care about object creation so I'd vote to just remove the static and put the initialization in the @Before method.
| graph = new GraphHopperStorage(new RAMDirectory(), new EncodingManager("generic,foot", 8), | ||
| true, new GraphExtension.NoOpExtension()).create(100); | ||
|
|
||
| dataFlagEncoder = (DataFlagEncoder) graph.getEncodingManager().getEncoder("generic"); |
There was a problem hiding this comment.
BTW: If you dislike casts you can also do:
dataFlagEncoder = new DataFlagEncoder();
FootFlagEncoder foot = new FootFlagEncoder();
graph = new GraphHopperStorage(new RAMDirectory(), new EncodingManager(Arrays.asList(dataFlagEncoder,foot), 8),
true, new GraphExtension.NoOpExtension()).create(100);
|
|
||
| import gnu.trove.set.hash.TIntHashSet; | ||
|
|
||
| public class TunnelElevationInterpolatorTest { |
There was a problem hiding this comment.
Do we really need a separate test for tunnels? What is different with them from the code concept? Or maybe create an abstract tester with two subclasses or one test with two instances?
There was a problem hiding this comment.
Tunnel test is actually the main test. Test for bridges is to smoke-test (if it works at all) as well to check interpolation of pillar nodes. So that's the same concept but these tests test different parts of interpolation.
| } | ||
|
|
||
| @Test | ||
| public void testTunnel() { |
There was a problem hiding this comment.
Strange that this appears as change, as it is already there: https://github.com/graphhopper/graphhopper/blob/master/core/src/test/java/com/graphhopper/routing/util/DataFlagEncoderTest.java#L61-L103
There was a problem hiding this comment.
Perhaps formatting? Spaces vs. tabs.
| @Test | ||
| public void testHighwaySpeed() { | ||
| Map<String, Double> map = new HashMap<>(); | ||
| Map<String, Double> map = new LinkedHashMap<>(); |
There was a problem hiding this comment.
Ah, ups - thanks! Maybe we should require a LinkedHashMap for encoder.getHighwaySpeedMap etc?
There was a problem hiding this comment.
I think it's a bit weird to pass have an implementation class like LinkedHashMap in the signature. I'll think of options.
| assertTrue(arsp.getPoints().is3D()); | ||
|
|
||
| assertEquals(Helper.createPointList3D( | ||
| 43.7405648173518, 7.429926635993867, 17.0, |
There was a problem hiding this comment.
Oh, the many digits after comma are a bit ugly as they suggests a false precision. Should we round it when interpolating e.g. to cm or even just dm?
There was a problem hiding this comment.
This is due to using equals of PointList. Normally doubles are compared with certain precision, this does not work when doing equals on the whole object.
I think I'll just check elevations with some epsilon.
There was a problem hiding this comment.
I meant in general: when we store elevation with so many digits people could assume that we have also very precise data, so regardless of this ugliness in the test it might be wise to cut off precision?
There was a problem hiding this comment.
I think I'll round it to 0.5m. I think that's the precision we get from elevation provider so we shouldn't pretend our interpolation is much better.
Next, 0.5m would allow us to use just 16 bits for elevations (14 bits for 0...16384, 1 bit for sign, 1 bit for .0/0.5).
There was a problem hiding this comment.
0.5m is good here but might not be sufficient in general, not sure. E.g. I know from a customer using GH with more precise elevation data plus doing interpolation in between so they really need cm precision at least otherwise they would have to big steps in-between making some other routing algorithms a bit more ugly
There was a problem hiding this comment.
Ok, unerstood. 0.01m then.
|
Thank you for the review! I'll make the changes and commit shortly. |
Conflicts: core/src/main/java/com/graphhopper/routing/util/DataFlagEncoder.java
| @Test | ||
| public void interpolatesElevationOfTunnelWithFourOuterNodes() { | ||
|
|
||
| // @formatter:off |
There was a problem hiding this comment.
You should be able to trick all formatters via // on all lines
There was a problem hiding this comment.
No, not Eclipse. :( It strips leading spaces in comments.
|
|
||
| @After | ||
| public void tini() { | ||
| graph.close(); |
There was a problem hiding this comment.
Here it is safer to use Helper.close(graph) as this checks for null and null could happen e.g. if init failed.
BTW: tini is a fun name :) I used setUp and tearDown
| /** | ||
| * @author Alexey Valikov | ||
| */ | ||
| public abstract class AbstractEdgeElevationInterpolatorTest { |
There was a problem hiding this comment.
The abstract test class should be named XYTester otherwise it will be executed automatically from maven and fail
There was a problem hiding this comment.
No problem to rename, but I don't think Maven/JUnit does not execute test in abstract classes (I've just rechecked it).
There was a problem hiding this comment.
Ah, ok then you can keep it. Maybe this changed in some more recent maven or junit version.
|
Done. |
| TODO.txt | ||
| *-gh/ | ||
| *.osm | ||
| *.osm* |
There was a problem hiding this comment.
This is a bit dangerous as we have many *osm.gz files under core/files/ ... probably better to create a folder like osm-data (and put the temp osm files there?) which is then ignored?
| * Abstract base class for tunnel/bridge edge elevation interpolators. This | ||
| * class estimates elevation of inner nodes of a tunnel/bridge based on | ||
| * elevations of entry nodes. See | ||
| * <a href="https://github.com/graphhopper/graphhopper/issues/713">#713</a> for |
There was a problem hiding this comment.
I do not link to github explicitly inside of the code and just say #xy. This way we can easily migrate to other issue trackers in the future.
| } | ||
|
|
||
| public double roundToPrecision(double value) { | ||
| return Math.round(value / PRECISION) * PRECISION; |
There was a problem hiding this comment.
See also Helper.round (not sure if applicable though)
|
Thanks again! Merged via 90a3844 |
This is the fix for #713. It adds elevation interpolation for tunnels and bridges.
For this to work you'll need to turn on the
genericflag encoder:This will probably also require using 8 bytes for flags:
Elevation can be turned off via:
(Both settings are
trueby default.)See this tweet for example.
How it works.
For each tunnel (or bridge) edge, the algorithm first finds a connected component of tunnel (bridge) edges. Thus we can find "inner" and "outer" nodes of the tunnel. Outer nodes have tunnel and non-tunnel edges whereas inner nodes only have tunnel edges. Outer nodes are considered to be on the Earth surface, therefore we can trust the elevation provider for their elevation.
Once we've found inner and outer nodes, we'll interpolate elevations of inner nodes based on the elevations of the outer nodes. Interpolation method depends on the number of outer nodes. Cases for 0 or 1 outer nodes are trivial. For 2 outer nodes we use linear interpolation. For 3 outer nodes we use planar interpolation. For 4 and more outer nodes we use the following method.
This is how we get elevations of the inner nodes. Elevation of pillar points is then calculated using linear interpolation using distances from the pilar node to the tower nodes.
Few further hints.
DataFlagEncoder.flagsDefault, as it was called during initialization.motorroadandforrestryto thehighwayListinDataFlagEncoder, otherwise GraphHopper was failing withGraph not prepared for highway=forestryerror during initialization.DataFlagEncoderseems to require 26 bits which I think is a bit too much. It does not feel completely finished yet, but is sufficient for this issue.I'd be grateful for the review and merge.