I get results using pgRouting
I cannot explain.
I created a topology
using osm2pgrouting
. I then want to find the shortest way through this topology, for someone walking on foot.
To calculate the costs I wrote a function. This function works, sort of.
CREATE OR REPLACE FUNCTION costs_foot(valueHighway text, costs double
RETURNS double precision AS
$$
SELECT CASE
WHEN $1 IN ('motorway', 'motorway_link', 'motorway_junction', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary', 'tertiary', 'bus_guideway', 'cycleway', 'byway', 'secondary_link', 'tertiary_link', 'lane', 'opposite_lane', 'opposite', 'road', 'unclassified', 'grade1', 'grade2', 'grade3', 'grade4', 'grade5')
THEN -1 -- lines in question
ELSE costs -- lines in question
END
$$
language 'sql';
And then I calculate the route
CREATE TABLE route_foot AS
SELECT route.*, w.the_geom, w.length_m FROM pgr_dijkstra('
SELECT gid AS id,
source,
target,
costs_foot(str_name, cost_s) AS cost,
reverse_cost_s AS reverse_cost
FROM ways',
pgr_pointToEdgeNode('ways', ST_SetSRID(
ST_Point(-73.930397, 40.783351),
4326), 0.01
),
pgr_pointToEdgeNode('ways', ST_SetSRID(
ST_Point(-73.882022, 40.852214),
4326), 0.01),
directed := false) AS route
LEFT JOIN ways w
ON route.edge = w.gid
ORDER BY seq;
That gives me route which goes over highways
, motorways
, ... basically everything I am trying to avoid with -1
.
However, when I change the function to this:
CREATE OR REPLACE FUNCTION costs_foot(valueHighway text, costs double
RETURNS double precision AS
$$
SELECT CASE
WHEN $1 IN ('motorway', 'motorway_link', 'motorway_junction', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary', 'tertiary', 'bus_guideway', 'cycleway', 'byway', 'secondary_link', 'tertiary_link', 'lane', 'opposite_lane', 'opposite', 'road', 'unclassified', 'grade1', 'grade2', 'grade3', 'grade4', 'grade5')
THEN costs -- lines in question
ELSE -1 -- lines in question
END
$$
language 'sql';
I get a route that only choses footways
, pedestrians
, ...
Looking at the logic in the SQL Function
it should be the other way around.
Why is that?