Rasterize vector data using GeoPandas and GeoCube

Rasterize vector data using GeoPandas and GeoCube

In this tutorial, we are gonna convert a Polygon and Points vector data into raster format by using GeoPandas and GeoCube in python.

To get full source code for this tutorial, click here. All the related Polygon and Point data used in this tutorial are in repository.

Before rasterizing the vector, we will set up conda environment and install required python GIS packages for this task. Make sure conda is installed on your system. Use following commands to create a conda environment and to install python libraries.

When you install geocube library using conda, it will automatically install related dependencies that includes gdal, shapely, rasterio, geopandas, xarray, rioxarray etc.
And we will also install pygeos library which is used to speed up the vectorized operations in GeoPandas and Shapely.

In this tutorial, we will rasterize both Polygon and Points data.

1. Import Dependencies

Import following libraries in your code. Pandas and Geopandas will used to read csv and shapefile respectively. Geocube library will be used to rasterize the polygon and vector data.

2. Rasterize Polygons Data

We are using a shapefile for California which is a polygon dataset and stores population value for each feature. The file is in “shps/census_pops.zip” path. We are using geopandas to read this shapefile directly from zip file.
This dataset is in 4326 coordinate system. We will reproject it to 3857 projected coordinate system at line number 7 to make measurement in meters.
After that we will use make_geocube module from geocube library to rasterize the the vector. This module takes geodataframe in vector_data argument. In our case, the geodataframe is the shapefile that we read using geopandas at line number 3. The measurement parameter specifies which column to use from geodataframe to burn the values in pixel. In our case, it is pops column. The resolution parameter specifies the size of of pixel. In our case, it is 500X500 meter per pixel size.
The make_geocube returns the rioxarray object which then can be used to save the raster. At line number 18, we are saving the rasterized data.

3. Rasterize Points Data

We are having the uk accidents records for 2005-2007,2009-2011 and 2012-2014 years in csv and compressed format . Each csv file have latitude and longitude coordinates of each accident.
We are reading these csvs using pandas(line number 3,4,5) and merging them into single pandas dataframe(line number 7).
At line number 10 and 11, we changing the column type to float type of Lat and Lon column. After that, we will use geopandas to convert lat long values in Point Geometry and will create a Point type geodataframe using geopandas. See line number 14. This geodataframe is in 4326 coordinate system. We will reproject this geodataframe to 3857 projected coordinate system.
Now we have a point geodataframe of uk_accidents csvs . We will pass this point geodataframe to rasterize it. Rasterization is same as we did for Polygon. The only difference here is that we are passing a rasterize_function parameter in make_geocube. This parameter will let you decide how you want to burn the values in pixels. Here, this parameter, will calculate the number of points intersects with a given pixel of 500×500 meter size and then sum the values given in measurement parameter and finally burn this value in given pixel.

That’s it. We have successfully rasterized the both Polygon and Point vector data. Geo_Cube library makes it really easy to rasterize the vector data in Python.

Full Source code is below

I hope this tutorial will create a good foundation for you. If you want tutorials on another GIS topic or you have any queries, please send an email at contact@spatial-dev.guru.

We also offer freelancing services. Please email us at contact@spatial-dev.guru for any query.

Leave a Reply Cancel reply