Skip to content

Instantly share code, notes, and snippets.

@tobwen
Last active May 23, 2020 09:29
Show Gist options
  • Save tobwen/24ce4ed70f92663a60689bf4a0e5ecff to your computer and use it in GitHub Desktop.
Save tobwen/24ce4ed70f92663a60689bf4a0e5ecff to your computer and use it in GitHub Desktop.
Variants to generate an INSPIRE-compliant grid in EPSG:3035 for Germany's federal state Northrhine-Westfalia in PostGIS

Variants to generate an INSPIRE-compliant grid in EPSG:3035 for Germany's federal state Northrhine-Westfalia in PostGIS

Get the polygons, which have been released under a zero license: https://www.opengeodata.nrw.de/produkte/geobasis/vkg/dvg/dvg1/dvg1_EPSG25832_Shape.zip

variant 1

expand for SQL query
CREATE TEMPORARY TABLE dvg1bld_nw_3035 AS (
    SELECT
        kn::text COLLATE "C",
        ST_Transform(geom, 3035) AS geom
    FROM
        dvg1bld_nw
);
ALTER TABLE dvg1bld_nw_3035 SET (parallel_workers = 32);
CREATE INDEX idx_dvg1bld_nw_3035_spgist ON dvg1bld_nw_3035 USING spgist(geom);
VACUUM ANALYZE dvg1bld_nw_3035;

CREATE TEMPORARY TABLE slices AS ( 
    SELECT
        kn,
        ST_Subdivide(geom) as geom
    FROM
        dvg1bld_nw_3035
);
ALTER TABLE slices SET (parallel_workers = 32);
CREATE INDEX idx_slices_spgist ON slices USING spgist(geom);
CREATE INDEX idx_slices_kn1 ON slices USING btree(kn);
CREATE INDEX idx_slices_kn2 ON slices USING gist(kn, geom);
CREATE INDEX idx_slices_kn3 ON slices USING gist(geom, kn);
VACUUM ANALYZE slices;

CREATE TEMPORARY TABLE grid AS ( 
    SELECT
        (ST_SquareGrid(100, geom)).geom
    FROM
        dvg1bld_nw_3035
);
ALTER TABLE grid SET (parallel_workers = 32);
CREATE INDEX idx_grid_spgist ON grid USING spgist(geom);
VACUUM ANALYZE grid;

SELECT
    CASE WHEN ST_Covers(t1.geom, t2.geom)
     THEN 10000
     ELSE ST_Area(ST_Intersection(t2.geom, t3.geom))
    END
FROM
    slices t1
    JOIN grid t2 ON ST_Intersects(t1.geom, t2.geom)
    JOIN dvg1bld_nw_3035 t3 USING (kn);

variant 2

expand for SQL query
WITH
dvg1bld_nw_3035 AS MATERIALIZED (
    SELECT
        kn,
        ST_Transform(geom, 3035) AS geom
    FROM
        dvg1bld_nw
),
slices AS MATERIALIZED (
    SELECT
        kn,
        ST_Subdivide(geom) as geom
    FROM
        dvg1bld_nw_3035
),
grid AS MATERIALIZED (
    SELECT
        (ST_SquareGrid(100, geom)).geom
    FROM
        dvg1bld_nw_3035
)
SELECT
    CASE WHEN ST_Covers(t1.geom, t2.geom)
     THEN 10000
     ELSE ST_Area(ST_Intersection(t2.geom, t3.geom))
    END
FROM
    slices t1
    JOIN grid t2 ON ST_Intersects(t1.geom, t2.geom)
    JOIN dvg1bld_nw_3035 t3 USING (kn);

variant 3

expand for SQL query
WITH
dvg1bld_nw_3035 AS MATERIALIZED (
    SELECT
        kn,
        ST_Transform(geom, 3035) AS geom
    FROM
        dvg1bld_nw
),
slices AS (
    SELECT
        kn,
        ST_Subdivide(geom) as geom
    FROM
        dvg1bld_nw_3035
),
grid AS (
    SELECT
        (ST_SquareGrid(100, geom)).geom
    FROM
        dvg1bld_nw_3035
)
SELECT
    CASE WHEN ST_Covers(t1.geom, t2.geom)
     THEN 10000
     ELSE ST_Area(ST_Intersection(t2.geom, t3.geom))
    END
