Python Geospatial Development
Total Page:16
File Type:pdf, Size:1020Kb
Python Geospatial Development Build a complete and sophisticated mapping application from scratch using Python tools for GIS development Erik Westra BIRMINGHAM - MUMBAI Python Geospatial Development Copyright © 2010 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: December 2010 Production Reference: 1071210 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849511-54-4 www.packtpub.com Cover Image by Asher Wishkerman ([email protected]) Credits Author Editorial Team Leader Erik Westra Mithun Sehgal Reviewers Project Team Leader Tomi Juhola Priya Mukherji Silas Toms Project Coordinator Acquisition Editor Jovita Pinto Steven Wilding Proofreader Development Editor Jonathan Todd Hyacintha D'Souza Graphics Technical Editor Nilesh R. Mohite Kartikey Pandey Production Coordinator Indexers Kruthika Bangera Hemangini Bari Cover Work Tejal Daruwale Kruthika Bangera About the Author Erik Westra has been a professional software developer for over 25 years, and has worked almost exclusively in Python for the past decade. Erik's early interest in graphical user-interface design led to the development of one of the most advanced urgent courier dispatch systems used by messenger and courier companies worldwide. In recent years, Erik has been involved in the design and implementation of systems matching seekers and providers of goods and services across a range of geographical areas. This work has included the creation of real-time geocoders and map-based views of constantly changing data. Erik is based in New Zealand, and works for companies worldwide. "For Ruth, The love of my life." About the Reviewers Tomi Juhola is a software development professional from Finland. He has a wide range of development experience from embedded systems to modern distributed enterprise systems in various roles, such as tester, developer, consultant, and trainer. Currently, he works in a company called Lindorff and shares this time between development lead duties and helping other projects to adopt Scrum and agile methodologies. He likes to spend his free time with new and interesting development languages and frameworks. Silas Toms is a GIS Analyst for ICF International, working at the San Francisco and San Jose offices. His undergraduate degree is in Geography (from Humboldt State University), and he is currently finishing a thesis for an MS in GIS at San Francisco State University. He has been a GIS professional for four years, working with many local and regional governments before taking his current position. Python experience was gained through classes at SFSU and professional experience. This is the first book he has helped review. I would like to thank everyone at Packt Publishing for allowing me to help review this book and putting up with my ever-shifting schedule. I would also like to thank my family for being supportive in my quest to master this somewhat esoteric field, and for never asking if I am going to teach with this degree. www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read, and search across Packt's entire library of books. Why Subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface 1 Chapter 1: Geo-Spatial Development Using Python 7 Python 7 Geo-spatial development 9 Applications of geo-spatial development 11 Analyzing geo-spatial data 12 Visualizing geo-spatial data 13 Creating a geo-spatial mash-up 16 Recent developments 17 Summary 19 Chapter 2: GIS 21 Core GIS concepts 21 Location 22 Distance 25 Units 27 Projections 28 Cylindrical projections 29 Conic projections 31 Azimuthal projections 31 The nature of map projections 32 Coordinate systems 32 Datums 35 Shapes 36 GIS data formats 37 Working with GIS data manually 39 Summary 46 Table of Contents Chapter 3: Python Libraries for Geo-Spatial Development 47 Reading and writing geo-spatial data 47 GDAL/OGR 48 GDAL design 48 GDAL example code 50 OGR design 51 OGR example code 52 Documentation 53 Availability 53 Dealing with projections 54 pyproj 54 Design 54 Proj 55 Geod 56 Example code 57 Documentation 58 Availability 58 Analyzing and manipulating geo-spatial data 59 Shapely 59 Design 60 Example code 61 Documentation 62 Availability 62 Visualizing geo-spatial data 63 Mapnik 63 Design 64 Example code 66 Documentation 67 Availability 68 Summary 68 Chapter 4: Sources of Geo-Spatial Data 71 Sources of geo-spatial data in vector format 72 OpenStreetMap 72 Data format 73 Obtaining and using OpenStreetMap data 74 TIGER 76 Data format 77 Obtaining and using TIGER data 78 Digital Chart of the World 79 Data format 80 Available layers 80 Obtaining and using DCW data 80 [ ii ] Table of Contents GSHHS 82 Data format 83 Obtaining the GSHHS database 84 World Borders Dataset 84 Data format 85 Obtaining the World Borders Dataset 85 Sources of geo-spatial data in raster format 85 Landsat 86 Data format 86 Obtaining Landsat imagery 87 GLOBE 90 Data format 90 Obtaining and using GLOBE data 91 National Elevation Dataset 92 Data format 92 Obtaining and using NED data 93 Sources of other types of geo-spatial data 94 GEOnet Names Server 94 Data format 95 Obtaining and using GEOnet Names Server data 95 GNIS 96 Data format 97 Obtaining and using GNIS data 97 Summary 98 Chapter 5: Working with Geo-Spatial Data in Python 101 Prerequisites 101 Reading and writing geo-spatial data 102 Task: Calculate the bounding box for each country in the world 102 Task: Save the country bounding boxes into a Shapefile 104 Task: Analyze height data using a digital elevation map 108 Changing datums and projections 115 Task: Change projections to combine Shapefiles using geographic and UTM coordinates 115 Task: Change datums to allow older and newer TIGER data to be combined 119 Representing and storing geo-spatial data 122 Task: Calculate the border between Thailand and Myanmar 123 Task: Save geometries into a text file 126 Working with Shapely geometries 127 Task: Identify parks in or near urban areas 128 Converting and standardizing units of geometry and distance 132 Task: Calculate the length of the Thai-Myanmar border 133 Task: Find a point 132.7 kilometers west of Soshone, California 139 [ iii ] Table of Contents Exercises 141 Summary 143 Chapter 6: GIS in the Database 145 Spatially-enabled databases 145 Spatial indexes 146 Open source spatially-enabled databases 149 MySQL 149 PostGIS 152 Installing and configuring PostGIS 152 Using PostGIS 155 Documentation 157 Advanced PostGIS features 157 SpatiaLite 158 Installing SpatiaLite 158 Installing pysqlite 159 Accessing SpatiaLite from Python 160 Documentation 160 Using SpatiaLite 161 SpatiaLite capabilities 163 Commercial spatially-enabled databases 164 Oracle 164 MS SQL Server 165 Recommended best practices 165 Use the database to keep track of spatial references 166 Use the appropriate spatial reference for your data 168 Option 1: Use a database that supports geographies 169 Option 2: Transform features as required 169 Option 3: Transform features from the outset 169 When to use unprojected coordinates 170 Avoid on-the-fly transformations within a query 170 Don't create geometries within a query 171 Use spatial indexes appropriately 172 Know the limits of your database's query optimizer 173 MySQL 174 PostGIS 175 SpatiaLite 177 Working with geo-spatial databases using Python 178 Prerequisites 179 Working with MySQL 179 Working with PostGIS 182 Working with SpatiaLite 184 Speed comparisons 188 Summary 189 [ iv ] Table of Contents Chapter 7: Working with Spatial Data 191 About DISTAL 191 Designing and building the database 195 Downloading the data 199 World Borders Dataset