Advertisement
tasty_data

Untitled

Feb 27th, 2023
1,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE TABLE nodes (
  2.   id SERIAL PRIMARY KEY,
  3.   geom GEOMETRY(POINT, 3857)
  4. );
  5. CREATE TABLE edges (
  6.   id SERIAL PRIMARY KEY,
  7.   source INTEGER NOT NULL,
  8.   target INTEGER NOT NULL,
  9.   geom geometry(LineString, 3857),
  10.   cost FLOAT8
  11. );
  12. INSERT INTO nodes (geom) VALUES
  13. (ST_SetSRID(ST_MakePoint(-74.0059, 40.7128), 3857)),
  14. (ST_SetSRID(ST_MakePoint(-73.9866, 40.7484), 3857)),
  15. (ST_SetSRID(ST_MakePoint(-73.9904, 40.7397), 3857)),
  16. (ST_SetSRID(ST_MakePoint(-73.9991, 40.7250), 3857)),
  17. (ST_SetSRID(ST_MakePoint(-73.9926, 40.7488), 3857));
  18. CREATE INDEX idx_nodes_geom ON nodes USING GIST (geom);
  19. CREATE TABLE distances AS
  20. SELECT a.id AS source, b.id AS target, ST_Distance(a.geom, b.geom) AS cost, st_setsrid(st_makeline(a.geom, b.geom), 3857) as geom
  21. FROM nodes a, nodes b;
  22. INSERT INTO edges (source, target, cost, geom)
  23. SELECT source, target, cost, geom FROM distances;
  24. SELECT pgr_createTopology('edges', 0.0001, 'geom', 'id');
  25.  
  26. SELECT DISTINCT geom FROM edges;
  27.  
  28. CREATE OR REPLACE VIEW road_ext AS
  29.     SELECT *, st_startpoint(geom), st_endpoint(geom)
  30.     FROM edges;
  31.  
  32. DROP TABLE node;
  33. CREATE TABLE node AS
  34. SELECT row_number() OVER (ORDER BY foo.p)::integer AS id,
  35. foo.p AS the_geom
  36. FROM (
  37. SELECT DISTINCT road_ext.st_startpoint AS p FROM road_ext
  38. UNION
  39. SELECT DISTINCT road_ext.st_endpoint AS p FROM road_ext
  40. ) foo
  41. GROUP BY foo.p;
  42.  
  43.  
  44. DROP TABLE network;
  45. CREATE TABLE network AS
  46. SELECT a.*, b.id as start_id, c.id as end_id
  47. FROM road_ext AS a
  48. JOIN node AS b ON a.st_startpoint = b.the_geom
  49. JOIN node AS c ON a.st_endpoint = c.the_geom;
  50.  
  51.  
  52.  
  53. SELECT id AS id,
  54. start_id AS source,
  55. end_id AS target,
  56. ST_Distance(st_startpoint, st_endpoint) AS cost,
  57. ST_X(st_startpoint) as x1,
  58. ST_Y(st_startpoint) as y1,
  59. ST_X(st_endpoint) as x2,
  60. ST_Y(st_endpoint) as y2
  61. FROM network;
  62.  
  63. SELECT * FROM pgr_dijkstra('
  64. SELECT id AS id,
  65. start_id AS source,
  66. end_id AS target,
  67. ST_Distance(st_startpoint, st_endpoint) AS cost,
  68. ST_X(st_startpoint) as x1,
  69. ST_Y(st_startpoint) as y1,
  70. ST_X(st_endpoint) as x2,
  71. ST_Y(st_endpoint) as y2
  72. FROM network',
  73. 1,
  74. 4, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement