Date post: | 21-Dec-2015 |
Category: |
Documents |
View: | 232 times |
Download: | 12 times |
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
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
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
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
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
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
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
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)
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
PostgreSQL/PostGIS InstallationPostgreSQL/PostGIS Installation
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
OpenGeo InstallationOpenGeo Installation
Creating Spatial DatabaseCreating Spatial Database (using template_postgis)(using template_postgis)
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)
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.
Metadata TablesMetadata Tables
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
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
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
QGIS InstallationQGIS Installation
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)
Java InstallationJava Installation
OpenJUMP InstallationOpenJUMP Installation
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
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
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
uDig InstallationuDig Installation
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
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
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
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
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