Bienvenue sur PostGIS.fr

Bienvenue sur PostGIS.fr , le site de la communauté des utilisateurs francophones de PostGIS.

PostGIS ajoute le support d'objets géographique à la base de données PostgreSQL. En effet, PostGIS "spatialise" le serverur PostgreSQL, ce qui permet de l'utiliser comme une base de données SIG.

Maintenu à jour, en fonction de nos disponibilités et des diverses sorties des outils que nous testons, nous vous proposons l'ensemble de nos travaux publiés en langue française.

source: trunk/workshop-foss4g/geometry_returning.rst @ 20

Revision 1, 8.3 KB checked in by djay, 13 years ago (diff)

Initial import of the svn tree

RevLine 
[1]1.. _geometry_returning:
2
3Section 18: Geometry Constructing Functions
4===========================================
5
6All the functions we have seen so far work with geometries "as they are" and returns
7 
8* analyses of the objects (:command:`ST_Length(geometry)`, :command:`ST_Area(geometry)`),
9* serializations of the objects (:command:`ST_AsText(geometry)`, :command:`ST_AsGML(geometry)`),
10* parts of the object (:command:`ST_RingN(geometry,n)`) or
11* true/false tests (:command:`ST_Contains(geometry,geometry)`, :command:`ST_Intersects(geometry,geometry)`).
12
13"Geometry constructing functions" take geometries as inputs and output new shapes.
14
15
16ST_Centroid / ST_PointOnSurface
17-------------------------------
18
19A common need when composing a spatial query is to replace a polygon feature with a point representation of the feature. This is useful for spatial joins (as discussed in :ref:`polypolyjoins`) because using :command:`ST_Intersects(geometry,geometry)` on two polygon layers often results in double-counting: a polygon on a boundary will intersect an object on both sides; replacing it with a point forces it to be on one side or the other, not both.
20
21 * :command:`ST_Centroid(geometry)` returns a point that is approximately on the center of mass of the input argument. This simple calculation is very fast, but sometimes not desirable, because the returned point is not necessarily in the feature itself. If the input feature has a convexity (imagine the letter 'C') the returned centroid might not be in the interior of the feature.
22 * :command:`ST_PointOnSurface(geometry)` returns a point that is guaranteed to be inside the input argument. It is substantially more computationally expensive than the centroid operation.
23 
24.. image:: ./geometry_returning/centroid.jpg
25
26
27ST_Buffer
28---------
29
30The buffering operation is common in GIS workflows, and is also available in PostGIS. :command:`ST_Buffer(geometry,distance)` takes in a buffer distance and geometry type and outputs a polygon with a boundary the buffer distance away from the input geometry.
31
32.. image:: ./geometry_returning/st_buffer.png
33
34For example, if the US Park Service wanted to enforce a marine traffic zone around Liberty Island, they might build a 500 meter buffer polygon around the island. Liberty Island is a single census block in our ``nyc_census_blocks`` table, so we can easily extract and buffer it.
35
36.. code-block:: sql
37
38  -- Make a new table with a Liberty Island 500m buffer zone
39  CREATE TABLE libery_island_zone AS
40  SELECT ST_Buffer(the_geom,500) AS the_geom
41  FROM nyc_census_blocks
42  WHERE blkid = '360610001009000';
43
44  -- Update the geometry_columns table
45  SELECT Populate_Geometry_Columns();
46 
47.. image:: ./geometry_returning/liberty_positive.jpg
48
49The :command:`ST_Buffer` function also accepts negative distances and builds inscribed polygons within polygonal inputs. For lines and points you will just get an empty return.
50
51.. image:: ./geometry_returning/liberty_negative.jpg
52
53
54ST_Intersection
55---------------
56
57Another classic GIS operation -- the "overlay" -- creates a new coverage by calculating the intersection of two superimposed polygons. The resultant has the property that any polygon in either of the parents can be built by merging polygons in the resultant.
58
59The :command:`ST_Intersection(geometry A, geometry B)` function returns the spatial area (or line, or point) that both arguments have in common. If the arguments are disjoint, the function returns an empty geometry.
60
61.. code-block:: sql
62
63  -- What is the area these two circles have in common?
64  -- Using ST_Buffer to make the circles!
65 
66  SELECT ST_AsText(ST_Intersection(
67    ST_Buffer('POINT(0 0)', 2),
68    ST_Buffer('POINT(3 0)', 2)
69  ));
70
71.. image:: ./geometry_returning/intersection.jpg
72
73
74
75ST_Union
76--------
77
78In the previous example we intersected geometries, creating a new geometry that had lines from both the inputs. The :command:`ST_Union` does the reverse; it takes inputs and removes common lines. There are two forms of the :command:`ST_Union` function:
79
80 * :command:`ST_Union(geometry, geometry)`: A two-argument version that takes in two geometries and returns the merged union.  For example, our two-circle example from the previous section looks like this when you replace the intersection with a union.
81 
82   .. code-block:: sql
83
84     -- What is the total area these two circles cover?
85     -- Using ST_Buffer to make the circles!
86 
87     SELECT ST_AsText(ST_Union(
88       ST_Buffer('POINT(0 0)', 2),
89       ST_Buffer('POINT(3 0)', 2)
90     ));
91 
92   .. image:: ./geometry_returning/union.jpg
93   
94
95 * :command:`ST_Union([geometry])`: An aggregate version that takes in a set of geometries and returns the merged geometry for the entire group. The aggregate ST_Union can be used with the ``GROUP BY`` SQL statement to create carefully merged subsets of basic geometries. It is very powerful,
96 
97As an example of :command:`ST_Union` aggregation, consider our ``nyc_census_blocks`` table. Census geography is carefully constructed so that larger geographies can be built up from smaller ones. So, we can create a census tracts map by merging the blocks that form each tract (as we do later in :ref:`creatingtractstable`). Or, we can create a county map by merging blocks that fall within each county.
98
99To carry out the merge, note that the unique key ``blkid`` actually embeds information about the higher level geographies. Here are the parts of the key for Liberty Island we used earlier:
100
101::
102
103  360610001009000 = 36 061 00100 9000
104 
105  36     = State of New York
106  061    = New York County (Manhattan)
107  000100 = Census Tract
108  9      = Census Block Group
109  000    = Census Block
110 
111So, we can create a county map by merging all geometries that share the same first 5 digits of their ``blkid``.
112
113.. code-block:: sql
114
115  -- Create a nyc_census_counties table by merging census blocks
116  CREATE TABLE nyc_census_counties AS
117  SELECT
118    ST_Union(the_geom) AS the_geom,
119    SubStr(blkid,1,5) AS countyid
120  FROM nyc_census_blocks
121  GROUP BY countyid;
122 
123  -- Update the geometry_columns table
124  SELECT Populate_Geometry_Columns();
125 
126.. image:: ./geometry_returning/union_counties.png
127
128An area test can confirm that our union operation did not lose any geometry. First, we calculate the area of each individual census block, and sum those areas grouping by census county id.
129
130.. code-block:: sql
131
132  SELECT SubStr(blkid,1,5) AS countyid, Sum(ST_Area(the_geom)) AS area
133  FROM nyc_census_blocks
134  GROUP BY countyid;
135
136::
137
138  countyid |       area       
139 ----------+------------------
140  36005    | 109807439.720947
141  36047    | 184906575.839355
142  36061    | 58973521.6225586
143  36081    | 283764734.207275
144  36085    | 149806077.958252
145
146Then we calculate the area of each of our new county polygons from the county table:
147
148.. code-block:: sql
149
150  SELECT countyid, ST_Area(the_geom) AS area
151  FROM nyc_census_counties;
152
153::
154
155  countyid |       area       
156 ----------+------------------
157  36005    | 109807439.720947
158  36047    | 184906575.839355
159  36061    | 58973521.6225586
160  36081    | 283764734.207275
161  36085    | 149806077.958252
162
163The same answer! We have successfully built an NYC county table from our census blocks data.
164
165Function List
166-------------
167
168`ST_AsText(text) <http://postgis.org/docs/ST_AsText.html>`_: Returns the Well-Known Text (WKT) representation of the geometry/geography without SRID metadata.
169
170`ST_Buffer(geometry, distance) <http://postgis.org/docs/ST_Buffer.html>`_: For geometry: Returns a geometry that represents all points whose distance from this Geometry is less than or equal to distance. Calculations are in the Spatial Reference System of this Geometry. For geography: Uses a planar transform wrapper.
171
172`ST_Intersection(geometry A, geometry B) <http://postgis.org/docs/ST_Intersection.html>`_: Returns a geometry that represents the shared portion of geomA and geomB. The geography implementation does a transform to geometry to do the intersection and then transform back to WGS84.
173
174`ST_Union() <http://postgis.org/docs/ST_Union.html>`_: Returns a geometry that represents the point set union of the Geometries.
175
176`substring(string [from int] [for int]) <http://www.postgresql.org/docs/8.1/static/functions-string.html>`_: PostgreSQL string function to extract substring matching SQL regular expression.
177
178`sum(expression) <http://www.postgresql.org/docs/8.2/static/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE>`_: PostgreSQL aggregate function that returns the sum of records in a set of records.
Note: See TracBrowser for help on using the repository browser.