Polygonize Raster using Rioxarray and GeoPandas

Polygonize raster using Rioxrray and GeoPandas

Rioxarray is python library which combines xarray and rasterio library for raster analysis. Xarray is open source and python packages that makes working with labelled multi dimensional array very efficient. RasterIO is open source and python package based on gdal to read/write and analyze raster data. Rioxarray extends xarray with rasterio accessor.

One of the advantage of xarray is that it can be integrated with Dask for parallel computing in local machines as well as in cluster machines. Well, there are lot of applications of rioxarray/xarray in raster domain. We will discuss those applications in another tutorials.

Raster Polygonize

In this tutorial, we will use rioxarray and geopandas to polygonize the raster the data. Code is given at the end of tutorials. Basically, we will have 4 steps to polygonize the raster data.

  1. Read raster file using rioxarray.
  2. Compute xy coordinates of centroids of every pixel.
  3. Create buffer polygon for every xy coordinates based on pixel size.
  4. Perform union on all the polygons

Lets discuss the the code that we have written to polygonize the raster. The code is given at the end. Please refer there when I mentioned any line number.

  1. Import essential python packages that is needed for this task. Import numpy, pandas, geopandas, rioxarray and shapely.
  2. Read the raster data using rioxarray and store it in dem variable. See line number 14 in below code snippet. In our case, we are reading DEM file that represent the elevation profile of a area of interest. The height and width of the raster used is 1413 and 1099 respectively. UTM projection of raster used is EPSG:26718. And pixel is size10 meter by 10 meter(10×10). It has only single band raster. Pixel value represent elevation height in meters.
  3. When you read the raster data, rioxarray will automatically compute the x-axis and y-axis coordinates of raster. Only thing we need to is create pair of xy coordinates. We will use numpy meshgrid to create the pairs. See the below image for reference and line number 16 in below code snippet.
  4. At this stage, we have centroid xy coordinates of every pixel and elevation values of every pixel in x,y and elevation variables respectively. These all variables are numpy array and they have same dimension as of raster data that is 1413x1099(height and width) These all are 2 dimensional numpy array.
  5. x,y and elevation variables are 2-dimensional array. We will use flatten method to make them 1-dimensional array. See line number 17 in below code snippet. Then, we will convert these variables to pandas frame. We will not vectorize all the pixels here. We will only vectorize those pixels which has values more than 170 meters. We will filter the newly created pandas frame based on this 170 threshold. See line number from 20-22 in below code snippet.
  6. Now we have only those pixel coordinates in pandas frame whose values are greater than 170. We will convert this pandas frame to Geodataframe based on xy coordinates. See line number 23.
  7. So far, we have GeoDataframe of xy coordinates whose geometry is of point type. But our end result should be a polygon representing the those pixels which has a values greater than 170. To convert this point data to polygon, we will create buffer square of 5 meters for every point. And we are using 5 meters buffer because pixel size is 10 meter. This will create a polygon geometries of 10 by 10 meter for every point. See line number from 23-25.
  8. We will use multiprocessing to perform union on chunks of geometries for parallel computing . Here we are using size of 10000 as chunk size. We have performed union for every 10000 geometries. And then we will perform total union on these unioned geometries and finally save then to shapefile on disk. See line number from 28-26 in below code snippet.

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

Leave a Reply Cancel reply