adds initial version of distance matrix function
This commit is contained in:
45
src/pg/sql/26_distance_matrix.sql
Normal file
45
src/pg/sql/26_distance_matrix.sql
Normal file
@@ -0,0 +1,45 @@
|
||||
-- Calculate the distance matrix using underlying road network
|
||||
-- Sample usage:
|
||||
-- select * from cdb_distancematrix("fake_drains", "cvxopt_fake_sources")
|
||||
CREATE OR REPLACE FUNCTION CDB_DistanceMatrix(
|
||||
origin_table regclass,
|
||||
destination_table regclass,
|
||||
transit_mode text DEFAULT 'car')
|
||||
RETURNS TABLE(origin_id bigint, destination_id bigint,
|
||||
the_geom geometry(geometry, 4326),
|
||||
length_km numeric, duration_sec numeric)
|
||||
AS $$
|
||||
DECLARE
|
||||
query_string text;
|
||||
BEGIN
|
||||
query_string := format('
|
||||
WITH pairs AS (
|
||||
SELECT
|
||||
o."cartodb_id" AS origin_id,
|
||||
d."cartodb_id" AS destination_id,
|
||||
o."the_geom" AS origin_point,
|
||||
d."the_geom" AS destination_point
|
||||
FROM
|
||||
(SELECT * FROM %I) AS o,
|
||||
(SELECT * FROM %I) AS d),
|
||||
results AS (
|
||||
SELECT
|
||||
origin_id,
|
||||
destination_id,
|
||||
(cdb_route_point_to_point(origin_point,
|
||||
destination_point,
|
||||
''%s'')).*
|
||||
FROM pairs)
|
||||
SELECT
|
||||
origin_id::bigint AS origin_id,
|
||||
destination_id::bigint AS destination_id,
|
||||
shape AS the_geom,
|
||||
length::numeric AS length_km,
|
||||
duration::numeric AS duration_sec
|
||||
FROM results;', origin_table, destination_table, transit_mode);
|
||||
RAISE NOTICE '%', query_string;
|
||||
RETURN QUERY
|
||||
EXECUTE query_string;
|
||||
RETURN;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
Reference in New Issue
Block a user