Anda di halaman 1dari 71

iirs

SPATIAL DATABASE MANAGEMENT


SYSTEM- POSTGRESQL/POSTGIS

www.iirs.gov.in

Kamal Pandey
kamal@iirs.gov.in

Adopted from the slides of Mr. Kapil Oberoi, Scientist IIRS

iirs

Lecture Outline

Introduction
Definition and Classification of Spatial Database
Systems
Evolution of spatial data processing
Introduction to PostgreSQL & PostGIS
Spatial Data (Shapefile) in PostgreSQL/PostGIS
Spatial Queries using PostGIS
Visualizing queries result in QGIS

iirs

Introduction

The volume of spatial data available for processing increases


rapidly over the years with the evolution of sensing devices
and telecommunication technology.
Most data that can be stored and analyzed carry spatial
information; as a result, the management of location and
geometric features of entities is an essential component of a
modern Database Management System (DBMS).
Spatial Database Management Systems (SDBMSs) manage
large collections of spatial objects, which apart from
conventional features include spatial characteristics, such as
geometric position and extent.

iirs

Definition

Guting (1994) defined spatial database system as


a class of database system that have the following
three characteristics:
A

spatial database system is a database system


It offers spatial data types (SDT) in its data model and
query language
It supports SDTs in its implementation, providing at
least spatial indexing and efficient algorithms for
spatial join

iirs

Definition Contd

Guting(1994) in his definition considered spatial


databases only as the underlying technology for
GIS.
Hence, spatial database system are by no means
fully operational GIS and may only facilitate a
small subset of the functions that GIS provide.

Evolution of spatial data processing

iirs

iirs

GIS and SDBMS

Division of work between Spatial Database and


GIS

iirs

GIS and SDBMS Contd


System
Geographic
Information System

Spatial Database
Management
System

Data

Task

Data load
Editing
Visualization
Mapping
Analysis

Storage
Indexing
Security
Query

iirs

Spatial Databases must integrate with


other applications and data.
HTML Viewer
Java Viewer

(Internet)

GIS Desktop
Applications

Wireless
Mobile
Devices
Network
Custom
Applications

Map Renderer
Spatial
DB

Server Side
Applications

Source: Spatial database lecture slides by Pat Browne;


http://www.comp.dit.ie/pbrowne/Spatial%20Databases%20SDEV4005/Spatial%20Databases%20SDEV4005.htm

iirs

Classification
of
Spatial Operators

iirs

Why use a database for GIS?


GIS are not database systems, although they
can be connected to a DBMS.
A
GIS cannot efficiently manage large
quantities of non-spatial data (e.g. at
government department level).
They lack adhoc querying capability (they
provide a restricted form of predefined
queries)
They lack indexing structures for fast external
data access

iirs

Why use a database for GIS?

Databases offer the following functions:

Reliability
Integrity: enforces
consistency
Security
User views
User interface
Querying
Updating
DB theory has a
Mathematical basis

Data independence
Data Abstraction
Self-describing
Concurrency
Distributed capabilities
High performance
Supports spatial data
types using ADTs.
Alternative: files

iirs

Why use a database for GIS?


Spatial querying using SQL

use simple SQL expressions to determine


spatial relationships

distance
adjacency
containment

use simple SQL expressions to perform spatial


operations

area
length
intersection
union
buffer

iirs

Advantages of Spatial Databases

WHERE distance(<me>,hos_loc) < 1000


WHERE inside(hos_loc,city_area) and city_name =
...

iirs

What is a Spatial Database? (Recap)

Database that:
Stores spatial objects
Manipulates spatial objects just like other objects
in the database
Three aspects
Spatial data types
Spatial indexing
Spatial functions

iirs

Spatial Database Offerings

ESRI ArcSDE (on top of several different DBs)


Oracle Spatial
IBM DB2 Spatial Extender
MS SQL Server
SAP SYBASE SQL Anywhere
MySQL Spatial
PostGIS / PostgreSQL

iirs

Standard Database vs Spatial Database

Has data types


varchar
integer
real
date

Has spatial data types


point
linestring
polygon
multipoint
multilinestring
multipolygon

iirs

Standard Database vs Spatial Database

Has functions
Has spatial functions
Work against standard Work against spatial types
types
ST_Area(geometry)
lower()
ST_Distance(geometry,geometry)
round()
ST_Intersects(geometry,geometry)
substring()
ST_DWithin(geometry,geometry,radius)
trim()
dayofweek ()

This is your Database


Queries

iirs

Is there a Main Street?


How many patients does the Health center
serve?
How many buildings in the city have more than
two stories?
What is the total amount of cash withdrawals for
customer X?

This is your Database


on Spatial

iirs

