| 42 | |
| 43 | 2. extract rings out of polygons |
| 44 | |
| 45 | {{{ |
| 46 | create table rings as ( |
| 47 | select st_exteriorRing((st_dumpRings(geom)).geom) as g |
| 48 | from poly |
| 49 | ); |
| 50 | }}} |
| 51 | |
| 52 | 3. Simplify the rings. At this step, we choose the simplification ratio we want (some trials can be made by calling st_simplifyPreserveTopology on departement table). |
| 53 | Here, no points further than 10km: |
| 54 | |
| 55 | {{{ |
| 56 | create table simplerings as ( |
| 57 | select st_simplifyPreserveTopology(st_linemerge(st_union(g)), 10000) as g |
| 58 | from rings |
| 59 | ); |
| 60 | }}} |
| 61 | |
| 62 | 4. extract lines as individual objects, in order to rebuild polygons from these simplified lines |
| 63 | |
| 64 | {{{ |
| 65 | create table simplelines as ( |
| 66 | select (st_dump(g)).geom as g |
| 67 | from simplerings |
| 68 | ); |
| 69 | }}} |
| 70 | |
| 71 | 5.rebuild the polygons, first by polygonizing the lines, with a distinct clause to eliminate overlaping segments that may prevent polygon to be created, then dump the collection of polygons into individual parts, in order to rebuild our layer. |
| 72 | {{{ |
| 73 | create table simplepolys as ( |
| 74 | select (st_dump(st_polygonize(distinct g))).geom as g |
| 75 | from simplelines |
| 76 | ); |
| 77 | }}} |
| 78 | |
| 79 | 6. Add an id column to help us identify objects and a spatial index |
| 80 | |
| 81 | {{{ |
| 82 | alter table simplepolys add column gid serial primary key; |
| 83 | create index simplepolys_geom_gist on simplepolys using gist(g); |
| 84 | }}} |
| 85 | |
| 86 | 7. attribute association between input layer and simplified polygons: First method to retrieve attribute is based on containment of a point of the surface of simplified polygons. |
| 87 | |
| 88 | {{{ |
| 89 | create table simpledep as ( |
| 90 | select code_dept, g |
| 91 | from departement d, simplepolys s |
| 92 | where st_contains(d.geom, st_pointOnSurface(s.g)) |
| 93 | ); |
| 94 | }}} |
| 95 | |
| 96 | It does not work: code_dept=92 is a curved polygon and fails with st_contains() test: |
| 97 | |
| 98 | [[Image(SPT_bad_attributes.png)]] |
| 99 | |
| 100 | 8. Second method is based on percentage of overlaping area comparison. It gives better results in our testcase. |
| 101 | {{{ |
| 102 | create table simpledep as ( |
| 103 | select distinct on (d.code_dept) d.code_dept, s.g as geom |
| 104 | from departement d, simplepolys s |
| 105 | where st_intersects(d.geom, s.g) |
| 106 | order by d.code_dept, st_area(st_intersection(s.g, d.geom))/st_area(s.g) desc |
| 107 | ); |
| 108 | }}} |
| 109 | |
| 110 | 9. rebuild departements by grouping them by code_dept (other attributes could be re-associated here): |
| 111 | |
| 112 | {{{ |
| 113 | create table simple_departement as ( |
| 114 | select code_dept, st_collect(geom) as geom |
| 115 | from simpledep |
| 116 | group by code_dept |
| 117 | ); |
| 118 | }}} |
| 119 | |
| 120 | == Result == |
| 121 | |
| 122 | Layer now looks like this: |
| 123 | |
| 124 | [[Image(SPT_simple_dept.png)]] |
| 125 | |
| 126 | Small island where collapsed during process: |
| 127 | |
| 128 | [[Image(SPT_islands_removed.png)]] |