Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to PostGIS

Similar presentations


Presentation on theme: "Introduction to PostGIS"— Presentation transcript:

1 Introduction to PostGIS
Presented by: Mark Leslie Mike Pumphrey Paul Ramsey

2 Introduction to PostGIS
Workshop Format Interactive C:\workshops\intro_to_postgis Hands On All examples are executable Copy and Paste from HTML Exercises Answers will be provided (eventually) Try This sections for greater challenge 20/10/2009 Introduction to PostGIS

3 What is a Spatial Database?
Spatial Data Types Point a single coordinate of two to four dimensions 20/10/2009 Introduction to PostGIS

4 What is a Spatial Database?
Spatial Data Types Linestring a set of two or more coordinates linear interpretation of path between coordinates 20/10/2009 Introduction to PostGIS

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 20/10/2009 Introduction to PostGIS

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 20/10/2009 Introduction to PostGIS

7 What is a Spatial Database?
Spatial Data Types Multi-geometries (Multipoint, Multilinestring, Multipolygon) a set of like geometries 20/10/2009 Introduction to PostGIS

8 What is a Spatial Database?
Spatial Data Types Geometrycollection a set of various (unmatched) geometries 20/10/2009 Introduction to PostGIS

9 What is a Spatial Database?
Spatial Data Types Spatial Indexing R-tree Quadtree Grid-based 20/10/2009 Introduction to PostGIS

10 What is a Spatial Database?
Spatial Data Types Spatial Indexing Spatial Functions Construction Serialisation Predicates Analysis Accessors Builders Aggregates 20/10/2009 Introduction to PostGIS

11 Introduction to PostGIS
What is PostGIS? Spatial Extensions for PostgreSQL Provides Spatial Data Type Provides Spatial Indexing Provides Spatial Functions 20/10/2009 Introduction to PostGIS

12 Introduction to PostGIS
What is PostGIS? Spatial Extensions for PostgreSQL PostgreSQL Extensions for Spatial ACID transaction guarantees Enterprise reliability Crash recovery Hot backup Replication SQL support 20/10/2009 Introduction to PostGIS

13 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

14 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

15 Introduction to PostGIS
PostgreSQL Setup Installing PostgreSQL Software provided it C:\workshops\PostGIS\software Double click postgresql windows.exe Creating a Spatial Database Using pgAdmin III Loading Spatial Data The horrors of the command line 20/10/2009 Introduction to PostGIS

16 PostgreSQL Installation
20/10/2009 Introduction to PostGIS

17 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

18 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

19 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

20 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

21 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

22 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

23 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

24 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

25 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

26 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

27 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

28 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

29 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

30 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

31 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

32 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

33 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

34 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

35 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

36 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

37 PostGIS Documentation
20/10/2009 Introduction to PostGIS

38 Creating a Spatial Database
20/10/2009 Introduction to PostGIS

39 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

40 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

41 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

42 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

43 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

44 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

45 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

46 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

47 Introduction to PostGIS
Loading Data 20/10/2009 Introduction to PostGIS

48 Introduction to PostGIS
Loading Data Change to C:\workshops\PostGIS\data Execute set_environment.bat 20/10/2009 Introduction to PostGIS

49 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

50 Load the Parks Shapefile
shp2pgsql -s 2270 medford_parks.shp medford_parks > medford_parks.sql psql -f medford_parks.sql workshop 20/10/2009 Introduction to PostGIS

51 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

52 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

53 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

54 Load the Schools Shapefile
shp2pgsql -s D jacksonco_schools.shp jacksonco_schools > jacksonco_schools.sql psql -f jacksonco_schools.sql workshop 20/10/2009 Introduction to PostGIS

55 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

56 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

57 Introduction to PostGIS
Load Additional Data psql -f medford.sql workshop 20/10/2009 Introduction to PostGIS

58 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

59 Geometries – Creating and Manipulating
Creating simple geometries Consistent geometries through constraints Geometry types and output Accessing geometry components Measurement Functions 20/10/2009 Introduction to PostGIS

