55
66-- first, find matches based on watershed code
77-- where there is more than one match, match to closest
8- INSERT INTO whse_basemapping .fwa_streams_watersheds_lut
9- (
10- linear_feature_id,
11- watershed_feature_id
12- )
13- SELECT DISTINCT ON (linear_feature_id)
14- s .linear_feature_id ,
15- w .watershed_feature_id
16- FROM whse_basemapping .fwa_stream_networks_sp s
17- INNER JOIN whse_basemapping .fwa_watersheds_poly w
18- ON (s .wscode_ltree = w .wscode_ltree AND
19- s .localcode_ltree = w .localcode_ltree AND
20- s .watershed_group_code = w .watershed_group_code )
21- WHERE s .watershed_group_code = :' wsg'
22- AND s .fwa_watershed_code NOT LIKE ' 999%'
23- AND s .local_watershed_code IS NOT NULL
24- AND s .edge_type != 6100
25- ORDER BY s .linear_feature_id , ST_Distance(ST_LineInterpolatePoint(s .geom , .5 ), ST_Centroid(w .geom ));
8+ BEGIN ;
269
10+ INSERT INTO whse_basemapping .fwa_streams_watersheds_lut
11+ (
12+ linear_feature_id,
13+ watershed_feature_id
14+ )
15+ SELECT DISTINCT ON (linear_feature_id)
16+ s .linear_feature_id ,
17+ w .watershed_feature_id
18+ FROM whse_basemapping .fwa_stream_networks_sp s
19+ INNER JOIN whse_basemapping .fwa_watersheds_poly w
20+ ON (s .wscode_ltree = w .wscode_ltree AND
21+ s .localcode_ltree = w .localcode_ltree AND
22+ s .watershed_group_code = w .watershed_group_code )
23+ WHERE s .watershed_group_code = :' wsg'
24+ AND s .fwa_watershed_code NOT LIKE ' 999%'
25+ AND s .local_watershed_code IS NOT NULL
26+ AND s .edge_type != 6100
27+ ORDER BY s .linear_feature_id , ST_Distance(ST_LineInterpolatePoint(s .geom , .5 ), ST_Centroid(w .geom ));
2728
28- -- For streams with no matching watershed based on watershed codes,
29- -- do a spatial join, selecting watershed that intersects the midpoint of the stream,
30- -- and where more than one watershed intersects, select the one with the closest centroid
31- INSERT INTO whse_basemapping .fwa_streams_watersheds_lut
32- (
33- linear_feature_id,
34- watershed_feature_id
35- )
36- SELECT DISTINCT ON (linear_feature_id)
37- s .linear_feature_id ,
38- w .watershed_feature_id
39- FROM whse_basemapping .fwa_stream_networks_sp s
40- LEFT JOIN whse_basemapping .fwa_streams_watersheds_lut l
41- ON s .linear_feature_id = l .linear_feature_id
42- INNER JOIN whse_basemapping .fwa_watersheds_poly w
43- ON ST_Intersects(ST_LineInterpolatePoint(s .geom , .5 ), w .geom )
44- WHERE l .watershed_feature_id IS NULL -- extract only streams that are not already matched
45- AND s .watershed_group_code = :' wsg'
46- AND s .fwa_watershed_code NOT LIKE ' 999%'
47- AND s .local_watershed_code IS NOT NULL
48- AND s .edge_type != 6100
49- ORDER BY s .linear_feature_id , ST_Distance(ST_LineInterpolatePoint(s .geom , .5 ), ST_Centroid(w .geom ));
29+
30+ -- For streams with no matching watershed based on watershed codes,
31+ -- do a spatial join, selecting watershed that intersects the midpoint of the stream,
32+ -- and where more than one watershed intersects, select the one with the closest centroid
33+ INSERT INTO whse_basemapping .fwa_streams_watersheds_lut
34+ (
35+ linear_feature_id,
36+ watershed_feature_id
37+ )
38+ SELECT DISTINCT ON (linear_feature_id)
39+ s .linear_feature_id ,
40+ w .watershed_feature_id
41+ FROM whse_basemapping .fwa_stream_networks_sp s
42+ LEFT JOIN whse_basemapping .fwa_streams_watersheds_lut l
43+ ON s .linear_feature_id = l .linear_feature_id
44+ INNER JOIN whse_basemapping .fwa_watersheds_poly w
45+ ON ST_Intersects(ST_LineInterpolatePoint(s .geom , .5 ), w .geom )
46+ WHERE l .watershed_feature_id IS NULL -- extract only streams that are not already matched
47+ AND s .watershed_group_code = :' wsg'
48+ AND s .fwa_watershed_code NOT LIKE ' 999%'
49+ AND s .local_watershed_code IS NOT NULL
50+ AND s .edge_type != 6100
51+ ORDER BY s .linear_feature_id , ST_Distance(ST_LineInterpolatePoint(s .geom , .5 ), ST_Centroid(w .geom ));
52+
53+ COMMIT ;
0 commit comments