I have GeoPandas dataframe which has one column with code.
id geometry code
0 1 MULTIPOLYGION (((-52.65767 -13.75468, -52.65767... 1
1 2 MULTIPOLYGON (((-52.64790 -13.73459, -52.64650... 2
...
I want to rasterize the polygon based on "code" field, as described in this tutorial. In addition, I want the rasterized result to be inside defined bounding box:
[-52.6655134035483, -13.78656, -52.5453277772702, -13.6867505007449]
I would like to use GeoCube for that.
This is how the dataframe looks like when I plot it with GeoPandas:
tmp.plot(column='crop_code',legend=True)
However, when I try to rasterize it using geocube , I get the polygons without the value rasterized:
out_grid = make_geocube(
vector_data=tmp,
measurements=["code"],
output_crs="epsg:4326",
resolution=(-10,10),
fill=-9999
)
fig = plt.figure(figsize=(15, 13))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
tmp.plot(ax=ax, facecolor='None', edgecolor='r')
da_grib = xr.where(out_grid.code<-1999.0, np.nan, out_grid.code)
da_grib.plot(ax=ax, add_colorbar=False)
#ax.set_extent([112.5, 154.0, -42.116943, -9.142176])
ax.set_title("Rasterized with 0.05$^o$ Grids", fontsize=24);
As it can be seen , the result doesn't have the values of the code and basicaly looks like the empty polygons. Moreover, it is not match to the extent of the bounding box.
My end goal is to get new raster, with the polygons rasterized based on code value, in the bounding box extent. How can I do this?