Skip to content

Commit cb57910

Browse files
committed
Tidy up functionality into a single post-import script. Rename from geo-faker to geofaker. Cleanup
1 parent 800f1a6 commit cb57910

File tree

9 files changed

+79
-94
lines changed

9 files changed

+79
-94
lines changed

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
FROM rustprooflabs/pgosm-flex
22

33
COPY ./db /app/faker/db
4-
4+
COPY ./faker.ini /app/flex-config/layerset/
5+
COPY ./run_faker.sh /app/
6+
COPY ./run_faker.sql /app/

db/deploy/001.sql

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
BEGIN;
44

5-
CREATE SCHEMA geo_faker;
5+
CREATE SCHEMA geofaker;
66

77

8-
CREATE PROCEDURE geo_faker.point_in_place_landuse()
8+
CREATE PROCEDURE geofaker.point_in_place_landuse()
99
LANGUAGE plpgsql
1010
AS $$
1111
BEGIN
@@ -223,7 +223,7 @@ BEGIN
223223

224224
DROP TABLE IF EXISTS faker_store_location;
225225
CREATE TEMP TABLE faker_store_location AS
226-
SELECT ROW_NUMBER() OVER () AS id, a.place_osm_id, a.place_osm_type, a.place_name, a.road_osm_id,
226+
SELECT ROW_NUMBER() OVER () AS store_id, a.place_osm_id, a.place_osm_type, a.place_name, a.road_osm_id,
227227
r.osm_type AS road_osm_type, r.name AS road_name, r.ref AS road_ref,
228228
public.ST_LineInterpolatePoint(public.ST_LineMerge(r.geom), random()) AS geom
229229
FROM selected_roads a
@@ -235,13 +235,13 @@ END
235235
$$
236236
;
237237

238-
COMMENT ON PROCEDURE geo_faker.point_in_place_landuse IS 'Uses osm.landuse_polygon and osm.road_line to simulate probable locations for commercial store locations. Can be customized for custom landuse types by manually defining landuse_osm_types temp table.'
238+
COMMENT ON PROCEDURE geofaker.point_in_place_landuse IS 'Uses osm.landuse_polygon and osm.road_line to simulate probable locations for commercial store locations. Can be customized for custom landuse types by manually defining landuse_osm_types temp table.'
239239
;
240240

241241

242242
-- From: https://trac.osgeo.org/postgis/wiki/UserWikiRandomPoint
243243

244-
CREATE FUNCTION geo_faker.n_points_in_polygon(geom geometry, num_points integer)
244+
CREATE FUNCTION geofaker.n_points_in_polygon(geom geometry, num_points integer)
245245
RETURNS SETOF geometry
246246
LANGUAGE plpgsql VOLATILE
247247
COST 100
@@ -288,51 +288,53 @@ END
288288
$$
289289
;
290290

291-
COMMENT ON FUNCTION geo_faker.n_points_in_polygon(GEOMETRY, INT) IS 'Creates N points randomly within the given polygon.';
291+
COMMENT ON FUNCTION geofaker.n_points_in_polygon(GEOMETRY, INT) IS 'Creates N points randomly within the given polygon.';
292292

293293

294294

295295
-- Call the procedure to ensure the required temp table exists, avoids deploy failure
296-
CALL geo_faker.point_in_place_landuse();
296+
CALL geofaker.point_in_place_landuse();
297297

298298

299-
CREATE PROCEDURE geo_faker.points_around_point()
300-
LANGUAGE plpgsql
301-
AS $$
302-
DECLARE
303-
stores_to_process BIGINT;
299+
CREATE PROCEDURE geofaker.points_around_point()
300+
LANGUAGE plpgsql
301+
AS $$
302+
DECLARE
303+
stores_to_process BIGINT;
304304
t_row faker_store_location%rowtype;
305-
BEGIN
305+
BEGIN
306306

307-
SELECT COUNT(*) INTO stores_to_process
308-
FROM faker_store_location
309-
;
310-
RAISE NOTICE 'Stores to process: %', stores_to_process;
307+
SELECT COUNT(*) INTO stores_to_process
308+
FROM faker_store_location
309+
;
310+
RAISE NOTICE 'Generating customers for % stores...', stores_to_process;
311311