FROM
    slices t1
    JOIN grid t2 ON ST_Intersects(t1.geom, t2.geom)
    JOIN dvg1bld_nw_3035 t3 USING (kn);

variant 4

expand for SQL query
CREATE TEMPORARY TABLE dvg1bld_nw_3035 AS (
    SELECT
        kn::text COLLATE "C",
        ST_Transform(geom, 3035) AS geom
    FROM
        dvg1bld_nw
);
ALTER TABLE dvg1bld_nw_3035 SET (parallel_workers = 32);
CREATE INDEX idx_dvg1bld_nw_3035_spgist ON dvg1bld_nw_3035 USING spgist(geom);
VACUUM ANALYZE dvg1bld_nw_3035;

WITH
slices AS (
    SELECT
        kn,
        ST_Subdivide(geom) as geom
    FROM
        dvg1bld_nw_3035
),
grid AS (
    SELECT
        (ST_SquareGrid(100, geom)).geom
    FROM
        dvg1bld_nw_3035
)
SELECT
    CASE WHEN ST_Covers(t1.geom, t2.geom)
     THEN 10000
     ELSE ST_Area(ST_Intersection(t2.geom, t3.geom))
    END
FROM
    slices t1
    JOIN grid t2 ON ST_Intersects(t1.geom, t2.geom)
    JOIN dvg1bld_nw_3035 t3 USING (kn);

variant 5

expand for SQL query
CREATE TEMPORARY TABLE slices AS ( 
    WITH
    dvg1bld_nw_3035 AS MATERIALIZED (
        SELECT
            kn,
            ST_Transform(geom, 3035) AS geom
        FROM
            dvg1bld_nw
    )
    SELECT
        kn,
        ST_Subdivide(geom) as geom
    FROM
        dvg1bld_nw_3035
);
ALTER TABLE slices SET (parallel_workers = 32);
CREATE INDEX idx_slices_spgist ON slices USING spgist(geom);
CREATE INDEX idx_slices_kn1 ON slices USING btree(kn);
CREATE INDEX idx_slices_kn2 ON slices USING gist(kn, geom);
CREATE INDEX idx_slices_kn3 ON slices USING gist(geom, kn);
VACUUM ANALYZE slices;

WITH
dvg1bld_nw_3035 AS MATERIALIZED (
    SELECT
        kn,
        ST_Transform(geom, 3035) AS geom
    FROM
        dvg1bld_nw
),
grid AS (
    SELECT
        (ST_SquareGrid(100, geom)).geom
    FROM
        dvg1bld_nw_3035
)
SELECT
    CASE WHEN ST_Covers(t1.geom, t2.geom)
     THEN 10000
     ELSE ST_Area(ST_Intersection(t2.geom, t3.geom))
    END
FROM
    slices t1
    JOIN grid t2 ON ST_Intersects(t1.geom, t2.geom)
    JOIN dvg1bld_nw_3035 t3 USING (kn);

variant 6

expand for SQL query
CREATE TEMPORARY TABLE grid AS ( 
    WITH
    dvg1bld_nw_3035 AS MATERIALIZED (
        SELECT
            kn,
            ST_Transform(geom, 3035) AS geom
        FROM
            dvg1bld_nw
    )
    SELECT
        (ST_SquareGrid(100, geom)).geom
    FROM
        dvg1bld_nw_3035
);
ALTER TABLE grid SET (parallel_workers = 32);
CREATE INDEX idx_grid_spgist ON grid USING spgist(geom);
VACUUM ANALYZE grid;

WITH
dvg1bld_nw_3035 AS MATERIALIZED (
    SELECT
        kn,
        ST_Transform(geom, 3035) AS geom
    FROM
        dvg1bld_nw
),
slices AS MATERIALIZED (
    SELECT
        kn,
        ST_Subdivide(geom) as geom
    FROM
        dvg1bld_nw_3035
)
SELECT
    CASE WHEN ST_Covers(t1.geom, t2.geom)
     THEN 10000
     ELSE ST_Area(ST_Intersection(t2.geom, t3.geom))
    END
