vlarmet / cppRouting

Algorithms for Routing and Solving the Traffic Assignment Problem
106 stars 9 forks source link

Example of using osm2po for osm data extraction #6

Closed mguzmann closed 2 years ago

mguzmann commented 2 years ago

I'm using cppRouting for some work I'm doing with openstreetmaps but I can't figure out how you extracted the data using osm2po. So far I'm creating the graph myself by importing the data with osmextract, and then calculating the distances between vertices but this is very slow for large files.

Could you provide an example of how you did this with osm2po?

vlarmet commented 2 years ago

Hi,

Sorry for this late response. I haven't used it for a while but you need to have PostgreSQL/Postgis installed on your machine. I have found an old batch file on my computer, once you have downloaded a pbf file you have to :

  1. Extract pbf file using osm2po CLI.

java -Xmx3g -jar osm2po-core-5.2.43-signed.jar prefix=output_folder cmd=tjsp tileSize=x \input_folder\spain-latest.osm.pbf postp.0.class=de.cm.osm2po.plugins.postp.PgRoutingWriter It generate a .sql file in the "output_folder".

  1. Run the sql file using psql.exe
set PSQL="C:\Program Files\PostgreSQL\10\bin\psql.exe"    
set PGPORT=5432    
set PGHOST=localhost    
set PGPASSWORD=xxxx    
%PSQL% -U postgres -d OSM -q -f "...\output_folder\hh_2po_4pgr.sql"

Now, a new postgis table (containing edges attributes and geometries) named hh_2po_4pgr is in the OSM database. At this point, you can either import the routable graph directly in R with the RPostgres package, or export it to a csv file.

  1. Optionnal. Export it in csv format

%PSQL% -U postgres -d OSM -c "COPY hh_2po_4pgr(osm_source_id,osm_target_id,km,cost,reverse_cost,x1,y1,x2,y2) TO 'my_folder\spain.csv' DELIMITER ',' CSV HEADER ENCODING 'UTF8';"

After that, you can drop the table :

%PSQL% -U postgres -d OSM -c "DROP TABLE hh_2po_4pgr;"

Hope it help.

MTueting commented 7 months ago

Just wanted to add this note to the previous comment by vlarmet. I think the most efficient way to proceed after exporting the csv is to use data.table in R:

library(data.table)
# Load table
network <- fread("./my_folder/spain.csv")

# Get node coords
nodes <- rbindlist(list(
  network[, .(osm_id = osm_source_id, x = x1, y = y1)],
  network[, .(osm_id = osm_target_id, x = x2, y = y2)]
))

# Remove duplicate rows
coord <- unique(nodes)

# Make directed
# Duplicate the original data table
network_reversed <- copy(network)
# Swap osm_source_id and osm_target_id
network_reversed[, c("osm_source_id", "osm_target_id")] <- network_reversed[, c("osm_target_id", "osm_source_id")]
# Replace cost with reverse_cost
network_reversed[, cost := reverse_cost]
# Combine the original and reversed data tables
reshaped_network <- rbind(network, network_reversed)
reshaped_network[, c("from", "to") := .(osm_source_id, osm_target_id)]
reshaped_network <- reshaped_network[,c("from", "to", "cost")]
# Optionally, order the combined data table if needed
roads <- reshaped_network[order(from, to)]

# Use cppRouting to make graph
head(coord)
head(roads)

After this, we can continue with the examples provided in the vignette.