312-
DROP TABLE IF EXISTS faker_customer_location;
313-
CREATE TEMP TABLE faker_customer_location
314-
(
315-
id BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
316-
store_id BIGINT NOT NULL,
317-
customer_id BIGINT NOT NULL,
318-
geom GEOMETRY(POINT, 3857) NOT NULL
319-
);
312+
DROP TABLE IF EXISTS faker_customer_location;
313+
CREATE TEMP TABLE faker_customer_location
314+
(
315+
id BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
316+
store_id BIGINT NOT NULL,
317+
customer_id BIGINT NOT NULL,
318+
geom GEOMETRY(POINT, 3857) NOT NULL
319+
);
320320

321321

322322
FOR t_row IN SELECT * FROM faker_store_location LOOP
323-
RAISE NOTICE 'Store ID: %', t_row.id;
323+
IF t_row.store_id % 10 = 0 THEN
324+
RAISE NOTICE 'Store ID: %', t_row.store_id;
325+
END IF;
324326

325327
DROP TABLE IF EXISTS place_buffer;
326328
CREATE TEMP TABLE place_buffer AS
327-
SELECT id AS store_id, geom, ST_Buffer(geom, 5000) AS geom_buffer
329+
SELECT store_id, geom, ST_Buffer(geom, 5000) AS geom_buffer
328330
FROM faker_store_location
329-
WHERE id = t_row.id
331+
WHERE store_id = t_row.store_id
330332
;
331333

332334
DROP TABLE IF EXISTS store_potential_customers;
333335
CREATE TEMP TABLE store_potential_customers AS
334336
SELECT store_id,
335-
geo_faker.n_points_in_polygon(geom_buffer, 1000)
337+
geofaker.n_points_in_polygon(geom_buffer, 1000)
336338
AS geom
337339
FROM place_buffer
338340
;
@@ -374,7 +376,7 @@ END;
374376
$$;
375377

376378

377-
COMMENT ON PROCEDURE geo_faker.points_around_point IS 'Creates fake customer locations around a store. Locations are snapped to roads. Locations not scoped to landuse at this time. Requires faker_store_location temp table with fake store data.';
379+
COMMENT ON PROCEDURE geofaker.points_around_point IS 'Creates fake customer locations around a store. Locations are snapped to roads. Locations not scoped to landuse at this time. Requires faker_store_location temp table with fake store data.';
378380

379381
COMMIT;
380382

db/revert/001.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
BEGIN;
44

5-
DROP SCHEMA geo_faker CASCADE;
5+
DROP SCHEMA geofaker CASCADE;
66

77
COMMIT;

docs/book.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ title = "Geo Faker"
88
[output.html]
99
default-theme = "rust"
1010
preferred-dark-theme = "navy"
11-
git-repository-url = "https://github.com/rustprooflabs/geo-faker"
11+
git-repository-url = "https://github.com/rustprooflabs/geofaker"
1212
git-repository-icon = "fa-github"

docs/src/docker-image.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Build latest. Occasionally run with `--no-cache` to force some software updates
88

99
```bash
1010
docker pull rustprooflabs/pgosm-flex:latest
11-
docker build -t rustprooflabs/geo-faker:latest .
11+
docker build -t rustprooflabs/geofaker:latest .
1212
```
1313

1414

1515
```bash
16-
docker push rustprooflabs/geo-faker:latest
16+
docker push rustprooflabs/geofaker:latest
1717
```

