15. Appendix: osm2pgrouting Import ToolΒΆ

osm2pgrouting is a command line tool that allows to import OpenStreetMap data into a pgRouting database. It builds the routing network topology automatically and creates tables for feature types and road classes.

Note

There are some limitations, especially regarding the network size. The way to handle large data sets is to current version of osm2pgrouting needs to load all data into memory, which makes it fast but also requires a lot or memory for large datasets. An alternative tool to osm2pgrouting without the network size limitation is osm2po (http://osm2po.de). It’s available under “Freeware License”.

Raw OpenStreetMap data contains much more features and information than needed for routing. Also the format is not suitable for pgRouting out-of-the-box. An .osm XML file consists of three major feature types:

  • nodes
  • ways
  • relations

Version and Help Options

This workshop use the osm2pgrouting version 2.2

osm2pgrouting -v
osm2pgrouting --help
osm2pgrouting -v
This is osm2pgrouting Version 2.2



osm2pgrouting --help
Allowed options:

Help:
  --help                                Produce help message for this version.
  -v [ --version ]                      Print version string

General:
  -f [ --file ] arg                     REQUIRED: Name of the osm file.
  -c [ --conf ] arg (=/usr/share/osm2pgrouting/mapconfig.xml)
                                        Name of the configuration xml file.
  --schema arg                          Database schema to put tables.
                                          blank: defaults to default schema 
                                                dictated by PostgreSQL 
                                                search_path.
  --prefix arg                          Prefix added at the beginning of the 
                                        table names.
  --suffix arg                          Suffix added at the end of the table 
                                        names.
  --addnodes                            Import the osm_nodes table.
  --clean                               Drop previously created tables.

Database options:
  -d [ --dbname ] arg                   Name of your database (Required).
  -U [ --username ] arg (=postgres)     Name of the user, which have write 
                                        access to the database.
  -h [ --host ] arg (=localhost)        Host of your postgresql database.
  -p [ --port ] arg (=5432)             db_port of your database.
  -W [ --password ] arg                 Password for database access.

An osm file example looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API">
    <note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
    <meta osm_base="2016-03-31T22:37:01Z"/>
    ...	
    <node id="2702022" lat="50.7654175" lon="7.1376180" version="11"
        timestamp="2011-10-30T18:29:52Z"
        changeset="9696743" uid="116044" user="berndw"/>
    <node id="2702027" lat="50.7704923" lon="7.1432462" version="5"
        timestamp="2014-12-09T18:47:47Z"
        changeset="27362963" uid="1751908" user="Blues Brothers"/>
    <node id="2703359" lat="50.7548501" lon="7.0964640" version="10"
        timestamp="2013-10-19T22:50:43Z"
        changeset="18442764" uid="45243" user="odna">
        <tag k="TMC:cid_58:tabcd_1:Class" v="Point"/>
        <tag k="TMC:cid_58:tabcd_1:Direction" v="positive"/>
        <tag k="TMC:cid_58:tabcd_1:LCLversion" v="9.00"/>
        <tag k="TMC:cid_58:tabcd_1:LocationCode" v="39278"/>
        <tag k="TMC:cid_58:tabcd_1:NextLocationCode" v="11766"/>
        <tag k="TMC:cid_58:tabcd_1:PrevLocationCode" v="11765"/>
    </node>
    ...	
    <way id="4372665" version="14"
        timestamp="2012-07-03T10:29:34Z"
        changeset="12098548" uid="133419" user="dachefte">
        <tag k="footway:right:smoothness" v="good"/>
        <tag k="footway:right:surface" v="paving_stones"/>
        <tag k="footway:right:width" v="3"/>
        <tag k="highway" v="unclassified"/>
    </way>
    ...	
    <relation id="399785" version="2"
        timestamp="2012-08-08T18:27:05Z"
        changeset="12660262" uid="55687" user="dschuwa">
        <member type="way" ref="32285676" role="street"/>
        <member type="way" ref="17129333" role="street"/>
        <member type="way" ref="49305040" role="street"/>
        <member type="way" ref="49305039" role="street"/>
        <member type="way" ref="32286326" role="street"/>
        <member type="way" ref="32286325" role="street"/>
        <member type="way" ref="32286327" role="street"/>

        ...	
        <tag k="name" v="Brieger Weg"/>
        <tag k="type" v="associatedStreet"/>
    </relation>
    ...	
</osm>

The detailed description of all possible OpenStretMap types and classes can be found here: http://wiki.openstreetmap.org/index.php/Map_features.

mapconfig.xml

When using osm2pgrouting, we take only nodes and ways of types and classes specified in a mapconfig.xml file that will be imported into the routing database:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <type name="highway" id="1">
    <class name="motorway" id="101" />
    <class name="motorway_link" id="102" />
    <class name="motorway_junction" id="103" />
    ...
    <class name="road" id="100" />
  </type>    
  <type name="junction" id="4">
    <class name="roundabout" id="401" />
  </type>  
</configuration> 

The default mapconfig.xml is installed in /usr/share/osm2pgrouting/.

  • osm2pgrouting creates more tables and imports more attributes than we will use in this workshop.
  • See the description of the tables