Class OSMReader

java.lang.Object
com.graphhopper.reader.osm.OSMReader

public class OSMReader extends Object
Parses an OSM file (xml, zipped xml or pbf) and creates a graph from it. The OSM file is actually read twice. During the first scan we determine the 'type' of each node, i.e. we check whether a node only appears in a single way or represents an intersection of multiple ways, or connects two ways. We also scan the relations and store them for each way ID in memory. During the second scan we store the coordinates of the nodes that belong to ways in memory and then split each way into several segments that are divided by intersections or barrier nodes. Each segment is added as an edge of the resulting graph. Afterwards we scan the relations again to determine turn restrictions.
  • Constructor Details

  • Method Details

    • setFile

      public OSMReader setFile(File osmFile)
      Sets the OSM file to be read. Supported formats include .osm.xml, .osm.gz and .xml.pbf
    • setAreaIndex

      public OSMReader setAreaIndex(AreaIndex<CustomArea> areaIndex)
      The area index is queried for each OSM way and the associated areas are added to the way's tags
    • setElevationProvider

      public OSMReader setElevationProvider(ElevationProvider eleProvider)
    • setCountryRuleFactory

      public OSMReader setCountryRuleFactory(CountryRuleFactory countryRuleFactory)
    • readGraph

      public void readGraph() throws IOException
      Throws:
      IOException
    • getDataDate

      public Date getDataDate()
      Returns:
      the timestamp given in the OSM file header or null if not found
    • acceptWay

      protected boolean acceptWay(ReaderWay way)
      This method is called for each way during the first and second pass of the WaySegmentParser. All OSM ways that are not accepted here and all nodes that are not referenced by any such way will be ignored.
    • isBarrierNode

      protected boolean isBarrierNode(ReaderNode node)
      Returns:
      true if the given node should be duplicated to create an artificial edge. If the node turns out to be a junction between different ways this will be ignored and no artificial edge will be created.
    • isCalculateWayDistance

      protected boolean isCalculateWayDistance(ReaderWay way)
      Returns:
      true if the length of the way shall be calculated and added as an artificial way tag
    • setArtificialWayTags

      protected void setArtificialWayTags(PointList pointList, ReaderWay way, double distance, List<Map<String,Object>> nodeTags)
      This method is called during the second pass of WaySegmentParser and provides an entry point to enrich the given OSM way with additional tags before it is passed on to the tag parsers.
    • addEdge

      protected void addEdge(int fromIndex, int toIndex, PointList pointList, ReaderWay way, List<Map<String,Object>> nodeTags)
      This method is called for each segment an OSM way is split into during the second pass of WaySegmentParser.
      Parameters:
      fromIndex - a unique integer id for the first node of this segment
      toIndex - a unique integer id for the last node of this segment
      pointList - coordinates of this segment
      way - the OSM way this segment was taken from
      nodeTags - node tags of this segment. there is one map of tags for each point.
    • preprocessWay

      protected void preprocessWay(ReaderWay way, WaySegmentParser.CoordinateSupplier coordinateSupplier, WaySegmentParser.NodeTagSupplier nodeTagSupplier)
      This method is called for each way during the second pass and before the way is split into edges. We currently use it to parse road names and calculate the distance of a way to determine the speed based on the duration tag when it is present. The latter cannot be done on a per-edge basis, because the duration tag refers to the duration of the entire way.
    • preprocessRelations

      protected void preprocessRelations(ReaderRelation relation)
      This method is called for each relation during the first pass of WaySegmentParser
    • processRelation

      protected void processRelation(ReaderRelation relation, LongToIntFunction getIdForOSMNodeId)
      This method is called for each relation during the second pass of WaySegmentParser We use it to save the relations and process them afterwards.
    • getArtificialEdgesByEdges

      public com.carrotsearch.hppc.IntIntMap getArtificialEdgesByEdges()
    • toString

      public String toString()
      Overrides:
      toString in class Object