Using PostGIS To Add Some Spatial Flavor To Your Application
- PostGIS adds spatial capabilities like points, lines, polygons, and functions like area, distance to PostgreSQL. It allows spatial queries and analysis. - To install PostGIS, you need PostgreSQL and libraries like Proj and GEOS. Packages are available for many platforms. - With PostGIS, you can import spatial data like shapefiles, perform queries using spatial filters and functions, simplify geometries, and more to build mapping and location-based applications.
Using PostGIS To Add Some Spatial Flavor To Your Application
1.
Using PostGIS toadd some spatial flavor to your applications Steven Citron-Pousty Developer Evangelist deCarta
2.
Agenda What isPostGIS Installing Getting data and importing Using in an application ASK QUESTIONS ALL ALONG
3.
Assumptions about youKnow PostgreSQL and SQL New to spatial analysis and PostGIS Mainly using a commercial mapping service for your base layers Interested in writing cool applications This will only be a short shallow introduction
4.
5.
What is PostGISPostGIS adds support for geographic objects and geographic functions to PostgreSQL Points, Lines, Polygon, and Linear References Spatial function such as area, near, overlap, within, clip, and simplify
6.
Competitive Offerings MySQL– doesn’t really implement all the functions and it only deals with bounding boxes SQLServer – only in SQLServer2008 but supported on all editions Oracle Spatial – full featured and includes a broad range of client libraries DB2 and Sybase and Informix and Ingres ESRI’s SDE can use PostGIS as a DB
7.
Installing Other librariesyou want Proj4.5 for projection GEOS 3.0 for spatial operations Windows – use the “application stack builder” There are RPMs on the PostGIS site – not always the latest You can use Synaptic for Ubuntu – not always the latest Build is really simple, as long as you have required libraries installed
8.
Other Steps Addthe objects and functions from the provided SQL files Best practice is to make these into a template for future databases From there making DBs is easy cheesy
9.
Two more thingsyou need to understand Almost there – I promise
More Projection GoodnessEPSG (European Petroleum Survey Group) has a number for most projections and datum combinations deCarta, and those other no names, expect geographic coordinates with WGS84 datum EPSG:4326 Geographic coordinates system with WGS84 datum. Degrees are not constant distance as you move North and South Not very accurate for distance and area calcs except over small areas You want to use projected layers (or project on the fly) for area and distance calcs
15.
Projecting using ogr2ogrAssuming there is a .prj file you need to find your output projection http://Spatialreference.org ogr2ogr -t_srs EPSG:4326 -f "ESRI Shapefile" water_bodies_gcs.shp water_bodies.shp
16.
Now let’s makesome SQL Shp2pgsql is a command-line utility to take a shapefile to a SQL file The geometry field is called the_geom shp2pgsql.exe -s 4326 -I C:\data\water_bodies_gcs.shp waterbodies > C:\data\waterbodies.sql
17.
Y’all know whatto do with SQL psql -q -U postgres -f C:\data\waterbodies.sql kerncounty With lots-o-features you want to use –q option to prevent spamming your terminal
18.
19.
Let’s get thisparty started SELECT gid, the_geom FROM waterbodies WHERE gid = 2; gid | the_geom -----+------------------------------------------------------------------------------------------------------------------------ 2 | 106000020E6100000010000000103000000010000002E00000002CB8FC170C65DC06200F8D0E7B341408625910970C65DC0DE898BCAE7B3414053748B1A6FC65DC0199092E7E6B34140576BAFCB6EC65DC07815C607E6B34140926B64A66EC65DC0D0D11854E4B34140CA783CC66EC65DC091477CD7E2B34140FD8B372B6FC65DC02D75F091E1B341400748F1756FC65DC02C1C7C06E0B34140A0CBF8846FC65DC01B057AC2DEB34140BD6BDE586FC65DC036C25B1ADEB34140E73E8FBC6EC65DC051B6DE2DDDB3414037EFA3B86DC65DC0A6E75E4DDCB34140EECF5AF16CC65DC007312311DBB341405304E1D16CC65DC0C653541FDAB34140B352AB826CC65DC06800DD22D9B34140FDECCB8C6CC65DC0A8D8188ED7B3414028DA62606DC65DC03020AA9DD5B341401345D79D6EC65DC045CF8743D5B3414039BFF5A56FC65DC071F2D61AD6B34140D4391F4E70C65DC01F041E4CD7B34140FD50463A70C65DC0534507A5D8B3414087FB5FFA6FC65DC0C436FF22DAB3414092BF83B46FC65DC05EEC9961DBB34140456F69BC6FC65DC0E74915C3DCB34140DCCD550170C65DC022859B35DEB34140A929A59870C65DC014E6EA42DFB34140D7388A8E70C65DC0B2B22CD4DFB341405EA0F73270C65DC0E92146C4E0B341402EC4B3DE6FC65DC066AD87D3E1B3414083B732D26FC65DC0583D9907E3B3414017F8104E70C65DC07C37AB39E4B341404DFB09DA70C65DC0AB6838C7E4B341404E376F4871C65DC0D88F88EAE4B34140E290F4CC71C65DC04318C2B1E4B34140B2B1373672C65DC001CD36A7E3B3414094289CBD72C65DC032BF27F1E2B34140551D1E3773C65DC053E9A4C1E2B34140B247EEC273C65DC03C50B08FE3B34140EFFF2CD573C65DC0849F2D84E4B34140BB03E9A973C65DC0D397181FE5B341406292782B73C65DC0103A5EA9E5B34140B2EB752172C65DC0E72BDC89E5B341407818E39771C65DC0978C430BE6B34140AAE00CA471C65DC0E6C241AEE6B341405DD5367B71C65DC050325840E7B3414002CB8FC170C65DC06200F8D0E7B34140 WTF!
Lesson Learned Askfor your responses in Text format when you want to know what is going on eWKT includes the projection information 'SRID=4326;MULTIPOLYGON(((-119.5 35.0, -119.0 35.0, -119.0 35.5, -119.5 35.5, -119.5 35.0)))' asKML, asSVG, or asGML SELECT asKML(the_geom) FROM watercourses WHERE gid = 17;
Inserting a newrecord Projection 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) );
Distance Inunits of the underlying coverage SELECT gid FROM waterbodies WHERE ST_DWithin('SRID=4326;POINT (-119.103762382096 35.4122898449297)', the_geom, 0.005); gid ----- 4 5 6 7 8
27.
Let’s find allthe parcels in Agricultural 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);
Calculating Area SELECT sum(ST_Area(z.the_geom)) FROM zoning AS z WHERE z.comb_zn LIKE 'NR%'; sum -------------------- 0.0131834159551545
31.
Turn polygons intopoints 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)
32.
Create new tablesusing a select CREATE TABLE biglake WITH OIDS AS SELECT gid, the_geom FROM waterbodies WHERE gid = 124; SELECT ST_AREA(the_geom) FROM biglake; st_area --------------------- 0.00313986591299908
Some SQL tobuffer SELECT ST_Area(ST_Buffer(the_geom, 0.1)) FROM biglake; st_area -------------------- 0.0718529848413709 Remember that is used to be 0.003
Simplifying geometries Mostgeometries have more information than you can display There are algorithms for weeding out points Trade-off retaining “shape” versus less points
So how doyou bring this together in an app Never send 3000 pts to the browser Putting your information in a DB gives you way more control over what you return and display http://demo.decarta.com/opensearchservice/index.html
43.
Where do yougo now - PostGIS Home http://postgis.refractions.net/ Tools to use with PostGIS http://postgis.refractions.net/support/wiki/index.php?ToolsSupportPostgis Good support Wiki http://postgis.refractions.net/support/wiki/ Another Introduction http://www.mapbender.org/presentations/Spatial_Data_Management_Arnulf_Christl/Spatial_Data_Management_Arnulf_Christl.pdf Great OS-GEO resources http://www.bostongis.com/ PostGIS specific http://www.bostongis.com/postgis_quickguide.bqg?outputformat=PDF Nice GIS on a Stick for Windows http://www.archaeogeek.com/blog/portable-gis /
44.
Where to getData Kern County http://www.co.kern.ca.us/gis/downloads.asp California http://gis.ca.gov/casil/ US Census http://www.census.gov/geo/www/tiger/ Some UN http://www.grida.no/gis/index.htm http://www.geographynetwork.com/ http://gos2.geodata.gov/wps/portal/gos Open source Data http://www.fortiusone.com/ OSM http:// wiki.openstreetmap.org/index.php/Planet.osm http://code.google.com/p/osm2shp/
45.
Map API touse http://developer.decarta.com Free to develop – sign up and be mapping and mashing within 12 hours If you want a web page with JavaScript look at the JavaScript API If you want to use Web Services then look at the web services area on the left