Skip to content

Commit b7657da

Browse files
committed
Filling out documentation a bit. Add function for generating points within polygons for starting point to create fake customer points
1 parent 6c335df commit b7657da

File tree

4 files changed

+115
-29
lines changed

4 files changed

+115
-29
lines changed

db/deploy/001.sql

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ BEGIN
227227

228228
DROP TABLE IF EXISTS faker_store_location;
229229
CREATE TEMP TABLE faker_store_location AS
230-
SELECT a.place_osm_id, a.place_osm_type, a.place_name, a.road_osm_id,
230+
SELECT ROW_NUMBER() OVER () AS id, a.place_osm_id, a.place_osm_type, a.place_name, a.road_osm_id,
231231
r.osm_type AS road_osm_type, r.name AS road_name, r.ref AS road_ref,
232232
public.ST_LineInterpolatePoint(public.ST_LineMerge(r.geom), random()) AS geom
233233
FROM selected_roads a
@@ -240,4 +240,59 @@ $$
240240
;
241241

242242

243+
244+
245+
-- From: https://trac.osgeo.org/postgis/wiki/UserWikiRandomPoint
246+
247+
CREATE FUNCTION pgosm_flex_faker.n_points_in_polygon(geom geometry, num_points integer)
248+
RETURNS SETOF geometry
249+
LANGUAGE plpgsql VOLATILE
250+
COST 100
251+
ROWS 1000
252+
AS $$
253+
DECLARE
254+
target_proportion numeric;
255+
n_ret integer := 0;
256+
loops integer := 0;
257+
x_min float8;
258+
y_min float8;
259+
x_max float8;
260+
y_max float8;
261+
srid integer;
262+
rpoint geometry;
263+
BEGIN
264+
-- Get envelope and SRID of source polygon
265+
SELECT ST_XMin(geom), ST_YMin(geom), ST_XMax(geom), ST_YMax(geom), ST_SRID(geom)
266+
INTO x_min, y_min, x_max, y_max, srid;
267+
-- Get the area proportion of envelope size to determine if a
268+
-- result can be returned in a reasonable amount of time
269+
SELECT ST_Area(geom)/ST_Area(ST_Envelope(geom)) INTO target_proportion;
270+
RAISE DEBUG 'geom: SRID %, NumGeometries %, NPoints %, area proportion within envelope %',
271+
srid, ST_NumGeometries(geom), ST_NPoints(geom),
272+
round(100.0*target_proportion, 2) || '%';
273+
IF target_proportion < 0.0001 THEN
274+
RAISE EXCEPTION 'Target area proportion of geometry is too low (%)',
275+
100.0*target_proportion || '%';
276+
END IF;
277+
RAISE DEBUG 'bounds: % % % %', x_min, y_min, x_max, y_max;
278+
279+
WHILE n_ret < num_points LOOP
280+
loops := loops + 1;
281+
SELECT ST_SetSRID(ST_MakePoint(random()*(x_max - x_min) + x_min,
282+
random()*(y_max - y_min) + y_min),
283+
srid) INTO rpoint;
284+
IF ST_Contains(geom, rpoint) THEN
285+
n_ret := n_ret + 1;
286+
RETURN NEXT rpoint;
287+
END IF;
288+
END LOOP;
289+
RAISE DEBUG 'determined in % loops (% efficiency)', loops, round(100.0*num_points/loops, 2) || '%';
290+
END
291+
$$
292+
;
293+
294+
295+
296+
297+
243298
COMMIT;

docs/src/SUMMARY.md

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

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

docs/src/customize.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Customize
2+
3+
This section builds on the [Quick Start](quick-start.md) section.
4+
5+
6+
## External Postgres connections
7+
8+
The quick start uses the built-in Postgres/PostGIS instance. See
9+
the PgOSM Flex section on [Using external Postgres connections](https://pgosm-flex.com/postgres-external.html) to use your own Postgres instance.
10+
This approach does load a lot of data to the target database which may not be
11+
desired. Consider using `pg_dump` to load only the target data to your
12+
database of choice.
13+
14+
15+
## Each time is new data
16+
17+
Rerun, save second set.
18+
19+
```sql
20+
CALL pgosm_flex_faker.point_in_place_landuse();
21+
CREATE TABLE my_fake_stores_v2 AS
22+
SELECT *
23+
FROM faker_store_location
24+
;
25+
```
26+
27+
## Custom Places for Shops
28+
29+
The procedure `pgosm_flex_faker.point_in_place_landuse()` allows overriding
30+
the inclusion of `retail` and `commercial` landuse.
31+
This is done by creating a custom `landuse_osm_types` table before
32+
running the stored procedure.
33+
34+
35+
36+
```sql
37+
DROP TABLE IF EXISTS landuse_osm_types;
38+
CREATE TEMP TABLE IF NOT EXISTS landuse_osm_types AS
39+
SELECT 'college' AS osm_type
40+
UNION
41+
SELECT 'recreation_ground' AS osm_type
42+
UNION
43+
SELECT 'vineyard' AS osm_type
44+
;
45+
```

docs/src/quick-start.md

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Quick Start to PgOSM Flex Faker
22

3-
Words
3+
This section covers how to get started with the Faker version of PgOSM Flex.
4+
45

56
## Load OpenStreetMap Data
67

@@ -51,44 +52,28 @@ random results.
5152

5253
## Run Faker generation
5354

55+
The stored procedure `pgosm_flex_faker.point_in_place_landuse()` places points
56+
along roads that are within (or nearby) specific `landuse` areas. The generated
57+
data is available after calling the stored procedure in a temporary table
58+
named `faker_store_location`.
59+
The generated data is scoped to named places currently, though that will
60+
likely become adjustable in the future.
61+
62+
5463
```sql
5564
CALL pgosm_flex_faker.point_in_place_landuse();
5665
SELECT COUNT(*) FROM faker_store_location;
5766
```
5867

5968

60-
Save the data somewhere you want, in a non-temp table.
69+
The following query saves the data in a new, non-temporary table named
70+
`my_fake_stores`.
6171

62-
```sql
63-
CREATE TABLE my_fake_stores AS
64-
SELECT *
65-
FROM faker_store_location
66-
;
67-
```
6872

6973

70-
Rerun, save second set.
71-
7274
```sql
73-
CALL pgosm_flex_faker.point_in_place_landuse();
74-
CREATE TABLE my_fake_stores_v2 AS
75+
CREATE TABLE my_fake_stores AS
7576
SELECT *
7677
FROM faker_store_location
7778
;
7879
```
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)