Convert *.osm.pbf files to CSV for contraction hierarchies library
With this CLI tool you can convert *.osm.pbf (Compressed Open Street Map) file to CSV (Comma-Separated Values) file, which is used in our contraction hierarchies library. What it does:
- Edge expansion (single edge == single vertex);
- Handles some kind and types of restrictions:
- Supported kind of restrictions:
- EdgeFrom - NodeVia - EdgeTo.
- Supported types of restrictions:
- only_left_turn;
- only_right_turn;
- only_straight_on;
- no_left_turn;
- no_right_turn;
- no_straight_on.
- Supported kind of restrictions:
- Saves CSV file with geom in WKT format;
- Currently supports tags for 'highway' OSM entity only.
PRs are welcome!
-
Via 'go get':
go get github.com/LdDl/osm2ch go install github.com/LdDl/osm2ch/cmd/osm2ch@v1.5.1
After installation step is complete you can call 'osm2ch' from any place in your system.
-
Or download prebuilt binary and make updates in yours PATH environment varibale (both Linux and Windows):
Note: There is zlib support in OSM-pbf parser. If you do not want use it then disable CGO:
export CGO_ENABLED=0
# Then build osm2ch from source
osm2ch -h
Output:
Usage of osm2ch:
-file string
Filename of *.osm.pbf file (it has to be compressed) (default "my_graph.osm.pbf")
-geomf string
Format of output geometry. Expected values: wkt / geojson (default "wkt")
-out string
Filename of 'Comma-Separated Values' (CSV) formatted file (default "my_graph.csv")
E.g.: if file name is 'map.csv' then 3 files will be produced: 'map.csv' (edges), 'map_vertices.csv', 'map_shortcuts.csv'
-tags string
Set of needed tags (separated by commas) (default "motorway,primary,primary_link,road,secondary,secondary_link,residential,tertiary,tertiary_link,unclassified,trunk,trunk_link,motorway_link")
-units string
Units of output weights. Expected values: km for kilometers / m for meters (default "km")
-contract
Prepare contraction hierarchies? (default true)
The default list of tags is this, since usually these tags are used for routing for personal cars.
You can find example file of *.osm.pbf file in nested child /example_data.
If you want WKT format for output geometry:
osm2ch --file example_data/moscow_center_reduced.osm.pbf --out graph.csv --geomf wkt --units m --tags motorway,primary,primary_link,road,secondary,secondary_link,residential,tertiary,tertiary_link,unclassified,trunk,trunk_link,motorway_link --contract=true
If you want GeoJSON format for output geometry:
osm2ch --file example_data/moscow_center_reduced.osm.pbf --out graph.csv --geomf geojson --units m --tags motorway,primary,primary_link,road,secondary,secondary_link,residential,tertiary,tertiary_link,unclassified,trunk,trunk_link,motorway_link --contract=true
If you dont want to prepare contraction hierarchies then:
osm2ch --file example_data/moscow_center_reduced.osm.pbf --out graph.csv --geomf wkt --units m --tags motorway,primary,primary_link,road,secondary,secondary_link,residential,tertiary,tertiary_link,unclassified,trunk,trunk_link,motorway_link --contract=false
After that files 'graph.csv', 'graph_vertices.csv', 'graph_shortcuts.csv' will be created (or only 'graph.csv' and 'graph_vertices' if 'contract' flag is set to False).
Header of edges CSV-file is: from_vertex_id;to_vertex_id;weight;geom;was_one_way;edge_id;osm_way_from;osm_way_to;osm_way_from_source_node;osm_way_from_target_node;osm_way_to_source_node;osm_way_to_target_node
- from_vertex_id - Generated source vertex;
- to_vertex_id - Generated target vertex;
- weight - Traveling cost from source to target (actually length of an edge in kilometers/meters);
- geom - Geometry of edge (Linestring) in WKT or GeoJSON format.
- was_one_way - Boolean value. When source OSM way was "one way" then it's true, otherwise it's false. Might be helpfull for ignore edges with WasOneWay=true when offesting overlapping two-way geometries in some GIS viewer
- edge_id - ID of generated edge
- osm_way_from - ID of source OSM Way
- osm_way_to - ID of target OSM Way
- osm_way_from_source_node - ID of first OSM Node in source OSM Way
- osm_way_from_target_node - ID of last OSM Node in source OSM Way
- osm_way_to_source_node - ID of first OSM Node in target OSM Way
- osm_way_to_target_node - ID of last OSM Node in target OSM Way
Header of vertices CSV-file is: vertex_id;order_pos;importance;geom
- vertex_id - Vertex;
- order_pos - Order position in contraction hierarchies;
- importance - Importance of vertex with respect to contraction hierarchies
- geom - Geometry of vertex (Point) in WKT or GeoJSON format.
[Optional] Header of shortcuts CSV-file is: from_vertex_id;to_vertex_id;weight;via_vertex_id
- from_vertex_id - Source vertex;
- to_vertex_id - Target vertex;
- weight - Traveling cost from source to target (actually length of the shortcut in kilometers/meters);
- via_vertex_id - ID of vertex through which the shortcut exists
Now you can use this graph in contraction hierarchies library.
Thanks to paulmach for his OSM-parser written in Go.
Paulmach's license is here (it's MIT)
- Please see here