FROM
    slices t1
    JOIN grid t2 ON ST_Intersects(t1.geom, t2.geom)
    JOIN dvg1bld_nw_3035 t3 USING (kn);

variant 1 - modified

expand for SQL query
CREATE TEMPORARY TABLE dvg1bld_nw_3035 AS (
    SELECT
        kn::text COLLATE "C",
        ST_Transform(geom, 3035) AS geom
    FROM
        dvg1bld_nw
);
ALTER TABLE dvg1bld_nw_3035 SET (parallel_workers = 32);
CREATE INDEX idx_dvg1bld_nw_3035_spgist ON dvg1bld_nw_3035 USING spgist(geom);
VACUUM ANALYZE dvg1bld_nw_3035;

CREATE TEMPORARY TABLE slices AS ( 
    SELECT
        kn,
        ST_Subdivide(geom) as geom
    FROM
        dvg1bld_nw_3035
);
ALTER TABLE slices SET (parallel_workers = 32);
CREATE INDEX idx_slices_spgist ON slices USING spgist(geom);
CREATE INDEX idx_slices_kn1 ON slices USING btree(kn);
CREATE INDEX idx_slices_kn2 ON slices USING gist(kn, geom);
CREATE INDEX idx_slices_kn3 ON slices USING gist(geom, kn);
VACUUM ANALYZE slices;

CREATE TEMPORARY TABLE grid AS ( 
    SELECT
        (ST_SquareGrid(100, geom)).geom
    FROM
        dvg1bld_nw_3035
);
ALTER TABLE grid SET (parallel_workers = 32);
CREATE INDEX idx_grid_spgist ON grid USING spgist(geom);
VACUUM ANALYZE grid;

SELECT
    CASE WHEN ST_Covers(t3.geom, t2.geom)
     THEN 10000
     ELSE ST_Area(ST_Intersection(t2.geom, t3.geom))
    END
FROM
    slices t1
    JOIN grid t2 ON ST_Intersects(t1.geom, t2.geom)
    JOIN dvg1bld_nw_3035 t3 USING (kn);

variant 2 - modified

expand for SQL query
WITH
dvg1bld_nw_3035 AS MATERIALIZED (
    SELECT
        kn,
        ST_Transform(geom, 3035) AS geom
    FROM
        dvg1bld_nw
),
slices AS MATERIALIZED (
    SELECT
        kn,
        ST_Subdivide(geom) as geom
    FROM
        dvg1bld_nw_3035
),
grid AS MATERIALIZED (
    SELECT
        (ST_SquareGrid(100, geom)).geom
    FROM
        dvg1bld_nw_3035
)
SELECT
    CASE WHEN ST_Covers(t3.geom, t2.geom)
     THEN 10000
     ELSE ST_Area(ST_Intersection(t2.geom, t3.geom))
    END
FROM
    slices t1
    JOIN grid t2 ON ST_Intersects(t1.geom, t2.geom)
    JOIN dvg1bld_nw_3035 t3 USING (kn);

variant 3 - modified

expand for SQL query
WITH
dvg1bld_nw_3035 AS MATERIALIZED (
    SELECT
        kn,
        ST_Transform(geom, 3035) AS geom
    FROM
        dvg1bld_nw
),
slices AS (
    SELECT
        kn,
        ST_Subdivide(geom) as geom
    FROM
        dvg1bld_nw_3035
),
grid AS (
    SELECT
        (ST_SquareGrid(100, geom)).geom
    FROM
        dvg1bld_nw_3035
)
SELECT
    CASE WHEN ST_Covers(t3.geom, t2.geom)
     THEN 10000
     ELSE ST_Area(ST_Intersection(t2.geom, t3.geom))
    END
FROM
    slices t1
    JOIN grid t2 ON ST_Intersects(t1.geom, t2.geom)
    JOIN dvg1bld_nw_3035 t3 USING (kn);

variant 4 - modified

  • execution time: (still running)
  • explain analyze: (still running)
