Working with GeoSpatial Data — Part:1 using GDAL library
Introduction
As with most data, being able to analyse, query, transform, and visualize geospatial data makes your work easier and more useful. If you’re new to working with and using geospatial data, though, the world of geo-tools available can be overwhelming.
In this series, we’ll learn about a powerful, widely-used, and broadly applicable Free & Open Source Software (FOSS) tool in python that will empower you to do more with your data.
Objectives
The objective of this series is to :
- Introduce to most popular geospatial libraries like GDAL
- Give hands on experience working with GeoSpatial data.
Getting Started
Geospatial data can be of two forms Vector and Raster. GDAL can work with both vectors and raster formats. Although several wrappers like rasterio, fiona also exist that deal specifically with the formats.
Install GDAL
- Download Miniconda distribution for python3 from here.
- Install GDAL using
conda install gdal
Working with Rasters
- Reading TIF data
Georeferenced TIF data can confusing to mange and process. Parsing it in numpy array will give us flexibility to manipulate it. Example below will parse TIF data to array.
2. After reading it as numpy array, and then processing it as per your needs, We need to write this processed data back to georeferenced TIF. Writing numpy array as georeferenced TIF follow code below.
Note -1 You must assign outdata = None after flushing data to disk. Else you will face problem of corrupted data saving.Note -2 While writing we often take a geo-referenced dataset, do some processing then again it to same referenced coordinate system.
3. Reading/Writing TIFs larger than Memory (RAM)
There is often cases when TIFs size is so huge that it can’t be loaded in the memory at once. So to do processing of such datasets, you can refer to snippet below. This kind of approach gives us freedom to work on TBs of data.
Working with Vectors
- Reading vector data (SHP)
Here OGR is used which comes install with GDAL library by default.
Features- list containing geometry (coordinate information)
Attributes- list containing dict of attributes
SpatialRef- Coordinate system reference
Bonus
- Merging/Mosaic muliple rasters:
It is often observed that we save large data into number of small manageable files. It make dataset easy to share/upload. But If you want to put these pieces together then, mosaicing/merging is required. You can achieve it easily and steps are given below.
To merge multiple rasters, we need to simply combine all the rasters.
Note -3 list_tif = list of path of individual tifs
Let’s connect over email, write me at mainshsahu53@live.com, if you have any questions regarding Geospatial tech. We can also discuss on either of our projects.