Find what polygons are not fully covered by union of polygons from another layer in PostGISUpdate a newly...

Taking an academic pseudonym?

In a world with multiracial creatures, what word can be used instead of mankind?

Coworker is trying to get me to sign his petition to run for office. How to decline politely?

How can I make my enemies feel real and make combat more engaging?

STM32 PWM problem

Why do we divide Permutations to get to Combinations?

How should I ship cards?

Sauna: Wood does not feel so hot

How bad is a Computer Science course that doesn't teach Design Patterns?

Why Third 'Reich'? Why is 'reich' not translated when 'third' is? What is the English synonym of reich?

The Late Queen Gives in to Remorse - Reverse Hangman

Why don't reads from /dev/zero count as I/O?

Ramanujan's radical and how we define an infinite nested radical

Manager has noticed coworker's excessive breaks. Should I warn him?

Was the Soviet N1 really capable of sending 9.6 GB/s of telemetry?

Have any astronauts or cosmonauts died in space?

TikZ-Tree with asymmetric siblings

Short story where Earth is given a racist governor who likes species of a certain color

Is there a way to pause a running process on Linux systems and resume later?

What dissuades people from lying about where they live in order to reduce state income taxes?

What happens if you declare more than $10,000 at the US border?

Why didn't Lorentz conclude that no object can go faster than light?

Which was the first story to feature space elevators?

How to know if I am a 'Real Developer'



Find what polygons are not fully covered by union of polygons from another layer in PostGIS


Update a newly added column of a table using ST_Intersection as a filterPostgis 2.0 simplifying multipolygonCombined Hstore key/value and spatial query too slow to handle bigger OSM extractsHow to import country administrative boundaries from OSM Planet to PostGIS PolygonsST_Difference on linestrings and polygons slow and failsPostGIS geography intersect slower than geometry intersectdissolving polygon records that only fall completely in another polygon from the same tablePerforming polygon drilldown/overlay in PostGIS?Find and remove portions of polygons from one geometry column that overlap from another geometry column in PostGIS'UPDATE 0' results for ST_Within query - locating points within polygons













1















I have two tables in a PostGIS database



>>> select postgis_version();
"2.5 USE_GEOS=1 USE_PROJ=1 USE_STATS=1"


One represents the country borders and another one is administrative boundaries. Both are of global coverage covering the whole world. Both tables have spatial indices on the geometry columns.



My goal is to find what countries are not fully covered by the administrative boundaries and, if feasible to achieve within a reasonable time, the location/geometry of part of country geometry where it is not covered by the administrative boundaries.



To give you an example.



