Changes between Version 1 and Version 2 of UsersWikiSimplifyPreserveTopology


Ignore:
Timestamp:
Apr 8, 2012, 6:21:14 AM (12 years ago)
Author:
nicolasribotosgeo
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiSimplifyPreserveTopology

    v1 v2  
    33== What we want ==
    44
    5 Simplifying this layer [[Image(SPT_dept_ori.png)]] into this: [[image:SPT_dept_sim.png]]
     5Simplifying this layer:
     6
     7 [[Image(SPT_dept_ori.png)]]
     8
     9into this:
     10
     11 [[Image(SPT_dept_sim.png)]]
    612
    713== The data ==
     
    2430== Steps ==
    2531
     32''Steps are divided into individual queries for the sake of clarity. The process could be done in a PL/PGSQL function to generalize it'''
     33
    26341. First extract the input multipolygons into polygons, keeping their departement code. This will allow us to associate attributes to each part of multipolygons at the end of the process.
    2735
    2836{{{
    29 create table dep_poly as select gid, code, st_dump
     37create table poly as (
     38    select gid, code_dept, (st_dump(geom)).*
     39    from departement
     40);
    3041}}}
     42
     432. extract rings out of polygons
     44
     45{{{
     46create table rings as (
     47    select st_exteriorRing((st_dumpRings(geom)).geom) as g
     48    from poly
     49);
     50}}}
     51
     523. 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).
     53Here, no points further than 10km:
     54
     55{{{
     56create table simplerings as (
     57    select st_simplifyPreserveTopology(st_linemerge(st_union(g)), 10000) as g
     58    from rings
     59);
     60}}}
     61
     624. extract lines as individual objects, in order to rebuild polygons from these simplified lines
     63
     64{{{
     65create table simplelines as (
     66    select (st_dump(g)).geom as g
     67    from simplerings
     68);
     69}}}
     70
     715.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{{{
     73create table simplepolys as (
     74    select (st_dump(st_polygonize(distinct g))).geom as g
     75    from simplelines
     76);
     77}}}
     78
     796. Add an id column to help us identify objects and a spatial index
     80
     81{{{
     82alter table simplepolys  add column gid serial primary key;
     83create index simplepolys_geom_gist on simplepolys  using gist(g);
     84}}}
     85
     867. 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{{{
     89create 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
     96It does not work: code_dept=92 is a curved polygon and fails with st_contains() test:
     97
     98 [[Image(SPT_bad_attributes.png)]]
     99
     1008. Second method is based on percentage of overlaping area comparison. It gives better results in our testcase.
     101{{{
     102create 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
     1109. rebuild departements by grouping them by code_dept (other attributes could be re-associated here):
     111
     112{{{
     113create 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
     122Layer now looks like this:
     123
     124[[Image(SPT_simple_dept.png)]]
     125
     126Small island where collapsed during process:
     127
     128[[Image(SPT_islands_removed.png)]]