60 Introduction to PostGIS
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; 20/10/2009 Introduction to PostGIS

61 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

62 Introduction to PostGIS
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; 20/10/2009 Introduction to PostGIS

63 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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)); 20/10/2009 Introduction to PostGIS

65 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

66 Constrained Geometries
INSERT INTO lines VALUES ('Not a line', 'POINT(0 0)'); INSERT INTO lines VALUES ('4d line', 'LINESTRING( , )'); ('WGS84 line', ST_SetSRID('LINESTRING(-1 1,1 -1)',4326)); 20/10/2009 Introduction to PostGIS

67 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

68 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

69 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

70 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

71 Introduction to PostGIS
Metadata Table SELECT * FROM geometry_columns; 20/10/2009 Introduction to PostGIS

72 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

73 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

74 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

75 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

76 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

77 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

78 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

79 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

80 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

81 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

82 Accessing Geometry Components
Multi-geometry ST_NumGeometries(geometry) ST_GeometryN(geometry, index) 20/10/2009 Introduction to PostGIS

83 Accessing Geometry Components
Multi-geometry Polygon ST_NumInteriorRings(geometry) ST_NRings(geometry) ST_ExteriorRing(geometry) ST_InteriorRingN(geometry, index) 20/10/2009 Introduction to PostGIS

84 Accessing Geometry Components
Multi-geometry Polygon Linestring ST_NumPoints(geometry) ST_NPoints(geometry) ST_PointN(geometry,index) 20/10/2009 Introduction to PostGIS

85 Accessing Geometry Components
Multi-geometry Polygon Linestring Point ST_X(geometry) ST_Y(geometry) ST_Z(geometry) ST_M(geometry) 20/10/2009 Introduction to PostGIS

86 Introduction to PostGIS
Length SELECT ST_Length(ST_GeomFromText( 'LINESTRING(0 0,5 0,5 10)')); 20/10/2009 Introduction to PostGIS

87 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

88 Introduction to PostGIS
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)')); 20/10/2009 Introduction to PostGIS

89 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

90 Introduction to PostGIS
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)')); 20/10/2009 Introduction to PostGIS

91 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

92 Introduction to PostGIS
Length SELECT ST_Length2D_Spheroid(g, 'SPHEROID["GRS 1980", , ]'), ST_Length3D_Spheroid(g, 'SPHEROID["GRS 1980", , ]') FROM ( VALUES ( ST_GeomFromEWKT('LINESTRING( , )') ) ) AS query(g); 20/10/2009 Introduction to PostGIS

93 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

94 Introduction to PostGIS
Length - Perimeter SELECT ST_Perimeter(ST_GeomFromEWKT(g)) FROM ( VALUES ('POLYGON(( ,2 -2 1,2 2 2,-2 2 1, ))'), ('POLYGON((-2 -2,2 -2,2 2,-2 2,-2 -2),(1 1,-1 1,-1 -1,1 -1,1 1))') ) AS query(g); 20/10/2009 Introduction to PostGIS

95 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

96 Introduction to PostGIS
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))' ) ); 20/10/2009 Introduction to PostGIS

97 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

98 Introduction to PostGIS
Area SELECT ST_Area(ST_GeomFromEWKT(g)) FROM ( VALUES ('POLYGON(( ,2 -2 1,2 2 2,-2 2 1, ))'), ('POLYGON((-2 -2,2 -2,2 2,-2 2,-2 -2),(1 1,-1 1,-1 -1,1 -1,1 1))') ) AS query(g); 20/10/2009 Introduction to PostGIS

99 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

100 Introduction to PostGIS
Distance SELECT ST_Distance(ST_GeomFromEWKT('POINT(0 5)'), ST_GeomFromEWKT('LINESTRING(-2 2,2 2)')); 20/10/2009 Introduction to PostGIS

101 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

