Book Image

Python Geospatial Development - Third Edition

By : Erik Westra
Book Image

Python Geospatial Development - Third Edition

By: Erik Westra

Overview of this book

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.
Table of Contents (20 chapters)
Python Geospatial Development Third Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

With the increasing use of map-based web sites and spatially aware devices and applications, geospatial development is a rapidly growing area. As a Python developer, you can't afford to be left behind. In today's location-aware world, every Python developer can benefit from understanding geospatial concepts and development techniques.

Working with geospatial data can get complicated because you are dealing with mathematical models of the earth's surface. Since Python is a powerful programming language with many high-level toolkits, it is ideally suited to geospatial development. This book will familiarize you with the Python tools required for geospatial development. It walks you through the key geospatial concepts of location, distance, units, projections, datums, and geospatial data formats. We will then examine a number of Python libraries and use these with freely available geospatial data to accomplish a variety of tasks. The book provides an in-depth look at storing spatial data in a database and how you can use spatial databases as tools to solve a range of geospatial problems.

It goes into the details of generating maps using the Mapnik map-rendering toolkit and helps you build a sophisticated web-based geospatial map-editing application using GeoDjango, Mapnik, and PostGIS. By the end of the book, you will be able to integrate spatial features into your applications and build complete mapping applications from scratch.

This book is a hands-on tutorial, teaching you how to access, manipulate, and display geospatial data efficiently using a range of Python tools for GIS development.

What this book covers

Chapter 1, Geospatial Development Using Python, provides an overview of the Python programming language and the concepts behind geospatial development. Major use cases of geospatial development and recent and upcoming developments in the field are also covered.

Chapter 2, GIS, introduces the core concepts of location, distance, units, projections, shapes, datums, and geospatial data formats, before discussing the process of working with geospatial data by hand.

Chapter 3, Python Libraries for Geospatial Development, explores the major Python libraries available for geospatial development, including the available features, how to install them, the major concepts you need to understand about the libraries, and how they can be used.

Chapter 4, Sources of Geospatial Data, investigates the major sources of freely available geospatial data, what information is available, the data format used, and how to import the data once you have downloaded it.

Chapter 5, Working with Geospatial Data in Python, uses the libraries introduced earlier to perform various tasks using geospatial data, including changing projections, importing and exporting data, converting and standardizing units of geometry and distance, and performing geospatial calculations.

Chapter 6, Spatial Databases, introduces the concepts behind spatial databases before looking in detail at the PostGIS spatially enabled database and how to install and use it from a Python program.

Chapter 7, Using Python and Mapnik to Produce Maps, provides a detailed look at the Mapnik map-generation toolkit and how to use it to produce a variety of maps.

Chapter 8, Working with Spatial Data, works through the design and implementation of a complete geospatial application called DISTAL, using freely available geospatial data stored in a spatial database.

Chapter 9, Improving the DISTAL Application, improves the application written in the previous chapter to solve various usability and performance issues.

Chapter 10, Tools for Web-based Geospatial Development, examines the concepts of web application frameworks, web services, JavaScript UI libraries, and slippy maps. It introduces a number of standard web protocols used by geospatial applications and finishes with a survey of the tools and techniques that will be used to build the complete mapping application in the final three chapters of this book.

Chapter 11, Putting it all Together – a Complete Mapping Application, introduces ShapeEditor, a complete and sophisticated web application built using PostGIS, Mapnik, and GeoDjango. We start by designing the overall application, and we then build the ShapeEditor's database models.

Chapter 12, ShapeEditor – Importing and Exporting Shapefiles, continues with the implementation of the ShapeEditor system, concentrating on displaying a list of imported shapefiles, along with logic for importing and exporting shapefiles via a web browser.

Chapter 13, ShapeEditor – Selecting and Editing Features, concludes the implementation of the ShapeEditor, adding logic to let the user select and edit features within an imported shapefile. This involves the creation of a custom tile map server and the use of the OpenLayers JavaScript library to display and interact with geospatial data.

What you need for this book

The third edition of this book has been extended to support Python 3, though you can continue to use Python 2 if you wish to. You will also need to download and install the following tools and libraries, though full instructions are given in the relevant sections of this book:

  • GDAL/OGR

  • GEOS

  • Shapely

  • Proj

  • pyproj

  • PostgreSQL

  • PostGIS

  • pyscopg2

  • Mapnik

  • Django

Who this book is for

This book is aimed at experienced Python developers who want to get up to speed with open source geospatial tools and techniques in order to build their own geospatial applications or integrate geospatial technology into their existing Python programs.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "The dataset, an instance of gdal.Dataset, represents a file containing raster-format data."

A block of code is set as follows:

import pyproj

lat1,long1 = (37.8101274,-122.4104622)
lat2,long2 = (37.80237485,-122.405832766082)

geod = pyproj.Geod(ellps="WGS84")
angle1,angle2,distance = geod.inv(long1, lat1, long2, lat2)

print("Distance is {:0.2f} meters".format(distance))

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

for value in values:
    if value != band.GetNoDataValue():
        try:
            histogram[value] += 1
        except KeyError:
            histogram[value] = 1

Any command-line input or output is written as follows:

% python calcBoundingBoxes.py
Afghanistan (AFG) lat=29.4061..38.4721, long=60.5042..74.9157
Albania (ALB) lat=39.6447..42.6619, long=19.2825..21.0542
Algeria (DZA) lat=18.9764..37.0914, long=-8.6672..11.9865
...

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Click on the Download Domestic Names hyperlink".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Python-Geospatial-Development-Third-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.