expand for SQL query
CREATE TEMPORARY TABLE dvg1bld_nw_3035 AS (
    SELECT
        kn::text COLLATE "C",
        ST_Transform(geom, 3035) AS geom
    FROM
        dvg1bld_nw
);
ALTER TABLE dvg1bld_nw_3035 SET (parallel_workers = 32);
CREATE INDEX idx_dvg1bld_nw_3035_spgist ON dvg1bld_nw_3035 USING spgist(geom);
VACUUM ANALYZE dvg1bld_nw_3035;

WITH
slices AS (
    SELECT
        kn,
        ST_Subdivide(geom) as geom
    FROM
        dvg1bld_nw_3035
),
grid AS (
    SELECT
        (ST_SquareGrid(100, geom)).geom
    FROM
        dvg1bld_nw_3035
)
SELECT
    CASE WHEN ST_Covers(t3.geom, t2.geom)
     THEN 10000
     ELSE ST_Area(ST_Intersection(t2.geom, t3.geom))
    END
FROM
    slices t1
    JOIN grid t2 ON ST_Intersects(t1.geom, t2.geom)
    JOIN dvg1bld_nw_3035 t3 USING (kn);

variant 5 - modified

expand for SQL query
CREATE TEMPORARY TABLE slices AS ( 
    WITH
    dvg1bld_nw_3035 AS MATERIALIZED (
        SELECT
            kn,
            ST_Transform(geom, 3035) AS geom
        FROM
            dvg1bld_nw
    )
    SELECT
        kn,
        ST_Subdivide(geom) as geom
    FROM
        dvg1bld_nw_3035
);
ALTER TABLE slices SET (parallel_workers = 32);
CREATE INDEX idx_slices_spgist ON slices USING spgist(geom);
CREATE INDEX idx_slices_kn1 ON slices USING btree(kn);
CREATE INDEX idx_slices_kn2 ON slices USING gist(kn, geom);
CREATE INDEX idx_slices_kn3 ON slices USING gist(geom, kn);
VACUUM ANALYZE slices;

WITH
dvg1bld_nw_3035 AS MATERIALIZED (
    SELECT
        kn,
        ST_Transform(geom, 3035) AS geom
    FROM
        dvg1bld_nw
),
grid AS (
    SELECT
        (ST_SquareGrid(100, geom)).geom
    FROM
        dvg1bld_nw_3035
)
SELECT
    CASE WHEN ST_Covers(t3.geom, t2.geom)
     THEN 10000
     ELSE ST_Area(ST_Intersection(t2.geom, t3.geom))
    END
FROM
    slices t1
    JOIN grid t2 ON ST_Intersects(t1.geom, t2.geom)
    JOIN dvg1bld_nw_3035 t3 USING (kn);

variant 6 - modified

expand for SQL query
CREATE TEMPORARY TABLE grid AS ( 
    WITH
    dvg1bld_nw_3035 AS MATERIALIZED (
        SELECT
            kn,
            ST_Transform(geom, 3035) AS geom
        FROM
            dvg1bld_nw
    )
    SELECT
        (ST_SquareGrid(100, geom)).geom
    FROM
        dvg1bld_nw_3035
);
ALTER TABLE grid SET (parallel_workers = 32);
CREATE INDEX idx_grid_spgist ON grid USING spgist(geom);
VACUUM ANALYZE grid;

WITH
dvg1bld_nw_3035 AS MATERIALIZED (
    SELECT
        kn,
        ST_Transform(geom, 3035) AS geom
    FROM
        dvg1bld_nw
),
slices AS MATERIALIZED (
    SELECT
        kn,
        ST_Subdivide(geom) as geom
    FROM
        dvg1bld_nw_3035
)
SELECT
    CASE WHEN ST_Covers(t3.geom, t2.geom)
     THEN 10000
     ELSE ST_Area(ST_Intersection(t2.geom, t3.geom))
    END
FROM
    slices t1
    JOIN grid t2 ON ST_Intersects(t1.geom, t2.geom)
    JOIN dvg1bld_nw_3035 t3 USING (kn);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment