diff --git a/src/pg/sql/26_distance_matrix.sql b/src/pg/sql/26_distance_matrix.sql new file mode 100644 index 0000000..bf782ca --- /dev/null +++ b/src/pg/sql/26_distance_matrix.sql @@ -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;