102 Introduction to PostGIS
Distance SELECT ST_Distance_Sphere(a, b), ST_Distance_Spheroid(a, b, 'SPHEROID["GRS 1980", , ]') FROM ( VALUES ( ST_GeomFromText('POINT( )'), ST_GeomFromText('POINT( )') ) ) AS query (a, b); 20/10/2009 Introduction to PostGIS

103 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

104 Introduction to PostGIS
Within a Distance SELECT namelow FROM jacksonco_streets, medford_parks WHERE ST_Distance(medford_parks.the_geom, jacksonco_streets.the_geom) < 5000 / AND medford_parks.name = 'Hawthorne Park / Pool'; 20/10/2009 Introduction to PostGIS

105 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

106 Introduction to PostGIS
Within a Distance SELECT namelow FROM jacksonco_streets, medford_parks WHERE ST_DWithin(medford_parks.the_geom, jacksonco_streets.the_geom, 5000 / ) AND medford_parks.name = 'Hawthorne Park / Pool'; 20/10/2009 Introduction to PostGIS

107 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

108 Introduction to PostGIS
Spatial Indexing CREATE INDEX jacksonco_streets_gix ON jacksonco_streets USING GIST (the_geom); 20/10/2009 Introduction to PostGIS

109 Introduction to PostGIS
Spatial Indexing CREATE INDEX jacksonco_streets_gix ON jacksonco_streets USING GIST (the_geom); 20/10/2009 Introduction to PostGIS

110 Introduction to PostGIS
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'; 20/10/2009 Introduction to PostGIS

111 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

112 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

113 Introduction to PostGIS
Spatial Indexing SELECT namelow FROM jacksonco_streets, medford_parks WHERE ST_DWithin(medford_parks.the_geom, jacksonco_streets.the_geom, 5000 / ) AND medford_parks.name = 'Hawthorne Park / Pool'; 20/10/2009 Introduction to PostGIS

114 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

115 Introduction to PostGIS
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); 20/10/2009 Introduction to PostGIS

116 Introduction to PostGIS
Vacuum Vacuum Recover or reuse disk space from obsolete rows Analyze Update query planner statistics Cluster Rewrite tables based on index ordering 20/10/2009 Introduction to PostGIS

117 Introduction to PostGIS
Spatial Joins – Within SELECT name FROM jacksonco_schools, medford_citylimits WHERE ST_Within(jacksonco_schools.the_geom, medford_citylimits.the_geom); 20/10/2009 Introduction to PostGIS

118 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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_citylimit s.the_geom); 20/10/2009 Introduction to PostGIS

120 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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); 20/10/2009 Introduction to PostGIS

122 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

123 Introduction to PostGIS
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) 20/10/2009 Introduction to PostGIS

124 Introduction to PostGIS
Projecting Data SELECT SUM(ST_Length(the_geom)) FROM jacksonco_streets WHERE namelow = 'E Main St'; 20/10/2009 Introduction to PostGIS

125 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

126 Introduction to PostGIS
Projecting Data SELECT SUM(ST_Length(ST_Transform(the_geom, 2839))) FROM jacksonco_streets WHERE namelow = 'E Main St'; 20/10/2009 Introduction to PostGIS

127 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

128 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

129 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

130 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

131 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

132 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

133 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

134 Introduction to PostGIS
Exercises Twenty minutes to try the exercises 20/10/2009 Introduction to PostGIS

135 Introduction to PostGIS
Exercises How big is the largest building in Medford in square feet? In square metres? 20/10/2009 Introduction to PostGIS

136 Introduction to PostGIS
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; 20/10/2009 Introduction to PostGIS

137 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

138 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

139 Introduction to PostGIS
Exercises What is the elevation of the 'South Medford' high school building? 20/10/2009 Introduction to PostGIS

140 Introduction to PostGIS
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'; 20/10/2009 Introduction to PostGIS

141 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

142 Introduction to PostGIS
Exercises What are the expected percentages of children in poverty at each school with a Kindergarten class? 20/10/2009 Introduction to PostGIS

