0% found this document useful (0 votes)
151 views

Introduction To PostGIS v1.0

This document provides an introduction and overview of PostGIS, including what PostGIS is, why it was created, what capabilities it provides, who uses it, and how to install and load spatial data into PostGIS. PostGIS allows spatial extensions for PostgreSQL, enabling geometry and location data to be stored and analyzed. It provides functions for constructing, accessing, editing, processing, and relating geometric features to other tables. The document outlines how to install PostGIS on Debian, Windows, and from source. It also covers loading data from shapefiles, OGR, and OSM files and provides examples of spatial queries and functions for analyzing geometries.

Uploaded by

Zmicer Kozhukh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

Introduction To PostGIS v1.0

This document provides an introduction and overview of PostGIS, including what PostGIS is, why it was created, what capabilities it provides, who uses it, and how to install and load spatial data into PostGIS. PostGIS allows spatial extensions for PostgreSQL, enabling geometry and location data to be stored and analyzed. It provides functions for constructing, accessing, editing, processing, and relating geometric features to other tables. The document outlines how to install PostGIS on Debian, Windows, and from source. It also covers loading data from shapefiles, OGR, and OSM files and provides examples of spatial queries and functions for analyzing geometries.

Uploaded by

Zmicer Kozhukh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Introduction to PostGIS Spatial Extensions for PostgreSQL

Mark Leslie Geospatial Architect LISAsoft


What is PostGIS? Why is PostGIS? What can PostGIS do? Who is using PostGIS? Why use PostGIS?

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

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

Installation - Requirements
Proj4 - Cartographic Projections Library GEOS - Geometry Engine - Open Source Provides spatial predicate functions, spatial operators and topological functions. PostgreSQL - helpful little application

Installation - Debian
sudo apt-get install postgresql-8.3-postgis

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 ./configure make sudo make install

Installation - Windows
Included in the PostgreSQL installer

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/

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.

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)

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

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

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.109s user 394m41.460s sys 5m8.507s

Loading Data Spatial Data Integrator

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.

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

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)"...

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))"...

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

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

Accessing Data
select the_geom from road where gid = 1; 0102000020E6100000020000000000008029F65B40000000 0000000000000000800BF65B4000000080F9595FBF

Accessing Data
select the_geom from road where gid = 1; 0102000020E6100000020000000000008029F65B4000000000000000000 00000800BF65B4000000080F9595FBF

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

Accessing Data
select the_geom from road where gid = 1; 0102000020E6100000020000000000008029F65B400000000000000000000000800BF65B 4000000080F9595FBF

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.8462829589 84,0 111.844451904297,0.00191354146227241</gml:coordinates></gml:LineString>

Accessing Data
select the_geom from road where gid = 1; 0102000020E6100000020000000000008029F65B400000000000000000000000800BF65B 4000000080F9595FBF

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

Accessing Data
select the_geom from road where gid = 1; 0102000020E6100000020000000000008029F65B400000000000000000000000800BF65B 4000000080F9595FBF

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]]}

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)

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)

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)

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)

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

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

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

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

ST_Disjoint
Geometries do not share any space together.

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

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

ST_Intersects
Geometries share any portion of space.

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

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

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

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';

Real Estate Evaluation


name --------------Stratford Craven Bunyah Chichester Salisbury Coolongolook Wootton

... (36 rows)

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

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);

Verify Assumptions

Table nveg_1788 pveg_1988 change

Count Area 1642 695.234798 1897 695.234808 9263 695.234780

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

Environmental Change
SELECT count(*), change FROM change GROUP BY change; count | change -------+----------4785 | No Change 2013 | Gain 2465 | Loss

Environmental Change
SELECT count(*), change, sum(ST_Area(ST_Intersection)) AS area FROM change GROUP BY change; | sum count | change -------+-----------+-----------------4785 | No Change | 521.507080938973 2013 | Gain | 54.5533372716394 2465 | Loss | 119.174361913388

Environmental Change

ST_Relate
<http://docs.codehaus.org/display/GEOTDOC/Point+Set+ Theory+and+the+DE9IM+Matrix#PointSetTheoryandtheDE-9IMMatrix9IntersectionMatrix>

Advanced Operations
Nearest Neighbor <http://postgis.refractions.net/support/wiki/ index.php?Nearest%20Neighbor%20Search> Relational Constraints <http://www.nabble.com/AnyProjected-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>

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.

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.

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.

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_compar e

Limitations
No true geodetic support

Fewer hosting providers than Oracle/MSSQLServer/mySQL Incomplete curve support Non-production topology support

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]

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

You might also like