Getting Started with PostGIS geographic database - Lasma Sietinsone, EDINA


Published on

Presentation given as part of the DevCSI/JISC GECO Open Mapping Workshop which was held at the Electron Club, CCA, Glasgow on Thursday 25th August 2011. The event was connected to the OpenStreetMap State of the Map Scotland event.

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Getting Started with PostGIS geographic database - Lasma Sietinsone, EDINA

  1. 1. Lasma Sietinsone [email_address] Getting started with PostGIS geographic database
  2. 2. Survey <ul><li>Experience of audience: </li></ul><ul><ul><li>SQL? </li></ul></ul><ul><ul><li>RDBMS? </li></ul></ul><ul><ul><li>OS? </li></ul></ul><ul><li>This demo requires: </li></ul><ul><ul><li>SQL – basic knowledge (SELECT, UPDATE, DELETE) </li></ul></ul><ul><ul><li>RDBMS – none </li></ul></ul><ul><ul><li>OS - Windows </li></ul></ul>
  3. 3. Contents <ul><li>Theoretical background (~15 mins) </li></ul><ul><li>Demonstrator (~40 minutes): </li></ul><ul><ul><li>Install and configure PostgreSQL with PostGIS </li></ul></ul><ul><ul><li>Build example database to support location based queries </li></ul></ul>
  4. 4. What is PostGIS? <ul><li>PostGIS is a spatial extension to the PostgreSQL object-relational database that supports work with geographic data </li></ul>
  5. 5. Advantages of a DBMS <ul><li>Data Independence from application programs </li></ul><ul><li>Efficient Data Access </li></ul><ul><li>Data Integrity and Security </li></ul><ul><li>Centralized Data Administration </li></ul><ul><li>Concurrent Access and Crash Recovery </li></ul><ul><li>Reduced Application Development Time – common data access functions </li></ul>
  6. 6. PostGIS extension provides <ul><li>Geometry/geography data types for points, linestrings, polygons, curves, geometry collections, 3D geometries </li></ul><ul><li>Spatial operators such as contains @, overlaps, intersects &&, same as =, is to the left/right/below/above </li></ul><ul><li>Spatial functions </li></ul>
  7. 7. Spatial functions <ul><li>Constructors </li></ul><ul><ul><li>e.g. ST_GeomFromText(), ST_MakePoint(), ST_MakePolygon </li></ul></ul><ul><li>Output </li></ul><ul><ul><li>e.g. ST_AsGML(), ST_AsGeoJSON(), ST_AsBinary() </li></ul></ul><ul><li>Accessors : getters and setters </li></ul><ul><ul><li>e.g. ST_Transform(), ST_GeometryType(), ST_IsValid(), ST_npoints(), ST_SetDRID() </li></ul></ul><ul><li>Measurement </li></ul><ul><ul><li>e.g. ST_Distance_Sphere(), ST_Length_2D(), ST_Perimeter(), ST_Azimuth() </li></ul></ul>
  8. 8. Spatial functions (continued) <ul><li>Decomposition </li></ul><ul><ul><li>e.g. ST_Centroid(), ST_Box2D(), ST_Boundary(), ST_GeometryN(), ST_DumpRings() </li></ul></ul><ul><li>Composition </li></ul><ul><ul><li>e.g. ST_MakePoint(), ST_MakePolygon(), ST_BuildArea(), ST_Polygonize() </li></ul></ul><ul><li>Simplification </li></ul><ul><ul><li>e.g. ST_Simplify(), ST_SimplifyPreserveTopology(), ST_SnapToGrid() </li></ul></ul>
  9. 9. Common workflow <ul><li>Load/populate table </li></ul><ul><li>Add geometry column with certain dimensions and spatial reference system (SRS/SRID) </li></ul><ul><li>Register geometry column in geometry_columns table </li></ul><ul><li>Optimise performance by: </li></ul><ul><ul><li>Creating spatial index </li></ul></ul><ul><ul><li>Clustering data based on spatial index </li></ul></ul>
  10. 10. Common workflow (continued) <ul><li>View/Edit/Query data with: </li></ul><ul><ul><li>desktop GIS, e.g. QGIS, OpenJUMP, GRASS etc. </li></ul></ul><ul><ul><li>Web map servers, e.g. GeoServer, MapServer, Deegree etc. </li></ul></ul><ul><ul><li>Custom applications communicating via database connectors such as JDBC </li></ul></ul>
  11. 11. Data Loading <ul><li>Standard formats: CSV, TEXT </li></ul><ul><ul><li>$ psql --help </li></ul></ul><ul><ul><li>$ psql –h [host] –p [port] –U [user] –c “COPY [tablename] FROM [filename] WITH CSV DELIMITER ‘,’” [database] </li></ul></ul><ul><li>Utilities: </li></ul><ul><ul><li>shp2pgsql, shp2pgsql-gui: load SHP, DBF files </li></ul></ul><ul><ul><li>$ shp2pgsql --help </li></ul></ul><ul><ul><li>$ shp2pgsql [options] [shapefile schema.table | psql –h [host] –p [port] –U [user] [database] </li></ul></ul><ul><ul><li>osm2pgsql : load OSM data </li></ul></ul><ul><ul><li>$ osm2pgsql --help </li></ul></ul><ul><ul><li>$ osm2pgsql [options] planet.osm </li></ul></ul><ul><ul><li>$ osm2pgsql [options] planet.osm.{gz,bz2} </li></ul></ul><ul><ul><li>$ osm2pgsql [options] file1.osm file2.osm file3.osm </li></ul></ul>
  12. 12. Demonstrator OSM based nearest POI queries for location enabled mobile device e.g. pub? (-> food?) -> cash? -> food?
  13. 13. Install & configure PostgreSQL <ul><li>Download Postgres Plus Standard Server 9.0: </li></ul><ul><ul><li> </li></ul></ul><ul><li>Install package (database server + pgAdmin III + PostGIS) </li></ul><ul><li>Add the new directory path to your system variable path </li></ul><ul><li>Create PostGIS database template </li></ul><ul><li>Install PostGIS: </li></ul><ul><li>C:Program FilesPostgresPlus9.0SSsharecontribpostgis.sql </li></ul><ul><li>C:Program FilesPostgresPlus9.0SSsharecontribspatial_ref_sys.sql </li></ul>
  14. 14. Install & configure PostgreSQL <ul><li>Install any other contribution modules such as “hstore”: </li></ul><ul><li>C:Program FilesPostgresPlus9.0SSsharecontribhstore.sql </li></ul><ul><li>Grant permissions to use PostGIS tables to “public” </li></ul><ul><li>Create your own username </li></ul><ul><li>Create a new database, e.g. pg_intro from template_postgis </li></ul>
  15. 15. Install osm2pgsql <ul><li>Download: </li></ul><ul><li>Extract the zip to a new directory on your local disk </li></ul><ul><li>Add the new directory path to your system variable path </li></ul><ul><li>log out and back in so the new variable is active </li></ul><ul><li>Open up command prompt ie... run </li></ul><ul><li>Change directories so that you are located in the extracted osm2pgsql directory </li></ul><ul><li>now run the > osm2pgsql -h this will show you the help </li></ul><ul><li>on windows it would be important to use parameter -S or --style to locate the stylefile. Default is a unix like path </li></ul><ul><li>Example prompt> osm2pgsql -c -d gis -U postgres -W -H localhost -P 5432 N:Geo_dataOpenStreetMapOSM_xmlyourOSM_filename.bz2 </li></ul><ul><li>Wiki: </li></ul>
  16. 16. Data loading <ul><li>Download OSM data (Edinburgh): </li></ul><ul><li>,55.9126578,-3.1441498,55.9720691 </li></ul><ul><li>$ curl --location --globoff &quot;,55.9126578,-3.1441498,55.9720691&quot; -o edinburgh.osm </li></ul><ul><li>Load file: $ osm2pgsql --create --database pg_intro --username lasma --password --host localhost --port 5432 --style &quot;C:Program; --merc --hstore --slim edinburgh.osm </li></ul>
  17. 17. Create user tables <ul><li>Look at data, make a plan </li></ul><ul><li>Create user tables for OSM POIs </li></ul><ul><li>> create_amenities_tables.sql </li></ul><ul><li>Parse data </li></ul><ul><li>> get_amenities.sql </li></ul><ul><li>Create stored procedure for location queries </li></ul>
  18. 18. Desktop GIS Tools – Fun! <ul><li>View database contents </li></ul><ul><li>Create on-the-fly maps – example choropleth map with pub stats </li></ul><ul><li>QGIS – Quantum GIS </li></ul><ul><li>OPENJump </li></ul>
  19. 19. Thank you! <ul><li>Read online: </li></ul><ul><li>PostgreSQL docs ***** </li></ul><ul><li>PostGIS docs ***** </li></ul>