143 Introduction to PostGIS
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'; 20/10/2009 Introduction to PostGIS

144 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

145 Introduction to PostGIS
Exercises What is the length of 'E Main St'? 20/10/2009 Introduction to PostGIS

146 Introduction to PostGIS
Exercises What is the length of 'E Main St'? SELECT Sum(ST_Length(the_geom)) FROM jacksonco_streets WHERE legalname ~* 'E Main St'; 20/10/2009 Introduction to PostGIS

147 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

148 Introduction to PostGIS
Exercises How much park area is there within the Medford city limits? 20/10/2009 Introduction to PostGIS

149 Introduction to PostGIS
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); 20/10/2009 Introduction to PostGIS

150 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

151 Introduction to PostGIS
Exercises How many buildings are located within wetlands? 20/10/2009 Introduction to PostGIS

152 Introduction to PostGIS
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); 20/10/2009 Introduction to PostGIS

153 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

154 Introduction to PostGIS
Exercises Which school is farthest from a park? Which is closest? 20/10/2009 Introduction to PostGIS

155 Introduction to PostGIS
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; 20/10/2009 Introduction to PostGIS

156 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

157 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

158 Introduction to PostGIS
Exercises Which schools have the most park area within 400 feet? Within 1 km? 20/10/2009 Introduction to PostGIS

159 Introduction to PostGIS
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? 20/10/2009 Introduction to PostGIS

160 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

161 Introduction to PostGIS
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; 20/10/2009 Introduction to PostGIS

162 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

163 Introduction to PostGIS
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 / ) GROUP BY jacksonco_schools.name ORDER BY area DESC; 20/10/2009 Introduction to PostGIS

164 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

165 Introduction to PostGIS
Exercises What are the expected percentages of unmarried families for each school? 20/10/2009 Introduction to PostGIS

166 Introduction to PostGIS
Exercises What are the expected percentages of unmarried families for each school? SELECT s.name, * t.hh_unmarried / t.hh_total FROM jacksonco_schools s, unmarriedbytract t WHERE ST_Contains(t.the_geom, s.the_geom); 20/10/2009 Introduction to PostGIS

167 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

168 Introduction to PostGIS
Exercises How many storm drains are within 500 feet of 'Bear Creek'? 20/10/2009 Introduction to PostGIS

169 Introduction to PostGIS
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'; 20/10/2009 Introduction to PostGIS

170 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

171 Tuning PostgreSQL for Spatial
C:\Program Files\PostgreSQL\8.4\data\postgresql.conf pgAdmin provides a Configuration Editor File ->Open postgresql.conf 20/10/2009 Introduction to PostGIS

172 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

173 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

174 Introduction to PostGIS
shared_buffers Determines the amount of memory that is shared by back-end processes Default Value = 32MB Recommended Value = 500MB 20/10/2009 Introduction to PostGIS

175 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

176 Introduction to PostGIS
work_mem Defines the amount of memory that a single process can use for sorting or hash operations Default Value = 1MB Recommended Value = 16MB 20/10/2009 Introduction to PostGIS

177 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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'; 20/10/2009 Introduction to PostGIS

179 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

180 Introduction to PostGIS
wal_buffers Amount of memory used by the write-ahead log (WAL). Default Value = 64kB Recommended Value = 1MB 20/10/2009 Introduction to PostGIS

181 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

182 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

183 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

184 Introduction to PostGIS
random_page_cost Represents the cost of random page access from disk. Default Value = 4.0 Recommended Value = 2.0 20/10/2009 Introduction to PostGIS

185 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

186 Introduction to PostGIS
seq_page_cost Represents the cost of a sequential page access from disk. Default Value = 1.0 Recommended Value = 1.0 20/10/2009 Introduction to PostGIS

187 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

188 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

189 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

190 Introduction to PostGIS
Test Query SELECT namelow FROM jacksonco_streets, medford_citylimits WHERE ST_Intersects(jacksonco_streets.the_geom, medford_citylimits.the_geom) GROUP BY namelow; 20/10/2009 Introduction to PostGIS

