Reader small image

You're reading from  Mastering Geospatial Analysis with Python

Product typeBook
Published inApr 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788293334
Edition1st Edition
Languages
Right arrow
Authors (3):
Silas Toms
Silas Toms
author image
Silas Toms

Silas Toms is a long-time geospatial professional and author who has previously published ArcPy and ArcGIS and Mastering Geospatial Analysis with Python. His career highlights include developing the real-time common operational picture used at Super Bowl 50, building geospatial software for autonomous cars, designing computer vision for next-gen insurance, and developing mapping systems for Zillow. He now works at Volta Charging, predicting the future of electric vehicle adoption and electric charging infrastructure.
Read more about Silas Toms

Paul Crickard
Paul Crickard
author image
Paul Crickard

Paul Crickard authored a book on the Leaflet JavaScript module. He has been programming for over 15 years and has focused on GIS and geospatial programming for 7 years. He spent 3 years working as a planner at an architecture firm, where he combined GIS with Building Information Modeling (BIM) and CAD. Currently, he is the CIO at the 2nd Judicial District Attorney's Office in New Mexico.
Read more about Paul Crickard

Eric van Rees
Eric van Rees
author image
Eric van Rees

Eric van Rees was first introduced to Geographical Information Systems (GIS) when studying Human Geography in the Netherlands. For 9 years, he was the editor-in-chief of GeoInformatics, an international GIS, surveying, and mapping publication and a contributing editor of GIS Magazine. During that tenure, he visited many geospatial user conferences, trade fairs, and industry meetings. He focuses on producing technical content, such as software tutorials, tech blogs, and innovative new use cases in the mapping industry.
Read more about Eric van Rees

View More author details
Right arrow

Chapter 5. Vector Data Analysis

This chapter will cover geospatial analysis and processing of vector data. The following three Python libraries will be covered—Shapely, OGR, and GeoPandas. The reader will learn how to use these Python libraries to perform geospatial analysis, including the writing of basic and advanced analysis scripts.

Each library is covered separately, with an overview of its data structures, methods, and classes where appropriate. We'll discuss the best use cases for each library and how to use them together for geospatial workflows. Short example scripts illustrate how to perform the basic geographical analysis. The GeoPandas library enables more complex functionality for doing data science tasks and incorporating geospatial analysis.

In this chapter, we'll cover the following topics:

  • Reading and writing vector data
  • Creating and manipulating vector data
  • Visualizing (plotting) vector data on a map
  • Working with map projections and reproject data
  • Performing spatial operations...

OGR Simple Features Library


OGR Simple Features Library (part of the Geospatial Data Abstraction Library (GDAL)) offers a set of tools for dealing with vector data. Although both GDAL and OGR are now more integrated than they used to be, we can still divide GDAL between a vector part (OGR) and a raster part (GDAL). While OGR was written in C++ and the documentation is also in C++, with Python bindings we can access all of GDAL's functionality using Python.

We can distinguish the following components of OGR:

  • OGR batch commands for describing and processing vector data
  • ogrmerge, an instant Python script for merging multiple vector data files
  • The OGR library itself

We'll briefly cover these components first, before moving on to some examples of how to use all three.

OGR batch commands

OGR offers a series of batch commands that can be used to describe and convert existing geospatial vector data. We've already mentioned two of them, ogrinfo and ogr2ogr, in Chapter 4, Data Types, Storage, and Conversion...

Shapely and Fiona


The Shapely and Fiona libraries have been introduced in Chapter 2Introduction to Geospatial Code Libraries, in the sections Shapely and Fiona. It makes sense to cover both of them together, as Shapely depends on other libraries for reading and writing files and Fiona fits the bill. As we'll see in the examples, we can use Fiona to open and read files and then pass geometry data to Shapely objects.

Shapely objects and classes

The Shapely library is used for creating and manipulating 2D vector data without the need for a spatial database. Not only does it do away with a database, it also does away with projections and data formats, focusing on geometry only. The strength of Shapely is that it uses easily-readable syntax to create a variety of geometries that can be used for geometric operations.

With the aid of other Python packages, these geometries and the results of geometric operations can be written to a vector file format and projected if necessary—we'll cover examples...

GeoPandas


GeoPandas has been introduced in the GeoPandas section of  Chapter 2Introduction to Geospatial Code Libraries, where its data structures and methods have also been covered.

Geospatial analysis with GeoPandas

GeoPandas was created to offer data to scientists who want to work with spatial data similar to pandas, and this means giving access to geospatial attribute data through data structures not available through pandas. Combine this with a set of geometric operations, data overlay capabilities, geocoding and plotting capabilities and you have an idea of this library's capabilities. In the examples mentioned as we proceed, we'll cover GeoPandas' plotting methods, explain how to access and subset spatial data, and provide a typical workflow for doing geospatial analysis with GeoPandas, where data processing is an important condition for being able to analyze and interpret the data correctly.

Let's have a look at a few code examples of GeoPandas.

Selecting and plotting geometry data...

Summary


This chapter covered three Python libraries for working with vector data—OGR, Shapely, and GeoPandas. In particular, we showed how to use all three for doing geospatial analysis and processing. Each library was covered separately, with their classes, methods, data structures and popular use cases. Short example scripts showed how to get started doing data processing and analysis. Taken as a whole, the reader now knows how to use each library separately, as well as how to combine all three for doing the following tasks:

  • Reading and writing vector data
  • Creating and manipulating vector data
  • Plotting vector data
  • Working with map projections
  • Performing spatial operations
  • Working with vector geometries and attribute data in tabular form
  • Presenting and analyzing the data to answer questions with a spatial component

The next chapter discusses raster data processing and how to use the GDAL and Rasterio libraries. Using these libraries, the reader will learn how to perform raster-based geospatial...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Geospatial Analysis with Python
Published in: Apr 2018Publisher: PacktISBN-13: 9781788293334
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Silas Toms

Silas Toms is a long-time geospatial professional and author who has previously published ArcPy and ArcGIS and Mastering Geospatial Analysis with Python. His career highlights include developing the real-time common operational picture used at Super Bowl 50, building geospatial software for autonomous cars, designing computer vision for next-gen insurance, and developing mapping systems for Zillow. He now works at Volta Charging, predicting the future of electric vehicle adoption and electric charging infrastructure.
Read more about Silas Toms

author image
Paul Crickard

Paul Crickard authored a book on the Leaflet JavaScript module. He has been programming for over 15 years and has focused on GIS and geospatial programming for 7 years. He spent 3 years working as a planner at an architecture firm, where he combined GIS with Building Information Modeling (BIM) and CAD. Currently, he is the CIO at the 2nd Judicial District Attorney's Office in New Mexico.
Read more about Paul Crickard

author image
Eric van Rees

Eric van Rees was first introduced to Geographical Information Systems (GIS) when studying Human Geography in the Netherlands. For 9 years, he was the editor-in-chief of GeoInformatics, an international GIS, surveying, and mapping publication and a contributing editor of GIS Magazine. During that tenure, he visited many geospatial user conferences, trade fairs, and industry meetings. He focuses on producing technical content, such as software tutorials, tech blogs, and innovative new use cases in the mapping industry.
Read more about Eric van Rees