Skip to content

Extract WaySegmentParser from OSMReader - #2448

Merged
easbar merged 21 commits into
masterfrom
reader2
Oct 29, 2021
Merged

easbar merged 21 commits into
masterfrom
reader2

Conversation

@easbar

@easbar easbar commented Oct 28, 2021 •

Copy link
Copy Markdown
Member

OSMReader is quite complicated and does a lot of things. One of its main features is efficiently (fast and low memory usage) finding intersections between OSM ways and splitting these ways into several 'segments' which are then added as edges to the routing graph. This PR extracts a separate class from OSMReader that does just this. This should make it easier to understand, easier to test and easier to customize.

WaySegmentParser provides several callback functions to customize the OSM file parsing. Take for example this very small change that turns GraphHopper into a simple router for waterways: 8d331af. Note that OSMReader is not really needed for this and we can build a graph just with WaySegmentParser. OSMReader is just the default way GraphHopper uses to build a graph, but not the thing that is strictly needed to do so.

Here is a route along the Danube river in Germany:

image

easbar added 20 commits October 16, 2021 23:32
# Conflicts:
#	core/src/main/java/com/graphhopper/reader/osm/OSMReader.java
#	core/src/main/java/com/graphhopper/routing/util/EncodingManager.java
#	core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java
# Conflicts:
#	core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java
# Conflicts:
#	core/src/main/java/com/graphhopper/reader/osm/OSMReader.java
# Conflicts:
#	core/src/main/java/com/graphhopper/reader/osm/OSMReader.java
#	core/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java
@easbar easbar changed the title Extract way segment parser from OSMReader Extract WaySegmentParser from OSMReader Oct 28, 2021
@easbar

easbar commented Oct 28, 2021 •

Copy link
Copy Markdown
Member Author

Just for completeness here is the waterway example again:

 WaySegmentParser waySegmentParser = new WaySegmentParser.Builder(ghStorage.getNodeAccess())
   .setDirectory(ghStorage.getDirectory())
   // setup the OSM ways we are interested in. normally highways, but here we use waterways
   .setWayFilter(way -> way.getNodes().size() > 2 && way.hasTag("waterway"))
     // here we retrieve the segments the OSM ways were split into and already receive 
     // unique ids (from/to) that represent the intersections between the ways. We use these
     // as node ids for our routing graph
   .setEdgeHandler((from, to, pointList, way, nodeTags) -> {
       EdgeIteratorState edge = ghStorage.edge(from, to).setWayGeometry(pointList);
        for (FlagEncoder encoder : encodingManager.fetchEdgeEncoders()) {
         edge.set(encoder.getAccessEnc(), true, true);
         edge.set(encoder.getAverageSpeedEnc(), Math.min(30, encoder.getMaxSpeed()));
         edge.setDistance(DIST_EARTH.calcDistance(pointList));
       }
     })
         
// this is all we really need. now read the file
waySegmentParser.readOSM(osmFile);

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.

1 participant