Skip to content

Commit ada7c8a

Browse files
committed
Add common table polygons
1 parent 4213f3c commit ada7c8a

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

Diff for: analysers/Analyser_Osmosis.py

+50-6
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,39 @@ class Analyser_Osmosis(Analyser):
204204
CREATE INDEX idx_multipolygons_poly_proj ON {0}.multipolygons USING gist(poly_proj);
205205
CREATE INDEX idx_multipolygons_tags ON {0}.multipolygons USING gist (tags);
206206
ANALYZE {0}.multipolygons;
207+
"""
208+
209+
sql_create_polygons = """
210+
CREATE UNLOGGED TABLE {0}.polygons AS
211+
SELECT
212+
'R' AS type,
213+
id,
214+
tags,
215+
poly,
216+
poly_proj
217+
FROM
218+
{0}.multipolygons
219+
WHERE
220+
is_valid
221+
UNION ALL
222+
SELECT
223+
'W' AS type,
224+
id,
225+
tags,
226+
ST_MakePolygon(linestring) AS poly,
227+
ST_MakePolygon(ST_Transform(linestring, {1})) AS poly_proj
228+
FROM
229+
ways
230+
WHERE
231+
tags != ''::hstore AND
232+
is_polygon AND
233+
ST_IsValid(ST_MakePolygon(ST_Transform(linestring, {1})))
234+
;
235+
236+
CREATE INDEX idx_polygons ON {0}.polygons USING gist(poly);
237+
CREATE INDEX idx_polygons_proj ON {0}.polygons USING gist(poly_proj);
238+
CREATE INDEX idx_polygons_tags ON {0}.polygons USING gist(tags);
239+
ANALYZE {0}.polygons;
207240
"""
208241

209242
sql_create_buildings = """
@@ -368,6 +401,15 @@ def requires_tables_build(self, tables):
368401
elif table == 'touched_multipolygons':
369402
self.requires_tables_build(['multipolygons'])
370403
self.create_view_touched('multipolygons', 'R')
404+
elif table == 'polygons':
405+
self.requires_tables_build(["multipolygons"])
406+
self.giscurs.execute(self.sql_create_polygons.format(self.config.db_schema.split(',')[0], self.config.options.get("proj")))
407+
elif table == 'touched_polygons':
408+
self.requires_tables_build(["polygons"])
409+
self.create_view_touched('polygons', ['W', 'R'])
410+
elif table == 'not_touched_polygons':
411+
self.requires_tables_build(["polygons"])
412+
self.create_view_not_touched('polygons', ['W', 'R'])
371413
elif table == 'buildings':
372414
self.giscurs.execute(self.sql_create_buildings.format(self.config.db_schema.split(',')[0], self.config.options.get("proj")))
373415
elif table == 'touched_buildings':
@@ -497,10 +539,11 @@ def create_view_touched(self, table, type, id = 'id'):
497539
FROM
498540
{0}
499541
JOIN transitive_touched ON
500-
transitive_touched.data_type = '{1}' AND
501-
{0}.{2} = transitive_touched.id
542+
transitive_touched.data_type = ANY(%s) AND
543+
{0}.{1} = transitive_touched.id
502544
"""
503-
self.giscurs.execute(sql.format(table, type, id))
545+
type = type if isinstance(type, (list, tuple)) else [type]
546+
self.giscurs.execute(sql.format(table, id), (type, ))
504547

505548
def create_view_not_touched(self, table, type, id = 'id'):
506549
"""
@@ -513,12 +556,13 @@ def create_view_not_touched(self, table, type, id = 'id'):
513556
FROM
514557
{0}
515558
LEFT JOIN transitive_touched ON
516-
transitive_touched.data_type = '{1}' AND
517-
{0}.{2} = transitive_touched.id
559+
transitive_touched.data_type = ANY(%s) AND
560+
{0}.{1} = transitive_touched.id
518561
WHERE
519562
transitive_touched.id IS NULL
520563
"""
521-
self.giscurs.execute(sql.format(table, type, id))
564+
type = type if isinstance(type, (list, tuple)) else [type]
565+
self.giscurs.execute(sql.format(table, id), (type, ))
522566

523567
def run00(self, sql, callback = None):
524568
if self.explain_sql:

Diff for: doc/3-SQL-basics.md

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ The available common tables are (see full definition in [Analyser_Osmosis.py](ht
173173
* `highway_ends`: the start and ends of all highways ways.
174174
* `buildings`: from ways (and not from multipolygon relations), with tags normalization and re-projected in local country _projection_ as _polygons_.
175175
* `multipolygons`: multipolygon relations with relation id, relation tags, geometry and projected geometry, and whether the multipolygon is valid.
176+
* `polygons`: valid polygons from relations and ways with osm object type, id, tags, geometry and projected geometry
176177

177178
The dependencie on this common tables should be declared in the analyzer class:
178179
```python

0 commit comments

Comments
 (0)