Skip to content

Commit

Permalink
Simplify landcover layer generalisation SQL (#1255)
Browse files Browse the repository at this point in the history
I was reading through the SQL used to generalise the landcover layer ([`layers/landcover/generalized.sql`](https://github.com/openmaptiles/openmaptiles/blob/596f44aa26bf3711cd5775d111105e09a34916a0/layers/landcover/generalized.sql)) and I noticed that when creating the generalised landcover tables for zooms 9-13, the SQL was doing something like this:

```sql
SELECT * FROM simplify_vw_z13 WHERE ST_NPoints(geometry) < 50
UNION ALL
SELECT * FROM simplify_vw_z13 WHERE ST_NPoints(geometry) >= 50 AND ST_NPoints(geometry) < 300
```

As in, the simplification/clustering of landcover generalisations was being done in two steps: once for polygons with fewer than fifty points, and once for polygons with 50+ points.

As far as I can see, there's no benefit to doing this — it's probably just an artefact of the dev work done for the [original pull request](#1035). Of course, I might be completely missing the reason for clustering polygons into two groups (<50 points and 50+ points). But assuming I haven't, I've created this pull request to simplify the SQL used in the generalisation, merging the two steps into something like this:

```sql
SELECT * FROM simplify_vw_z13 WHERE ST_NPoints(geometry) < 300
```

The effect is to slightly reduce the number of landcover features (polygons with <50 points can now be clustered/unioned with those 50+), and to reduce the time taken to generalise the landcover tables (in a small test using OSM's Iceland data I saw a 7% reduction in the time spent). It doesn't alter the features that are shown on the map.

Builds upon work from commits 4a1b0af and da689f9.
  • Loading branch information
flother committed Oct 8, 2021
1 parent 91c6d3b commit 4d6945b
Showing 1 changed file with 5 additions and 75 deletions.
80 changes: 5 additions & 75 deletions layers/landcover/generalized.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,12 @@ CREATE INDEX ON simplify_vw_z13 USING GIST (geometry);
-- etldoc: simplify_vw_z13 -> osm_landcover_gen_z13
CREATE TABLE osm_landcover_gen_z13 AS
(
SELECT subclass,
ST_MakeValid(
(ST_dump(
ST_Union(geometry))).geom) AS geometry
FROM (
SELECT subclass,
ST_ClusterDBSCAN(geometry, eps := 0, minpoints := 1) over () AS cid, geometry
FROM simplify_vw_z13
WHERE ST_NPoints(geometry) < 50
AND subclass IN ('wood', 'forest')) union_geom50
GROUP BY subclass,
cid
UNION ALL
SELECT subclass, ST_MakeValid((ST_dump(ST_Union(geometry))).geom) AS geometry
FROM (
SELECT subclass,
ST_ClusterDBSCAN(geometry, eps := 0, minpoints := 1) over () AS cid, geometry
FROM simplify_vw_z13
WHERE ST_NPoints(geometry) >= 50
AND ST_NPoints(geometry) < 300
WHERE ST_NPoints(geometry) < 300
AND subclass IN ('wood', 'forest')) union_geom300
GROUP BY subclass,
cid
Expand Down Expand Up @@ -79,26 +65,12 @@ CREATE INDEX ON simplify_vw_z12 USING GIST (geometry);
-- etldoc: simplify_vw_z12 -> osm_landcover_gen_z12
CREATE TABLE osm_landcover_gen_z12 AS
(
SELECT subclass,
ST_MakeValid(
(ST_dump(
ST_Union(geometry))).geom) AS geometry
FROM (
SELECT subclass,
ST_ClusterDBSCAN(geometry, eps := 0, minpoints := 1) over () AS cid, geometry
FROM simplify_vw_z12
WHERE ST_NPoints(geometry) < 50
AND subclass IN ('wood', 'forest')) union_geom50
GROUP BY subclass,
cid
UNION ALL
SELECT subclass, ST_MakeValid((ST_dump(ST_Union(geometry))).geom) AS geometry
FROM (
SELECT subclass,
ST_ClusterDBSCAN(geometry, eps := 0, minpoints := 1) over () AS cid, geometry
FROM simplify_vw_z12
WHERE ST_NPoints(geometry) >= 50
AND ST_NPoints(geometry) < 300
WHERE ST_NPoints(geometry) < 300
AND subclass IN ('wood', 'forest')) union_geom300
GROUP BY subclass,
cid
Expand Down Expand Up @@ -129,26 +101,12 @@ CREATE INDEX ON simplify_vw_z11 USING GIST (geometry);
-- etldoc: simplify_vw_z11 -> osm_landcover_gen_z11
CREATE TABLE osm_landcover_gen_z11 AS
(
SELECT subclass,
ST_MakeValid(
(ST_dump(
ST_Union(geometry))).geom) AS geometry
FROM (
SELECT subclass,
ST_ClusterDBSCAN(geometry, eps := 0, minpoints := 1) over () AS cid, geometry
FROM simplify_vw_z11
WHERE ST_NPoints(geometry) < 50
AND subclass IN ('wood', 'forest')) union_geom50
GROUP BY subclass,
cid
UNION ALL
SELECT subclass, ST_MakeValid((ST_dump(ST_Union(geometry))).geom) AS geometry
FROM (
SELECT subclass,
ST_ClusterDBSCAN(geometry, eps := 0, minpoints := 1) over () AS cid, geometry
FROM simplify_vw_z11
WHERE ST_NPoints(geometry) >= 50
AND ST_NPoints(geometry) < 300
WHERE ST_NPoints(geometry) < 300
AND subclass IN ('wood', 'forest')) union_geom300
GROUP BY subclass,
cid
Expand Down Expand Up @@ -179,26 +137,12 @@ CREATE INDEX ON simplify_vw_z10 USING GIST (geometry);
-- etldoc: simplify_vw_z10 -> osm_landcover_gen_z10
CREATE TABLE osm_landcover_gen_z10 AS
(
SELECT subclass,
ST_MakeValid(
(ST_dump(
ST_Union(geometry))).geom) AS geometry
FROM (
SELECT subclass,
ST_ClusterDBSCAN(geometry, eps := 0, minpoints := 1) over () AS cid, geometry
FROM simplify_vw_z10
WHERE ST_NPoints(geometry) < 50
AND subclass IN ('wood', 'forest')) union_geom50
GROUP BY subclass,
cid
UNION ALL
SELECT subclass, ST_MakeValid((ST_dump(ST_Union(geometry))).geom) AS geometry
FROM (
SELECT subclass,
ST_ClusterDBSCAN(geometry, eps := 0, minpoints := 1) over () AS cid, geometry
FROM simplify_vw_z10
WHERE ST_NPoints(geometry) >= 50
AND ST_NPoints(geometry) < 300
WHERE ST_NPoints(geometry) < 300
AND subclass IN ('wood', 'forest')) union_geom300
GROUP BY subclass,
cid
Expand Down Expand Up @@ -229,26 +173,12 @@ CREATE INDEX ON simplify_vw_z9 USING GIST (geometry);
-- etldoc: simplify_vw_z9 -> osm_landcover_gen_z9
CREATE TABLE osm_landcover_gen_z9 AS
(
SELECT subclass,
ST_MakeValid(
(ST_dump(
ST_Union(geometry))).geom) AS geometry
FROM (
SELECT subclass,
ST_ClusterDBSCAN(geometry, eps := 0, minpoints := 1) over () AS cid, geometry
FROM simplify_vw_z9
WHERE ST_NPoints(geometry) < 50
AND subclass IN ('wood', 'forest')) union_geom50
GROUP BY subclass,
cid
UNION ALL
SELECT subclass, ST_MakeValid((ST_dump(ST_Union(geometry))).geom) AS geometry
FROM (
SELECT subclass,
ST_ClusterDBSCAN(geometry, eps := 0, minpoints := 1) over () AS cid, geometry
FROM simplify_vw_z9
WHERE ST_NPoints(geometry) >= 50
AND ST_NPoints(geometry) < 300
WHERE ST_NPoints(geometry) < 300
AND subclass IN ('wood', 'forest')) union_geom300
GROUP BY subclass,
cid
Expand Down

0 comments on commit 4d6945b

Please sign in to comment.