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 6. Raster Data Processing

Geographic information systems (GIS) are often comprised of points, lines, and polygons. These data types are called vector data. There is, however, another data type in GIS—rasters. In this chapter, you will learn the basics of working with raster data. You will learn how to:

  • Use the Geospatial Data Abstraction Library (GDAL) to load and query rasters
  • Use GDAL to modify and save rasters
  • Use GDAL to create rasters
  • Load rasters into PostgreSQL
  • Perform queries on rasters using PostgreSQL

Note

Installing GDAL can be difficult. By using virtual environments and running Anaconda, you can simplify this process by using the GUI of the environment.

Raster operations using GDAL


The GDAL library allows you to read and write both vector and raster data. To install GDAL on Windows, you will need the appropriate binaries:

Note

You can download OSGeo4W, which contains the binaries, at: https://trac.osgeo.org/osgeo4w/

When you have the binaries, you can install gdal using conda, as follows:

conda install -c conda-forge gdal

In the following sections, you will learn how to load and work with a .tif file.

Using the GDAL library to load and query rasters

Now that you have gdal installed, import it using:

from osgeo import gdal

GDAL 2 is the most recent version. If you have an older version of gdal installed, you may need to import it using the following code:

import gdal

If this is the case, you may want to look into upgrading your version of gdal. Once you have gdal imported, you can open a raster image. First, let's get an image from the web. The Earth Data Analysis Center at the University of New Mexico maintains the Resource Geographic Information...

Raster operations using PostgreSQL


In the first section of this chapter, you were able to load, display, and query rasters using gdal. In this section, you will learn how to load and query rasters using a spatial database—PostgreSQL. As you start to model your data, you will most likely hold it in a spatial database. You can leverage your database to perform the queries on your rasters.

Loading rasters into PostgreSQL

To load a raster into PostgreSQL, you can use the raster2pgsql binary. If it is not in your path, you may need to add it. You should be able to find the binary in your PostgreSQL install directory on Windows at \PostgreSQL\10\bin.

The following command should be executed from your operating system's command line. It will load the TIF you created earlier in this chapter into an existing PostgreSQL database:

>raster2pgsql -I -C -s 4326 C:\Users\Paul\Desktop\BigI.tif public.bigi | psql -U postgres -d pythonspatial

The previous command uses raster2pgsql with the -I (creates an index...

Summary


In this chapter, you learned how to use GDAL and PostgreSQL to work with raster data.

First, you learned how to use the GDAL to load and query rasters. You also learned how to use GDAL to modify and save rasters. Then, you learned how to create your own raster data. You learned how to load raster data into PostgreSQL using the raster2pgsql tool. Once in PostgreSQL, you learned how to query for metadata, attributes, values, and geometry. You learned several common functions within PostgreSQL for raster data analysis. 

While this chapter only scratched the surface of working with raster data, you should have enough knowledge now to know how to learn new techniques and methods for working with rasters. In the next chapter, you will learn how to work with vector data in PostgreSQL.

 

 

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