What is the total length of Main Street?


What is the total number of patients within 3
miles of the prenatal center?
How many buildings of more than 2 stories are
along the parade route?
Have all the cash withdrawals for customer X
been within 50 miles of their primary
residence?

iirs

About PostgreSQL

http://www.postgresql.org/

PostgreSQL is a powerful, open source object-relational


database system.
It has more than 15 years of active development and a
proven architecture that has earned it a strong
reputation for reliability, data integrity, and correctness.
It runs on all major operating systems
It is fully ACID compliant, has full support for foreign
keys, joins, views, triggers, and stored procedure.
It has native programming interfaces for C/C++, Java,
.Net, Perl, Python, Ruby, Tcl, ODBC, among others, and
Exceptional documentation.

iirs

About PostgreSQL Contd

There are active PostgreSQL systems in production


environments that manage in excess of 4 terabytes of data.
Current Stable Version 9.4.5
Limit

Value

Maximum Database Size

Unlimited

Maximum Table Size

32 TB

Maximum Row Size

1.6 TB

Maximum Field Size

1 GB

Maximum Rows per Table

Unlimited

Maximum Columns per Table

250 - 1600

Maximum Indexes per Table

Unlimited

About PostGIS

iirs
http://postgis.net/

PostGIS is a spatial database extender for


PostgreSQL object-relational database.
It adds support for geographic objects allowing
location queries to be run in SQL.
SELECT superhero.name FROM city, superhero
WHERE ST_Contains(city.geom, superhero.geom)
AND city.name = 'Gotham';

Current Version 2.2.0

Open source
Proprietary/open source clients

iirs

PostGIS and OGC standard

PostGIS implements and is compliant with the OGCs Simple


Features for SQL standard.
PostGIS supports all OGC types: Point, Line, Polygon, MultiPoint,
MultiLine, MultiPolygon, GeometryCollection and operations.
PostGIS uses OGC well-known text format on the SQL command-line
to represent GIS features.
POINT(1 1)
LINESTRING(1 1, 2 2, 3 4)
POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))
PostGIS adds extra types (geometry, geography, raster and others)
to the PostgreSQL database.
It also adds functions, operators, and index enhancements that apply
to these spatial types.

iirs

Installing PostgreSQL/PostGIS

http://www.postgresql.org/download/windows/
Both 32 bit and 64 bit installers
Many options
Download

and install PostgreSQL from EnterpriseDB.


Run the StackBuilder utility and install the PostGIS
add-on
Download and install the appropriate PostGIS installer
for your PostgreSQL version from the PostGIS Windows
download site
OpenGeo Suite.

PostgreSQL/PostGIS Installation

iirs

PostgreSQL/PostGIS Installation contd

iirs

PostgreSQL/PostGIS Installation contd

iirs

PostgreSQL/PostGIS Installation contd

iirs

iirs

Creating a Spatial Database

PostgreSQL has a number of administrative front-ends.


psql and pgAdmin.
Start PgAdmin
Right-click
on
the
Databases item and select
New Database

iirs

Creating a Spatial Database Contd

iirs

Creating a Spatial Database Contd


Select the new postgisdemo
database and open it up to display
the tree of objects.
Public schema is present
Click on the SQL query button
(or go to Tools > Query
Tool).
CREATE EXTENSION postgis;
Click the > Play button in the
toolbar.
SELECT postgis_full_version();

iirs

Creating a Spatial Database Contd

iirs

PostGIS geography vs geometry type

Data in geography data type must always be stored in WGS-84


GCS (Geographic Coordinate System) i.e. long lat degrees.
All measurements in geography are expressed in meters.
Geometry data can be stored in any spatial reference system.
The measurement is in units of spatial ref. system.
If your data is geographically compact (contained within a state,
county or city), use the geometry type with a cartesian projection
based on your data.
If you need to measure distance with a dataset that is
geographically dispersed (covering much of the world), use the
geography type.

iirs

Metadata tables

spatial_ref_sysTable recording spatial reference


systems
geometry_columnsRead-only view listing spatial
(or feature) tables, geometry columns and geometry
types present in the database
geography_columnsRead-only
view
listing
spatial (or feature) tables, geography columns and
geography types present in the database

iirs

Metadata tables contd


SPATIAL_REF_SYS table definition

GEOMETRY_COLUMNS VIEW

CREATE TABLE spatial_ref_sys

Column | Type | Modifiers

-------------------+------------------------+-----------

srid INTEGER NOT NULL PRIMARY KEY,

f_table_catalog | character varying(256) |

auth_name VARCHAR(256),

f_table_schema | character varying(256) |

auth_srid INTEGER,

f_table_name | character varying(256) |

srtext VARCHAR(2048),

