[postgis-users] Removing tiny polygons

381 views
Skip to first unread message

Tom McCallum

unread,
Jan 27, 2014, 2:09:56 PM1/27/14
to postgi...@lists.osgeo.org
Hi all,

If I have a polygon geometry field which seems to be made up of a large
number of tiny polygons and some larger ones, what is the best way to
remove polygons below a certain size? Would I need to preprocess this and
save in a new table/record or can I dynamically do it?

Thanks

Tom
_______________________________________________
postgis-users mailing list
postgi...@lists.osgeo.org
http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users

Morten Sickel

unread,
Jan 27, 2014, 2:15:18 PM1/27/14
to PostGIS Users Discussion
It depends on what you mean with "remove" - do you want to just have the
large polygons without the area that is covered by the small ones or do
you want to add the area of the small polygons onto one or more of the
larges? (e.g. how one typically wants to handle shiver polygons)

In the first case it is as easy as a query like

select the_geom where ST_area(the_geom) > ...

Morten


Tom McCallum skrev:

Tom McCallum

unread,
Jan 27, 2014, 2:48:25 PM1/27/14
to mor...@sickel.net, PostGIS Users Discussion
Thanks Morten. I wanted the first case, so that helps.

On Mon, 27 Jan 2014 19:15:18 -0000, Morten Sickel <mor...@sickel.net>
wrote:

Tom McCallum

unread,
Jan 27, 2014, 3:29:20 PM1/27/14
to PostGIS Users Discussion
Ok, really silly question now, how can a polygon field contain what looks
like multiple polygons.

So I have a Shapefile of GB but its only got 3 polygons in, according to
PostGis, but there are lots of small island polygons as well which are not
attached to any of the main land masses, so I don't see how they can all
be one polygon? Can a polygon field have multiple polygons in without
being a multi-polygon?

Thanks

On Mon, 27 Jan 2014 19:15:18 -0000, Morten Sickel <mor...@sickel.net>
wrote:

Mike Toews

unread,
Jan 27, 2014, 3:48:42 PM1/27/14
to PostGIS Users Discussion
On 28 January 2014 09:29, Tom McCallum <ter...@googlemail.com> wrote:
> Ok, really silly question now, how can a polygon field contain what looks
> like multiple polygons.
>
> So I have a Shapefile of GB but its only got 3 polygons in, according to
> PostGis, but there are lots of small island polygons as well which are not
> attached to any of the main land masses, so I don't see how they can all be
> one polygon? Can a polygon field have multiple polygons in without being a
> multi-polygon?

It's probably a MultiPolygon. However, it is possible to have a
regular Polygon of the ocean surrounding GB, with many internal rings
for each island. See Well-known text [1] to get an idea of how these
things are actually stored. Furthermore, you can query to see the
geometry type using GeometryType [2].

If you have a MultiPolygon, you can split it into several regular
polygons with ST_Dump.

-Mike

[1] https://en.wikipedia.org/wiki/Well-known_text
[2] http://postgis.net/docs/GeometryType.html
[3] http://postgis.net/docs/ST_Dump.html

Astrid Bjørnerød

unread,
Jan 28, 2014, 1:18:56 AM1/28/14
to PostGIS Users Discussion
Is there anyone you have written some sql-code for removing the small polygons onto one of the neighbor-polygons ?
Any stabil functions for this task?

Astrid

-----Opprinnelig melding-----
Fra: postgis-us...@lists.osgeo.org [mailto:postgis-us...@lists.osgeo.org] På vegne av Tom McCallum
Sendt: 27. januar 2014 20:48
Til: mor...@sickel.net; PostGIS Users Discussion
Emne: Re: [postgis-users] Removing tiny polygons

Morten Sickel

unread,
Jan 28, 2014, 3:01:21 AM1/28/14
to PostGIS Users Discussion
I never used it, but it should be along the lines discussed here:
http://gis.stackexchange.com/questions/49650/merge-polygons-in-postgis

if it is just a few, it should be doable to do manually - I don't know if
it is possible to do automatically in SQL

