55CREATE SCHEMA pgosm_flex_faker ;
66
77
8- CREATE PROCEDURE pgosm_flex_faker .location_in_place_landuse ()
8+ CREATE PROCEDURE pgosm_flex_faker .point_in_place_landuse ()
99LANGUAGE plpgsql
1010AS $$
1111BEGIN
1212
13- -- Define a custom `place_osm_types ` table before executing to customize areas
14- CREATE TEMP TABLE IF NOT EXISTS place_osm_types AS
13+ -- Define a custom `landuse_osm_types ` table before executing to customize areas
14+ CREATE TEMP TABLE IF NOT EXISTS landuse_osm_types AS
1515 SELECT ' retail' AS osm_type
1616 UNION
1717 SELECT ' commercial' AS osm_type
@@ -24,16 +24,19 @@ BEGIN
2424 WITH base AS (
2525 SELECT osm_id, name, osm_type, admin_level, nest_level,
2626 -- Rounding is assuming SRID 3857, or another unit in Meters or Feet.
27- ROUND(public .ST_Area (geom)::NUMERIC , 0 ) AS geom_area, geom
27+ ROUND(public .ST_Area (geom)::NUMERIC , 0 ) AS geom_area,
28+ geom
2829 FROM osm .place_polygon_nested
2930 -- Using innermost places to reduce likelihood over overlap
3031 WHERE innermost
31- AND name <> ' '
32- AND admin_level < 99
32+ -- originally had following more strict checks, considering leaving
33+ -- them off to make more flexible
34+ /* AND name <> ''
35+ AND admin_level < 99*/
3336 ), with_space AS (
3437 -- Within each Place, find how many places are "near" (intersects)
3538 -- or contain the types of places (commercial, retail, residential, etc)
36- -- defined in place_osm_types
39+ -- defined in landuse_osm_types
3740 SELECT b .osm_id ,
3841 COUNT (lp .osm_id ) AS near_areas,
3942 COALESCE(SUM (public .ST_Area (lp .geom )), 0 ) AS near_space,
@@ -42,10 +45,10 @@ BEGIN
4245 FROM base b
4346 LEFT JOIN osm .landuse_polygon lp
4447 ON public .ST_Intersects (b .geom , lp .geom )
45- AND lp .osm_type IN (SELECT osm_type FROM place_osm_types )
48+ AND lp .osm_type IN (SELECT osm_type FROM landuse_osm_types )
4649 LEFT JOIN osm .landuse_polygon c
4750 ON public .ST_Contains (b .geom , c .geom )
48- AND c .osm_type IN (SELECT osm_type FROM place_osm_types )
51+ AND c .osm_type IN (SELECT osm_type FROM landuse_osm_types )
4952 GROUP BY b .osm_id
5053 )
5154 SELECT b.* , ws .contained_areas , ws .contained_space ,
@@ -117,6 +120,7 @@ BEGIN
117120 CREATE TEMP TABLE selected AS
118121 WITH a AS (
119122 SELECT p .osm_id ,
123+ -- Range of total_score: .02 - .65
120124 s .contained_space_score + s .near_space_score
121125 AS total_score,
122126 random() as rnd
@@ -179,21 +183,28 @@ BEGIN
179183 ;
180184
181185
182- -- ---------------------------------------
183- -- Identify roads where a building could be
184- -- Not using actual buildings / addresses because:
185- -- -- a) privacy
186- -- -- b) coverage
186+ /*
187+ Identify roads where a building could be
188+ Not using actual buildings / addresses because:
189+ a) privacy
190+ b) coverage
191+
192+ Main limitation of this is the point chosen on the road could extend far
193+ outside of the landuse.
194+ As I'm writing these initial versions I don't care, consider splitting road
195+ lines on the place boundaries to limit in the future if desired.
196+ */
187197 DROP TABLE IF EXISTS selected_roads ;
188198 CREATE TEMP TABLE selected_roads AS
189199 WITH road_ranks AS (
190- SELECT p .osm_id AS place_osm_id, p .name AS place_name,
200+ SELECT p .osm_id AS place_osm_id, p .osm_type AS place_osm_type,
201+ p .name AS place_name,
191202 rr .normalized_rnk AS road_type_score,
192203 r .osm_id AS road_osm_id
193204 FROM faker_place_polygon p
194205 INNER JOIN osm .landuse_polygon c
195206 ON public .ST_Contains (p .geom , c .geom )
196- AND c .osm_type IN (SELECT osm_type FROM place_osm_types )
207+ AND c .osm_type IN (SELECT osm_type FROM landuse_osm_types )
197208 INNER JOIN osm .road_line r
198209 ON c .geom && r .geom
199210 AND r .route_motor
@@ -216,8 +227,8 @@ BEGIN
216227
217228 DROP TABLE IF EXISTS faker_store_location;
218229 CREATE TEMP TABLE faker_store_location AS
219- SELECT a .place_osm_id , a .place_name , a .road_osm_id ,
220- r .osm_type , r .name , r .ref ,
230+ SELECT a .place_osm_id , a .place_osm_type , a . place_name , a .road_osm_id ,
231+ r .osm_type AS road_osm_type , r .name AS road_name , r .ref AS road_ref ,
221232 public .ST_LineInterpolatePoint (public .ST_LineMerge (r .geom ), random()) AS geom
222233 FROM selected_roads a
223234 INNER JOIN osm .road_line r ON a .road_osm_id = r .osm_id
0 commit comments