[postgis-users] reparing invalid geometry (polygon defined by oneor two points)

Obe, Regina robe.dnd at cityofboston.gov
Tue Mar 24 05:13:48 PDT 2009


I think a variant of Simon's script would work for you except instead of
keeping rings with a large enough hole, keep rings that are closed and
have enough points


so something like

CREATE OR REPLACE FUNCTION remove_invalidrings(geometry)
  RETURNS geometry AS
SELECT ST_BuildArea(ST_Collect(b.final_geom)) as filtered_geom
  FROM (SELECT ST_MakePolygon((/* Get outer ring of polygon */
		SELECT ST_ExteriorRing(a.the_geom) as outer_ring /* ie
the outer ring */
		),  ARRAY(/* Get all inner rings > a particular area */
		 SELECT ST_ExteriorRing(b.geom) as inner_ring
		   FROM (SELECT (ST_DumpRings(a.the_geom)).*) b
		  WHERE b.path[1] > 0 /* ie not the outer ring */
		    AND (ST_NPoints(b.geom) > 2 AND ST_IsClosed(b.geom))
		) ) as final_geom
         FROM (SELECT ST_GeometryN(ST_Multi($1),/*ST_Multi converts any
Single Polygons to MultiPolygons */
                                   ) as the_geom
               ) a
       ) b

Hope that helps,


-----Original Message-----
From: postgis-users-bounces at postgis.refractions.net
[mailto:postgis-users-bounces at postgis.refractions.net] On Behalf Of Ivan
Sent: Tuesday, March 24, 2009 5:03 AM
To: PostGIS Users Discussion
Subject: [postgis-users] reparing invalid geometry (polygon defined by
oneor two points)

Dear PostGIS users,
we have quite huge MULTIPOLYGON database table containing some 3
percents of invalid geometries. 
Most problematic to repair are MULTIPOLYGONs which often contain polygon
defined by one or two points.

Example is here (second polygon is defined by one point):
MULTIPOLYGON(((-266469.87 -1233431.41,-266423.25 -1233425.84,-266423.23
-1233426.17,-266422.74 -1233434.54,-266422.75 -1233439.44,-266425.68
-266457.02 -1233444.89,-266463.17 -1233437.07,-266463.27
-1233436.29,-266469.15 -1233437.08,-266469.84 -1233431.65,-266469.87
-1233431.41),(-266463.92 -1233431.02),
(-266459.41 -1233430.46,-266458.67 -1233436.67,-266451.29
-1233435.76,-266451.1 -1233437.26,-266438.9 -1233435.75,-266439.07
-1233434.35,-266426.37 -1233432.65,-266427.07 -1233426.46,
-266439.85 -1233428.04,-266459.41 -1233430.46)))

It is not possible to run buffer(0) or many other functions on these
geometries because of "POSTGIS2GEOS conversion failed" (no suprise in
case of such non-sense geometry).

Is there any possibility how to repair them directly in database?


postgis-users mailing list
postgis-users at postgis.refractions.net
The substance of this message, including any attachments, may be
confidential, legally privileged and/or exempt from disclosure
pursuant to Massachusetts law. It is intended
solely for the addressee. If you received this in error, please
contact the sender and delete the material from any computer.

More information about the postgis-users mailing list