12.07.2015 Views

PostGIS 1.5.8 Manual - Fedora Project Packages GIT repositories

PostGIS 1.5.8 Manual - Fedora Project Packages GIT repositories

PostGIS 1.5.8 Manual - Fedora Project Packages GIT repositories

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>PostGIS</strong> <strong>1.5.8</strong> <strong>Manual</strong>40 / 3103. What is the largest municipality in the province, by area?This query brings a spatial measurement into the query condition. There are several ways of approaching this problem, butthe most efficient is below:SELECTname,ST_Area(the_geom)/10000 AS hectaresFROMbc_municipalityORDER BY hectares DESCLIMIT 1;name| hectares---------------+-----------------TUMBLER RIDGE | 155020.02556131(1 row)Note that in order to answer this query we have to calculate the area of every polygon. If we were doing this a lot it wouldmake sense to add an area column to the table that we could separately index for performance. By ordering the results in adescending direction, and them using the PostgreSQL "LIMIT" command we can easily pick off the largest value withoutusing an aggregate function like max().4. What is the length of roads fully contained within each municipality?This is an example of a "spatial join", because we are bringing together data from two tables (doing a join) but using aspatial interaction condition ("contained") as the join condition rather than the usual relational approach of joining on acommon key:SELECTm.name,sum(ST_Length(r.the_geom))/1000 as roads_kmFROMbc_roads AS r,bc_municipality AS mWHEREST_Contains(m.the_geom,r.the_geom)GROUP BY m.nameORDER BY roads_km;name| roads_km----------------------------+------------------SURREY | 1539.47553551242VANCOUVER | 1450.33093486576LANGLEY DISTRICT | 833.793392535662BURNABY | 773.769091404338PRINCE GEORGE | 694.37554369147...This query takes a while, because every road in the table is summarized into the final result (about 250K roads for ourparticular example table). For smaller overlays (several thousand records on several hundred) the response can be veryfast.5. Create a new table with all the roads within the city of Prince George.This is an example of an "overlay", which takes in two tables and outputs a new table that consists of spatially clipped orcut resultants. Unlike the "spatial join" demonstrated above, this query actually creates new geometries. An overlay is likea turbo-charged spatial join, and is useful for more exact analysis work:CREATE TABLE pg_roads asSELECTST_Intersection(r.the_geom, m.the_geom) AS intersection_geom,ST_Length(r.the_geom) AS rd_orig_length,r.*

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!