docs/src/quick-start.md

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The basic process to using Geo Faker are:
1919
Load the region/subregion you want using the PgOSM Flex Docker image.
2020
These instructions are modified from
2121
[PgOSM Flex's Quick Start](https://pgosm-flex.com/quick-start.html)
22-
section. The following loads the data into a PostGIS enabled database in a `geo-faker`
22+
section. The following loads the data into a PostGIS enabled database in a `geofaker`
2323
Docker container available on port 5433.
2424

2525

@@ -28,84 +28,35 @@ mkdir ~/pgosm-data
2828
export POSTGRES_USER=postgres
2929
export POSTGRES_PASSWORD=mysecretpassword
3030

31-
docker pull rustprooflabs/geo-faker:latest
31+
docker pull rustprooflabs/geofaker:latest
3232

33-
docker run --name pgosm-faker -d --rm \
33+
docker run --name geofaker -d --rm \
3434
-v ~/pgosm-data:/app/output \
35-
-v ~/git/geo-faker/:/custom-layerset \
3635
-v /etc/localtime:/etc/localtime:ro \
36+
-e POSTGRES_USER=$POSTGRES_USER \
3737
-e POSTGRES_PASSWORD=$POSTGRES_PASSWORD \
38-
-p 5433:5432 -d rustprooflabs/geo-faker
38+
-p 5433:5432 -d rustprooflabs/geofaker
3939

4040
docker exec -it \
41-
pgosm-faker python3 docker/pgosm_flex.py \
41+
geofaker python3 docker/pgosm_flex.py \
4242
--ram=8 \
4343
--region=north-america/us \
4444
--subregion=nebraska \
45-
--layerset=faker_layerset \
46-
--layerset-path=/custom-layerset/
45+
--layerset=faker
4746
```
4847

4948

50-
## Load Faker Objects
49+
## Load and Run Faker Objects
5150

5251
After the data completes processing, load the Geo Faker database structures
53-
in the `geo_faker` schema.
52+
in the `geofaker` schema.
5453
This is done using Sqitch.
5554

5655

5756
```bash
58-
cd geo-faker/db
59-
sqitch db:pg://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5433/pgosm deploy
57+
docker exec -it geofaker /bin/bash run_faker.sh
6058
```
6159

6260
Connect to the database and call this stored procedure. The generated data
6361
is left in a temp table, each run of the stored procedure will produce new,
6462
random results.
65-
66-
## Run Faker generation
67-
68-
There are two stored procedures in the `geo_faker` schema that
69-
generate the fake stores and customers.
70-
71-
72-
The stored procedure `geo_faker.point_in_place_landuse()` places points
73-
along roads that are within (or nearby) specific `landuse` areas. The generated
74-
data is available after calling the stored procedure in a temporary table
75-
named `faker_store_location`.
76-
The generated data is scoped to named places currently, though that will
77-
likely become adjustable in the future.
78-
79-
80-
The `geo_faker.point_in_place_landuse()` stored procedure requires
81-
the `faker_store_location` temp table created by the first stored procedure.
82-
83-
84-
85-
```sql
86-
CALL geo_faker.point_in_place_landuse();
87-
CALL geo_faker.points_around_point();
88-
```
89-
90-
The following query saves the data in a new, non-temporary table named
91-
`my_fake_stores`.
92-
93-
94-
95-
96-
97-
98-
```sql
99-
CREATE TABLE my_fake_stores AS
100-
SELECT *
101-
FROM faker_store_location
102-
;
103-
104-
CREATE TABLE my_fake_customers AS
105-
SELECT *
106-
FROM faker_customer_location
107-
;
108-
```
109-
110-
111-
File renamed without changes.

run_faker.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
echo 'Deploying geofaker schema via Sqitch...'
3+
cd /app/faker/db
4+
sqitch db:pg://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/pgosm deploy
5+
6+
echo 'Running GeoFaker generation...'
7+
psql -d postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost/pgosm \
8+
-f /app/run_faker.sql

run_faker.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
CALL geofaker.point_in_place_landuse();
2+
CALL geofaker.points_around_point();
3+
4+
5+
DROP TABLE IF EXISTS geofaker.store;
6+
CREATE TABLE geofaker.store AS
7+
SELECT *
8+
FROM faker_store_location
9+
ORDER BY store_id
10+
;
11+
COMMENT ON TABLE geofaker.store IS 'Created by Geo Faker, a PgOSM Flex based project.';
12+
13+
DROP TABLE IF EXISTS geofaker.customer;
14+
CREATE TABLE geofaker.customer AS
15+
SELECT *
16+
FROM faker_customer_location
17+
ORDER BY store_id, customer_id
18+
;
19+
COMMENT ON TABLE geofaker.customer IS 'Created by Geo Faker, a PgOSM Flex based project.';
20+
21+
22+

0 commit comments

Comments
 (0)