Top Banner
Introduction to PostGIS Spatial Extensions for PostgreSQL Mark Leslie Geospatial Architect LISAsoft
59

Introduction to PostGIS v1.0

Apr 21, 2015

Download

Documents

Zmicer Kozhukh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to PostGIS v1.0

Introduction to PostGISSpatial Extensions for PostgreSQL

Mark LeslieGeospatial Architect

LISAsoft

Page 2: Introduction to PostGIS v1.0

 

•What is PostGIS?

•Why is PostGIS?

•What can PostGIS do?

•Who is using PostGIS?

•Why use PostGIS?

Page 3: Introduction to PostGIS v1.0

What is PostGIS?

•Spatial Extensions for PostgreSQL

•Geometry represented by linear shapes

•Spatial Functions (Constructors, Accessors, Editors, Processors, Relational Operators, Processing Functions)

•Advanced geometric indexing

Page 4: Introduction to PostGIS v1.0

Why is PostGIS

•Shapefile are limited in their transactional capacity

•Data management of files problematic

•Existing GIS data services (as of 2001) were terribly expensive

•No existing integrated spatial databases

•Refractions had some time to kill

Page 5: Introduction to PostGIS v1.0

Installation - Requirements

•Proj4 - Cartographic Projections Library

•GEOS - Geometry Engine - Open Source

•             Provides spatial predicate functions, spatial operators and topological functions.

•PostgreSQL - helpful little application

Page 6: Introduction to PostGIS v1.0

Installation - Debian

•sudo apt-get install postgresql-8.3-postgis••

Page 7: Introduction to PostGIS v1.0

Installation - Debian

sudo apt-get install postgresql-8.3-postgis

or from source

svn co http://svn.refractions.net/postgis/trunk/ postgis/cd postgis./autogen.sh./configuremakesudo make install

Page 8: Introduction to PostGIS v1.0

Installation - Windows

•Included in the PostgreSQL installer•

Page 9: Introduction to PostGIS v1.0

Installation - Windows

•Included in the PostgreSQL installer•

•or need a more recent version?•

•Use the Stack Builder application for version PostgreSQL 8.3•

•Download the installer for PostgreSQL 8.3 from •http://postgis.refractions.net/download/windows/

Page 10: Introduction to PostGIS v1.0

Installation - Windows

•Included in the PostgreSQL installer•

•or need a more recent version?•

•Use the Stack Builder application for version PostgreSQL 8.3•

•Download the installer for PostgreSQL 8.3 from •http://postgis.refractions.net/download/windows/•

•or from source?•

•Good luck.

Page 11: Introduction to PostGIS v1.0

Loading Data - shp2pgsql

•shp2pgsql -s 4326 -D -I -S roads.shp roads | psql demo•

•-s 4326 – Use WGA83 coordinate reference system•-D – Use PostgreSQL dump format (faster than inserts)•-I – Create spatial (GIST) index after loading•-S – Generate simple geometries (eg. Line instead of Multiline)•

Page 12: Introduction to PostGIS v1.0

Loading Data - shp2pgsql

• shp2pgsql -s 4326 -D -I -S roads.shp roads | psql demo•

•On Windows use:•

•shp2pgsql -s 4326 -D -I -S roads.shp roads > roads.sql•psql -f roads.sql demo•

•or setup a .pgpass file•

Page 13: Introduction to PostGIS v1.0

Loading Data - ogr2ogr

•ogr2ogr -f "PostgreSQL" PG:dbname=demo cities/••Format options include:

• ESRI Shapefile MapInfo File TIGER• S57 DGN Memory BNA CSV MySQL• GML GPX KML GeoJSON Interlis 1• Interlis 2 GMT SQLite ODBC PostgreSQL

••

Page 14: Introduction to PostGIS v1.0

Loading Data - osm2pgsql

•osm2pgsql --host seymour.ms.a2end.com --database osm --verbose planet-latest.osm.bz2• •Then get some coffee, in Perth. •Node stats: total(231808103), max(256523410)Way stats: total(18781596), max(23689770)Relation stats: total(6967), max(9763)

