OpenSource GIS 2004 Ottawa, Canada Building MapServer Applications with PostGIS Chris Hodgson & Paul Ramsey Refractions Research
OpenSource GIS 2004 Ottawa, Canada Spatial Objects for PostgreSQL Store, Query, and Manipulate Spatial data objects as first-class citizens in the Database Follows the OGC Simple Features for SQL specification What is PostGIS?
OpenSource GIS 2004 Ottawa, Canada Why Use PostGIS? Fast Application Development –SQL Interface –Spatial and attribute query engine –Run functions like relate() and intersect() INSIDE the database –Leverage existing DB infrastructure –Already lots of interfaces to PostGIS
OpenSource GIS 2004 Ottawa, Canada Extension of existing Interfaces –SQL –ODBC –JDBC –Other language bindings (i.e. python, perl, php, C, C++, java,…) –WMS –WFS –Mapserver –OGR –FME –Geotools/GeoServer –PostGRASS –Lots of other Why Use PostGIS
OpenSource GIS 2004 Ottawa, Canada Why Use PostGIS? DB Goodies (things a shapefile can’t do) –Concurrent updates –Backup/recovery –Real transactions –Triggers/validation –Attach to existing information –SQL interface
OpenSource GIS 2004 Ottawa, Canada An SQL Join Joining Attribute Data to Geometry Data
Joining two tables Table with Geometry Table with Geometry and Attributes
SELECT * FROM roads LEFT JOIN roads_attr ON roads.gid = roads_attr.gid; For each gid in roads Find the corresponding gid in road_attr Combine the columns from the two tables SELECT * FROM roads, road_attr WHERE roads.gid = road_attr.gid;
OpenSource GIS 2004 Ottawa, Canada Label Uncluttering A MapServer/PostGIS Trick
Label Uncluttering
Basic idea is to group all the independent segments together. This forces MapServer to put at most one label per road, instead of one label for every segment.
OpenSource GIS 2004 Ottawa, Canada What do we do with these groups? 1. Use the GEOS union() function to construct a single line 2. Use collect() which takes a set of lines and produces a MULTILINE.
OpenSource GIS 2004 Ottawa, Canada SELECT street, collect(the_geom) FROM roads GROUP BY street;
MULTILINESTRING ( ( , , ), ( , ), ( , ), ( , , ) ) MULTILINESTRING ( ( , ), ( , ), ( , ), ( , ), ( , ) )
OpenSource GIS 2004 Ottawa, Canada
A Spatial Function “How Far is the Nearest Hospital?”
OpenSource GIS 2004 Ottawa, Canada
SELECT * FROM hospitals, roads;
OpenSource GIS 2004 Ottawa, Canada SELECT distance(hospitals.the_geom,roads.the_geom) FROM hospitals, roads;
OpenSource GIS 2004 Ottawa, Canada The groups are defined by gid, street.
SELECT road.gid, road.street, min(distance(hospitals.the_geom,r.the_geom)) as min_distance FROM roads,hospitals GROUP BY road.gid, street;
OpenSource GIS 2004 Ottawa, Canada