Skip to content

OSMReader has problems with ways that reference missing nodes - #2221

Merged
easbar merged 13 commits into
masterfrom
missing_osm_nodes
Dec 29, 2020
Merged

easbar merged 13 commits into
masterfrom
missing_osm_nodes

Conversation

@easbar

@easbar easbar commented Dec 21, 2020

Copy link
Copy Markdown
Member

Sometimes OSM ways reference nodes that do not exist in the OSM file. This problem was partly fixed in #19 (yes long ago), but only for the case where the missing nodes where somewhere in the middle of a way. However, when the missing nodes are at the beginning of the way I ran into two problems:

  1. In some cases the entire way is left out and not added to the graph at all
  2. Even worse: In some cases an edge is added with
    edge.getDistance() != calcDistance(edge.fetchWayGeometry(FetchMode.ALL)).

Such missing nodes appear frequently when we extract a box using Osmosis. In this case often the first or last nodes are missing, because the way starts/ends outside of the bounding box. See #1984. I am not sure if this can also happen with original OSM files.

If this data issue only arises when using certain tools like Osmosis I think we should simply reject such data. Its easy to throw an appropriate error in this case and It should be easy enough use some tool to fix the issue before importing it with GH. In this case maybe we can also get rid of the code that was added for #19. Otherwise we would need to add some code that handles this situation correctly. Even leaving out such ways entirely would be an option IMO, but there should definitely be no edges with inconsistent distances, because this leads to problems like #1984.

easbar added a commit that referenced this pull request Dec 28, 2020
@easbar
easbar marked this pull request as ready for review December 29, 2020 10:45
@easbar easbar changed the title WIP: OSMReader has problems with ways that reference missing nodes OSMReader has problems with ways that reference missing nodes Dec 29, 2020
@easbar

easbar commented Dec 29, 2020

Copy link
Copy Markdown
Member Author

This PR now fixes the issue: When there are ways that start/end with references to non-existing nodes the missing nodes are ignored. I also added two checks that run during the import and make sure that the edge distance always matches the edge geometry and the coordinates of the first/last points in the temporary pointList match those of the tower nodes. The first check is now possible because of #2222.

int lastExisting = -1;
for (int i = 0; i < osmNodeIds.size(); ++i) {
final long tmpNode = getNodeMap().get(osmNodeIds.get(i));
if (tmpNode > -TOWER_NODE || tmpNode < TOWER_NODE) {

@easbar easbar Dec 29, 2020 •

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.

Btw one of the major difficulties I had when trying to understand OSMReader is that the node map is used in two different ways during the import. In the first pass it simply separates between nodes that occur in two or more, only one or zero ways. For this we use the TOWER/PILLAR/EMPTY_NODE constants. But then in the second run we use the same map and assign pillar/tower node ids and this leads to very confusing code like this:

  // skip osmIds with no associated pillar or tower id (e.g. !OSMReader.isBounds)
                if (tmpNode == TOWER_NODE)
                    continue;

                if (tmpNode == PILLAR_NODE) {

where tmpNode == TOWER/PILLAR_NODE does not mean that tmpNode is a tower/pillar node but it means something very different(!): Because no 'real' tower/pillar node id was assigned for this node it means that tmpNode does not exist.

This is probably the first thing I would clean up in OSMReader, because it really tripped me up. I understand that we probably want to re-use the map for performance reasons, but at least we should use a second reference to it (using a different name) or use some helper methods to make this more clear. Then again I wonder if we could exploit the fact that during the first pass we only need to distinguish three values (zero ways, one way, multiple ways) to achieve faster lookups somehow.

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 could exploit the fact that during the first pass we only need to distinguish three values

For example we could use a LongHashSet to store all OSM node Ids that occur in any way and a second (smaller) one to keep track which of these nodes are tower nodes. Not sure if this would be an improvement, because before pass2 we would still have to build the mapping between OSM node Ids and GH tower/pillar IDs.

@karussell karussell Dec 29, 2020 •

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 at least we should use a second reference to it (using a different name) or use some helper methods to make this more clear.

👍

For example we could use a LongHashSet to store all OSM node Ids that occur in any way

The problem might be for big maps with millions of nodes as sets behave very ugly then: when increasing the size you double the size (additionally to the load factor of usually 0.75) and so you will need 3*1/0.75 = 4 and then you need to copy such big arrays.
But surely we can just try as the memory usage requirements changed significantly over the last years. Still at some point not too far in the past I tried with a database like mapdb but it was still much slower and used more resources (disk+memory).

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.

Ok yes this was just an idea, its probably hard to say without trying it. Also I am not even sure if there is really much need to speed up the OSM parsing and improving readability seems more important.

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.

Yes, a speed up is not necessary. Still I would like it, if the new version is not much slower :) (maybe max 2x slower? probably depends on the gained readability)

Comment on lines +578 to +580
// #2221: ways might include nodes at the beginning or end that do not exist -> skip them
int firstExisting = -1;
int lastExisting = -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.

The actual fix is a bit lengthy, but rather trivial: We scan the osmNodeIds list from the beginning and end to find the first node that does exist (and skip those that do not exist).

@easbar

easbar commented Dec 29, 2020 •

Copy link
Copy Markdown
Member Author

If this data issue only arises when using certain tools like Osmosis I think we should simply reject such data. Its easy to throw an appropriate error in this case and It should be easy enough use some tool to fix the issue before importing it with GH.

This still seems like a good solution to the problem to me, but I am not sure if these data errors occur in e.g. Geofabrik extracts. Also some of our testing maps contain these kind of errors so we would have to replace them as well. For now I think we should apply the fix (and maybe consider this again when fixing #2220, #2223 etc.)

@easbar
easbar requested a review from karussell December 29, 2020 11:47

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

Sometimes OSM ways reference nodes that do not exist in the OSM file.

The assumption before was that this will never happen (in geofabrik PBFs this did not occur to my knowledge). But is nice that we know and partially fix this now when it happens.

Also some of our testing maps contain these kind of errors

Interesting. Whenever I created a test PBF I used the export feature of openstreetmap.org and if it is a pbf converted the xml to a pbf with probably osmosis. So maybe the most tools are not 100% strict.

@easbar

easbar commented Dec 29, 2020

Copy link
Copy Markdown
Member Author

The assumption before was that this will never happen (in geofabrik PBFs this did not occur to my knowledge). But is nice that we know and partially fix this now when it happens.

We actually already handled a similar case before, see #19. But this was only for missing nodes in the middle of a way. The worst part was that we did not throw an error in case some nodes were missing unexpectedly.

@easbar
easbar merged commit 7881b5c into master Dec 29, 2020
@easbar
easbar deleted the missing_osm_nodes branch December 29, 2020 13:25
@easbar easbar added this to the 3.0 milestone Dec 29, 2020
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