+ All Categories
Home > Documents > PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON),...

PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON),...

Date post: 21-Dec-2015
Category:
View: 232 times
Download: 12 times
Share this document with a friend
Popular Tags:
31
PostgreSQL/PostGIS PostgreSQL/PostGIS Santosh Gaikwad, Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History Salim Ali Centre For Ornithology And Natural History (SACON), (SACON), Hyderabad Hyderabad [email protected] An OSGeo Training workshop An OSGeo Training workshop at at Centre For Space Science And Techonology Centre For Space Science And Techonology Education In Assia And The Pacific Education In Assia And The Pacific 12th – 14th January 2011 12th – 14th January 2011 Dehradun Dehradun
Transcript
Page 1: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

PostgreSQL/PostGISPostgreSQL/PostGIS

Santosh Gaikwad, Santosh Gaikwad,

Salim Ali Centre For Ornithology And Natural History (SACON),Salim Ali Centre For Ornithology And Natural History (SACON),

Hyderabad Hyderabad

[email protected]

An OSGeo Training workshopAn OSGeo Training workshopatat

Centre For Space Science And Techonology Centre For Space Science And Techonology Education In Assia And The Pacific Education In Assia And The Pacific

12th – 14th January 201112th – 14th January 2011

DehradunDehradun

Page 2: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

What is Spatial data?What is Spatial data?

Data which describes either location or shapeData which describes either location or shape

e.g.House or Fire Hydrant locatione.g.House or Fire Hydrant locationRoads, Rivers, Pipelines, Power linesRoads, Rivers, Pipelines, Power linesForests, Parks, Municipalities, LakesForests, Parks, Municipalities, Lakes

Page 3: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

What is a Spatial Database?What is a Spatial Database?

Database that:Database that:• Stores spatial objectsStores spatial objects• Manipulates spatial objects just like other objects in Manipulates spatial objects just like other objects in

the databasethe database

Three aspectsThree aspects• Spatial data typesSpatial data types• Spatial indexingSpatial indexing• Spatial functionsSpatial functions

Page 4: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

What is PostGIS?What is PostGIS?

PostGIS turns the PostgreSQL Database PostGIS turns the PostgreSQL Database Management System into a spatial database by Management System into a spatial database by adding adding support for the three features: adding adding support for the three features: spatial types, indexes, and functionsspatial types, indexes, and functions

Page 5: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Why choose PostgreSQL?Why choose PostgreSQL?

PostgreSQL has:PostgreSQL has:• Proven reliability and respectProven reliability and respect• No cost (open source)No cost (open source)• Supports most of the SQL standardSupports most of the SQL standard• Ability to add new data-typesAbility to add new data-types• TOAST - no limit on column sizeTOAST - no limit on column size• GiST index / Index extensionsGiST index / Index extensions• Easy to add custom functionsEasy to add custom functions

Page 6: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Why not Shapefiles?Why not Shapefiles?

Files require special software to read and writeFiles require special software to read and write Concurrent users can cause corruptionConcurrent users can cause corruption Complicated questions require complicated Complicated questions require complicated

software to answersoftware to answer

Page 7: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

What applications support PostGIS?What applications support PostGIS?Open Source/ Free Closed /Proprietary•Loading/Extracting

• Shp2Pgsql• ogr2ogr• Dxf2PostGIS

•Web-Based• Mapserver• GeoServer (Java-based WFS /

WMS -server )• SharpMap SDK - for ASP.NET

2.0• MapGuide Open Source (using

FDO)•Desktop

• uDig• QGIS• mezoGIS• OpenJUMP• OpenEV• SharpMap SDK for

Microsoft.NET 2.0• ZigGIS for

ArcGIS/ArcObjects.NET• GvSIG• GRASS

•Loading/Extracting• Safe FME Desktop

Translator/Converter•Web-Based

• Ionic Red Spider (now ERDAS)• Cadcorp GeognoSIS• Iwan Mapserver• MapDotNet Server• MapGuide Enterprise (using

FDO)• ESRI ArcGIS Server 9.3+

•Desktop• Cadcorp SIS• Microimages TNTmips GIS• ESRI ArcGIS 9.3+• Manifold• GeoConcept• MapInfo (v10)• AutoCAD Map 3D (using FDO)

Page 8: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Practical PostgreSQL/PostgisPractical PostgreSQL/Postgis

InstallationInstallation Creating a Spatial DatabaseCreating a Spatial Database How to Spatially Enable an Existing DatabaseHow to Spatially Enable an Existing Database Loading spatial dataLoading spatial data

