3

I want to convert a GeoPandas GeoDataframe to raster (.tif). I have multiple attributes in my GeoDataframe and want to create a raster using a single field from the attribute table. The underlying vector format are 16x16m square polygons, which should be represented later by a single pixel.

Up to now I haven't found any straight-forward solutions or useful tutorials, so I am looking for some helpful answers or hints of where to find practical information.

I'm using Windows 10 and Python 3.8.

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
DrSnuggles
  • 399
  • 4
  • 13
  • What have you tried? Can you provide some of your python code? See this q also: https://gis.stackexchange.com/questions/151339/rasterize-a-shapefile-with-geopandas-or-fiona-python – GISHuman Feb 03 '20 at 20:04
  • 1
    Take the centroid of each polygon and run it through scipy's griddata() using a 16m grid, then use rasterio to write it out – mikewatt Feb 03 '20 at 20:10

1 Answers1

8

You may be interested in geocube (https://github.com/corteva/geocube):

Examples: https://corteva.github.io/geocube/stable/examples/examples.html

from geocube.api.core import make_geocube

gdf = geopandas.read_file(...) out_grid= make_geocube(vector_data=gdf, measurements=["column_name"], resolution=(-16, 16)) #for most crs negative comes first in resolution out_grid["column_name"].rio.to_raster("my_rasterized_column.tif")

Salman
  • 33
  • 2
snowman2
  • 7,321
  • 12
  • 29
  • 54