191 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

192 Introduction to PostGIS
Sequence Scan Linear scan of every row in the table (medford_citylimits) Can evaluate filter conditions on scan 20/10/2009 Introduction to PostGIS

193 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

194 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

195 Introduction to PostGIS
Hash Aggregate Performs the grouping based on an attribute Only available for attributes with a hashing algorithm Executes after the nested loop completes 20/10/2009 Introduction to PostGIS

196 Introduction to PostGIS
Visualisation uDig is used for visualisation It is available in c:\workshops\PostGIS\software\udig Double-click on udig.bat 20/10/2009 Introduction to PostGIS

197 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

198 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

199 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

200 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

201 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

202 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

203 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

204 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

205 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

206 Introduction to PostGIS
Add Layers medford_hydro medford_parks medford_citylimits agebysexbytract 20/10/2009 Introduction to PostGIS

207 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

208 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

209 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

210 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

211 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

212 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

213 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

214 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

215 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

216 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

217 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

218 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

219 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

220 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

221 Introduction to PostGIS
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; 20/10/2009 Introduction to PostGIS

222 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

223 Introduction to PostGIS
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; 20/10/2009 Introduction to PostGIS

224 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

225 Introduction to PostGIS
Equality PostGIS provides three different levels of equality Exactly Equal Spatially Equal BBox Equal 20/10/2009 Introduction to PostGIS

226 Introduction to PostGIS
Equality CREATE TABLE polygons (name varchar, poly geometry); INSERT INTO polygons VALUES ('Polygon 1', 'POLYGON(( , ,2 0, , ,-2 0, ))'), ('Polygon 2', 'POLYGON(( ,-2 0, , , 2 0, , ))'), ('Polygon 3', 'POLYGON(( ,2 0, , , -2 0, , ))'), ('Polygon 4', 'POLYGON(( , , , , 2 0, , , , , , -2 0, , ))'), ('Polygon 5', 'POLYGON(( , , , , ))'); 20/10/2009 Introduction to PostGIS

227 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

228 Introduction to PostGIS
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; 20/10/2009 Introduction to PostGIS

229 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

230 Introduction to PostGIS
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; 20/10/2009 Introduction to PostGIS

231 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

232 Introduction to PostGIS
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; 20/10/2009 Introduction to PostGIS

233 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

234 Introduction to PostGIS
Advanced Material Advanced Functions Aggregates / Deaggregates Processing Set Operations Performance Tools Manipulating the Query Planner Denormalization Data Partitioning 20/10/2009 Introduction to PostGIS

235 Introduction to PostGIS
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, , ,-11 38, )))')), (ST_GeomFromText('MULTIPOLYGON(((-49 63,-32 24, , ,-72 -9,-74 31,-49 63)))'))) AS query(the_geom); SELECT populate_geometry_columns(); 20/10/2009 Introduction to PostGIS

236 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

237 Introduction to PostGIS
ST_Union(geometry) Merges geometries into a single (often multi-) geometry Support aggregate form as well as: ST_Union(geomA, geomB) ST_Union(geomArray[ ]) 20/10/2009 Introduction to PostGIS

238 Introduction to PostGIS
ST_Union(geometry) SELECT ST_AsText(ST_Union(st_geomfromtext)) FROM (SELECT ST_GeomFromText( 'MULTIPOLYGON(((-77 56,-52 18, , ,-11 38, )))') UNION ALL SELECT ST_GeomFromText( 'MULTIPOLYGON(((-49 63,-32 24,-39 -7, ,-72 -9, ,-49 63)))') ) as a; 20/10/2009 Introduction to PostGIS

239 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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[ ]) 20/10/2009 Introduction to PostGIS

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 SELECT 'LINESTRING(0 0,1 0)'::geometry the_geom SELECT 'LINESTRING(1 1, 0 1)'::geometry the_geom) as a; 20/10/2009 Introduction to PostGIS

242 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