• Command prompt (shp2pgsql)Command prompt (shp2pgsql)• (GUI) shp2pgsql loader(GUI) shp2pgsql loader• OpenJUMPOpenJUMP• QGIS (SPIT)QGIS (SPIT)• GDALGDAL• GeokettleGeokettle

Loading data from non-spatial sourcesLoading data from non-spatial sources Viewing the dataViewing the data Querying the dataQuerying the data

Page 9: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

PostgreSQL/PostGIS InstallationPostgreSQL/PostGIS Installation

Page 10: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

OpenGeo SuiteOpenGeo Suite

It is the complete , OGC standards- compliant It is the complete , OGC standards- compliant web mapping platform built on powerful, cutting-web mapping platform built on powerful, cutting-edge, open source geospatial components.edge, open source geospatial components.

It is the bundle of following softwareIt is the bundle of following software• PostgreSQL/PostGISPostgreSQL/PostGIS• GeoServerGeoServer• GeoWebCacheGeoWebCache• OpenLayersOpenLayers• GeoExtGeoExt

PostgreSQL/PostGIS database run on 54321 portPostgreSQL/PostGIS database run on 54321 port http://opengeo.org

Page 11: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

OpenGeo InstallationOpenGeo Installation

Page 12: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Creating Spatial DatabaseCreating Spatial Database (using template_postgis)(using template_postgis)

Page 13: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Creating Spatial DatabaseCreating Spatial Database(without template_postgis)(without template_postgis)

Create a new databaseCreate a new database Connect to the databaseConnect to the database Load/run the PostGIS extension (postgis.sql)Load/run the PostGIS extension (postgis.sql) Earlier version has lwpostgis.sqlEarlier version has lwpostgis.sql Load/run the PostGIS spatial reference systems Load/run the PostGIS spatial reference systems

(spatial_ref_sys.sql)(spatial_ref_sys.sql)

Page 14: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Metadata TablesMetadata Tables

spatial_ref_sys:spatial_ref_sys:

defines all the spatial reference systems known to defines all the spatial reference systems known to the database.the database.

geometry_columns:geometry_columns:

provides a listing of all “features” and the basic provides a listing of all “features” and the basic details of those features.details of those features.

Page 15: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Metadata TablesMetadata Tables

Page 16: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Loading Shape FilesLoading Shape Files

shp2pgsql [opts] shp2pgsql [opts] shapefileshapefile tablename > file.sqltablename > file.sql• Shp2pgsql –s 32644 –D C:\churches.shp Shp2pgsql –s 32644 –D C:\churches.shp churches(table) > chueches.sqlchurches(table) > chueches.sql

Read in .shp fileRead in .shp file Write out .sql fileWrite out .sql file Load .sql file into PostgreSQL Load .sql file into PostgreSQL

• using psql using psql • using PgAdminusing PgAdmin

Page 17: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Command Line OptionsCommand Line Options

-D = Use “dump” format-D = Use “dump” format

-i = Do not use “bigint”, even for long numbers-i = Do not use “bigint”, even for long numbers

-I = Create a GiST index on the geometry column-I = Create a GiST index on the geometry column

-g = Specify the name of the geometry column-g = Specify the name of the geometry column

-s <#> = Use this SRID-s <#> = Use this SRID

-W <encoding> = Use this character encoding-W <encoding> = Use this character encoding

-a = Run in append mode-a = Run in append mode

Page 18: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Loading Shape FilesLoading Shape Files

psql psql –d postgis –d postgis –U postgres –U postgres –f bc_data.sql–f bc_data.sql

psql –d database –U postgres –f file.sqlpsql –d database –U postgres –f file.sql

To convert database table to shapefilesTo convert database table to shapefiles

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

Page 19: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

QGIS InstallationQGIS Installation

Page 20: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

OpenJUMPOpenJUMP

• OpenJUMP is Open Source GIS software written in OpenJUMP is Open Source GIS software written in Java Programming languageJava Programming language

• Installation:Installation:

1.1. Install JavaInstall Java

2.2. Install OpenJUMPInstall OpenJUMP

3.3. Install PostGIS database driver (Put Install PostGIS database driver (Put PostGIS132.jar file in /lib/ext folder of OpenJUMP)PostGIS132.jar file in /lib/ext folder of OpenJUMP)

Page 21: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Java InstallationJava Installation

Page 22: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

OpenJUMP InstallationOpenJUMP Installation

Page 23: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Geometry Input and OutputGeometry Input and Output Well-known text (Well-known text (WKT))

• ST_GeomFromText(text)ST_GeomFromText(text) returns geometry returns geometry• ST_AsText(geometry)ST_AsText(geometry) returns text returns text• ST_AsEWKT(geometry)ST_AsEWKT(geometry) returns text returns text

