Skip to content

Commit 6c335df

Browse files
Merge pull request #2 from rustprooflabs/continued-updates
Renaming things, cleanup, and docs
2 parents 76b7835 + 0191ff8 commit 6c335df

File tree

4 files changed

+77
-23
lines changed

4 files changed

+77
-23
lines changed

db/deploy/001.sql

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ BEGIN;
55
CREATE 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()
99
LANGUAGE plpgsql
1010
AS $$
1111
BEGIN
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

docs/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
- [What is PgOSM Flex Faker?](pgosm-flex-faker.md)
44
- [Quick Start](quick-start.md)
5+
- [Docker image](docker-image.md)

docs/src/docker-image.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Docker image
2+
3+
## Building the image
4+
5+
Build latest. Occasionally run with `--no-cache` to force some software updates.
6+
7+
```bash
8+
docker pull rustprooflabs/pgosm-flex:latest
9+
docker build -t rustprooflabs/pgosm-flex-faker:latest .
10+
```
11+

docs/src/quick-start.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ mkdir ~/pgosm-data
1515
export POSTGRES_USER=postgres
1616
export POSTGRES_PASSWORD=mysecretpassword
1717

18-
docker stop pgosm-faker
19-
docker build -t rustprooflabs/pgosm-flex-faker .
18+
docker pull rustprooflabs/pgosm-flex-faker:latest
2019

2120
docker run --name pgosm-faker -d --rm \
2221
-v ~/pgosm-data:/app/output \
@@ -35,6 +34,8 @@ docker exec -it \
3534
```
3635

3736

37+
## Load Faker Objects
38+
3839
After the data completes processing, load the PgOSM Flex Faker database structures.
3940
This is done using Sqitch.
4041

@@ -48,16 +49,46 @@ Connect to the database and call this stored procedure. The generated data
4849
is left in a temp table, each run of the stored procedure will produce new,
4950
random results.
5051

52+
## Run Faker generation
53+
5154
```sql
52-
CALL pgosm_flex_faker.location_in_place_landuse();
55+
CALL pgosm_flex_faker.point_in_place_landuse();
5356
SELECT COUNT(*) FROM faker_store_location;
5457
```
5558

59+
5660
Save the data somewhere you want, in a non-temp table.
5761

5862
```sql
59-
CREATE TABLE AS my_fake_stores AS
63+
CREATE TABLE my_fake_stores AS
6064
SELECT *
61-
FROM faker_store_locations
65+
FROM faker_store_location
6266
;
6367
```
68+
69+
70+
Rerun, save second set.
71+
72+
```sql
73+
CALL pgosm_flex_faker.point_in_place_landuse();
74+
CREATE TABLE my_fake_stores_v2 AS
75+
SELECT *
76+
FROM faker_store_location
77+
;
78+
```
79+
80+
## Custom Places for Shops
81+
82+
The procedure `pgosm_flex_faker.point_in_place_landuse()` allows overriding
83+
the inclusion of `retail` and `commercial` landuse.
84+
85+
```sql
86+
DROP TABLE IF EXISTS landuse_osm_types;
87+
CREATE TEMP TABLE IF NOT EXISTS landuse_osm_types AS
88+
SELECT 'college' AS osm_type
89+
UNION
90+
SELECT 'recreation_ground' AS osm_type
91+
UNION
92+
SELECT 'vineyard' AS osm_type
93+
;
94+
```

0 commit comments

Comments
 (0)