32
OpenSource GIS 2004 Ottawa, Canada www.refractions.net Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions Research

OpenSource GIS 2004 Ottawa, Canada Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

Page 1: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Introduction to PostGIS

PostGIS Basics for the New User

Paul Ramsey & Chris HodgsonRefractions Research

Page 2: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

PostgreSQL

• 1986 : Postgres project at Berkley– Successor to Ingres– Relational Model– Complex Objects– Extensibility

• 1995 : Postgres95 adds SQL Support

• 1996 : Open Source Community

Page 3: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

PostGIS

• PostgreSQL 7.1 (Tuple Toaster)• Real GIS Support• First Release in 2001• OpenGIS “Simple Features for

SQL”• Current Release 0.8.2

Page 4: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Why PostGIS?

• Because Databases are Better than Files!

• Unified Storage, Management, Access– SQL Everywhere

• Transactional Integrity– Multiple Users, Multiple Edits

Page 5: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

PostGIS Users

• Data Handlers• Unified Access Language (SQL)• Unified Metadata Location

– GlobeXplorer– i-cubed– Refractions

Page 6: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

PostGIS Users

• Real Time Systems Developers• Unified Access Language (SQL)• Standard Access Protocols

– JDBC– ODBC

Page 7: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Team CIMAR : DARPA Grand Challenge

Page 8: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

PostGIS Users

• Spatial Infrastructures

LANW

MS

/ WFS

PostGIS

Internet

JUMP

QGISWFS

Client

WMSClient

Page 9: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

PostgreSQL

• Version 7.5 Native Windows Support

Page 10: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Windows PostgreSQL

\bin Executables \include Include files for compilation\lib DLL shared library files\share Extensionsenv.bat Command promptinitdb.bat Initialize \data areapgstart.bat Start the database server

Page 11: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Create a Database

• createdb postgis– Make the database

• psql postgis– Connect to the database

• create, insert, select– Try the database

Page 12: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Load PostGIS

• PostgreSQL Extension– libpostgis.so– postgis.sql

• Requires PL/PgSQL• createlang plpgsql postgis• psql -f postgis.sql postgis• psql -f spatial_ref_sys.sql postgis

Page 13: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Simple Spatial SQL

POINT(5 5)

POINT(5 0)

POINT(0 5)

POINT(0 0)

Page 14: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Load Shape Files

• select count(*) from bc_roads• select count(*) from

bc_voting_areas

Page 15: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

$

$$

$

$$$

$

$

$

$ $

$

$$

$

$

$

$

$

$

$$$

$

$

$

$ $ $$$

$

$

$

$

$$$$$

$

$

$ $$

$

$

$

$$$

$

$

$$$$$$$

$

$

$

$$ $$$$ $$

$

$$$

$

$$

$

$$

$

$

$

$

$

$

$

$

$

$$

$

$$

$

$$

$$$

$

$$$$$

$

$$

$

$

$

$ $

$

$$$

$

$

$$$

$$

$

$$ $$

$

$ $

$

$

$$

$ $$

$

$

$

$

$ $$

$

$

$$

$

$$

$$$$$$$$ $

$

$ $

$

$ $$

$

$

$

$

$$$

$$

$$

$

$

$

$

$

$

$

$

$

$

$

$

$

$

$

$

$

$

$

$ $$ $$$

$

$$$

$

$

$

$

$

$

$

$

$

$ $

$

$

$$

$

$

$

$

$

$

$

$

$

$$

$

$$$$ $

$

$$

$

$$

$$ $

$

$

$

$ $

$

$$ $

$

$

$ $

$

$

$

$

$

$ $

$

$ $

$

$ $$

$

$$

$

$

$

$ $$$

$

$ $$$

$

$

$

$

$$

$

$

$

$

$

$

$

$

$$

$

$

$

$$

$$

$

$

$

$$

$$

$ $

$

$

$

$

$

$

$

$

$

$

$

$

$

$$

bc_pubs

Page 16: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

bc_roads

Page 17: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

##

#######

#

#

#

#

##

#

#

#

#

#

#

##

#

#######

#

##

#

#

##

##

#

#

#

#

bc_hospitals

Page 18: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

bc_municipality

Page 19: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

bc_voting_areas

Page 20: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

bc_voting_areas

Page 21: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Creating Spatial Indexes

Page 22: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Using Spatial Indexes

select gid, name from bc_roads where crosses( the_geom, GeomFromText(‘LINESTRING(…)’,42102) )

select gid, name from bc_roads where crosses( the_geom, GeomFromText(‘LINESTRING(…)’,42102) )and the_geom && GeomFromText(‘LINESTRING(…)’,42102)

Page 23: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Query Plans

Page 24: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

#---------------------------------------------------------------------------# CONNECTIONS AND AUTHENTICATION#---------------------------------------------------------------------------

# - Connection Settings -

#listen_addresses = 'localhost' # what IP interface(s) to listen on; # defaults to localhost, '*' = any

#port = 5432max_connections = 100#superuser_reserved_connections = 2#rendezvous_name = '' # defaults to the computer name

# - Security & Authentication -

#authentication_timeout = 60 # 1-600, in seconds#ssl = false#password_encryption = true#db_user_namespace = false

#---------------------------------------------------------------------------# RESOURCE USAGE (except WAL)#---------------------------------------------------------------------------

# - Memory -

shared_buffers = 1000 # min 16, at least max_connections*2, 8KB each#work_mem = 1024 # min 64, size in KB#maintenance_work_mem = 16384 # min 1024, size in KB#max_stack_depth = 2048 # min 100, size in KB

#vacuum_cost_page_hit = 1 # 0-10000 credits#vacuum_cost_page_miss = 10 # 0-10000 credits

Postg

reSQ

L O

ptim

izatio

n

Page 25: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Data Integrity

Valid Invalid

Page 26: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Distance Queries

select sum(upbc) as unity_voters from bc_voting_areas where the_geom && setsrid( expand(‘POINT(…)’::geometry,2000) 42102 ) and distance( the_geom, geomfromtext(‘POINT(…)’, 42102) ) < 2000;

Page 27: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Spatial Joins

• Associate two tables based on a spatial relationship, rather than an attribute relationship.

Page 28: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

select m.name, sum(v.ndp) as ndp, sum(v.lib) as liberal, sum(v.gp) as green, sum(v.upbc) as unity, sum(v.vtotal) as total from bc_voting_areas v, bc_municipality m, where v.the_geom && m.the_geom and intersects(v.the_geom, m.the_geom) group by m.name order by m.name;

Page 29: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Overlays

• Table on table overlays are possible with the Intersection() function.

• Our example will only overlay one polygon with another table.

Page 30: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

create table pg_voting_areas as select intersection(v.the_geom, m.the_geom) as intersection_geom, area(v.the_geom) as va_area, v.*, m.name from bc_voting_areas v, bc_municipality m where v.the_geom && m.the_geom and intersects(v.the_geom, m.the_geom) and m.name = ‘PRINCE GEORGE’;

Page 31: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Coordinate Projection

SRID=42102;MULTILINESTRING((1004687.04355194 594291.053764096,1004729.74799931 594258.821943696))

SRID=4326;MULTILINESTRING((-125.9341 50.3640700000001,-125.9335 50.36378))

Page 32: OpenSource GIS 2004 Ottawa, Canada  Introduction to PostGIS PostGIS Basics for the New User Paul Ramsey & Chris Hodgson Refractions

OpenSource GIS 2004

Ottawa, Canadawww.refractions.net

Exercises &Questions