InsideDarkWeb.com

PostGIS: create new polygons in between existing?

PostgreSQL: I have a PostGIS multipolygon table containing millions of features. It covers 95% of the country, however there are gaps between individual polygons. How can I fill these gaps with new polygons? Preserving and not changing the existing features.

See image below. I’d want to fill in that gap in the middle. And all the slithers and smaller polys on the right (potentially ignoring very small polys below an area of Xm2). Afterwards I can dice the shape up for better performance if it’s too big.

enter image description here

note: these empty areas may very well extend all the way to the coast. I’d need to use the outline of the country to restrict this operation. I don’t need new polygons appearing over the sea!

EDIT:

So the blue polys below are the shapes I’d like to create in this example

enter image description here

Geographic Information Systems Asked on November 14, 2021

2 Answers

2 Answers

So my colleague came up with this solution. It works great for small areas, but I'd like to find a solution for millions of shapes across an entire country. I can see the ST_UNION here causing a blockage in this respect. The ST_BUFFER is just to close out thin slithers.

SELECT ST_DIFFERENCE(foo.geom, bar.geom)
FROM (SELECT ST_CONVEXHULL(ST_COLLECT(shape::geometry)) as geom FROM schema.polytable) as foo, 
(SELECT ST_BUFFER(ST_UNION(shape),0.5) as geom FROM schema.polytable) as bar

result:

enter image description here

If anyone has suggestions for larger tables, I'm all ears.


UPDATE: I have found a solution for the entire country whereby I execute a similar process to that above, but using a gridded version of the country and iterating through each grid using ST_Intersect.

(optional) Before we start we may want to make a grid that does not extend beyond the country outline. So we'll take the entire 25x25km square grid table and a simple outline polygon of the country, then create a new table using SELECT (ST_DUMP(ST_INTERSECTION(a.geom,b.geom))).geom as geom to produce:

enter image description here

standard grid or country outline defined grid, we can then use:

SELECT ST_SUBDIVIDE(ST_DIFFERENCE(a.geom, b.geom)) as geom
FROM 
(SELECT ST_BUFFER(ST_UNION(b.geom),0.5) as geom
 FROM schema.polytable b, schema.gridtable a 
 WHERE ST_INTERSECTS(b.geom,a.geom) AND a.grid_id = [use id number as a iteration variable here]) as b, schema.gridtable a 
WHERE a.grid_id = [use the same id number as a iteration variable here];

So slightly different from the previous SQL statement. There's no need for ST_CONVEXHULL this time as we're using a square grid to contain the output. Also again we use ST_BUFFER 0.5 to remove any thin inter-polygon slithers from the output. For better rendering and performance of the output, we use ST_SUBDIVIDE to divide up the resulting, and potentially huge, multipart polygon.

I need to put this in a python pipeline using the psycopg2 library, then I'll post the results on here. Testing on one grid (out of 500) takes 30secs. So could be 4 hours to run in total.

Answered by Theo F on November 14, 2021

this code will fill the gaps and holes in the polygons. adjust according to your data

SELECT id, ST_Collect(ST_MakePolygon(geom)) As geom
FROM (
    SELECT gid, ST_ExteriorRing((ST_Dump(geom)).geom) As geom
    FROM layer
    ) s
GROUP BY id

Answered by ziggy on November 14, 2021

Add your own answers!

Related Questions

How to integrate some Python code in “PythonCaller” FME?

1  Asked on September 17, 2020 by gisgis

   

MapView overlays blocking zooming

1  Asked on September 17, 2020 by developerman1234

   

Adding image to existing data frame using ArcPy

3  Asked on September 17, 2020 by megan

       

Calculating elevation profile for a polyline in ArcGIS 10?

2  Asked on September 16, 2020 by robert-buckley

         

Select extent on canvas – QGIS Plugin

1  Asked on September 15, 2020 by vincent-br

       

Viewing MSSQL data in QGIS

1  Asked on September 15, 2020

     

QGIS 3.10.9 ImportError

0  Asked on September 15, 2020 by aarthi

 

Changing shapefile’s field type using fiona?

3  Asked on September 14, 2020 by franois-leblanc

       

Encoded Polyline – Animation

0  Asked on September 14, 2020 by vvid

 

How to calculate the hill area in Google Earth

0  Asked on September 13, 2020

 

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.