Skip to content

Commit

Permalink
Add common table polygons
Browse files Browse the repository at this point in the history
  • Loading branch information
frodrigo committed Jul 8, 2024
1 parent 26fbee4 commit b4fab34
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
56 changes: 50 additions & 6 deletions analysers/Analyser_Osmosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,39 @@ class Analyser_Osmosis(Analyser):
CREATE INDEX idx_multipolygons_poly_proj ON {0}.multipolygons USING gist(poly_proj);
CREATE INDEX idx_multipolygons_tags ON {0}.multipolygons USING gist (tags);
ANALYZE {0}.multipolygons;
"""

sql_create_polygons = """
CREATE UNLOGGED TABLE {0}.polygons AS
SELECT
'R' AS type,
id,
tags,
poly,
poly_proj
FROM
{0}.multipolygons
WHERE
is_valid
UNION ALL
SELECT
'W' AS type,
id,
tags,
ST_MakePolygon(linestring) AS poly,
ST_MakePolygon(ST_Transform(linestring, {1})) AS poly_proj
FROM
ways
WHERE
tags != ''::hstore AND
is_polygon AND
ST_IsValid(ST_MakePolygon(ST_Transform(linestring, {1})))
;
CREATE INDEX idx_polygons ON {0}.polygons USING gist(poly);
CREATE INDEX idx_polygons_proj ON {0}.polygons USING gist(poly_proj);
CREATE INDEX idx_polygons_tags ON {0}.polygons USING gist(tags);
ANALYZE {0}.polygons;
"""

sql_create_buildings = """
Expand Down Expand Up @@ -368,6 +401,15 @@ def requires_tables_build(self, tables):
elif table == 'touched_multipolygons':
self.requires_tables_build(['multipolygons'])
self.create_view_touched('multipolygons', 'R')
elif table == 'polygons':
self.requires_tables_build(["multipolygons"])
self.giscurs.execute(self.sql_create_polygons.format(self.config.db_schema.split(',')[0], self.config.options.get("proj")))
elif table == 'touched_polygons':
self.requires_tables_build(["polygons"])
self.create_view_touched('polygons', ['W', 'R'])
elif table == 'not_touched_polygons':
self.requires_tables_build(["polygons"])
self.create_view_not_touched('polygons', ['W', 'R'])
elif table == 'buildings':
self.giscurs.execute(self.sql_create_buildings.format(self.config.db_schema.split(',')[0], self.config.options.get("proj")))
elif table == 'touched_buildings':
Expand Down Expand Up @@ -497,10 +539,11 @@ def create_view_touched(self, table, type, id = 'id'):
FROM
{0}
JOIN transitive_touched ON
transitive_touched.data_type = '{1}' AND
{0}.{2} = transitive_touched.id
transitive_touched.data_type = ANY(%s) AND
{0}.{1} = transitive_touched.id
"""
self.giscurs.execute(sql.format(table, type, id))
type = type if isinstance(type, (list, tuple)) else [type]
self.giscurs.execute(sql.format(table, id), (type, ))

def create_view_not_touched(self, table, type, id = 'id'):
"""
Expand All @@ -513,12 +556,13 @@ def create_view_not_touched(self, table, type, id = 'id'):
FROM
{0}
LEFT JOIN transitive_touched ON
transitive_touched.data_type = '{1}' AND
{0}.{2} = transitive_touched.id
transitive_touched.data_type = ANY(%s) AND
{0}.{1} = transitive_touched.id
WHERE
transitive_touched.id IS NULL
"""
self.giscurs.execute(sql.format(table, type, id))
type = type if isinstance(type, (list, tuple)) else [type]
self.giscurs.execute(sql.format(table, id), (type, ))

def run00(self, sql, callback = None):
if self.explain_sql:
Expand Down
1 change: 1 addition & 0 deletions doc/3-SQL-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ The available common tables are (see full definition in [Analyser_Osmosis.py](ht
* `highway_ends`: the start and ends of all highways ways.
* `buildings`: from ways (and not from multipolygon relations), with tags normalization and re-projected in local country _projection_ as _polygons_.
* `multipolygons`: multipolygon relations with relation id, relation tags, geometry and projected geometry, and whether the multipolygon is valid.
* `polygons`: valid polygons from relations and ways with osm object type, id, tags, geometry and projected geometry

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

0 comments on commit b4fab34

Please sign in to comment.