You are previewing Python Geospatial Development - Third Edition.
O'Reilly logo
Python Geospatial Development - Third Edition

Book Description

Develop sophisticated mapping applications from scratch using Python 3 tools for geospatial development

About This Book

  • Build web applications based around maps and geospatial data using Python 3.x

  • Install and use various toolkits and obtain geospatial data for use in your programs

  • This practical, hands-on book will teach you all about geospatial development in Python

  • Who This Book Is For

    This book is for experienced Python developers who want to learn about geospatial concepts, obtain and work with geospatial data, solve spatial problems, and build sophisticated map-based applications using Python.

    What You Will Learn

  • Access, manipulate, and display geospatial data from within your Python programs

  • Master the core geospatial concepts of location, distance, units, projections, and datums

  • Read and write geospatial data in both vector and raster format

  • Perform complex, real-world geospatial calculations using Python

  • Store and access geospatial information in a database

  • Use points, lines, and polygons within your Python programs

  • Convert geospatial data into attractive maps using Python-based tools

  • Build complete web-based mapping applications using Python

  • In Detail

    Geospatial development links your data to locations on the surface of the Earth. Writing geospatial programs involves tasks such as grouping data by location, storing and analyzing large amounts of spatial information, performing complex geospatial calculations, and drawing colorful interactive maps. In order to do this well, you’ll need appropriate tools and techniques, as well as a thorough understanding of geospatial concepts such as map projections, datums, and coordinate systems.

    This book provides an overview of the major geospatial concepts, data sources, and toolkits. It starts by showing you how to store and access spatial data using Python, how to perform a range of spatial calculations, and how to store spatial data in a database. Further on, the book teaches you how to build your own slippy map interface within a web application, and finishes with the detailed construction of a geospatial data editor using the GeoDjango framework.

    By the end of this book, you will be able to confidently use Python to write your own geospatial applications ranging from quick, one-off utilities to sophisticated web-based applications using maps and other geospatial data.

    Style and approach

    This book is a comprehensive course in geospatial development. The concepts you need to know are presented in a hands-on fashion with example code to help you to solve real-world problems right away. Larger programs are built up step by step while guiding you through the process of building your own sophisticated mapping applications.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Python Geospatial Development Third Edition
      1. Table of Contents
      2. Python Geospatial Development Third Edition
      3. Credits
      4. About the Author
      5. About the Reviewer
        1. eBooks, discount offers, and more
          1. Why subscribe?
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Geospatial Development Using Python
        1. Python
          1. Python 3
        2. Geospatial development
        3. Applications of geospatial development
          1. Analysing geospatial data
          2. Visualizing geospatial data
          3. Creating a geospatial mash-up
        4. Recent developments
        5. Summary
      9. 2. GIS
        1. Core GIS concepts
          1. Location
          2. Distance
          3. Units
          4. Projections
            1. Cylindrical projections
            2. Conic projections
            3. Azimuthal projections
            4. The nature of map projections
          5. Coordinate systems
          6. Datums
          7. Shapes
        2. GIS data formats
        3. Working with GIS data manually
          1. Obtaining the data
          2. Installing GDAL
            1. Installing GDAL on Linux
            2. Installing GDAL on Mac OS X
            3. Installing GDAL on MS Windows
            4. Testing your GDAL installation
            5. Examining the Downloaded Shapefile
        4. Summary
      10. 3. Python Libraries for Geospatial Development
        1. Reading and writing geospatial data
          1. GDAL/OGR
          2. Installing GDAL/OGR
          3. Understanding GDAL
          4. GDAL example code
          5. Understanding OGR
          6. OGR example code
          7. GDAL/OGR documentation
        2. Dealing with projections
          1. pyproj
          2. Installing pyproj
          3. Understanding pyproj
            1. Proj
            2. Geod
          4. Example code
          5. Documentation
        3. Analyzing and manipulating Geospatial data
          1. Shapely
          2. Installing Shapely
          3. Understanding Shapely
          4. Shapely example code
          5. Shapely documentation
        4. Visualizing geospatial data
          1. Mapnik
          2. Installing Mapnik
          3. Understanding Mapnik
          4. Mapnik example code
          5. Mapnik documentation
        5. Summary
      11. 4. Sources of Geospatial Data
        1. Sources of geospatial data in vector format
          1. OpenStreetMap
            1. The OpenStreetMap data format
            2. Obtaining and using OpenStreetMap data
              1. The OpenStreetMap APIs
              2. Planet.osm
              3. Mirror sites and extracts
              4. Working with OpenStreetMap data
          2. TIGER
            1. The TIGER data format
            2. Obtaining and using TIGER data
          3. Natural Earth
            1. The Natural Earth data format
            2. Obtaining and using Natural Earth vector data
          4. The Global Self-consistent, Hierarchical, High-resolution Geography Database (GSHHG)
            1. The GSHHG data format
            2. Obtaining the GSHHG database
          5. The World Borders Dataset
            1. The World Borders Dataset data format
            2. Obtaining the World Borders Dataset
        2. Sources of geospatial data in raster format
          1. Landsat
            1. The Landsat data format
            2. Obtaining Landsat imagery
          2. Natural Earth
            1. The Natural Earth data format
            2. Obtaining and using Natural Earth raster data
          3. Global Land One-kilometer Base Elevation (GLOBE)
            1. The GLOBE data format
            2. Obtaining and using GLOBE data
          4. The National Elevation Dataset (NED)
            1. The NED data format
            2. Obtaining and using NED data
        3. Sources of other types of geospatial data
          1. The GEOnet Names Server
            1. The GEOnet Names Server data format
            2. Obtaining and using GEOnet Names Server data
          2. The Geographic Names Information System (GNIS)
            1. The GNIS data format
            2. Obtaining and using GNIS data
        4. Choosing your geospatial data source
        5. Summary
      12. 5. Working with Geospatial Data in Python
        1. Pre-requisites
        2. Working with geospatial data
          1. Task – calculate the bounding box for each country in the world
          2. Task – calculate the border between Thailand and Myanmar
          3. Task – analyze elevations using a digital elevation map
        3. Changing datums and projections
          1. Task – changing projections to combine shapefiles using geographic and UTM coordinates
          2. Task – changing the datums to allow older and newer TIGER data to be combined
        4. Performing geospatial calculations
          1. Task – identifying parks in or near urban areas
        5. Converting and standardizing units of geometry and distance
          1. Task – calculating the length of the Thai-Myanmar border
          2. Task – finding a point 132.7 kilometers west of Shoshone, California
        6. Exercises
        7. Summary
      13. 6. Spatial Databases
        1. Spatially-enabled databases
        2. Spatial indexes
        3. Introducing PostGIS
          1. Installing PostgreSQL
          2. Installing PostGIS
          3. Installing psycopg2
        4. Setting up a database
          1. Creating a Postgres user account
          2. Creating a database
          3. Allowing the user to access the database
          4. Spatially enable the database
        5. Using PostGIS
          1. PostGIS documentation
          2. Advanced PostGIS features
        6. Recommended best practices
          1. Best practice: use the database to keep track of spatial references
          2. Best practice: use the appropriate spatial reference for your data
            1. Option 1: Using GEOGRAPHY fields
            2. Option 2: Transforming features as required
            3. Option 3: Transforming features from the outset
            4. When to use unprojected coordinates
          3. Best practice: avoid on-the-fly transformations within a query
          4. Best practice: don't create geometries within a query
          5. Best practice: use spatial indexes appropriately
          6. Best practice: know the limits of your database's query optimizer
        7. Summary
      14. 7. Using Python and Mapnik to Generate Maps
        1. Introducing Mapnik
        2. Creating an example map
        3. Mapnik concepts
          1. Data sources
            1. Shapefile
            2. PostGIS
            3. Gdal
            4. MemoryDatasource
          2. Rules, filters, and styles
            1. Filters
            2. "Else" rules
            3. Styles
          3. Symbolizers
            1. Drawing points
            2. Drawing lines
            3. Drawing polygons
            4. Drawing text
            5. Drawing raster images
          4. Maps and layers
          5. Map rendering
        4. Summary
      15. 8. Working with Spatial Data
        1. About DISTAL
        2. Designing and building the database
        3. Downloading and importing the data
          1. The World Borders Dataset
          2. The GSHHG shoreline database
          3. US place names
          4. Non-US place names
        4. Implementing the DISTAL application
          1. The "select country" script
          2. The "select area" script
            1. Calculating the bounding box
            2. Calculating the map's dimensions
            3. Rendering the map image
          3. The "show results" script
            1. Identifying the clicked-on point
            2. Identifying matching place names
            3. Displaying the results
        5. Using DISTAL
        6. Summary
      16. 9. Improving the DISTAL Application
        1. Dealing with the anti-meridian line
        2. Dealing with the scale problem
        3. Performance
          1. Finding the problem
          2. Improving performance
            1. Calculating the tiled shorelines
          3. Using the tiled shorelines
          4. Analyzing the performance improvement
        4. Summary
      17. 10. Tools for Web-based Geospatial Development
        1. Tools and techniques for geospatial web development
          1. Web applications
            1. A bare-bones approach
            2. Web application stacks
            3. Web application frameworks
            4. User interface libraries
          2. Web services
            1. An example web service
            2. Map rendering using a web service
            3. Tile caching
          3. The "slippy map" stack
          4. Geospatial web protocols
        2. A closer look at three specific tools and techniques
          1. The Tile Map Service protocol
          2. OpenLayers
          3. GeoDjango
            1. Learning Django
            2. GeoDjango
        3. Summary
      18. 11. Putting It All Together – a Complete Mapping System
        1. About the ShapeEditor
        2. Designing the ShapeEditor
          1. Importing a shapefile
          2. Selecting a feature
          3. Editing a feature
          4. Exporting a shapefile
        3. Prerequisites
        4. Setting up the database
        5. Setting up the ShapeEditor project
        6. Defining the ShapeEditor's applications
        7. Creating the shared application
        8. Defining the data models
          1. The Shapefile object
          2. The Attribute object
          3. The Feature object
          4. The AttributeValue object
          5. The file
        9. Playing with the admin system
        10. Summary
      19. 12. ShapeEditor – Importing and Exporting Shapefiles
        1. Implementing the shapefile list view
        2. Importing shapefiles
          1. The Import Shapefile form
          2. Extracting the uploaded shapefile
          3. Importing the shapefile's contents
            1. Opening the shapefile
            2. Adding the Shapefile object to the database
            3. Defining the shapefile's attributes
            4. Storing the shapefile's features
            5. Storing the shapefile's attributes
          4. Cleaning up
        3. Exporting shapefiles
          1. Define the OGR shapefile
          2. Saving the features into the shapefile
          3. Saving the attributes into the shapefile
          4. Compressing the shapefile
          5. Deleting temporary files
          6. Returning the ZIP archive to the user
        4. Summary
      20. 13. ShapeEditor – Selecting and Editing Features
        1. Selecting the feature to edit
          1. Implementing the Tile Map Server
            1. Setting up the base map
            2. Tile rendering
              1. Parsing the query parameters
              2. Setting up the map
              3. Defining the base layer
              4. Defining the feature layer
              5. Rendering the map Tile
            3. Completing the Tile Map Server
          2. Using OpenLayers to display the map
          3. Intercepting mouse clicks
          4. Implementing the "Find Feature" view
        2. Editing features
        3. Adding features
        4. Deleting features
        5. Deleting shapefiles
        6. Using the ShapeEditor
        7. Further improvements and enhancements
        8. Summary
      21. Index