Well-known binary (Well-known binary (WKBWKB))• ST_GeomFromWKB(bytea)ST_GeomFromWKB(bytea) returns geometry returns geometry• ST_AsBinary(geometry)ST_AsBinary(geometry) returns bytea returns bytea• ST_AsEWKB(geometry)ST_AsEWKB(geometry) returns bytea returns bytea

Geographic Mark-up Language (Geographic Mark-up Language (GMLGML))• ST_GeomFromGML(text)ST_GeomFromGML(text) returns geometry returns geometry• ST_AsGML(geometry)ST_AsGML(geometry) returns text returns text

Keyhole Mark-up Language (Keyhole Mark-up Language (KMLKML))• ST_GeomFromKML(text)ST_GeomFromKML(text) returns geometry returns geometry• ST_AsKML(geometry)ST_AsKML(geometry) returns text returns text

GeoJSONGeoJSON• ST_AsGeoJSON(geometry)ST_AsGeoJSON(geometry) returns text returns text

Scalable Vector Graphics (Scalable Vector Graphics (SVGSVG))• ST_AsSVG(geometry)ST_AsSVG(geometry) returns text returns text

Page 24: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Geoprocessing with PostGISGeoprocessing with PostGIS

PostGIS functions (~700) are available through SQLPostGIS functions (~700) are available through SQLCoordinate transformationCoordinate transformationIdentify Identify BufferBufferTouchesTouchesCrossesCrossesWithinWithinOverlapsOverlapsContainsContainsAreaAreaLengthLengthPoint on surfacePoint on surfaceReturn geometry as SVGReturn geometry as SVGMany, many, many moreMany, many, many more

Page 25: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Viewing Data in PostGISViewing Data in PostGIS

Quick desktop viewing optionsQuick desktop viewing options• uDiguDig• QGISQGIS• gvSIGgvSIG• CadCorp SIS*CadCorp SIS*• FME Viewer*FME Viewer*

Web based application optionsWeb based application options• MapGuideMapGuide• MapserverMapserver• GeoserverGeoserver

Page 26: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

uDig InstallationuDig Installation

Page 27: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Loading data from non spatial Loading data from non spatial sourcessources

Create table using pgAdminIIICreate table using pgAdminIII Copy data to the database using copy Copy data to the database using copy

commandcommand Create the Geometry field using Create the Geometry field using

AddGeometryColumn() spatial functionAddGeometryColumn() spatial function Populate the Geometry field using the Populate the Geometry field using the

Longitude and Latitude fields Longitude and Latitude fields

Page 28: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Calculate area in HectareCalculate area in Hectare

• Load area_slums.shp into database with SRID Load area_slums.shp into database with SRID 3264432644

• Add the area field with data type double Add the area field with data type double precisionprecision

• Update the area field using spatial function Update the area field using spatial function ST_area()ST_area()

• UPDATE area_slums set UPDATE area_slums set area=ST_area(geometry)/10000;area=ST_area(geometry)/10000;

Querying the dataQuerying the data

Page 29: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

What is the total length of all roads in What is the total length of all roads in Rajahmundry corporation in KilometersRajahmundry corporation in Kilometers

• Add rjyroads_Names.shp data to database with Add rjyroads_Names.shp data to database with SRID 32644SRID 32644

• SELECT sum(ST_length(the_geom))/1000 as SELECT sum(ST_length(the_geom))/1000 as kilometers from rjyroads_names;kilometers from rjyroads_names;

Querying the dataQuerying the data

Page 30: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Which the biggest slums in Rajahmundry Which the biggest slums in Rajahmundry Corporation by area in HectareCorporation by area in Hectare

• Add area_slums.shp data to the database with Add area_slums.shp data to the database with SRID 32644SRID 32644

• SELECT max(ST_area(geometry))/10000 from SELECT max(ST_area(geometry))/10000 from area_slums;area_slums;

Querying the dataQuerying the data

Page 31: PostgreSQL/PostGIS Santosh Gaikwad, Salim Ali Centre For Ornithology And Natural History (SACON), Hyderabad Hyderabad santosh@osgeo.in An OSGeo Training.

Find churches within a radius of distance Find churches within a radius of distance from slumsfrom slums

• Use ST_dwithin() functionUse ST_dwithin() function

• SELECT distinct c.name,c.address from SELECT distinct c.name,c.address from area_slums as s , churches as c WHERE area_slums as s , churches as c WHERE ST_dwithin(s.geometry, c.the_geom, 200) = ST_dwithin(s.geometry, c.the_geom, 200) = TRUE;TRUE;

Querying the dataQuerying the data


Recommended