f_geometry_column | character varying(256) |

proj4text VARCHAR(2048)

coord_dimension | integer

srid | integer |
type | character varying(30) |

iirs

Metadata tables contd

iirs

PostGIS Functions

Single Geometry
Float : st_Length(Geometry)
Float : st_Area(Geometry)
Geometry : st_Buffer(Geometry,Float)
Text : st_AsText(Geometry)
Multiple Geometry
Float : st_Distance(Geometry,Geometry)
Boolean : st_Touches(Geometry,Geometry)
Boolean : st_Intersects(Geometry,Geometry)
Boolean : st_Disjoint(Geometry,Geometry)
Geometry : st_Intersection(Geometry,Geometry)

iirs

Creating a Geography Table


CREATE TABLE global_points
(
id SERIAL PRIMARY KEY,
name VARCHAR(64),
location GEOGRAPHY(POINT,4326)
);

Regsiter in the "geography_columns" view

INSERT INTO global_points (name, location) VALUES ('Town', ST_GeographyFromText


('SRID=4326;POINT(-110 30)') );
INSERT INTO global_points (name, location) VALUES ('Forest', ST_GeographyFromText(
'SRID=4326;POINT(-109 29)') );
INSERT INTO global_points (name, location) VALUES ('London', ST_GeographyFromText(
SRID=4326;POINT(0 49)') );

iirs

Simple query

select name,location from global_points;

select name,st_astext(location) from global_points

Creating a Spatial Table using Geometry


Column

iirs

Creating a table with spatial data is done in two stages:


1. Create a normal non-spatial table.
CREATE TABLE ROADS_GEOM ( ID int4, NAME varchar(25) );
2. Add a spatial column to the table using the "AddGeometryColumn"
function.
SELECT AddGeometryColumn(<schema_name>, <table_name>,
<column_name>, <srid>, <type>, <dimension>) ;
SELECT AddGeometryColumn('public', 'roads_geom', 'geom',
0, 'LINESTRING', 2);
OR: Load in SHP file and spatial table will be created automatically

iirs

Adding Spatial data


Create a data upload file roads.sql:
INSERT
INTO
ROADS_GEOM
(ID,GEOM,NAME
)
VALUES
(1,ST_GeomFromText('LINESTRING(191232 243118,191108 243242)'), 'Jeff Rd');
INSERT
INTO
ROADS_GEOM
(ID,GEOM,NAME
)
VALUES
(2,
ST_GeomFromText('LINESTRING(189141 244158,189265 244817)'), 'Geordie Rd');
INSERT
INTO
ROADS_GEOM
(ID,GEOM,NAME
)
VALUES
ST_GeomFromText('LINESTRING(192783 228138,192612 229814)'),'Paul St');
psql -U username -d [database] -f roads.sql

SELECT id, ST_AsText(geom) AS geom, name FROM ROADS_GEOM;


id | geom
| name
---+-------------------------------------+----------1 | LINESTRING(191232 243118,191108 243242) | Jeff Rd
2 | LINESTRING(189141 244158,189265 244817) | Geordie Rd
3 | 'LINESTRING(192783 228138,192612 229814) | Paul St
(3 rows)

(3,

iirs

Convert Non-Spatial X and Y fields to PostGIS


create table user_locations (gid int4, user_name varchar);
select AddGeometryColumn
(public','user_locations','the_geom','4326','POINT',2);
insert into user_locations values ('1',location1', ST_GeometryFromText
('POINT(-71.060316 48.432044)', 4326));
insert into user_locations values ('2', location2', ST_GeometryFromText
('POINT(-48.6764 -26.8916)', 4326));
...
AddGeometryColumn()
this function adds the meta information of this field to the geometry_columns

DropGeometryColumn()
removes the meta information from the geometry_columns

iirs

Convert Non-Spatial X and Y fields to PostGIS


First select both X and Y coordinates into one
field of type character
objgeom =78.03 28.034".
Then call the function GeometryFromText,
add the geometry type, brackets and append
the EPSG code of the coordinates.

UPDATE test SET the_geom = St_GeometryFromText('POINT '(|| objgeom || ')"',


4326)

iirs

Loading Shape Files

shp2pgsql [opts] shapefile tablename > file.sql


Shp2pgsql s 32644 C:\churches.shp churches >
churches.sql

Read in .shp file


Write out .sql file
Load .sql file into PostgreSQL
using psql
using PgAdmin

iirs

Loading Shape Files


psql
psql -d <dbname> -f <filename>

d postgis
U postgres
f bc_data.sql
psql d database U postgres f file.sql

To convert database table to shapefiles


pgsql2shp -f "test.shp" -u postgres -p 5432 -P password
database schema.table

iirs

iirs

Using Shapefile loader

iirs

iirs

Loading Shape Files Using QGIS

iirs

Viewing Data in PostGIS

Desktop GIS

uDig
QGIS
gvSIG

Web based application options

MapGuide
Mapserver
Geoserver

iirs

PgAdmin (GUI)

iirs

Four verbs of SQL


SELECT,returnsrowsinresponsetoaquery
INSERT,addsnewrowstoatable
UPDATE,altersexistingrowsinatable
DELETE,removesrowsfromatable
Wewillbeworkingwith SELECT inordertoask
questionsoftablesusingspatialfunctions.

SELECT queries
A select query is generally of the form:
SELECT some_columns FROM some_data_source WHERE some_condition;

iirs

PostGIS Spatial Query

The examples in this section will make use of two tables


The table definitions for the bc_roads table is:

The table definition for the bc_municipality table is:

PgAdmin (GUI)

iirs

PgAdmin (GUI)

iirs

iirs

PostGIS Spatial Query

What is the total length of all roads, expressed in kilometers?


SELECT sum(length(the_geom))/1000 AS km_roads FROM bc_roads;
km_roads
-----------------70842.1243039643 (1 row)

How large is the city of Prince George, in hectares?


SELECT st_area(the_geom)/10000 AS hectares FROM bc_municipality
WHERE name = 'PRINCE GEORGE';
UPDATE bc_municipality set area=area(the_geom)/10000;

What is the largest municipality in the province, by area?


SELECT name, st_area(the_geom)/10000 AS hectares FROM
bc_municipality ORDER BY hectares DESC LIMIT 1;

iirs

PostGIS Spatial Query


What is the perimeter of the municipality of Vancouver?

SELECT ST_Perimeter(the_geom) FROM bc_municipality


WHERE name = 'VANCOUVER;

One can also make use of spatial functions, such as st_Distance(),


st_Intersects(), st_Contains() and st_Within()... to narrow down the
results of spatial search.

Find municipalities within a distance from roads


(Use ST_dwithin() function)
ST_DWithin(geometry A, geometry B, radius): Returns true if the
geometries are within the specified distance (radius) of one another.
SELECT distinct m.name from bc_roads as r , bc_municipality as m
WHERE ST_dwithin(r.geometry, m.the_geom, 200) = TRUE;

iirs

PostGIS Spatial Query

Find the names of all municipalities which are neighbors of the


"VANCOUVER" in the bc_municipalities table
select c1.name from bc_municipality c1, bc_municipality
c2 where st_touches(c1.geom,c2.geom)='T' and
2.name='VANCOUVER

To perform a reprojection, both the source and destination


coordinate systems must be defined (with SRID) in the
SPATIAL_REF_SYS table.
SELECT ST_Transform(GEOM,4269) FROM GEOTABLE;

iirs

Viewing query result (QGIS, DB plugin)

iirs

Viewing query result (QGIS, DB plugin)

iirs

SPATIAL JOIN

What is the length of roads fully contained within


each municipality?
This is an example of a "spatial join", because we are bringing together data
from two tables (doing a join) but using a spatial interaction condition
("contained") as the join condition rather than the usual relational approach
of joining on a common key

SELECT
m.name,sum(ST_Length(r.the_geom)) as roads_km
FROM bc_roads as r, bc_municipality as m
WHERE
ST_Contains(m.the_geom,r.the_geom)
GROUP BY m.name;

iirs

Overlays

We should distinguish the overlay operation and tests


for overlap.
Table-on-table overlays are possible with the
ST_Intersection() function
ST_Intersects(a,b)=TRUE | FALSE
ST_Intersection(a,b)=GEOMETRY

ST_Intersects() = TRUE
ST_Intersection() =

iirs

Overlays Contd

iirs

Overlays Contd
CREATE TABLE pg_voting_areas AS
SELECT
ST_Intersection(v.the_geom, m.the_geom)
AS intersection_geom,
ST_Area(v.the_geom) AS va_area,
v.*, m.name
FROM
bc_voting_areas v, bc_municipality m
WHERE
ST_Intersects(v.the_geom, m.the_geom) AND
m.name = PRINCE GEORGE;

Web GIS application for flood hazard management

iirs

iirs

iirs

CHSS BENEFICIARIES OF IIRS


2
1

iirs

Database Backup & Restore facility

Start pgAdmin
Database Backup
Right click on specific
DB to take Backup
Select backup menu
item.
Select filename and
press OK.

iirs

Database Backup & Restore facility


Database Restore
Create new
database
Right click on DB to
restore from
backup,
select restore
menu item.
Select *.backup file
and click ok.

iirs

Thanks for your kind attention

Anda mungkin juga menyukai