enter image description here




  • Two countries (marked as 1 (consists of three polygons) and 2 (consists of two polygons), dark green polygons (two rows in table in this case).


  • Three admin boundary polygons (marked as A, B, C) (can be multipolygon/polygon), transparent blue polygons (three rows in table in this case).


  • Country 1 is fully covered by the admin boundary A and B. Country 2 is not fully covered by admin boundaries (C), so I would like to get this reported. If possible, the hatched area of country 2 could be reported as well.



This sounded like a job for ST_Difference function however my problem is that it is just too slow running on the global dataset.



Running this SQL query on Singapore takes 4 seconds. This is because I have just a few admin boundaries located around the country which I've extracted beforehand and I use only single country geometry. I get the results I expect.



drop table sg_missing_;
WITH SG AS (SELECT iso_cc, geom FROM countries_agg WHERE iso_cc = 'SG'),
AD AS (SELECT ST_Union(geom) AS geom FROM admins_sample)
SELECT ST_Difference(c.geom, ad.geom) AS geom
INTO sg_missing_
FROM AD ad, SG c


Running the same query on a larger country, such as Indonesia, took 15 hours, so there is something wrong with this approach. I was trying to play smart and get into the analysis only those admin boundaries that intersect the boundary box (envelope) of the country, but it still was running for 10 hours for Indonesia.



--preparing a single country bbox to use
SELECT ST_Envelope(geom) AS geom
INTO country_bbox
FROM countries_agg
where iso_cc = 'ID';

--preparing single country table and union of admin boundary geometries that intersect the given country envelope
WITH
IND AS
(SELECT iso_cc, geom FROM countries_agg WHERE iso_cc = 'ID'),
AD AS
(SELECT ST_Union(ST_Intersection(IND.geom, AD.geom)) AS geom
FROM AD AD, country_bbox IND)

--get geometry of country shape that is not covered by the admin boundaries
SELECT ST_Difference(c.geom, ad.geom) AS geom
INTO sg_missing
FROM AD ad, IND c


It is acceptable if this query would take a few days to run as long as all countries could be analysed in this time. How should such a query be written, if possible in PostGIS? I am open to solutions that involve other tools and software, it does not have to be done solely in PostGIS.










share|improve this question























  • how many geometries are there in both tables? and can those admin polygons overlap more than one country? getting non-intersections/differences is always tricky, but if there'd be a 1 to 1 intersection relation between each country and admin area, one might get around expensive unions. also, having only non-multi geometries boosts the index search and vertex traversal significantly.

    – ThingumaBob
    4 hours ago













  • Thanks for getting on it. Country table has 240 features. Admin boundary table has 164000 features. Admin polygons can overlap more than one country, so unfortunately, there isn't 1:1 between each country and admin area. Admin areas have 70% polygons and 30% multipolygons. Thanks for the tip to explode those into polygons.

    – Alex Tereshenkov
    4 hours ago











  • You could try to explode the multipolygon, then check with st_contains+ st_intersects which polygon are contained by which admin area and finally use a group by to regroup the information by country. Don't forget to create a spatial index

    – obchardon
    3 hours ago













  • @obchardon, thanks for posting. I don't think I follow you on check with st_contains+ st_intersects which polygon are contained by which admin area. Could you please elaborate briefly - or even post an answer?

    – Alex Tereshenkov
    3 hours ago











  • so you actually have a lower admin level than 0, e.g. provinces? shouldn't there be some sort of reference attribute between both tables? i.e. admin polygons A & B each are provinces of country 1?

    – ThingumaBob
    3 hours ago
















1















I have two tables in a PostGIS database



>>> select postgis_version();
"2.5 USE_GEOS=1 USE_PROJ=1 USE_STATS=1"


One represents the country borders and another one is administrative boundaries. Both are of global coverage covering the whole world. Both tables have spatial indices on the geometry columns.



My goal is to find what countries are not fully covered by the administrative boundaries and, if feasible to achieve within a reasonable time, the location/geometry of part of country geometry where it is not covered by the administrative boundaries.



To give you an example.



enter image description here




  • Two countries (marked as 1 (consists of three polygons) and 2 (consists of two polygons), dark green polygons (two rows in table in this case).


  • Three admin boundary polygons (marked as A, B, C) (can be multipolygon/polygon), transparent blue polygons (three rows in table in this case).


  • Country 1 is fully covered by the admin boundary A and B. Country 2 is not fully covered by admin boundaries (C), so I would like to get this reported. If possible, the hatched area of country 2 could be reported as well.



This sounded like a job for ST_Difference function however my problem is that it is just too slow running on the global dataset.



Running this SQL query on Singapore takes 4 seconds. This is because I have just a few admin boundaries located around the country which I've extracted beforehand and I use only single country geometry. I get the results I expect.



drop table sg_missing_;
WITH SG AS (SELECT iso_cc, geom FROM countries_agg WHERE iso_cc = 'SG'),
AD AS (SELECT ST_Union(geom) AS geom FROM admins_sample)
SELECT ST_Difference(c.geom, ad.geom) AS geom
INTO sg_missing_
FROM AD ad, SG c


Running the same query on a larger country, such as Indonesia, took 15 hours, so there is something wrong with this approach. I was trying to play smart and get into the analysis only those admin boundaries that intersect the boundary box (envelope) of the country, but it still was running for 10 hours for Indonesia.



--preparing a single country bbox to use
SELECT ST_Envelope(geom) AS geom
INTO country_bbox
FROM countries_agg
where iso_cc = 'ID';

--preparing single country table and union of admin boundary geometries that intersect the given country envelope
WITH
IND AS
(SELECT iso_cc, geom FROM countries_agg WHERE iso_cc = 'ID'),
AD AS
(SELECT ST_Union(ST_Intersection(IND.geom, AD.geom)) AS geom
FROM AD AD, country_bbox IND)

--get geometry of country shape that is not covered by the admin boundaries
SELECT ST_Difference(c.geom, ad.geom) AS geom
INTO sg_missing
FROM AD ad, IND c


It is acceptable if this query would take a few days to run as long as all countries could be analysed in this time. How should such a query be written, if possible in PostGIS? I am open to solutions that involve other tools and software, it does not have to be done solely in PostGIS.










share|improve this question























  • how many geometries are there in both tables? and can those admin polygons overlap more than one country? getting non-intersections/differences is always tricky, but if there'd be a 1 to 1 intersection relation between each country and admin area, one might get around expensive unions. also, having only non-multi geometries boosts the index search and vertex traversal significantly.

    – ThingumaBob
    4 hours ago













  • Thanks for getting on it. Country table has 240 features. Admin boundary table has 164000 features. Admin polygons can overlap more than one country, so unfortunately, there isn't 1:1 between each country and admin area. Admin areas have 70% polygons and 30% multipolygons. Thanks for the tip to explode those into polygons.

    – Alex Tereshenkov
    4 hours ago











  • You could try to explode the multipolygon, then check with st_contains+ st_intersects which polygon are contained by which admin area and finally use a group by to regroup the information by country. Don't forget to create a spatial index

    – obchardon
    3 hours ago













  • @obchardon, thanks for posting. I don't think I follow you on check with st_contains+ st_intersects which polygon are contained by which admin area. Could you please elaborate briefly - or even post an answer?

    – Alex Tereshenkov
    3 hours ago











  • so you actually have a lower admin level than 0, e.g. provinces? shouldn't there be some sort of reference attribute between both tables? i.e. admin polygons A & B each are provinces of country 1?

    – ThingumaBob
    3 hours ago














1












1








1








I have two tables in a PostGIS database



>>> select postgis_version();
"2.5 USE_GEOS=1 USE_PROJ=1 USE_STATS=1"


One represents the country borders and another one is administrative boundaries. Both are of global coverage covering the whole world. Both tables have spatial indices on the geometry columns.



My goal is to find what countries are not fully covered by the administrative boundaries and, if feasible to achieve within a reasonable time, the location/geometry of part of country geometry where it is not covered by the administrative boundaries.



To give you an example.



enter image description here




  • Two countries (marked as 1 (consists of three polygons) and 2 (consists of two polygons), dark green polygons (two rows in table in this case).


  • Three admin boundary polygons (marked as A, B, C) (can be multipolygon/polygon), transparent blue polygons (three rows in table in this case).


  • Country 1 is fully covered by the admin boundary A and B. Country 2 is not fully covered by admin boundaries (C), so I would like to get this reported. If possible, the hatched area of country 2 could be reported as well.



This sounded like a job for ST_Difference function however my problem is that it is just too slow running on the global dataset.



Running this SQL query on Singapore takes 4 seconds. This is because I have just a few admin boundaries located around the country which I've extracted beforehand and I use only single country geometry. I get the results I expect.



drop table sg_missing_;
WITH SG AS (SELECT iso_cc, geom FROM countries_agg WHERE iso_cc = 'SG'),
AD AS (SELECT ST_Union(geom) AS geom FROM admins_sample)
SELECT ST_Difference(c.geom, ad.geom) AS geom
INTO sg_missing_
FROM AD ad, SG c


Running the same query on a larger country, such as Indonesia, took 15 hours, so there is something wrong with this approach. I was trying to play smart and get into the analysis only those admin boundaries that intersect the boundary box (envelope) of the country, but it still was running for 10 hours for Indonesia.



--preparing a single country bbox to use
SELECT ST_Envelope(geom) AS geom
INTO country_bbox
FROM countries_agg
where iso_cc = 'ID';

--preparing single country table and union of admin boundary geometries that intersect the given country envelope
WITH
IND AS
(SELECT iso_cc, geom FROM countries_agg WHERE iso_cc = 'ID'),
AD AS
(SELECT ST_Union(ST_Intersection(IND.geom, AD.geom)) AS geom
FROM AD AD, country_bbox IND)

--get geometry of country shape that is not covered by the admin boundaries
SELECT ST_Difference(c.geom, ad.geom) AS geom
INTO sg_missing
FROM AD ad, IND c


It is acceptable if this query would take a few days to run as long as all countries could be analysed in this time. How should such a query be written, if possible in PostGIS? I am open to solutions that involve other tools and software, it does not have to be done solely in PostGIS.










share|improve this question














I have two tables in a PostGIS database



>>> select postgis_version();
"2.5 USE_GEOS=1 USE_PROJ=1 USE_STATS=1"


One represents the country borders and another one is administrative boundaries. Both are of global coverage covering the whole world. Both tables have spatial indices on the geometry columns.



My goal is to find what countries are not fully covered by the administrative boundaries and, if feasible to achieve within a reasonable time, the location/geometry of part of country geometry where it is not covered by the administrative boundaries.



To give you an example.



enter image description here




  • Two countries (marked as 1 (consists of three polygons) and 2 (consists of two polygons), dark green polygons (two rows in table in this case).


  • Three admin boundary polygons (marked as A, B, C) (can be multipolygon/polygon), transparent blue polygons (three rows in table in this case).


  • Country 1 is fully covered by the admin boundary A and B. Country 2 is not fully covered by admin boundaries (C), so I would like to get this reported. If possible, the hatched area of country 2 could be reported as well.



This sounded like a job for ST_Difference function however my problem is that it is just too slow running on the global dataset.



Running this SQL query on Singapore takes 4 seconds. This is because I have just a few admin boundaries located around the country which I've extracted beforehand and I use only single country geometry. I get the results I expect.



drop table sg_missing_;
WITH SG AS (SELECT iso_cc, geom FROM countries_agg WHERE iso_cc = 'SG'),
AD AS (SELECT ST_Union(geom) AS geom FROM admins_sample)
SELECT ST_Difference(c.geom, ad.geom) AS geom
INTO sg_missing_
FROM AD ad, SG c


Running the same query on a larger country, such as Indonesia, took 15 hours, so there is something wrong with this approach. I was trying to play smart and get into the analysis only those admin boundaries that intersect the boundary box (envelope) of the country, but it still was running for 10 hours for Indonesia.



--preparing a single country bbox to use
SELECT ST_Envelope(geom) AS geom
INTO country_bbox
FROM countries_agg
where iso_cc = 'ID';

--preparing single country table and union of admin boundary geometries that intersect the given country envelope
WITH
IND AS
(SELECT iso_cc, geom FROM countries_agg WHERE iso_cc = 'ID'),
AD AS
(SELECT ST_Union(ST_Intersection(IND.geom, AD.geom)) AS geom
FROM AD AD, country_bbox IND)

--get geometry of country shape that is not covered by the admin boundaries
SELECT ST_Difference(c.geom, ad.geom) AS geom
INTO sg_missing
FROM AD ad, IND c


It is acceptable if this query would take a few days to run as long as all countries could be analysed in this time. How should such a query be written, if possible in PostGIS? I am open to solutions that involve other tools and software, it does not have to be done solely in PostGIS.







postgis postgresql spatial-analysis st-intersects st-difference






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 4 hours ago









Alex TereshenkovAlex Tereshenkov

26.2k13598




26.2k13598













  • how many geometries are there in both tables? and can those admin polygons overlap more than one country? getting non-intersections/differences is always tricky, but if there'd be a 1 to 1 intersection relation between each country and admin area, one might get around expensive unions. also, having only non-multi geometries boosts the index search and vertex traversal significantly.

    – ThingumaBob
    4 hours ago













  • Thanks for getting on it. Country table has 240 features. Admin boundary table has 164000 features. Admin polygons can overlap more than one country, so unfortunately, there isn't 1:1 between each country and admin area. Admin areas have 70% polygons and 30% multipolygons. Thanks for the tip to explode those into polygons.

    – Alex Tereshenkov
    4 hours ago











  • You could try to explode the multipolygon, then check with st_contains+ st_intersects which polygon are contained by which admin area and finally use a group by to regroup the information by country. Don't forget to create a spatial index

    – obchardon
    3 hours ago













  • @obchardon, thanks for posting. I don't think I follow you on check with st_contains+ st_intersects which polygon are contained by which admin area. Could you please elaborate briefly - or even post an answer?

    – Alex Tereshenkov
    3 hours ago











  • so you actually have a lower admin level than 0, e.g. provinces? shouldn't there be some sort of reference attribute between both tables? i.e. admin polygons A & B each are provinces of country 1?

    – ThingumaBob
    3 hours ago



















  • how many geometries are there in both tables? and can those admin polygons overlap more than one country? getting non-intersections/differences is always tricky, but if there'd be a 1 to 1 intersection relation between each country and admin area, one might get around expensive unions. also, having only non-multi geometries boosts the index search and vertex traversal significantly.

    – ThingumaBob
    4 hours ago













  • Thanks for getting on it. Country table has 240 features. Admin boundary table has 164000 features. Admin polygons can overlap more than one country, so unfortunately, there isn't 1:1 between each country and admin area. Admin areas have 70% polygons and 30% multipolygons. Thanks for the tip to explode those into polygons.

    – Alex Tereshenkov
    4 hours ago











  • You could try to explode the multipolygon, then check with st_contains+ st_intersects which polygon are contained by which admin area and finally use a group by to regroup the information by country. Don't forget to create a spatial index

    – obchardon
    3 hours ago













  • @obchardon, thanks for posting. I don't think I follow you on check with st_contains+ st_intersects which polygon are contained by which admin area. Could you please elaborate briefly - or even post an answer?

    – Alex Tereshenkov
    3 hours ago











  • so you actually have a lower admin level than 0, e.g. provinces? shouldn't there be some sort of reference attribute between both tables? i.e. admin polygons A & B each are provinces of country 1?

    – ThingumaBob
    3 hours ago

















how many geometries are there in both tables? and can those admin polygons overlap more than one country? getting non-intersections/differences is always tricky, but if there'd be a 1 to 1 intersection relation between each country and admin area, one might get around expensive unions. also, having only non-multi geometries boosts the index search and vertex traversal significantly.

– ThingumaBob
4 hours ago







how many geometries are there in both tables? and can those admin polygons overlap more than one country? getting non-intersections/differences is always tricky, but if there'd be a 1 to 1 intersection relation between each country and admin area, one might get around expensive unions. also, having only non-multi geometries boosts the index search and vertex traversal significantly.

– ThingumaBob
4 hours ago















Thanks for getting on it. Country table has 240 features. Admin boundary table has 164000 features. Admin polygons can overlap more than one country, so unfortunately, there isn't 1:1 between each country and admin area. Admin areas have 70% polygons and 30% multipolygons. Thanks for the tip to explode those into polygons.

– Alex Tereshenkov
4 hours ago





Thanks for getting on it. Country table has 240 features. Admin boundary table has 164000 features. Admin polygons can overlap more than one country, so unfortunately, there isn't 1:1 between each country and admin area. Admin areas have 70% polygons and 30% multipolygons. Thanks for the tip to explode those into polygons.

– Alex Tereshenkov
4 hours ago













You could try to explode the multipolygon, then check with st_contains+ st_intersects which polygon are contained by which admin area and finally use a group by to regroup the information by country. Don't forget to create a spatial index

– obchardon
3 hours ago







You could try to explode the multipolygon, then check with st_contains+ st_intersects which polygon are contained by which admin area and finally use a group by to regroup the information by country. Don't forget to create a spatial index

– obchardon
3 hours ago















@obchardon, thanks for posting. I don't think I follow you on check with st_contains+ st_intersects which polygon are contained by which admin area. Could you please elaborate briefly - or even post an answer?

– Alex Tereshenkov
3 hours ago





@obchardon, thanks for posting. I don't think I follow you on check with st_contains+ st_intersects which polygon are contained by which admin area. Could you please elaborate briefly - or even post an answer?

– Alex Tereshenkov
3 hours ago













so you actually have a lower admin level than 0, e.g. provinces? shouldn't there be some sort of reference attribute between both tables? i.e. admin polygons A & B each are provinces of country 1?

– ThingumaBob
3 hours ago





so you actually have a lower admin level than 0, e.g. provinces? shouldn't there be some sort of reference attribute between both tables? i.e. admin polygons A & B each are provinces of country 1?

– ThingumaBob
3 hours ago










1 Answer
1






active

oldest

votes


















1














I can't test right now, but I'd say try with



SELECT  <id>,
ST_Union(geom) AS geom
FROm (
SELECT a.<id>,
ST_Difference(a.geom, b.geom) AS geom
FROM <countries> AS a
JOIN <admins> AS b
ON a.geom && b.geom
WHERE ST_Intersects(a.geom, b.geom)
) q
GROUP BY
id
;


Run this on simple Polygons in a new spatially indexed table. I used an additional && join, see what the planner makes of that; it might help or not. ST_Difference and ST_Within/ST_Contains should do the same checks, so I included none of the latter as additional spatial relation check.



I think that unioning a possibly low number of smaller difference polygons should be a lot faster than a per se union of all intersecting admin polygons. It shoud also get rid of empty GeometryCollections that can result from ST_Difference.



Note that this query will return areas that are not covered by any admin region, so if a province of a country overlaps with another country, that part will count as covered. You'd need to have attribute reference to avoid this.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "79"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f313039%2ffind-what-polygons-are-not-fully-covered-by-union-of-polygons-from-another-layer%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I can't test right now, but I'd say try with



    SELECT  <id>,
    ST_Union(geom) AS geom
    FROm (
    SELECT a.<id>,
    ST_Difference(a.geom, b.geom) AS geom
    FROM <countries> AS a
    JOIN <admins> AS b
    ON a.geom && b.geom
    WHERE ST_Intersects(a.geom, b.geom)
    ) q
    GROUP BY
    id
    ;


    Run this on simple Polygons in a new spatially indexed table. I used an additional && join, see what the planner makes of that; it might help or not. ST_Difference and ST_Within/ST_Contains should do the same checks, so I included none of the latter as additional spatial relation check.



    I think that unioning a possibly low number of smaller difference polygons should be a lot faster than a per se union of all intersecting admin polygons. It shoud also get rid of empty GeometryCollections that can result from ST_Difference.



    Note that this query will return areas that are not covered by any admin region, so if a province of a country overlaps with another country, that part will count as covered. You'd need to have attribute reference to avoid this.






    share|improve this answer




























      1














      I can't test right now, but I'd say try with



      SELECT  <id>,
      ST_Union(geom) AS geom
      FROm (
      SELECT a.<id>,
      ST_Difference(a.geom, b.geom) AS geom
      FROM <countries> AS a
      JOIN <admins> AS b
      ON a.geom && b.geom
      WHERE ST_Intersects(a.geom, b.geom)
      ) q
      GROUP BY
      id
      ;


      Run this on simple Polygons in a new spatially indexed table. I used an additional && join, see what the planner makes of that; it might help or not. ST_Difference and ST_Within/ST_Contains should do the same checks, so I included none of the latter as additional spatial relation check.



      I think that unioning a possibly low number of smaller difference polygons should be a lot faster than a per se union of all intersecting admin polygons. It shoud also get rid of empty GeometryCollections that can result from ST_Difference.



      Note that this query will return areas that are not covered by any admin region, so if a province of a country overlaps with another country, that part will count as covered. You'd need to have attribute reference to avoid this.






      share|improve this answer


























        1












        1








        1







        I can't test right now, but I'd say try with



        SELECT  <id>,
        ST_Union(geom) AS geom
        FROm (
        SELECT a.<id>,
        ST_Difference(a.geom, b.geom) AS geom
        FROM <countries> AS a
        JOIN <admins> AS b
        ON a.geom && b.geom
        WHERE ST_Intersects(a.geom, b.geom)
        ) q
        GROUP BY
        id
        ;


        Run this on simple Polygons in a new spatially indexed table. I used an additional && join, see what the planner makes of that; it might help or not. ST_Difference and ST_Within/ST_Contains should do the same checks, so I included none of the latter as additional spatial relation check.



        I think that unioning a possibly low number of smaller difference polygons should be a lot faster than a per se union of all intersecting admin polygons. It shoud also get rid of empty GeometryCollections that can result from ST_Difference.



        Note that this query will return areas that are not covered by any admin region, so if a province of a country overlaps with another country, that part will count as covered. You'd need to have attribute reference to avoid this.






        share|improve this answer













        I can't test right now, but I'd say try with



        SELECT  <id>,
        ST_Union(geom) AS geom
        FROm (
        SELECT a.<id>,
        ST_Difference(a.geom, b.geom) AS geom
        FROM <countries> AS a
        JOIN <admins> AS b
        ON a.geom && b.geom
        WHERE ST_Intersects(a.geom, b.geom)
        ) q
        GROUP BY
        id
        ;


        Run this on simple Polygons in a new spatially indexed table. I used an additional && join, see what the planner makes of that; it might help or not. ST_Difference and ST_Within/ST_Contains should do the same checks, so I included none of the latter as additional spatial relation check.



        I think that unioning a possibly low number of smaller difference polygons should be a lot faster than a per se union of all intersecting admin polygons. It shoud also get rid of empty GeometryCollections that can result from ST_Difference.



        Note that this query will return areas that are not covered by any admin region, so if a province of a country overlaps with another country, that part will count as covered. You'd need to have attribute reference to avoid this.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 hours ago









        ThingumaBobThingumaBob

        6,0371323




        6,0371323






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f313039%2ffind-what-polygons-are-not-fully-covered-by-union-of-polygons-from-another-layer%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            (145452) 2005 RN43 Классификация | Примечания | Ссылки |...

            Щит и меч (фильм) Содержание Названия серий | Сюжет |...

            Энтрерриос (город) Содержание История | Географическое...