243 ST_Polygonize(geometry)
Generates a geometry containing all polygons that can be built from the input linework 20/10/2009 Introduction to PostGIS

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 SELECT 'LINESTRING(0 0,1 0)'::geometry the_geom SELECT 'LINESTRING(1 1, 0 1)'::geometry) as a; 20/10/2009 Introduction to PostGIS

245 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

246 Introduction to PostGIS
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] 20/10/2009 Introduction to PostGIS

247 Introduction to PostGIS
ST_Dump(geometry) SELECT ST_AsText((ST_Dump(the_geom)).geom) FROM jacksonco_taxlots WHERE gid = 90917; 20/10/2009 Introduction to PostGIS

248 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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] 20/10/2009 Introduction to PostGIS

250 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

251 Introduction to PostGIS
Set Operations Produce results based on inclusion or exclusion of points from a geometry Point includes only the point itself 20/10/2009 Introduction to PostGIS

252 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

253 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

254 Introduction to PostGIS
Set Operations MULTIPOLYGON(((-77 56,-52 18, , ,-11 38, ))) MULTIPOLYGON(((-49 63,-32 24,-39 -7, ,-72 -9,-74 31, ))) 20/10/2009 Introduction to PostGIS

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, , ,-11 38,-77 56)))'), 'MULTIPOLYGON(((-49 63,-32 24,-39 -7, ,-72 -9,-74 31, )))'))); 20/10/2009 Introduction to PostGIS

256 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

257 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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, , ,-11 38,-77 56)))'), 'MULTIPOLYGON(((-49 63,-32 24,-39 -7, ,-72 -9,-74 31, )))'))); 20/10/2009 Introduction to PostGIS

259 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

260 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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, , ,-11 38,-77 56)))'), 'MULTIPOLYGON(((-49 63,-32 24,-39 -7, ,-72 -9,-74 31, )))'))); 20/10/2009 Introduction to PostGIS

262 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

263 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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, , ,-11 38,-77 56)))'), 'MULTIPOLYGON(((-49 63,-32 24,-39 -7, ,-72 -9,-74 31, )))'))); 20/10/2009 Introduction to PostGIS

265 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

266 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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)); 20/10/2009 Introduction to PostGIS

268 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

269 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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)')); 20/10/2009 Introduction to PostGIS

271 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

272 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

273 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

274 Introduction to PostGIS
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) 20/10/2009 Introduction to PostGIS

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 20/10/2009 Introduction to PostGIS

276 ST_Simplify(geom, tolerance)
The line is simplified by producing a candidate line connecting the end points 20/10/2009 Introduction to PostGIS

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 20/10/2009 Introduction to PostGIS

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 20/10/2009 Introduction to PostGIS

279 ST_Simplify(geom, tolerance)
The final result is a geometry made up of all accepted candidate lines 20/10/2009 Introduction to PostGIS

280 ST_Simplify(geom, tolerance)
SELECT ST_AsText(geom) AS original, ST_AsText(ST_Simplify(geom, 3)) AS "3", ST_AsText(ST_Simplify(geom, 2.9)) AS "2.9" FROM ( SELECT ST_GeomFromText('LINESTRING(0 0,3 2.5,0 5)') AS geom) AS a; 20/10/2009 Introduction to PostGIS

281 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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; 20/10/2009 Introduction to PostGIS

283 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

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 20/10/2009 Introduction to PostGIS

285 Introduction to PostGIS
Denormalisation Split feature types into multiple tables based on known or expected access patterns 20/10/2009 Introduction to PostGIS

286 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

287 Introduction to PostGIS
20/10/2009 Introduction to PostGIS

288 Introduction to PostGIS
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 20/10/2009 Introduction to PostGIS

289 Introduction to PostGIS
Fin Workshop Evaluations are Online (url removed) This material is made available under the Creative Commons Attribution-ShareAlike 3.0 licence 20/10/2009 Introduction to PostGIS


Download ppt "Introduction to PostGIS"

Similar presentations


Ads by Google