PostGIS Cookbook Over 80 task-based recipes to store, organize, manipulate, and analyze spatial data in a PostGIS database Paolo Corti Thomas J Kraft Stephen Vincent Mather Bborie Park PUBLISHING open source I community experience distilled BIRMINGHAM - MUMBAI
Preface 1 Chapter 1: Moving Data In and Out of PostGIS 7 Introduction 7 Importing nonspatial tabular data (CSV) using PostGIS functions 7 Importing nonspatial tabular data (CSV) using GDAL 12 Importing shapefiles with shp2pgsql 17 Importing and exporting data with the ogr2ogr GDAL command 21 Handling batch importing and exporting of datasets 25 Exporting data to the shapefile with the pgsql2shp PostGIS command 33 Importing OpenStreetMap data with the osm2pgsql command 34 Importing raster data with the raster2pgsql PostGIS command 39 Importing multiple rasters at a time 45 Exporting rasters with the gdai_translate and gdalwarp GDAL commands 51 Chapter 2: Structures that Work 55 Introduction 55 Using geospatial views 56 Using triggers to populate a geometry column 58 Structuring spatial data with table inheritance 62 Extending inheritance - table partitioning 67 Normalizing imports 71 Normalizing internal overlays 76 Using polygon overlays for proportional census estimates 80
Chapter 3: Working with Vector Data - The Basics 85 Introduction 85 Working with GPS data 86 Fixing invalid geometries 92 GIS analysis with spatial joins 96 Simplifying geometries 101 Measuring distances 107 Merging polygons using a common attribute 110 Computing intersections 112 Clipping geometries to deploy data 116 Simplifying geometries with PostGIS topology 120 Chapter 4: Working with Vector Data - Advanced Recipes 127 Introduction 127 Improving proximity filtering with KNN 128 Improving proximity filtering with KNN - advanced 132 Rotating geometries 137 Improving ST_Polygonize 140 Translating, scaling, and rotating geometries - advanced 142 Generating detailed building footprints from LiDAR 148 Using external scripts to embed new 152 functionality in order to calculate a Voronoi diagram 152 Using external scripts to embed other 156 libraries in order to calculate a Voronoi 156 diagram - advanced 156 Chapter 5: Working with Raster Data 173 Introduction 173 Getting and loading rasters 174 Working with basic raster information and analysis 177 Performing simple map-algebra operations 182 Combining geometries with rasters for analysis 187 Converting between rasters and geometries 189 Processing and loading rasters with GDAL VRT 194 Warping and resampling rasters 197 Performing advanced map-algebra operations 200 Executing DEM operations 206 Sharing and visualizing rasters through SQL 209 -DD
Chapter 6: Working with pgrouting 215 Introduction 215 Startup - Dijkstra routing 215 Loading data from OpenStreetMap and finding the shortest path using A* 220 Driving distance/service area calculation 223 Calculating demographics using driving distance 229 Extracting the centerlines of polygons 232 Chapter 7: Into the Nth Dimension 241 Introduction 241 Importing LIDAR data 242 Performing 3D queries on a LIDAR point cloud 245 Constructing and serving buildings 2.5 D 247 Using ST_Extrude to extrude building footprints 256 Creating arbitrary 3D objects for PostGIS 257 Exporting models as X3D for the Web 261 Reconstructing Unmanned Aerial Vehicle (UAV) image footprints with PostGIS 3D 265 UAV photogrammetry in PostGIS - point cloud 272 UAV photogrammetry in PostGIS - orthorectification 274 UAV photogrammetry in PostGIS - DSM creation 279 Chapter 8: PostGIS Programming 281 Introduction 281 Writing PostGIS vector data with Psycopg 284 Writing PostGIS vector data with OGR Python bindings 291 Writing PostGIS functions with PL/Python 297 Geocoding and reverse-geocoding using the GeoNames datasets 302 Geocoding using the OSM datasets with trigrams 306 Geocoding with geopy and PL/Python 312 Importing netcdf datasets with Python and GDAL 316 Chapter 9: PostGIS and the Web 325 Introduction 325 Creating WMS and WFS services with MapServer 326 Creating WMS and WFS services with GeoServer 339 Creating a WMS Time with MapServer 352 Consuming WMS services with OpenLayers 359 Consuming WMS services with Leaflet 365
Consuming WFS-T services with OpenLayers Developing web applications with GeoDjango - part 1 Developing web applications with GeoDjango - part 2 369 375 386 Chapter 10: Maintenance r Optimization, and Performance Tuning 393 Introduction 393 Organizing the database 394 Setting up the correct data privilege mechanism 397 Backing up the database 403 Using indexes 405 Clustering for efficiency 409 Optimizing SQL queries 410 Migrating a PostGIS database to a different server 421 Replicating a PostGIS database with streaming replication 423 Chapter 11: Using Desktop Clients 429 Introduction 429 Adding PostGIS layers - QGIS 430 Using the Database Manager plugin - QGIS 434 Adding PostGIS layers - OpenJUMP GIS 443 Running database queries - OpenJUMP GIS 449 Adding PostGIS layers - gvsig 453 Adding PostGIS layers - udig 457 Index 461-01}