postgisopenstreetmapshortest-pathpgroutingosm2pgsql

pgrouting routing methods don't deliver shortest path


At first I'm pretty new to routing in general and need to use it for my bachelor thesis so I am pretty time limited to learn it. If there is anymore Information I can give to find the Problem or if you just have a idea please inform me. Thanks!

I have a problem with the routing methods offered from the postgis extension pgrouting where the results are far away from being a shortest path.

I'm using a docker container containing postgis with the extension of pgrouting: https://hub.docker.com/r/pgrouting/pgrouting/

My Graph is created with osm2po on a "Niedersachsen" map from Geofabrik.de.

I just followed the tutorials given from osm2po quickstart to create a graph and put it into my database.

My Table is build as:

CREATE TABLE hh_2po_4pgr(id integer, osm_id bigint, osm_name character varying, osm_meta character varying, osm_source_id bigint, osm_target_id bigint, clazz integer, flags integer, source integer, target integer, km double precision, kmh integer, cost double precision, reverse_cost double precision, x1 double precision, y1 double precision, x2 double precision, y2 double precision);
SELECT AddGeometryColumn('hh_2po_4pgr', 'geom_way', 4326, 'LINESTRING', 2);

The only thing i configured was setting my cost and reverse_cost to the distance. But without this change the issue stays the same. Also i did pgr_anaalyzeGraph which gave me back a OK which normally indicates that the graph should be configured fine.

When i now fire up a query like this:

SELECT * FROM pgr_astar('SELECT id, source, target, cost, x1, y1, x2, y2 FROM hh_2po_4pgr', 232516, 213104, FALSE, 2);

It gives me seemingly random routes that are either empty or way to long. The Id's I'm testing are pretty near to each other and all connected through streets. In this case it should be a route of a few hundret meters but I get a route with over 1000 Segments and almost 100km.

note: I also tryed other functions like pgr_djjkstra.


Solution

  • Solution:

    SELECT source FROM hh_2po_4pgr ORDER BY geom_way <-> ST_SetSRID(ST_Point(:pointX, :pointY),4326);
    
    SELECT target FROM hh_2po_4pgr ORDER BY geom_way <-> ST_SetSRID(ST_Point(:pointX, :pointY),4326);
    

    Use these SELECT Statements to get the right Id's to use in your pgrouting function.

    If anyone has issues on pgrouting pls pn me. If I look at my Stackoverflow messages I will try helping you. Seems like the documentations and tutorials are kind of irritating.