Intro To PostGIS

37,893 views

Published on

Slides from my Introduction to PostGIS workshop at the FOSS4G conference in 2009. The material is available at http://revenant.ca/www/postgis/workshop/

Published in: Technology
1 Comment
15 Likes
Statistics
Notes
  • enjoy and interesting subjec thank slideshare
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
37,893
On SlideShare
0
From Embeds
0
Number of Embeds
68
Actions
Shares
0
Downloads
581
Comments
1
Likes
15
Embeds 0
No embeds

No notes for slide

Intro To PostGIS

  1. 1. Introduction to PostGIS Presented by: Mark Leslie Mike Pumphrey Paul Ramsey
  2. 2. Workshop Format Interactive C:workshopsintro_to_postgis http://localhost/postgis/workshop Hands On All examples are executable Copy and Paste from HTML Exercises Answers will be provided (eventually) Try This sections for greater challenge
  3. 3. What is a Spatial Database? Spatial Data Types Point a single coordinate of two to four dimensions
  4. 4. What is a Spatial Database? Spatial Data Types Linestring a set of two or more coordinates linear interpretation of path between coordinates
  5. 5. What is a Spatial Database? Spatial Data Types Linearring a linestring with three or more coordinates the start and end points are the same
  6. 6. What is a Spatial Database? Spatial Data Types Polygon a set of one or more linearrings one ring defines the exterior boundary remainder defines the holes in the polygon
  7. 7. What is a Spatial Database? Spatial Data Types Multi-geometries (Multipoint, Multilinestring, Multipolygon) a set of like geometries
  8. 8. What is a Spatial Database? Spatial Data Types Geometrycollection a set of various (unmatched) geometries
  9. 9. What is a Spatial Database? Spatial Data Types Spatial Indexing R-tree Quadtree Grid-based
  10. 10. What is a Spatial Database? Spatial Data Types Spatial Indexing Spatial Functions Construction Serialisation Predicates Analysis Accessors Builders Aggregates
  11. 11. What is PostGIS? Spatial Extensions for PostgreSQL Provides Spatial Data Type Provides Spatial Indexing Provides Spatial Functions
  12. 12. What is PostGIS? Spatial Extensions for PostgreSQL PostgreSQL Extensions for Spatial ACID transaction guarantees Enterprise reliability Crash recovery Hot backup Replication SQL support
  13. 13. PostGIS History Initially released May 2001 with only load/store and index support. Functions added based on Simple Features for SQL (SFSQL) UMN MapServer added PostGIS support in mid-2001 Geometry Engine, Open Source (GEOS) was released, providing the hard SFSQL functions PostGIS 1.0 provided a faster, lightweight geometry object
  14. 14. Who uses PostGIS? Institut Geographique National, France National mapping agency of France Stores high-res topographic data GlobeXplorer Provides web-based access to petabytes of imagery PostGIS is used to manage metadata and search for relevant imagery
  15. 15. PostgreSQL Setup Installing PostgreSQL Software provided it C:workshopsPostGISsoftware Double click postgresql-8.1.1-1-windows.exe Creating a Spatial Database Using pgAdmin III Loading Spatial Data The horrors of the command line
  16. 16. PostgreSQL Installation
  17. 37. PostGIS Documentation
  18. 38. Creating a Spatial Database
  19. 47. Loading Data
  20. 48. Loading Data Change to C:workshopsPostGISdata Execute set_environment.bat
  21. 50. Load the Parks Shapefile shp2pgsql -s 2270 medford_parks.shp medford_parks > medford_parks.sql psql -f medford_parks.sql workshop
  22. 54. Load the Schools Shapefile shp2pgsql -s 2270 -D jacksonco_schools.shp jacksonco_schools > jacksonco_schools.sql psql -f jacksonco_schools.sql workshop
  23. 57. Load Additional Data psql -f medford.sql workshop
  24. 59. Geometries – Creating and Manipulating Creating simple geometries Consistent geometries through constraints Geometry types and output Accessing geometry components Measurement Functions
  25. 60. Point Creation CREATE TABLE points (name varchar, point geometry) INSERT INTO points VALUES ('Origin', 'POINT(0 0)'), ('North', 'POINT(0 1)'), ('East', 'POINT(1 0)'), ('West', 'POINT(-1 0)'), ('South', 'POINT(0 -1)'); SELECT name, ST_AsText(point) FROM points;
  26. 62. Line Creation CREATE TABLE lines (name varchar); SELECT AddGeometryColumn('lines', 'line', -1, 'LINESTRING', 2); INSERT INTO lines VALUES ('North West', 'LINESTRING(0 0,-1 1)'), ('North East', 'LINESTRING(0 0, 1 1)'), ('South West', 'LINESTRING(0 0,-1 -1)'), ('South East', 'LINESTRING(0 0,1 -1)'); SELECT name, ST_AsText(line) FROM lines;
  27. 64. Unconstrained Geometries INSERT INTO points VALUES ('Not a point', 'LINESTRING(1 1, -1 -1)'), ('3d point', 'POINT(0 0 3)'), ('WGS84 point', ST_SetSRID('POINT(0 1)', 4326));
  28. 66. Constrained Geometries INSERT INTO lines VALUES ('Not a line', 'POINT(0 0)'); INSERT INTO lines VALUES ('4d line', 'LINESTRING(1 1 3 0, -1 -1 0 3.4)'); INSERT INTO lines VALUES ('WGS84 line', ST_SetSRID('LINESTRING(-1 1,1 -1)',4326));
  29. 71. Metadata Table SELECT * FROM geometry_columns;
  30. 82. Accessing Geometry Components Multi-geometry ST_NumGeometries(geometry) ST_GeometryN(geometry, index)
  31. 83. Accessing Geometry Components Multi-geometry Polygon ST_NumInteriorRings(geometry) ST_NRings(geometry) ST_ExteriorRing(geometry) ST_InteriorRingN(geometry, index)
  32. 84. Accessing Geometry Components Multi-geometry Polygon Linestring ST_NumPoints(geometry) ST_NPoints(geometry) ST_PointN(geometry,index)
  33. 85. Accessing Geometry Components Multi-geometry Polygon Linestring Point ST_X(geometry) ST_Y(geometry) ST_Z(geometry) ST_M(geometry)
  34. 86. Length SELECT ST_Length(ST_GeomFromText( 'LINESTRING(0 0,5 0,5 10)'));
  35. 88. Length SELECT ST_Length(ST_GeomFromText( 'LINESTRING(0 0,5 0,5 10)')); SELECT ST_Length(ST_GeomFromText( 'LINESTRING(0 0 0,5 0 3,5 10 5)'));
  36. 90. Length SELECT ST_Length(ST_GeomFromText( 'LINESTRING(0 0,5 0,5 10)')); SELECT ST_Length(ST_GeomFromText( 'LINESTRING(0 0 0,5 0 3,5 10 5)')); SELECT ST_Length3D(ST_GeomFromText( 'LINESTRING(0 0 0,5 0 3,5 10 5)'));
  37. 92. Length SELECT ST_Length2D_Spheroid(g, 'SPHEROID["GRS 1980",6378137,298.257222101]'), ST_Length3D_Spheroid(g, 'SPHEROID["GRS 1980",6378137,298.257222101]') FROM ( VALUES ( ST_GeomFromEWKT('LINESTRING(151.1205 -33.7145 0,151.1218 -33.7087 54)') ) ) AS query(g);
  38. 94. Length - Perimeter SELECT ST_Perimeter(ST_GeomFromEWKT(g)) FROM ( VALUES ('POLYGON((-2 -2 0,2 -2 1,2 2 2,-2 2 1,-2 -2 0))'), ('POLYGON((-2 -2,2 -2,2 2,-2 2,-2 -2),(1 1,-1 1,-1 -1,1 -1,1 1))') ) AS query(g);
  39. 96. Length - Perimeter SELECT ST_Length( ST_ExteriorRing( ST_GeomFromEWKT( 'POLYGON((-2 -2,2 -2,2 2,-2 2,-2 -2),(1 1,-1 1,-1 -1,1 -1,1 1))' ) ) );
  40. 98. Area SELECT ST_Area(ST_GeomFromEWKT(g)) FROM ( VALUES ('POLYGON((-2 -2 0,2 -2 1,2 2 2,-2 2 1,-2 -2 0))'), ('POLYGON(( -2 -2,2 -2,2 2,-2 2,-2 -2 ),( 1 1,-1 1,-1 -1,1 -1,1 1 ))') ) AS query(g);
  41. 100. Distance SELECT ST_Distance(ST_GeomFromEWKT('POINT(0 5)'), ST_GeomFromEWKT('LINESTRING(-2 2,2 2)'));
  42. 102. Distance SELECT ST_Distance_Sphere(a, b), ST_Distance_Spheroid(a, b, 'SPHEROID["GRS 1980",6378137,298.257222101]') FROM ( VALUES ( ST_GeomFromText('POINT(151.1205 -33.7145)'), ST_GeomFromText('POINT(151.1218 -33.7087)') ) ) AS query (a, b);
  43. 104. Within a Distance SELECT namelow FROM jacksonco_streets, medford_parks WHERE ST_Distance(medford_parks.the_geom, jacksonco_streets.the_geom) < 5000 / 0.3048 AND medford_parks.name = 'Hawthorne Park / Pool';
  44. 106. Within a Distance SELECT namelow FROM jacksonco_streets, medford_parks WHERE ST_DWithin(medford_parks.the_geom, jacksonco_streets.the_geom, 5000 / 0.3048) AND medford_parks.name = 'Hawthorne Park / Pool';
  45. 108. Spatial Indexing CREATE INDEX jacksonco_streets_gix ON jacksonco_streets USING GIST (the_geom);
  46. 109. Spatial Indexing CREATE INDEX jacksonco_streets_gix ON jacksonco_streets USING GIST (the_geom);
  47. 110. Spatial Indexing SELECT namelow FROM jacksonco_streets, medford_parks WHERE jacksonco_streets.the_geom && medford_parks.the_geom AND medford_parks.name = 'Hawthorne Park / Pool';
  48. 113. Spatial Indexing SELECT namelow FROM jacksonco_streets, medford_parks WHERE ST_DWithin(medford_parks.the_geom, jacksonco_streets.the_geom, 5000 / 0.3048) AND medford_parks.name = 'Hawthorne Park / Pool';
  49. 115. Creating Indices CREATE INDEX jacksonco_schools_gix ON jacksonco_schools USING GIST (the_geom); CREATE INDEX jacksonco_taxlots_gix ON jacksonco_taxlots USING GIST (the_geom); CREATE INDEX medford_buildings_gix ON medford_buildings USING GIST (the_geom); CREATE INDEX medford_citylimits_gix ON medford_citylimits USING GIST (the_geom); CREATE INDEX medford_hydro_gix ON medford_hydro USING GIST (the_geom); CREATE INDEX medford_parks_gix ON medford_parks USING GIST (the_geom); CREATE INDEX medford_planzone_gix ON medford_planzone USING GIST (the_geom); CREATE INDEX medford_stormdrain_gix ON medford_stormdrain USING GIST (the_geom); CREATE INDEX medford_wards_gix ON medford_wards USING GIST (the_geom); CREATE INDEX medford_wetlands_gix ON medford_wetlands USING GIST (the_geom); CREATE INDEX medford_zoning_gix ON medford_zoning USING GIST (the_geom); CREATE INDEX tracts_gix ON tracts USING GIST (the_geom);
  50. 116. Vacuum Vacuum Recover or reuse disk space from obsolete rows Analyze Update query planner statistics Cluster Rewrite tables based on index ordering
  51. 117. Spatial Joins – Within SELECT name FROM jacksonco_schools, medford_citylimits WHERE ST_Within(jacksonco_schools.the_geom, medford_citylimits.the_geom);
  52. 119. Spatial Joins – Intersect SELECT SUM(ST_Length(jacksonco_streets.the_geom)) FROM jacksonco_streets, medford_citylimits WHERE ST_Intersects(jacksonco_streets.the_geom,medford_citylimits.the_geom);
  53. 121. Spatial Joins – Intersect SELECT jacksonco_schools.name, white_pop_1race * 1.0 / total_pop AS white_pop, black_pop_1race * 1.0 / total_pop AS black_pop, aindian_1race * 1.0 / total_pop AS indian_pop, asian_1race * 1.0 / total_pop AS asian_popp, hawaiian_1race * 1.0 / total_pop AS hawaiian_pop FROM tracts, race, jacksonco_schools WHERE tracts.ctidfp00 = race.geography_id2 AND ST_Intersects(tracts.the_geom, jacksonco_schools.the_geom);
  54. 123. Spatial Operators ST_Contains(geomA, geomB) ST_ContainsProperly(geomA, geomB) ST_Covers(geomA, geomB) ST_CoveredBy(geomA, geomB) ST_Crosses(geomA, geomB) ST_Disjoint(geomA, geomB) ST_Intersects(geomA, geomB) ST_Overlaps(geomA, geomB) ST_Touches(geomA, geomB) ST_Within(geomA, geomB)
  55. 124. Projecting Data SELECT SUM(ST_Length(the_geom)) FROM jacksonco_streets WHERE namelow = 'E Main St';
  56. 126. Projecting Data SELECT SUM(ST_Length(ST_Transform(the_geom, 2839))) FROM jacksonco_streets WHERE namelow = 'E Main St';
  57. 134. Exercises Twenty minutes to try the exercises
  58. 135. Exercises How big is the largest building in Medford in square feet? In square metres?
  59. 136. Exercises How big is the largest building in Medford in square feet? SELECT ST_Area(the_geom) AS area FROM medford_buildings ORDER BY area DESC LIMIT 1; In square metres? SELECT ST_Area(ST_Transform(the_geom, 2839)) AS area FROM medford_buildings ORDER BY area DESC LIMIT 1;
  60. 139. Exercises What is the elevation of the 'South Medford' high school building?
  61. 140. Exercises What is the elevation of the 'South Medford' high school building? SELECT medford_buildings.elevation FROM medford_buildings, jacksonco_schools WHERE ST_Within(jacksonco_schools.the_geom, medford_buildings.the_geom) AND jacksonco_schools.name = 'South Medford';
  62. 142. Exercises What are the expected percentages of children in poverty at each school with a Kindergarten class?
  63. 143. Exercises What are the expected percentages of children in poverty at each school with a Kindergarten class? SELECT jacksonco_schools.name, poverty.poverty_level_under5years FROM jacksonco_schools, tracts, poverty WHERE tracts.ctidfp00 = geography_id2 AND ST_Within( jacksonco_schools.the_geom, tracts.the_geom ) AND jacksonco_schools.grade ~ 'K';
  64. 145. Exercises What is the length of 'E Main St'?
  65. 146. Exercises What is the length of 'E Main St'? SELECT Sum(ST_Length(the_geom)) FROM jacksonco_streets WHERE legalname ~* 'E Main St';
  66. 148. Exercises How much park area is there within the Medford city limits?
  67. 149. Exercises How much park area is there within the Medford city limits? SELECT SUM(ST_Area(medford_parks.the_geom)) FROM medford_parks, medford_citylimits WHERE ST_Intersects(medford_parks.the_geom, medford_citylimits.the_geom);
  68. 151. Exercises How many buildings are located within wetlands?
  69. 152. Exercises How many buildings are located within wetlands? SELECT count(*) FROM medford_buildings, medford_wetlands WHERE ST_Within(medford_buildings.the_geom, medford_wetlands.the_geom);
  70. 154. Exercises Which school is farthest from a park? Which is closest?
  71. 155. Exercises Which school is farthest from a park? SELECT jacksonco_schools.name, ST_Distance(jacksonco_schools.the_geom, medford_parks.the_geom) AS distance FROM jacksonco_schools, medford_parks ORDER BY distance desc LIMIT 1; Which is closest? SELECT jacksonco_schools.name, ST_Distance(jacksonco_schools.the_geom, medford_parks.the_geom) AS distance FROM jacksonco_schools, medford_parks ORDER BY distance asc LIMIT 1;
  72. 158. Exercises Which schools have the most park area within 400 feet? Within 1 km?
  73. 159. Exercises Which schools have the most park area within 400 feet? SELECT jacksonco_schools.name, SUM(ST_Area(medford_parks.the_geom)) AS area FROM jacksonco_schools, medford_parks WHERE ST_DWithin(jacksonco_schools.the_geom, medford_parks.the_geom, 400) GROUP BY jacksonco_schools.name ORDER BY area DESC; Within 1 km?
  74. 161. Exercises Which schools have the most park area within 400 feet? Within 1 km? SELECT jacksonco_schools.name, SUM(ST_Area(medford_parks.the_geom)) AS area FROM jacksonco_schools, medford_parks WHERE ST_DWithin(ST_Transform(jacksonco_schools.the_geom, 2839), ST_Transform(medford_parks.the_geom, 2839), 1000) GROUP BY jacksonco_schools.name ORDER BY area DESC;
  75. 163. Exercises Which schools have the most park area within 400 feet? Within 1 km? SELECT jacksonco_schools.name, SUM(ST_Area(medford_parks.the_geom)) AS area FROM jacksonco_schools, medford_parks WHERE ST_DWithin(jacksonco_schools.the_geom, medford_parks.the_geom, 1000 / 0.3048) GROUP BY jacksonco_schools.name ORDER BY area DESC;
  76. 165. Exercises What are the expected percentages of unmarried families for each school?
  77. 166. Exercises What are the expected percentages of unmarried families for each school? SELECT s.name, 100.0 * t.hh_unmarried / t.hh_total FROM jacksonco_schools s, unmarriedbytract t WHERE ST_Contains(t.the_geom, s.the_geom);
  78. 168. Exercises How many storm drains are within 500 feet of 'Bear Creek'?
  79. 169. Exercises How many storm drains are within 500 feet of 'Bear Creek'? SELECT count(*) FROM medford_stormdrain, medford_hydro WHERE ST_DWithin(medford_hydro.the_geom, medford_stormdrain.the_geom, 500) AND medford_hydro.stream_nam = 'Bear Creek';
  80. 171. Tuning PostgreSQL for Spatial C:Program FilesPostgreSQL8.4datapostgresql.conf pgAdmin provides a Configuration Editor File ->Open postgresql.conf
  81. 174. shared_buffers Determines the amount of memory that is shared by back-end processes Default Value = 32MB Recommended Value = 500MB
  82. 176. work_mem Defines the amount of memory that a single process can use for sorting or hash operations Default Value = 1MB Recommended Value = 16MB
  83. 178. maintenance_work_mem Defines the amount of memory used for maintenance operations, such as vacuuming, index and foreign key creation. Default Value = 16MB Recommended Value = 16MB Can be set per-session before specific operations SET maintenance_work_mem TO '128MB'; VACUUM ANALYZE; SET maintenance_work_mem TO '16MB';
  84. 180. wal_buffers Amount of memory used by the write-ahead log (WAL). Default Value = 64kB Recommended Value = 1MB
  85. 182. checkpoint_segments Sets the number of log file segments that can be filled between WAL logs are flushed to disk. Default Value = 3 Recommended Value = 6
  86. 184. random_page_cost Represents the cost of random page access from disk. Default Value = 4.0 Recommended Value = 2.0
  87. 186. seq_page_cost Represents the cost of a sequential page access from disk. Default Value = 1.0 Recommended Value = 1.0
  88. 188. Query Plans Set of steps that PostgreSQL can use to generate the results of a query Multiple query plans are produced, costed and selected Cost is based on configuration parameters such as random_page_cost and seq_page_cost PostgreSQL and pgAdmin provides a way to view the victorious query plan
  89. 190. Test Query SELECT namelow FROM jacksonco_streets, medford_citylimits WHERE ST_Intersects(jacksonco_streets.the_geom, medford_citylimits.the_geom) GROUP BY namelow;
  90. 192. Sequence Scan Linear scan of every row in the table (medford_citylimits) Can evaluate filter conditions on scan
  91. 193. Index Scan Linear scan of an index (jacksonco_streets_gix) Evaluates the bounding box comparison during scan (jacksonco_streets.the_geom && medford_citylimits.the_geom) Comparison is evaluated for each result of the previous scan Execution time overlaps the sequence scan
  92. 194. Nested Loop Performs the join between the two scans One (sequence scan) is the outer loop Other (index scan) is the inner loop Further filter is evaluated Execution time includes index scan
  93. 195. Hash Aggregate Performs the grouping based on an attribute Only available for attributes with a hashing algorithm Executes after the nested loop completes
  94. 196. Visualisation uDig is used for visualisation It is available in c:workshopsPostGISsoftwareudig Double-click on udig.bat
  95. 206. Add Layers medford_hydro medford_parks medford_citylimits agebysexbytract
  96. 220. Validity Simple Features for SQL provides strict definitions of a valid feature Some functions require validity to perform as expected ST_IsValid(geometry) is provided to ensure feature validity
  97. 221. Validity Simple Features for SQL provides strict definitions of a valid feature Some functions require validity to perform as expected ST_IsValid(geometry) is provided to ensure feature validity SELECT count(*), ST_IsValid(the_geom) FROM jacksonco_taxlots GROUP BY ST_IsValid;
  98. 223. Fixing Validity UPDATE jacksonco_taxlots SET the_geom = ST_Multi(ST_Buffer(the_geom, 0)); SELECT count(*), ST_IsValid(the_geom) FROM jacksonco_taxlots GROUP BY ST_IsValid;
  99. 225. Equality PostGIS provides three different levels of equality Exactly Equal Spatially Equal BBox Equal
  100. 226. Equality CREATE TABLE polygons (name varchar, poly geometry); INSERT INTO polygons VALUES ('Polygon 1', 'POLYGON((-1 1.732,1 1.732,2 0,1 -1.732, -1 -1.732,-2 0,-1 1.732))'), ('Polygon 2', 'POLYGON((-1 1.732,-2 0,-1 -1.732,1 -1.732, 2 0,1 1.732,-1 1.732))'), ('Polygon 3', 'POLYGON((1 -1.732,2 0,1 1.732,-1 1.732, -2 0,-1 -1.732,1 -1.732))'), ('Polygon 4', 'POLYGON((-1 1.732,0 1.732, 1 1.732,1.5 0.866, 2 0,1.5 -0.866,1 -1.732,0 -1.732,-1 -1.732,-1.5 -0.866, -2 0,-1.5 0.866,-1 1.732))'), ('Polygon 5', 'POLYGON((-2 -1.732,2 -1.732,2 1.732, -2 1.732,-2 -1.732))');
  101. 228. Exactly Equal Point-by-point comparison of two geometries SELECT a.name, b.name, CASE WHEN a.poly ~= b.poly THEN 'Exactly Equal' ELSE 'Not Exactly Equal' end FROM polygons as a, polygons as b;
  102. 230. Spatially Equal Tests the topology of two geometries for equality SELECT a.name, b.name, CASE WHEN ST_Equals(a.poly, b.poly) THEN 'Spatially Equal' ELSE 'Not Equal' end FROM polygons as a, polygons as b;
  103. 232. Equal Bounds Tests for equality of the bounding box SELECT a.name, b.name, CASE WHEN a.poly = b.poly THEN 'Equal Bounds' ELSE 'Non-equal Bounds' end FROM polygons as a, polygons as b;
  104. 234. Advanced Material Advanced Functions Aggregates / Deaggregates Processing Set Operations Performance Tools Manipulating the Query Planner Denormalization Data Partitioning
  105. 235. Using uDig uDig lacks a dynamic query capability Queries can be viewed by creating views CREATE VIEW example1 AS SELECT * FROM (VALUES (ST_GeomFromText( 'MULTIPOLYGON(((-77 56,-52 18,-88 -27,-10 -13,-11 38,-77 56)))')), (ST_GeomFromText('MULTIPOLYGON(((-49 63,-32 24,-39 -7,-66 -19,-72 -9,-74 31,-49 63)))'))) AS query(the_geom); SELECT populate_geometry_columns();
  106. 237. ST_Union(geometry) Merges geometries into a single (often multi-) geometry Support aggregate form as well as: ST_Union(geomA, geomB) ST_Union(geomArray[ ])
  107. 238. ST_Union(geometry) SELECT ST_AsText(ST_Union(st_geomfromtext)) FROM (SELECT ST_GeomFromText( 'MULTIPOLYGON(((-77 56,-52 18,-88 -27,-10 -13,-11 38,-77 56)))') UNION ALL SELECT ST_GeomFromText( 'MULTIPOLYGON(((-49 63,-32 24,-39 -7,-66 -19,-72 -9,-74 31,-49 63)))') ) as a;
  108. 240. ST_Collect(geometry) Returns a single multi-geometry or collection, but performs no merging of geometries Faster that ST_Union Supports aggregate form as well as: ST_Collect(geomA, geomB) ST_Collect(geomArray[ ])
  109. 241. ST_Collect(geometry) SELECT ST_AsText(ST_Collect(the_geom)) FROM (SELECT 'LINESTRING(0 0, 0 1)'::geometry the_geom UNION ALL SELECT 'LINESTRING(1 0, 1 1)'::geometry the_geom UNION ALL SELECT 'LINESTRING(0 0,1 0)'::geometry the_geom UNION ALL SELECT 'LINESTRING(1 1, 0 1)'::geometry the_geom) as a;
  110. 243. ST_Polygonize(geometry) Generates a geometry containing all polygons that can be built from the input linework
  111. 244. ST_Polygonize(geometry) SELECT ST_AsText(ST_Polygonize(the_geom)) FROM (SELECT 'LINESTRING(0 0, 0 1)'::geometry the_geom UNION ALL SELECT 'LINESTRING(1 0, 1 1)'::geometry the_geom UNION ALL SELECT 'LINESTRING(0 0,1 0)'::geometry the_geom UNION ALL SELECT 'LINESTRING(1 1, 0 1)'::geometry) as a;
  112. 246. ST_Dump(geometry) Splits multi-geometries and collections into a set of simple geometries Inverse ST_Collect (ish) Provides an index of the geometry within the collection (path) and the geometry itself (ST_Dump(the_geom)).geom (ST_Dump(the_geom)).path[1]
  113. 247. ST_Dump(geometry) SELECT ST_AsText((ST_Dump(the_geom)).geom) FROM jacksonco_taxlots WHERE gid = 90917;
  114. 249. ST_DumpRings(geometry) Returns a set of polygons without holes Each polygon is one of the rings of the input polygon Also includes a path and geom components (ST_DumpRings(geom)).geom (ST_DumpRings(geom)).path[1]
  115. 250. Set Operations Produce results based on inclusion or exclusion of points from a geometry A geometry includes all point on or within its boundary Excludes all other points
  116. 251. Set Operations Produce results based on inclusion or exclusion of points from a geometry Point includes only the point itself
  117. 252. Set Operations Produce results based on inclusion or exclusion of points from a geometry Linestring includes the two end points and all point along its length
  118. 253. Set Operations Produce results based on inclusion or exclusion of points from a geometry Polygon Includes all exterior and interior rings Includes all points contained within the exterior ring and not contained within the interior rings Excludes all points contained within interior rings
  119. 254. Set Operations MULTIPOLYGON(((-77 56,-52 18,-88 -27,-10 -13,-11 38,-77 56))) MULTIPOLYGON(((-49 63,-32 24,-39 -7,-66 -19,-72 -9,-74 31,-49 63)))
  120. 255. ST_Union(geomA, geomB) Same as the ST_Union(geometry) aggregate Any point included in either geometry is included in the result SELECT ST_AsText(ST_Union( ST_GeomFromText( 'MULTIPOLYGON(((-77 56,-52 18,-88 -27,-10 -13,-11 38,-77 56)))'), ST_GeomFromText( 'MULTIPOLYGON(((-49 63,-32 24,-39 -7,-66 -19,-72 -9,-74 31,-49 63)))')));
  121. 258. ST_Difference(geomA, geomB) All point included in geomA that are not included in geomB Non-communicative; the order of geomA and geomB matters SELECT ST_AsText(ST_Difference( ST_GeomFromText( 'MULTIPOLYGON(((-77 56,-52 18,-88 -27,-10 -13,-11 38,-77 56)))'), ST_GeomFromText( 'MULTIPOLYGON(((-49 63,-32 24,-39 -7,-66 -19,-72 -9,-74 31,-49 63)))')));
  122. 261. ST_SymDifference(geomA, geomB) All points that or included in only one of geomA and geomB, but not both ST_Union(ST_Difference(A,B),ST_Difference(B,A)) SELECT ST_AsText(ST_SymDifference( ST_GeomFromText( 'MULTIPOLYGON(((-77 56,-52 18,-88 -27,-10 -13,-11 38,-77 56)))'), ST_GeomFromText( 'MULTIPOLYGON(((-49 63,-32 24,-39 -7,-66 -19,-72 -9,-74 31,-49 63)))')));
  123. 264. ST_Intersection(geomA, geomB) All points included in both geomA and geomB SELECT ST_AsText(ST_Intersection( ST_GeomFromText( 'MULTIPOLYGON(((-77 56,-52 18,-88 -27,-10 -13,-11 38,-77 56)))'), ST_GeomFromText( 'MULTIPOLYGON(((-49 63,-32 24,-39 -7,-66 -19,-72 -9,-74 31,-49 63)))')));
  124. 267. ST_Buffer(geometry, distance) Returns a geometry containing an area with distance of the input geometry. Can take a third argument defining number of segments used to approximate a quarter circle (defaults to 8) SELECT ST_AsText(ST_Buffer(ST_GeomFromText( 'LINESTRING(-2 -2,-2 2,2 2,2 4)'), 1));
  125. 270. ST_ConvexHull(geometry) Returns a polygon that encloses the input geometry, removing all possible concave angles Analogous to 'shrink wrapping' the geometry SELECT ST_AsText(ST_ConvexHull( 'LINESTRING(-2 -2,-2 2,2 2,2 4)'));
  126. 273. ST_SnapToGrid(...) Snaps every point in the input geometry to the defined grid Allows you to: control the precision of data for reliable comparison reduce size of data Numerous variants to give you what you need
  127. 274. ST_SnapToGrid(...) Numerous variants to give you what you need ST_SnapToGrid(geom, size) ST_SnapToGrid(geom, sizeX, sizeY) ST_SnapToGrid(geom, originX, originY, sizeX, sizeY) ST_SnapToGrid(geom, originPoint, sizeX, sizeY, sizeZ, sizeM)
  128. 275. ST_Simplify(geom, tolerance) Creates a simpler geometry Simplifications are made to ensure that the new line deviates from the original by less that the tolerance
  129. 276. ST_Simplify(geom, tolerance) The line is simplified by producing a candidate line connecting the end points
  130. 277. ST_Simplify(geom, tolerance) The greatest distance between the candidate line and the original line is calculated If the distance is greater than the tolerance, the candidate line is rejected and two candidate lines are created
  131. 278. ST_Simplify(geom, tolerance) Each new candidate line is tested in the same manner as before When the distances is less than the tolerance, the candidate line is accepted
  132. 279. ST_Simplify(geom, tolerance) The final result is a geometry made up of all accepted candidate lines
  133. 280. ST_Simplify(geom, tolerance) SELECT ST_AsText(geom) AS original, ST_AsText(ST_Simplify(geom, 3)) AS &quot;3&quot;, ST_AsText(ST_Simplify(geom, 2.9)) AS &quot;2.9&quot; FROM ( SELECT ST_GeomFromText('LINESTRING(0 0,3 2.5,0 5)') AS geom) AS a;
  134. 282. ST_SimplifyPreserveTopology Same algorithm as ST_Simplify Will not change the type of geometry SELECT ST_AsText(geom) AS original, ST_AsText(ST_Simplify(geom, 2)) AS Simplify, ST_AsText(ST_SimplifyPreserveTopology(geom, 2)) AS PreserveTopology FROM ( SELECT ST_GeomFromText('POLYGON((0 0,0 1,1 1,1 0,0 0))') AS geom) AS a;
  135. 284. Query Planner Manipulation Query planner only considers approved actions Various execution paths can be disabled Cost estimates can be manipulated on a per-session basis
  136. 285. Denormalisation Split feature types into multiple tables based on known or expected access patterns
  137. 286. Denormalisation Split feature types into multiple tables based on known or expected access patterns Roads are visualised with different style classes and rendered at different scales
  138. 288. Partitioning Data partitioning complicates things Updates need to be split across all tables Queries need to be directed at the appropriate table(s) Keeping both normalised and denormalised tables creates huge redundancy Partitioning addresses these problems Stores data in denormalised tables Provides a normalised interface to handle queries across the feature type
  139. 289. Fin Workshop Evaluations are Online (url removed) This material is made available under the Creative Commons Attribution-ShareAlike 3.0 licence http://creativecommons.org/licenses/by-sa/3.0/us/

×