Changes between Initial Version and Version 1 of UsersWikiExamplesOverlayTables


Ignore:
Timestamp:
Apr 14, 2009, 9:09:20 AM (15 years ago)
Author:
pierre
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiExamplesOverlayTables

    v1 v1  
     1= Examples Overlay Tables =
     2
     3Bill Thoen [http://postgis.refractions.net/pipermail/postgis-users/2006-September/013091.html asks]:
     4
     5"Using PopstgreSQL/PostGIS, what are the steps I need to use to split one layer of polygons with another layer and also maintain data attrributes among the polygons that are split? For example, I've got corn and soybean field polygons that need to be split at the borders of overlaying CLU (Common Land Unit) polygons, and if a corn field crosses under a CLU boundary, I want to split the field at the CLU boundary and copy the orignal field id, crop code and other attributes into the two (or more) resulting farm field polygons (and probably assign a new unique polygon id to them all too.) "
     6
     7Paul Ramsey [http://postgis.refractions.net/pipermail/postgis-users/2006-September/013093.html answers]:
     8
     9
     10{{{
     11CREATE TABLE new_fields AS
     12SELECT
     13   Intersection(f.the_geom, c.the_geom) AS the_geom,
     14   f.attr1,
     15   f.attr2,
     16   c.clu_name
     17FROM
     18   fields f,
     19   clu c
     20WHERE
     21   f.the_geom && c.the_geom
     22AND
     23   Intersects(f.the_geom, c.the_geom)
     24
     25}}}
     26
     27
     28Kevin Neufeld answers:
     29
     30Given two polygonal datasets, fields and clu boundaries,
     31[[http://postgis.refractions.net/support/wiki/overlay1.png]]
     32
     33One could perform an overlay operation by:
     34
     351. Extract the linework from the polygons (you may wish to add any interior rings to this query).
     36
     37{{{
     38CREATE TEMP TABLE all_lines AS
     39SELECT St_ExteriorRing(the_geom) AS the_geom
     40FROM fields
     41UNION ALL
     42SELECT St_ExteriorRing(the_geom) AS the_geom
     43FROM clu;
     44
     45}}}
     46
     47
     482. Node the linework:
     49
     50{{{
     51CREATE TEMP TABLE noded_lines AS
     52SELECT St_Union(the_geom) AS the_geom
     53FROM all_lines;
     54
     55}}}
     56
     57
     583. Re-polygonize all linework:
     59
     60{{{
     61CREATE TEMP TABLE new_polys (id serial PRIMARY KEY, the_geom geometry);
     62INSERT INTO new_polys (the_geom)
     63SELECT geom AS the_geom
     64FROM St_Dump((
     65 SELECT St_Polygonize(the_geom) AS the_geom
     66 FROM noded_lines
     67));
     68
     69}}}
     70
     71
     724. Transfer attributes from original polygons to newly formed polygons
     73  * Create point-in-polygon features.
     74
     75{{{
     76CREATE TEMP TABLE new_polys_pip AS
     77SELECT id, ST_PointOnSurface(the_geom) AS the_geom
     78FROM new_polys;
     79
     80}}}
     81
     82
     83  * Overlay points with original polygons, transferring the attributes.
     84
     85{{{
     86CREATE TEMP TABLE pip_with_attributes AS
     87SELECT a.id, f.attr1, c.attr2
     88FROM new_polys_pip a
     89LEFT JOIN fields f ON St_Within(a.the_geom, f.the_geom)
     90LEFT JOIN clu c ON St_Within(a.the_geom, c.the_geom);
     91
     92}}}
     93
     94
     95  * Join the points to the newly formed polygons, transferring the attributes:
     96
     97{{{
     98CREATE TABLE new_fields AS
     99SELECT *
     100FROM new_polys a LEFT JOIN pip_with_attributes b USING (id);
     101
     102}}}
     103
     104
     105[[http://postgis.refractions.net/support/wiki/overlay2.png]]