For each poly with area < some_limit{
find largest neighboring poly
// or smallest, or whatever were you are sure you'll get a
well-defined answer
join the two polys
}

Morten


Astrid Bjørnerød skrev:

Rémi Cura

unread,
Jan 28, 2014, 4:45:22 AM1/28/14
to mor...@sickel.net, PostGIS Users Discussion
It is possible to do by groupping :

the idea is :
you group small polygons by the coordinate of there centroid, rounded (or snapped ot grid) so that polygon less than XX meter are in the same group.

Example :
SELECT ST_Union(poly.geom )
FROM my_poly AS poly
GROUP BY ST_SnapToGrid(ST_Centroid(poly.geom),XX)

Please note that this code doesn't enforce the fact that polygons are touching to merge, but you don"t really need this, because separate polygons will get merged into a multipolygons, which is easy to deal with by dump .

Cheers,

Rémi-C



2014-01-28 Morten Sickel <mor...@sickel.net>

Tom McCallum

unread,
Jan 28, 2014, 9:25:04 AM1/28/14
to mor...@sickel.net, PostGIS Users Discussion
Yes I just want the largest polygons. The problem being that when I
simplify the polygon it appears to need a lot more simplification due to
these islands rather than if it just did did one mainland polygon (
asGeoJson comes in at 437 polygons rather than 1 polygon). Not sure why
GeoJson can split it into multiple polygons but Dump[] just hands me one.

Was hoping to use ST_Area but currently cannot break down the single
polygon.

Interestingly QGIS can, if I do single to multipart it can break down into
main landmass and islands. What might be an equivalent postgis method for
doing this?


On Mon, 27 Jan 2014 19:15:18 -0000, Morten Sickel <mor...@sickel.net>
wrote:

Hugues François

unread,
Jan 28, 2014, 9:52:13 AM1/28/14
to PostGIS Users Discussion
What you say sounds very weird to me. I don't understand how postgis can consider a multipolygon as a polygon. Could you provide us a test sample of your data ?

Hugues.

Rémi Cura

unread,
Jan 28, 2014, 10:46:21 AM1/28/14
to PostGIS Users Discussion
If you are saying that dump doesn't break your multi polygon into pieces,
either
_ dump has a bug (unlikely)
_ you don't have a multipolygon but a geometry collection (try ST_Dump(ST_CollectionExtract(geom,2)))
_you have an invalid simple polygon (try st_isvalid() )
_you have a simple polygon where interior rings should be stand alone polygon, use St_ringextract
Cheers,

Rémi-C


2014-01-28 Hugues François <hugues....@irstea.fr>

Pierre Racine

unread,
Jan 28, 2014, 2:46:33 PM1/28/14
to PostGIS Users Discussion
Give a try to ST_TrimMulti() in the PostGIS Add-ons:

http://geospatialelucubrations.blogspot.ca/2013/11/launching-postgis-add-ons-new-postgis.html

https://github.com/pedrogit/postgisaddons/releases/tag/1.21

Pierre

> -----Original Message-----
> From: postgis-us...@lists.osgeo.org [mailto:postgis-users-
> bou...@lists.osgeo.org] On Behalf Of Astrid Bjørnerød
> Sent: Tuesday, January 28, 2014 1:19 AM
> To: PostGIS Users Discussion
> Subject: Re: [postgis-users] Removing tiny polygons
>
> Is there anyone you have written some sql-code for removing the small
> polygons onto one of the neighbor-polygons ?
> Any stabil functions for this task?
>
> Astrid
>
> -----Opprinnelig melding-----
> Fra: postgis-us...@lists.osgeo.org [mailto:postgis-users-
> bou...@lists.osgeo.org] På vegne av Tom McCallum

Tom McCallum

unread,
Jan 29, 2014, 4:46:23 AM1/29/14
to PostGIS Users Discussion
select st_isvalid(wkb_geometry) from mytable
NOTICE: Ring Self-intersection at or near point 4173.162435251591
6968968.0578562981
st_isvalid
------------
f
(1 row)

It had 287 interior rings. So am searching now for the largest. Thanks
would not have found this without your help!

Reply all
Reply to author
Forward
0 new messages