Skip to content

Commit 910ab74

Browse files
Merge pull request #4 from rustprooflabs/polish-initial-functionality
Cleanup and streamline
2 parents 800f1a6 + bc2e5f5 commit 910ab74

File tree

10 files changed

+103
-113
lines changed

10 files changed

+103
-113
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: 37 additions & 31 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,57 @@ 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

295-
-- Call the procedure to ensure the required temp table exists, avoids deploy failure
296-
CALL geo_faker.point_in_place_landuse();
295+
-- Ensure the required temp table exists, avoids deploy failure on next sproc
296+
CREATE TEMP TABLE IF NOT EXISTS faker_store_location
297+
(
298+
store_id BIGINT, place_osm_id BIGINT, place_osm_type TEXT, place_name TEXT,
299+
road_osm_id BIGINT, geom GEOMETRY
300+
);
297301

298302

299-
CREATE PROCEDURE geo_faker.points_around_point()
300-
LANGUAGE plpgsql
301-
AS $$
302-
DECLARE
303-
stores_to_process BIGINT;
303+
CREATE PROCEDURE geofaker.points_around_point()
304+
LANGUAGE plpgsql
305+
AS $$
306+
DECLARE
307+
stores_to_process BIGINT;
304308
t_row faker_store_location%rowtype;
305-
BEGIN
309+
BEGIN
306310

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

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-
);
316+
DROP TABLE IF EXISTS faker_customer_location;
317+
CREATE TEMP TABLE faker_customer_location
318+
(
319+
id BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
320+
store_id BIGINT NOT NULL,
321+
customer_id BIGINT NOT NULL,
322+
geom GEOMETRY(POINT, 3857) NOT NULL
323+
);
320324

321325

322326
FOR t_row IN SELECT * FROM faker_store_location LOOP
323-
RAISE NOTICE 'Store ID: %', t_row.id;
327+
IF t_row.store_id % 10 = 0 THEN
328+
RAISE NOTICE 'Store ID: %', t_row.store_id;
329+
END IF;
324330

325331
DROP TABLE IF EXISTS place_buffer;
326332
CREATE TEMP TABLE place_buffer AS
327-
SELECT id AS store_id, geom, ST_Buffer(geom, 5000) AS geom_buffer
333+
SELECT store_id, geom, ST_Buffer(geom, 5000) AS geom_buffer
328334
FROM faker_store_location
329-
WHERE id = t_row.id
335+
WHERE store_id = t_row.store_id
330336
;
331337

332338
DROP TABLE IF EXISTS store_potential_customers;
333339
CREATE TEMP TABLE store_potential_customers AS
334340
SELECT store_id,
335-
geo_faker.n_points_in_polygon(geom_buffer, 1000)
341+
geofaker.n_points_in_polygon(geom_buffer, 1000)
336342
AS geom
337343
FROM place_buffer
338344
;
@@ -374,7 +380,7 @@ END;
374380
$$;
375381

376382

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.';
383+
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.';
378384

379385
COMMIT;
380386

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/customize.md

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,6 @@ This section builds on the [Quick Start](quick-start.md) section.
55
> Warning: This project is in early development! Things will be changing over the first few releases (e.g. before 0.5.0).
66
77

8-
## External Postgres connections
9-
10-
The quick start uses the built-in Postgres/PostGIS instance. See
11-
the PgOSM Flex section on [Using external Postgres connections](https://pgosm-flex.com/postgres-external.html) to use your own Postgres instance.
12-
This approach does load a lot of data to the target database which may not be
13-
desired. Consider using `pg_dump` to load only the target data to your
14-
database of choice.
15-
16-
The Sqitch deployment step should use additional parameters not set in the quick start
17-
instructions.
18-
19-
```bash
20-
source ~/.pgosm-faker-local
21-
cd pgosm-flex-faker/db
22-
sqitch db:pg://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB deploy
23-
```
24-
258

269
## Each time is new data
2710

@@ -54,3 +37,13 @@ UNION
5437
SELECT 'vineyard' AS osm_type
5538
;
5639
```
40+
41+
42+
## External Postgres connections
43+
44+
Geo Faker currently does not support running directly into an external database.
45+
Technically it can be operated that way, but the instructions and helper scripts
46+
are specific to using the in-Docker database.
47+
48+
Use `--pg-dump` to extract the generated data.
49+

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: 15 additions & 60 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,39 @@ 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.
54-
This is done using Sqitch.
52+
in the `geofaker` schema. This deploys the functions and procedures needed,
53+
runs the processing, and runs `pg_dump` saving the faked
54+
data into `~/pgosm-data/geofaker_stores_customers.sql`.
5555

5656

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

62-
Connect to the database and call this stored procedure. The generated data
63-
is left in a temp table, each run of the stored procedure will produce new,
64-
random results.
61+
Load the saved data into a database of your choice.
6562

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-
;
63+
```bash
64+
psql -d pgosm_faker -f ~/pgosm-data/geofaker_stores_customers.sql
10865
```
10966

110-
111-
File renamed without changes.

run_faker.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
echo 'Deploying geofaker schema via Sqitch...'
3+
cd /app/faker/db
4+
sqitch db:pg://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost/pgosm deploy
5+
6+
echo 'Running GeoFaker generation...'
7+
psql -d postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost/pgosm \
8+
-f /app/run_faker.sql
9+
10+
echo 'Running pg_dump...'
11+
pg_dump -d postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost/pgosm \
12+
-n geofaker -f /app/output/geofaker_stores_customers.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)