| 1 | = Clean Polygons = |
| 2 | |
| 3 | |
| 4 | It often happens that geometry data has invalid topology according |
| 5 | to the OGC model used by PostGIS. This situation causes |
| 6 | many operations to fail or give incorrect results. |
| 7 | |
| 8 | To correct this, it is necessary to correct the invalid polygonal topology by cleaning the polygon. |
| 9 | One the basic approach to do this is: |
| 10 | |
| 11 | * extract the linework from the polygon to a lineal geometry |
| 12 | * union the lineal geometry together along with a single point from the geometry (this effectively forces the the linework to be noded & dissolved) |
| 13 | * polygonize the resulting linework |
| 14 | |
| 15 | Caution: this may not create the expected topology if the polygon contains holes. |
| 16 | |
| 17 | Alternatively you can download a plpgsql function from [http://www.sogis1.so.ch/sogis/dl/postgis/cleanGeometry.sql http://www.sogis1.so.ch/sogis/dl/postgis/cleanGeometry.sql] |
| 18 | This function works with polygons containig holes. |
| 19 | |
| 20 | == Example: == |
| 21 | |
| 22 | {{{ |
| 23 | SELECT cleanGeometry(the_geom) FROM foo; |
| 24 | |
| 25 | }}} |
| 26 | |
| 27 | |
| 28 | |
| 29 | == Example: == |
| 30 | |
| 31 | |
| 32 | {{{ |
| 33 | -- Polygonize the noded linework |
| 34 | SELECT ST_BuildArea(geom) AS geom |
| 35 | FROM |
| 36 | ( |
| 37 | -- Node & dissolve the linear ring |
| 38 | SELECT ST_Union(geom, ST_Startpoint(geom)) AS geom |
| 39 | FROM |
| 40 | ( |
| 41 | -- Extract the exterior ring of the polygon |
| 42 | SELECT ST_ExteriorRing( |
| 43 | 'POLYGON(( |
| 44 | 9.50351715087891 47.3943328857422, |
| 45 | 9.50386047363281 47.3943328857422, |
| 46 | 9.50351715087891 47.3943328857422, |
| 47 | 9.50248718261719 47.3943328857422, |
| 48 | 9.50214385986328 47.3939895629883, |
| 49 | 9.50180053710938 47.3943328857422, |
| 50 | 9.50145721435547 47.3939895629883, |
| 51 | 9.50111389160156 47.3936462402344, |
| 52 | 9.50145721435547 47.3936462402344, |
| 53 | 9.50145721435547 47.3939895629883, |
| 54 | 9.50214385986328 47.3939895629883, |
| 55 | 9.50248718261719 47.3939895629883, |
| 56 | 9.50386047363281 47.3943328857422, |
| 57 | 9.50351715087891 47.3943328857422))'::geometry) AS geom |
| 58 | ) AS ring |
| 59 | ) AS fixed_ring |
| 60 | |
| 61 | }}} |
| 62 | |
| 63 | |
| 64 | which produces: |
| 65 | |
| 66 | MULTIPOLYGON ((( |
| 67 | 9.50351715087891 47.3943328857422, |
| 68 | 9.50386047363281 47.3943328857422, |
| 69 | 9.50248718261719 47.3939895629883, |
| 70 | 9.50214385986328 47.3939895629883, |
| 71 | 9.50248718261719 47.3943328857422, |
| 72 | 9.50351715087891 47.3943328857422 |
| 73 | )), (( |
| 74 | 9.50145721435547 47.3939895629883, |
| 75 | 9.50180053710938 47.3943328857422, |
| 76 | 9.50214385986328 47.3939895629883, |
| 77 | 9.50145721435547 47.3939895629883 |
| 78 | )), (( |
| 79 | 9.50145721435547 47.3939895629883, |
| 80 | 9.50145721435547 47.3936462402344, |
| 81 | 9.50111389160156 47.3936462402344, |
| 82 | 9.50145721435547 47.3939895629883 |
| 83 | ))) |