forked from azavea/pfb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.sql
67 lines (60 loc) · 2.07 KB
/
paths.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
----------------------------------------
-- INPUTS
-- location: neighborhood
-- proj: :nb_output_srid psql var must be set before running this script,
-- e.g. psql -v nb_output_srid=4326 -f paths.sql
----------------------------------------
DROP TABLE IF EXISTS generated.neighborhood_paths;
DROP INDEX IF EXISTS idx_neighborhood_ways_path_id;
CREATE TABLE generated.neighborhood_paths (
path_id SERIAL PRIMARY KEY,
geom geometry(multilinestring, :nb_output_srid),
path_length INTEGER,
bbox_length INTEGER
);
-- combine contiguous paths
INSERT INTO neighborhood_paths (geom)
SELECT ST_CollectionExtract(
ST_SetSRID(
unnest(ST_ClusterIntersecting(geom)),
:nb_output_srid
),
2 --linestrings
)
FROM neighborhood_ways
WHERE functional_class = 'path';
-- get raw lengths
UPDATE neighborhood_paths
SET path_length = ST_Length(geom);
-- get bounding box lengths
UPDATE neighborhood_paths
SET bbox_length = ST_Length(
ST_SetSRID(
ST_MakeLine(
ST_MakePoint(ST_XMin(geom), ST_YMin(geom)),
ST_MakePoint(ST_XMax(geom), ST_YMax(geom))
),
:nb_output_srid
)
);
-- set path_id on each road segment (if path)
UPDATE neighborhood_ways
SET path_id = (
SELECT paths.path_id
FROM neighborhood_paths paths
WHERE ST_Intersects(neighborhood_ways.geom,paths.geom)
AND ST_CoveredBy(neighborhood_ways.geom,paths.geom)
LIMIT 1
)
WHERE functional_class = 'path';
-- get stragglers
UPDATE neighborhood_ways
SET path_id = paths.path_id
FROM neighborhood_paths paths
WHERE neighborhood_ways.functional_class = 'path'
AND neighborhood_ways.path_id IS NULL
AND ST_Intersects(neighborhood_ways.geom,paths.geom)
AND ST_CoveredBy(neighborhood_ways.geom,ST_Buffer(paths.geom,1));
-- set index
CREATE INDEX idx_neighborhood_ways_path_id ON neighborhood_ways (path_id);
ANALYZE neighborhood_ways (path_id);