linuxfest nw - using postgis to add some spatial flavor to your app
DESCRIPTION
round 2 of the talk I gave before - a little has changed but not muchTRANSCRIPT
Using PostGIS to add some Using PostGIS to add some spatial flavor to your spatial flavor to your
applications applications
Steven Citron-PoustySteven Citron-Pousty
Applied Technology Leader Applied Technology Leader
ICF/Jones & StokesICF/Jones & Stokes
AgendaAgenda
What is PostGISWhat is PostGIS InstallingInstalling Getting data and importingGetting data and importing Using in an applicationUsing in an application
ASK QUESTIONS ALL ALONGASK QUESTIONS ALL ALONG
Assumptions about youAssumptions about you
Know PostgreSQL and SQLKnow PostgreSQL and SQL New to spatial analysis and PostGISNew to spatial analysis and PostGIS Mainly using a commercial mapping Mainly using a commercial mapping
service for your base layersservice for your base layers Interested in writing cool applicationsInterested in writing cool applications
This will only be a short shallow This will only be a short shallow introductionintroduction
What is PostGISWhat is PostGIS
PostGIS adds support for geographic PostGIS adds support for geographic objects and geographic functions to objects and geographic functions to PostgreSQLPostgreSQL Points, Lines, Polygon, and Linear Points, Lines, Polygon, and Linear
ReferencesReferences Spatial function such as area, near, overlap, Spatial function such as area, near, overlap,
within, clip, and simplifywithin, clip, and simplify
Competitive OfferingsCompetitive Offerings SQLLite – has spatiallite which is a kickin' SQLLite – has spatiallite which is a kickin'
compliment to PostGIScompliment to PostGIS MySQL – doesn’t really implement all the MySQL – doesn’t really implement all the
functions and it only deals with bounding functions and it only deals with bounding boxesboxes
SQLServer – only in SQLServer2008 but SQLServer – only in SQLServer2008 but supported on all editionssupported on all editions
Oracle Spatial – full featured and includes Oracle Spatial – full featured and includes a broad range of client librariesa broad range of client libraries
DB2 and Sybase and Informix and IngresDB2 and Sybase and Informix and Ingres ESRI’s SDE can use PostGIS as a DBESRI’s SDE can use PostGIS as a DB
InstallingInstalling
Other libraries you wantOther libraries you want Proj4.5 for projectionProj4.5 for projection GEOS 3.0 for spatial operationsGEOS 3.0 for spatial operations
Windows – use the “application stack builder”Windows – use the “application stack builder” There are RPMs on the PostGIS site – not always There are RPMs on the PostGIS site – not always
the latestthe latest You can use Synaptic for Ubuntu – not always the You can use Synaptic for Ubuntu – not always the
latestlatest Build is really simple, as long as you have required Build is really simple, as long as you have required
libraries installedlibraries installedhttp://www.paolocorti.net/2008/01/30/installing-http://www.paolocorti.net/2008/01/30/installing-
postgis-on-ubuntu/postgis-on-ubuntu/
Other StepsOther Steps
Add the objects and functions from the Add the objects and functions from the provided SQL filesprovided SQL files
Best practice is to make these into a Best practice is to make these into a template for future databasestemplate for future databases
From there making DBs is easy cheesyFrom there making DBs is easy cheesy
Two more things you Two more things you need to understandneed to understand
Almost there – I promise
ShapefilesShapefileswhen is a file not a filewhen is a file not a file
Projections and DatumsProjections and Datums
More Projection GoodnessMore Projection Goodness EPSG EPSG (European Petroleum Survey Group)(European Petroleum Survey Group) has a number for has a number for
most projections and datum combinationsmost projections and datum combinations Most online commercial mapping services, Most online commercial mapping services,
expect geographic coordinates with WGS84 expect geographic coordinates with WGS84 datumdatum
EPSG:4326EPSG:4326 Geographic coordinates system with WGS84 datum. Geographic coordinates system with WGS84 datum. Degrees are not constant distance as you move North Degrees are not constant distance as you move North
and Southand South Not very accurate for distance and area calcs except Not very accurate for distance and area calcs except
over small areasover small areas You want to use projected layers (or project on You want to use projected layers (or project on
the fly) for area and distance calcsthe fly) for area and distance calcs
Why should you careWhy should you care
• Because most of the data in the world is Because most of the data in the world is not in GCS WGS84 – eventhough your not in GCS WGS84 – eventhough your GPS isGPS is
Projecting using ogr2ogrProjecting using ogr2ogr
Assuming there is a .prj file you need to Assuming there is a .prj file you need to find your output projectionfind your output projection
http://Spatialreference.orghttp://Spatialreference.org
ogr2ogr -t_srs EPSG:4326 -f "ESRI ogr2ogr -t_srs EPSG:4326 -f "ESRI Shapefile" water_bodies_gcs.shp Shapefile" water_bodies_gcs.shp water_bodies.shpwater_bodies.shp
Now let’s make some SQLNow let’s make some SQL
Shp2pgsql is a command-line utility to take Shp2pgsql is a command-line utility to take a shapefile to a SQL filea shapefile to a SQL file
The geometry field is called The geometry field is called the_geomthe_geom
shp2pgsql.exe -s 4326 -I C:\data\shp2pgsql.exe -s 4326 -I C:\data\water_bodies_gcs.shp waterbodies > water_bodies_gcs.shp waterbodies > C:\data\waterbodies.sqlC:\data\waterbodies.sql
Y’all know what to do with SQLY’all know what to do with SQL
psql -q -U postgres -f C:\data\psql -q -U postgres -f C:\data\waterbodies.sql kerncountywaterbodies.sql kerncounty
With lots-o-features you want to use –q With lots-o-features you want to use –q option to prevent spamming your terminaloption to prevent spamming your terminal
Let’s get this party startedLet’s get this party startedSELECTSELECT gid, the_geom FROM waterbodies gid, the_geom FROM waterbodies WHEREWHERE gid = 2; gid = 2;
gid | the_geomgid | the_geom-----+------------------------------------------------------------------------------------------------------------------------ -----+------------------------------------------------------------------------------------------------------------------------
2 | 2 | 106000020E6100000010000000103000000010000002E00000002CB8FC170C65DC06200106000020E6100000010000000103000000010000002E00000002CB8FC170C65DC06200F8D0E7B341408625910970C65DC0DE898BCAE7B3414053748B1A6FC65DC0199092E7F8D0E7B341408625910970C65DC0DE898BCAE7B3414053748B1A6FC65DC0199092E7E6B34140576BAFCB6EC65DC07815C607E6B34140926B64A66EC65DC0D0D11854E4B3E6B34140576BAFCB6EC65DC07815C607E6B34140926B64A66EC65DC0D0D11854E4B34140CA783CC66EC65DC091477CD7E2B34140FD8B372B6FC65DC02D75F091E1B34144140CA783CC66EC65DC091477CD7E2B34140FD8B372B6FC65DC02D75F091E1B341400748F1756FC65DC02C1C7C06E0B34140A0CBF8846FC65DC01B057AC2DEB34140BD00748F1756FC65DC02C1C7C06E0B34140A0CBF8846FC65DC01B057AC2DEB34140BD6BDE586FC65DC036C25B1ADEB34140E73E8FBC6EC65DC051B6DE2DDDB3414037EF6BDE586FC65DC036C25B1ADEB34140E73E8FBC6EC65DC051B6DE2DDDB3414037EFA3B86DC65DC0A6E75E4DDCB34140EECF5AF16CC65DC007312311DBB341405304E1A3B86DC65DC0A6E75E4DDCB34140EECF5AF16CC65DC007312311DBB341405304E1D16CC65DC0C653541FDAB34140B352AB826CC65DC06800DD22D9B34140FDECCB8CD16CC65DC0C653541FDAB34140B352AB826CC65DC06800DD22D9B34140FDECCB8C6CC65DC0A8D8188ED7B3414028DA62606DC65DC03020AA9DD5B341401345D79D6EC6CC65DC0A8D8188ED7B3414028DA62606DC65DC03020AA9DD5B341401345D79D6EC65DC045CF8743D5B3414039BFF5A56FC65DC071F2D61AD6B34140D4391F4E70C65DC65DC045CF8743D5B3414039BFF5A56FC65DC071F2D61AD6B34140D4391F4E70C65DC01F041E4CD7B34140FD50463A70C65DC0534507A5D8B3414087FB5FFA6FC65DC0C4301F041E4CD7B34140FD50463A70C65DC0534507A5D8B3414087FB5FFA6FC65DC0C436FF22DAB3414092BF83B46FC65DC05EEC9961DBB34140456F69BC6FC65DC0E749156FF22DAB3414092BF83B46FC65DC05EEC9961DBB34140456F69BC6FC65DC0E74915C3DCB34140DCCD550170C65DC022859B35DEB34140A929A59870C65DC014E6EA42DC3DCB34140DCCD550170C65DC022859B35DEB34140A929A59870C65DC014E6EA42DFB34140D7388A8E70C65DC0B2B22CD4DFB341405EA0F73270C65DC0E92146C4E0B3FB34140D7388A8E70C65DC0B2B22CD4DFB341405EA0F73270C65DC0E92146C4E0B341402EC4B3DE6FC65DC066AD87D3E1B3414083B732D26FC65DC0583D9907E3B3414041402EC4B3DE6FC65DC066AD87D3E1B3414083B732D26FC65DC0583D9907E3B3414017F8104E70C65DC07C37AB39E4B341404DFB09DA70C65DC0AB6838C7E4B341404E3717F8104E70C65DC07C37AB39E4B341404DFB09DA70C65DC0AB6838C7E4B341404E376F4871C65DC0D88F88EAE4B34140E290F4CC71C65DC04318C2B1E4B34140B2B137366F4871C65DC0D88F88EAE4B34140E290F4CC71C65DC04318C2B1E4B34140B2B1373672C65DC001CD36A7E3B3414094289CBD72C65DC032BF27F1E2B34140551D1E3773C672C65DC001CD36A7E3B3414094289CBD72C65DC032BF27F1E2B34140551D1E3773C65DC053E9A4C1E2B34140B247EEC273C65DC03C50B08FE3B34140EFFF2CD573C65DC5DC053E9A4C1E2B34140B247EEC273C65DC03C50B08FE3B34140EFFF2CD573C65DC0849F2D84E4B34140BB03E9A973C65DC0D397181FE5B341406292782B73C65DC0103A0849F2D84E4B34140BB03E9A973C65DC0D397181FE5B341406292782B73C65DC0103A5EA9E5B34140B2EB752172C65DC0E72BDC89E5B341407818E39771C65DC0978C430B5EA9E5B34140B2EB752172C65DC0E72BDC89E5B341407818E39771C65DC0978C430BE6B34140AAE00CA471C65DC0E6C241AEE6B341405DD5367B71C65DC050325840E7BE6B34140AAE00CA471C65DC0E6C241AEE6B341405DD5367B71C65DC050325840E7B3414002CB8FC170C65DC06200F8D0E7B341403414002CB8FC170C65DC06200F8D0E7B34140
WTF!WTF!
Well Known Text (WKT)Well Known Text (WKT) SELECT gid, asText(the_geom) AS Geometry SELECT gid, asText(the_geom) AS Geometry
FROM waterbodies WHERE gid = 2;FROM waterbodies WHERE gid = 2; gid | geometrygid | geometry -----+-----------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------ 2 | MULTIPOLYGON(((-119.100632086203 35.4055119715638,-119.100588218445 2 | MULTIPOLYGON(((-119.100632086203 35.4055119715638,-119.100588218445
35.4055112058011,-119.100531231104 35.4054841485633,-119.100512429531 35.4055112058011,-119.100531231104 35.4054841485633,-119.100512429531 35.4054574696738,-119.100503538195 35.4054055329503,-119.100511130385 35.4054574696738,-119.100503538195 35.4054055329503,-119.100511130385 35.4053601605257,-119.100535206075 35.405321352397,-119.100553022028 35.4053601605257,-119.100535206075 35.405321352397,-119.100553022028 35.405274210549,-119.100556605305 35.4052355857659,-119.100546090324 35.405274210549,-119.100556605305 35.4052355857659,-119.100546090324 35.4052155445137,-119.100508823288 35.4051873528921,-119.100446853726 35.4052155445137,-119.100508823288 35.4051873528921,-119.100446853726 35.4051605904963,-119.100399340328 35.4051228925546,-119.100391835921 35.4051605904963,-119.100399340328 35.4051228925546,-119.100391835921 35.4050940667872,-119.100372950847 35.4050639704809,-119.100375365397 35.4050940667872,-119.100372950847 35.4050639704809,-119.100375365397 35.405015718586,-119.100425812278 35.4049565392201,-119.10050149939 35.405015718586,-119.100425812278 35.4049565392201,-119.10050149939 35.4049457944043,-119.100564470279 35.4049714612528,-119.100604563231 35.4049457944043,-119.100564470279 35.4049714612528,-119.100604563231 35.4050078531802,-119.100599831265 35.4050489697935,-119.100584596379 35.4050078531802,-119.100599831265 35.4050489697935,-119.100584596379 35.4050945039903,-119.100567940388 35.4051324845866,-35.4050945039903,-119.100567940388 35.4051324845866,-119.1005698232335.4051746229123,-119.10058625583 35.4052187928558,-119.1005698232335.4051746229123,-119.10058625583 35.4052187928558,-119.100622330944 35.4052508971182,-119.100619921668 35.4052682131054,-119.100622330944 35.4052508971182,-119.100619921668 35.4052682131054,-19.100598089049 35.4052968351787,-119.100577998689 35.4053291714179,-19.100598089049 35.4052968351787,-119.100577998689 35.4053291714179,-119.100575017479 35.4053658960572,-119.100604549953 35.405402382471,-119.100575017479 35.4053658960572,-119.100604549953 35.405402382471,-119.100637922046 35.4054192567285,-119.100664242372 35.4054234663778,-119.100637922046 35.4054192567285,-119.100664242372 35.4054234663778,-119.100695837816 35.4054166982392,-119.100720934285 35.4053849237043,-119.100695837816 35.4054166982392,-119.100720934285 35.4053849237043,-119.100753214358 35.4053632206036,-119.100782184037 35.405357556842,-119.100753214358 35.4053632206036,-119.100782184037 35.405357556842,-119.100815518089 35.4053821192651,-119.100819868035 35.4054112646836,-119.100815518089 35.4053821192651,-119.100819868035 35.4054112646836,-119.10080955272 35.4054297323306,-119.100779407259 35.4054462156383,-119.10080955272 35.4054297323306,-119.100779407259 35.4054462156383,-119.100715985448 35.4054424595886,-119.100683185361 35.405457885726,-119.100715985448 35.4054424595886,-119.100683185361 35.405457885726,-119.100686085296 35.4054773160076,-119.100676349209 35.4054947310111,-119.100686085296 35.4054773160076,-119.100676349209 35.4054947310111,-119.100632086203 35.4055119715638)))119.100632086203 35.4055119715638)))
Lesson LearnedLesson Learned Ask for your responses in Text format Ask for your responses in Text format
when you want to know what is going onwhen you want to know what is going on eWKT includes the projection informationeWKT includes the projection information
'SRID=4326;MULTIPOLYGON(((-119.5 'SRID=4326;MULTIPOLYGON(((-119.5 35.0, -119.0 35.0, -119.0 35.5, -119.5 35.0, -119.0 35.0, -119.0 35.5, -119.5 35.5, -119.5 35.0)))'35.5, -119.5 35.0)))'
asKML, asSVG, asGeoJSON (1.3.4) or asKML, asSVG, asGeoJSON (1.3.4) or asGMLasGML
SELECT asKML(the_geom) FROM watercourses WHERE gid = 17;
<MultiGeometry> <Polygon> <outerBoundaryIs> <LinearRing> <coordinates>-118.90476592907,35.4477387779291 -118.904082378315,35.4465853812531 -118.904745088997,35.4472133838697 -118.90476592907,35.4477387779291</coordinates> </LinearRing> </outerBoundaryIs> </Polygon></MultiGeometry>
Inserting a new recordInserting a new record
Projection has to matchProjection has to match
INSERT INTO firestations(station_na, the_geom) VALUES('Best LittleFire House in CA', ST_SetSRID(ST_MakePoint(-119.5, 34.5), 4326));
Select using the geometrySelect using the geometry
FOSS desktop GISFOSS desktop GIS
uDIGuDIG OpenJumpOpenJump gvSIGgvSIG QGISQGIS MapWindowsMapWindows
Distance Distance In units of the underlying coverageIn units of the underlying coverage
SELECT gid FROM waterbodies WHERE SELECT gid FROM waterbodies WHERE ST_DWithin('SRID=4326;POINTST_DWithin('SRID=4326;POINT(-119.103762382096 35.4122898449297)', (-119.103762382096 35.4122898449297)', the_geom, 0.005);the_geom, 0.005);
gidgid---------- 44 55 66 77 88
Let’s find all the parcels in Let’s find all the parcels in Agricultural ZonesAgricultural Zones
SELECT p.gid FROM parcels AS p, zoning AS z WHERE z.comb_zn LIKE 'A%' AND ST_Within(p.the_geom, z.the_geom);
Do something with the Do something with the geometry after selectiongeometry after selection
Calculating AreaCalculating Area
SELECT SELECT sum(ST_Area(z.the_geom))sum(ST_Area(z.the_geom)) FROM zoning AS z WHERE z.comb_zn FROM zoning AS z WHERE z.comb_zn LIKE 'NR%';LIKE 'NR%';
sumsum---------------------------------------- 0.01318341595515450.0131834159551545
Turn polygons into pointsTurn polygons into points
SELECT gid, asText(ST_Centroid(the_geom)) FROM waterbodies WHERE gid = 124; gid | geometry-----+------------------------------------------- 124 | POINT(-118.427120958287 35.6702299014044)
SELECT gid, asText(ST_PointOnSurface(the_geom)) FROM waterbodies WHERE gid = 124; gid | geometry-----+------------------------------------------- 124 | POINT(-118.449967685908 35.6911672413056)
Create new tables using a selectCreate new tables using a select
CREATE TABLE biglake WITH OIDS AS CREATE TABLE biglake WITH OIDS AS SELECT gid, the_geom FROM waterbodies SELECT gid, the_geom FROM waterbodies WHERE gid = 124;WHERE gid = 124;
SELECT SELECT ST_AREA(the_geom)ST_AREA(the_geom) FROM biglake; FROM biglake; st_areast_area------------------------------------------ 0.003139865912999080.00313986591299908
BufferingBuffering
Some SQL to bufferSome SQL to buffer
SELECT ST_Area(ST_Buffer(the_geom, 0.1)) FROM biglake;
st_area-------------------- 0.0718529848413709
Remember that is used to be 0.003
Spatial OperatorsSpatial Operators
Clip to the bounding boxClip to the bounding box
SELECT gid, ST_Intersection(the_geom, 'SRID=4326;MULTIPOLYGON(((-119.5 35.0, -119.0 35.0, -119.0 35.5, -119.5 35.5, -119.5 35.0)))') FROM watercourses WHERE ST_Intersects(the_geom, 'SRID=4326;MULTIPOLYGON(((-119.5 35.0, -119.0 35.0, -119.0 35.5, -119.5 35.5, -119.5 35.0)))');
Simplifying geometriesSimplifying geometries Most geometries have more information Most geometries have more information
than you can displaythan you can display There are algorithms for weeding out There are algorithms for weeding out
points points Trade-off retaining “shape” versus less Trade-off retaining “shape” versus less
pointspoints
Select ST_NPoints(the_geom) FROM watercourses WHERE gid IN (12, 14, 17); st_npoints------------ 265 68 48
SELECT ST_NPoints(ST_Simplify(the_geom, 0.00001)) FROM watercourses WHERE gid IN (12, 14, 17); st_npoints------------ 65 19 18
SELECT ST_NPoints(ST_Simplify(the_geom, 0.0001)) FROM watercourses WHERE gid IN (12, 14, 17); st_npoints------------ 18 6 7
So how do you bring this together So how do you bring this together in an appin an app
Never send 3000 pts to the browserNever send 3000 pts to the browser Putting your information in a DB gives you Putting your information in a DB gives you
way more control over what you return and way more control over what you return and displaydisplay
http://demo.decarta.com/opensearchservichttp://demo.decarta.com/opensearchservice/index.htmle/index.html
Where do you go now - PostGISWhere do you go now - PostGIS HomeHome
http://postgis.refractions.net/http://postgis.refractions.net/ Tools to use with PostGISTools to use with PostGIS
http://postgis.refractions.net/support/wiki/index.php?ToolsSupporhttp://postgis.refractions.net/support/wiki/index.php?ToolsSupportPostgistPostgis
Good support WikiGood support Wiki http://postgis.refractions.net/support/wiki/http://postgis.refractions.net/support/wiki/
Another IntroductionAnother Introduction http://www.mapbender.org/presentations/Spatial_Data_Managehttp://www.mapbender.org/presentations/Spatial_Data_Manage
ment_Arnulf_Christl/Spatial_Data_Management_Arnulf_Christl.pment_Arnulf_Christl/Spatial_Data_Management_Arnulf_Christl.pdfdf
Great OS-GEO resourcesGreat OS-GEO resources http://www.bostongis.com/http://www.bostongis.com/ PostGIS specific PostGIS specific
• http://www.bostongis.com/postgis_quickguide.bqg?outputformat=PDhttp://www.bostongis.com/postgis_quickguide.bqg?outputformat=PDFF
Nice GIS on a Stick for WindowsNice GIS on a Stick for Windowshttp://www.archaeogeek.com/blog/portable-gishttp://www.archaeogeek.com/blog/portable-gis//
Where to get DataWhere to get Data Kern CountyKern County
http://www.co.kern.ca.us/gis/downloads.asphttp://www.co.kern.ca.us/gis/downloads.asp CaliforniaCalifornia
http://gis.ca.gov/casil/http://gis.ca.gov/casil/ US Census US Census
http://www.census.gov/geo/www/tiger/http://www.census.gov/geo/www/tiger/ Some UNSome UN
http://www.grida.no/gis/index.htmhttp://www.grida.no/gis/index.htm http://www.geographynetwork.com/http://www.geographynetwork.com/ http://gos2.geodata.gov/wps/portal/goshttp://gos2.geodata.gov/wps/portal/gos Open source DataOpen source Data
http://www.fortiusone.com/http://www.fortiusone.com/ OSMOSM
• http://http://wiki.openstreetmap.org/index.php/Planet.osmwiki.openstreetmap.org/index.php/Planet.osm• http://code.google.com/p/osm2shp/http://code.google.com/p/osm2shp/
Map API to useMap API to use
OpenStreetMap - CloudMadeOpenStreetMap - CloudMade http://developer.decarta.comhttp://developer.decarta.com GoogleGoogle MicrosoftMicrosoft MapquestMapquest