[GIS] How to use the pgRouting TSP function

pgroutingpostgispostgresql

For more than two days I'm trying to make the TSP algorithm work in PostgreSQL/PostGIS. My problem seems to occur when I'm trying to take into account the road network for TSP algorithm. I know that the algorithm it will calculate the distance in Euclidean space as a default setting.

As an example:

SELECT vertex_id
FROM tsp('SELECT id as source_id, lon AS x, lat AS y 
          FROM point_layer','23,221,63,432,335,',63);

where the point_layer is the table which holds the points information (as PlaceMarks), numbers like: 23,221,63,432,335 stands for places (cities) and 63 as a starting point.

If you have experience with this approach please advise.

Best Answer

According to the examples and the documentation, you should only be loading the five nodes you are interested in in the select query, so I added a where clause to your select query. You also had a trailing comma, following the list of ids in parameter two that I removed.

So give this a try and see if it works better:

SELECT vertex_id
FROM tsp('SELECT id as source_id, lon AS x, lat AS y 
          FROM point_layer where id in (23,221,63,432,335)','23,221,63,432,335',63);

Here is a link to the doc: http://pgrouting.org/docs/1.x/tsp.html

Related Question