real    806m40.109suser    394m41.460ssys    5m8.507s

Page 15: Introduction to PostGIS v1.0

Loading Data – Spatial Data Integrator

Page 16: Introduction to PostGIS v1.0

Basic Schema Objects

•geometry_columns•    Allows standard discovery of geographic objects and their parameters: point dimension, type, projection. ••spatial_reference_sys•    Provides definitions of available projections.

Page 17: Introduction to PostGIS v1.0

Inserting Data

•INSERT INTO fishing_hole (name, the_geom)•VALUES ("Greenwich Ferry Wharf", •ST_SetSRID(ST_GeometryAsText(•"POINT(151.18323 -33.84162)"), 4283);

Page 18: Introduction to PostGIS v1.0

Inserting Data

•INSERT INTO fishing_hole (name, the_geom) VALUES ("Greenwich Ferry Wharf", ST_SetSRID(ST_GeometryAsText("POINT(151.18323 -33.84162)"), 4283);•

•..."LINESTRING(151.18456 -33.84225,151.18424 -33.84196,151.18342 -33.84122)"...

Page 19: Introduction to PostGIS v1.0

Inserting Data

•INSERT INTO fishing_hole (name, the_geom) VALUES ("Greenwich Ferry Wharf", ST_SetSRID(ST_GeometryAsText("POINT(151.18323 -33.84162)"), 4283);•

•..."LINESTRING(151.18456 -33.84225,151.18424 -33.84196,151.18342 -33.84122)"...•

•..."POLYGON((151.2111 -33.8754,151.2109 -33.8753,151.211 -33.8749,151.2111 -33.8749,151.2111 -33.8753)(151.211 -33.8750,151.2111 -33.8751,151.211 -33.8751,151.211 -33.875,151.211 -33.8750))"...

Page 20: Introduction to PostGIS v1.0

Inserting Data

•INSERT INTO fishing_hole (name, the_geom) VALUES ("Greenwich Ferry Wharf", ST_SetSRID(ST_GeometryAsText("POINT(151.18323 -33.84162)"), 4283);••..."LINESTRING(151.18456 -33.84225,151.18424 -33.84196,151.18342 -33.84122)"...•

•..."POLYGON((151.2111 -33.8754,151.2109 -33.8753,151.211 -33.8749,151.2111 -33.8749,151.2111 -33.8753)(151.211 -33.8750,151.2111 -33.8751,151.211 -33.8751,151.211 -33.875,151.211 -33.8750))"...•

•Plus MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, COLLECTION

Page 21: Introduction to PostGIS v1.0

Inserting Data

•INSERT INTO fishing_hole (name, the_geom) VALUES ("Greenwich Ferry Wharf", ST_SetSRID(ST_GeometryAsText("POINT(151.18323 -33.84162)"), 4283);••..."LINESTRING(151.18456 -33.84225,151.18424 -33.84196,151.18342 -33.84122)"...••..."POLYGON((151.2111 -33.8754,151.2109 -33.8753,151.211 -33.8749,151.2111 -33.8749,151.2111 -33.8753)(151.211 -33.8750,151.2111 -33.8751,151.211 -33.8751,151.211 -33.875,151.211 -33.8750))"...•

•Plus MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, COLLECTION•

•And even CIRCULARSTRING, COMPOUNDCURVE, CURVEPOLYGON, MULTICURVE and MULTISURFACE

Page 22: Introduction to PostGIS v1.0

Accessing Data

•select the_geom from road where gid = 1;0102000020E6100000020000000000008029F65B400000000000000000000000800BF65B4000000080F9595FBF

Page 23: Introduction to PostGIS v1.0

Accessing Data

•select the_geom from road where gid = 1;0102000020E6100000020000000000008029F65B400000000000000000000000800BF65B4000000080F9595FBF•

•select ST_asText(the_geom) from road where gid = 1;LINESTRING(111.846282958984 0,111.844451904297 -0.00191354146227241)

Page 24: Introduction to PostGIS v1.0

Accessing Data

•select the_geom from road where gid = 1;0102000020E6100000020000000000008029F65B400000000000000000000000800BF65B4000000080F9595FBF•

•select ST_asText(the_geom) from road where gid = 1;LINESTRING(111.846282958984 0,111.844451904297 -0.00191354146227241)•

•select ST_asGML(the_geom) from road where gid = 1;<gml:LineString srsName="EPSG:4326"><gml:coordinates>111.846282958984,0 111.844451904297,-0.00191354146227241</gml:coordinates></gml:LineString>

Page 25: Introduction to PostGIS v1.0

Accessing Data

•select the_geom from road where gid = 1;0102000020E6100000020000000000008029F65B400000000000000000000000800BF65B4000000080F9595FBF••select ST_asText(the_geom) from road where gid = 1;LINESTRING(111.846282958984 0,111.844451904297 -0.00191354146227241)•

•select ST_asGML(the_geom) from road where gid = 1;<gml:LineString srsName="EPSG:4326"><gml:coordinates>111.846282958984,0 111.844451904297,-0.00191354146227241</gml:coordinates></gml:LineString>•

select ST_asSVG(the_geom) from road where gid = 1;M 111.84628295898437 0 111.84445190429687 0.001913541462272

Page 26: Introduction to PostGIS v1.0

Accessing Data

•select the_geom from road where gid = 1;0102000020E6100000020000000000008029F65B400000000000000000000000800BF65B4000000080F9595FBF••select ST_asText(the_geom) from road where gid = 1;LINESTRING(111.846282958984 0,111.844451904297 -0.00191354146227241)••select ST_asGML(the_geom) from road where gid = 1;<gml:LineString srsName="EPSG:4326"><gml:coordinates>111.846282958984,0 111.844451904297,-0.00191354146227241</gml:coordinates></gml:LineString>•select ST_asSVG(the_geom) from road where gid = 1;M 111.84628295898437 0 111.84445190429687 0.001913541462272•

•select ST_asGeoJSON(the_geom) from road where gid = 1;{"type":"LineString","coordinates":[[111.846282958984375,0.000000000000000],[111.844451904296875,-0.001913541462272]]}

Page 27: Introduction to PostGIS v1.0

Basic Operations - Constructors

•ST_Polygonize(linestring set), ST_BuildArea(linestring)•ST_MakePoint(x, y, [z], [m]), ST_MakePointM(x, y, m)•ST_MakeLine(point, point), ST_MakeLine(point set)•ST_LineFromMultiPoint(multipoint)•ST_MakePolygon(linestring, linestring[])•ST_Collect(geometry, geometry), ST_Collect(geometry set)•ST_Dump(geometry)

Page 28: Introduction to PostGIS v1.0

Basic Operations - Transformation

•ST_Transform(geometry, integer)•ST_Affine(geometry, float8, float8, float8, float8, float8,

float8, [float8, float8, float8, float8, float8, float8]) •ST_Translate(geometry, x, y, z) •ST_Scale(geometry, xScale, yScale, zScale)•ST_RotateX(geometry, radians) •ST_RotateY(geometry, radians) •ST_RotateZ(geometry, radians)

Page 29: Introduction to PostGIS v1.0

Basic Operations - Validation

•ST_isValid(geometry) •ST_Reverse(geometry)•ST_ForceRHR(geometry)•ST_SnapToGrid(geometry, originX, originY, sizeX, sizeY)•ST_SnapToGrid(geometry, sizeX, sizeY)•ST_SnapToGrid(geometry, size)•ST_SnapToGrid(geometry, geometry, sizeX, sizeY, sizeZ, sizeM)

Page 30: Introduction to PostGIS v1.0

Basic Operations - Accessing and Editing

•ST_GeometryN(multigeom)•ST_ExteriorRing(polygon), ST_InteriorRingN(polygon)•ST_PointN(linestring), ST_StartPoint(linestring), ST_EndPoint(linestring)•ST_X(point), ST_Y(point), ST_Z(point), ST_M(point)•ST_AddPoint(linestring), ST_RemovePoint(linestring), ST_SetPoint(linestring)

Page 31: Introduction to PostGIS v1.0

Relational Operators

• Numerical relational operators work on 1d values with no width.

• <, <=, ==, >=, >, !=

Page 32: Introduction to PostGIS v1.0

Relational Operators

• Numerical relational operators work on 1d values with no width.• <, <=, ==, >=, >, !=•

• Geometric relational operators work on 2d-4d values with or without width, height or depth

Page 33: Introduction to PostGIS v1.0

ST_Within, ST_Contains

• Geometry A is completely within Geometry B• ST_Within(A, B) = ST_Contains(B, A)

Page 34: Introduction to PostGIS v1.0

ST_Overlaps

•Geometries share space, are of the same dimension, but are not completely contained by each other.• • 

Page 35: Introduction to PostGIS v1.0

ST_Disjoint

• Geometries do not share any space together.

Page 36: Introduction to PostGIS v1.0

ST_Touches

•Geometries have at least one point in common, but their interiors do not intersect.

Page 37: Introduction to PostGIS v1.0

ST_Crosses

•Geometries have some, but not all, interior points in common.

Page 38: Introduction to PostGIS v1.0

ST_Intersects

•Geometries share any portion of space.

Page 39: Introduction to PostGIS v1.0

ST_Covers, ST_CoveredBy

• No point in Geometry B is outside Geometry A• ST_Covers(A, B) = ST_CoveredBy(B, A)

Page 40: Introduction to PostGIS v1.0

Basic Operations - Relational Operators

•ST_DWithin(GeomA, GeomB, N) – GeomB is within N of GeomA•ST_Distance – Minimum distance beteen geometries•ST_Distance_Spheroid – Minimum distance between geometries over a specific spheroid•ST_DistanceSphere – Minimum distance between geometries over a perfect sphere

Page 41: Introduction to PostGIS v1.0

Real Estate Evaluation

• cities:

• wkb_geometry – Geometry column (Point)

• name – City name•

• boundaries:

• the_geom – Geometry column (Polygon)

• state – State containing boundary•

• earthquake_risk:

• the_geom – Geometry column (Polygon)

• riskval – Indicator of earthquake risk

Page 42: Introduction to PostGIS v1.0

Real Estate Evaluation

• SELECT c.name

• FROM cities c, earthquake_risk er, boundaries b

• WHERE er.riskval = '> 0.10'

• AND ST_Within(c.wkb_geometry, er.the_geom)

• AND c.name IS NOT NULL

• AND ST_Within(c.wkb_geometry, b.the_geom)

• AND b.state = 'NEW SOUTH WALES';

Page 43: Introduction to PostGIS v1.0

Real Estate Evaluation

• name

• ---------------

• Stratford

• Craven

• Bunyah

• Chichester

• Salisbury

• Coolongolook

• Wootton

• ...

• (36 rows)

Page 44: Introduction to PostGIS v1.0

Environmental Change

• Data describes the type and coverage of vegetation in Australia in 1788 and 1988.

•• nveg_1788, pveg_1988

• the_geom – Geometry (Polygon)

• tn_d – Percentage of ground shaded by vegetation

Page 45: Introduction to PostGIS v1.0

Environmental Change

• SELECT n.ts_d as n_ts_d, p.ts_d as p_ts_d,

• CASE WHEN n.ts_d > p.ts_d THEN 'Loss'

• WHEN n.ts_d < p.ts_d THEN 'Gain'

• ELSE 'No Change' END as change,

• ST_Intersection(n.the_geom, p.the_geom) as the_geom

• FROM nveg_1788 as n, pveg_1988 as p

• WHERE ST_Intersects(n.the_geom, p.the_geom);

Page 46: Introduction to PostGIS v1.0

Verify Assumptions

Table Count Area1642 695.2347981897 695.234808

change 9263 695.234780

nveg_1788pveg_1988

Page 47: Introduction to PostGIS v1.0

Better Verification

• SELECT ST_Area(ST_SymDifference(a.the_geom, b.the_geom)) FROM

• (SELECT ST_Union(the_geom) AS the_geom FROM nveg_1788) a,

• (SELECT ST_Union(the_Geom) AS the_geom FROM pveg_1988) b;

•• st_area

• 4.58800965870143e-05

Page 48: Introduction to PostGIS v1.0

Environmental Change

• SELECT count(*), change

• FROM change GROUP BY change;

•• count | change

• -------+-----------

• 4785 | No Change

• 2013 | Gain

• 2465 | Loss

Page 49: Introduction to PostGIS v1.0

Environmental Change

• SELECT count(*), change,

• sum(ST_Area(ST_Intersection)) AS area

• FROM change GROUP BY change;

•• count | change | sum

• -------+-----------+------------------

• 4785 | No Change | 521.507080938973

• 2013 | Gain | 54.5533372716394

• 2465 | Loss | 119.174361913388

Page 50: Introduction to PostGIS v1.0

Environmental Change

Page 51: Introduction to PostGIS v1.0

ST_Relate

•<http://docs.codehaus.org/display/GEOTDOC/Point+Set+Theory+and+the+DE-9IM+Matrix#PointSetTheoryandtheDE-9IMMatrix-9IntersectionMatrix>

Page 52: Introduction to PostGIS v1.0

Advanced Operations

•Nearest Neighbor <http://postgis.refractions.net/support/wiki/index.php?Nearest%20Neighbor%20Search>•Relational Constraints <http://www.nabble.com/Any-Projected-Straights-Function--td19439097.html#a19448155>•Tiger Geocoder <http://postgis.refractions.net/support/wiki/index.php?Tiger%20Geocoder>•Topology <http://postgis.refractions.net/support/wiki/index.php?PostgisTopology>

Page 53: Introduction to PostGIS v1.0

Who is Using PostGIS

•Infoterra, United Kingdom•

•Infoterra, a leading European satellite and aerial imagery provider, runs their data provision and sales systems on PostGIS, and stores the complete Ordnance Survey database on PostGIS.

Page 54: Introduction to PostGIS v1.0

Who is Using PostGIS

•Infoterra, United Kingdom•

•EU Joint Research Centre•

•With billion dollar satellites and PostGIS, the EU Joint Research Centre monitors fishing vessels in the North Atlantic and provides real-time reporting and mapping to decision makers and regulators.

Page 55: Introduction to PostGIS v1.0

Who is Using PostGIS

•Infoterra, United Kingdom•

•EU Joint Research Centre•

•MADEIRA GPS•

•MADEIRA GPS, a company that started in 2005, with a simple project in mind: Deliver PDA-based navigation, with auto-routing, for Madeira Island's tourism.

Page 56: Introduction to PostGIS v1.0

Why Use PostGIS

•Huge client/server/library support•

•Advanced indexing•

•Transformation support•

•Variety of output format options ((E)WKB, (E)WKT, GeoJSON, GML, KML, SVG)•

•Wide variety of processing and relational operators•Accessors and editors•Measurement and Linear Referencing•Spatial Aggregates• http://www.bostongis.com/PrinterFriendly.aspx?

content_name=sqlserver2008_postgis_mysql_compare

Page 57: Introduction to PostGIS v1.0

Limitations

•No true geodetic support•

•Fewer hosting providers than Oracle/MSSQLServer/mySQL•

•Incomplete curve support•

•Non-production topology support

Page 58: Introduction to PostGIS v1.0

Resources

•Support WIKI •http://postgis.refractions.net/support/wiki/•User List•http://postgis.refractions.net/mailman/listinfo/postgis-users••IRC•irc://chat.freenode.net/postgis••Me•[email protected]

Page 59: Introduction to PostGIS v1.0

• Presentations• Workshops and tutorials• Climate Change Integration Plugfest• Installfest• Bird of a Feather sessions• Code Sprint• Good times