I'm trying to calculate the cost of travelling in a network from one point to many points (adresses).
To increase the precision of the analysis i want to use the function pgr_withPointsCost
as it (as far as I understand) can calculate to and from points that are not the actual vertexes of the network.
I have loaded osm data into postgres using osm2pgrouting
and I have created a table containing the id's of my points of interest (poi), the id of the closest edge (edge_id
) and the projected location of my poi on the edge as a fraction.
When running the pgr_withPointsCost
query I get an output of 0 rows. Below is my create table
statements and the current query - can anyone help me find the problem with my query?
ways table from OSM2pgrouting :
CREATE TABLE osm_network.herlev_ways
(
gid bigint,
class_id integer,
length double precision,
length_m double precision,
name text,
source bigint,
target bigint,
x1 double precision,
y1 double precision,
x2 double precision,
y2 double precision,
cost double precision,
reverse_cost double precision,
cost_s double precision,
reverse_cost_s double precision,
rule text,
one_way integer,
maxspeed_forward integer,
maxspeed_backward integer,
osm_id bigint,
source_osm bigint,
target_osm bigint,
priority double precision,
the_geom geometry(LineString,4326)
)
Points of interest table:
CREATE TABLE base_data.geo_borger_aktiv
(
ogc_fid serial NOT NULL,
wkb_geometry geometry(Point,25832),
kvh_adr_key character varying,
CONSTRAINT geo_borger_aktiv_pkey PRIMARY KEY (ogc_fid)
)
Nearest network edge to poi - including fraction:
CREATE TABLE base_data.nearest_ways_borger_aktiv
(
id bigint,
pid character varying,
edge_id bigint,
fraction double precision
)
Query:
SELECT *
FROM pgr_withPointsCost('SELECT gid AS id, source,
target, length_m AS cost, length_m AS reverse_cost
FROM osm_network.herlev_ways
WHERE class_id > 105
ORDER BY gid',
'SELECT id*-1 AS pid, edge_id, fraction FROM
base_data.nearest_ways_borger_aktiv',
117558
,
(SELECT array_agg(nearest_ways_borger_aktiv.id*-1)
FROM base_data.nearest_ways_borger_aktiv
WHERE nearest_ways_borger_aktiv.id IS NOT NULL))
UPDATED withPointsCost query:
SELECT *
FROM pgr_withPointsCost('SELECT gid AS id, source, target, length_m AS cost, length_m AS reverse_cost
FROM osm_network.herlev_ways
ORDER BY gid',
'SELECT id AS pid, edge_id, fraction FROM base_data.nearest_ways_borger_aktiv',
-150980,
(SELECT array_agg(nearest_ways_borger_aktiv.id*-1)
FROM base_data.nearest_ways_borger_aktiv
WHERE fraction BETWEEN 0.0001 AND 0.9999
))
Creating edge_id
and fraction
:
CREATE TABLE base_data.nearest_ways_for_busstops AS
SELECT DISTINCT ON(ogc_fid)
ogc_fid AS id,
gid AS edge_id,
ST_LineLocatePoint(ver.the_geom,ST_Transform(akt.wkb_geometry,4326)) AS fraction
FROM osm_network.herlev_ways ver, base_data.nearest_ways_borger_aktiv akt
WHERE ST_DWithin(ver.the_geom, st_transform(akt.wkb_geometry,4326), 0.001)
ORDER BY ogc_fid, ST_Distance(ver.the_geom, ST_Transform(akt.wkb_geometry,4326)) ASC
Points of interest (nearest_ways_borger_aktiv
) visualised, including edge_id
of nearest ways and calculated fraction
, along with ways
with corresponding gid
: