Create GeoTiff using Datashader

I currently use datashader to create a tif file from an image object that is created by rasterizing geometries. I would like to create a GeoTiff instead with proper bounding boxes , bands and CRS information associated with it . Can I do this with Datashader?

You could look at the rioxarray project, which explains how to save an xarray object (which is what Datashader outputs) as a GeoTIFF.

1 Like

Thanks Phillip this was very helpful. Would you happen to know how to set proper extent for the raster created in data shader , the range value for the canvas plot just restricts the geometry to be included to that range but does not actually create a raster of the required extent.

This would be very surprising to me and would indicate a bug.

Hi Phillip, it works as expected, i guess it was a bug in my code. Thanks for your help. I now have an automated process to create raster files from geometries using datashaders and rasterio.

Thanks,
Sandhya

3 Likes

Can you share your code @raghavansandhya?

Can you share your code?

I am here to add a sample code as a reference,

import geopandas as gpd
import datashader as ds, pandas as pd, colorcet as cc


gdf = gpd.GeoDataFrame(data,geometry=gpd.points_from_xy(data.lon,data.lat),crs="4326")
gdf_projected = gdf.to_crs("epsg:3857")
gdf_projected["x"] = gdf_projected.geometry.x
gdf_projected["y"] = gdf_projected.geometry.y



[minx,miny,maxx,maxy]=gdf_projected.total_bounds
grid_length = 2000
canvas = ds.Canvas(plot_width=int((maxx-minx)/grid_length),plot_height= int((maxy-miny)/grid_length), 
                   x_range=(minx,maxx), y_range=(miny,maxy), 
                   x_axis_type='linear', y_axis_type='linear')
ag=canvas.points(gdf_projected, 'x', 'y', agg=ds.count())
ag.rio.write_crs("epsg:3857", inplace=True)
ag.rio.to_raster("../data/interim/test2.tif")
#sd = tf.shade(ag)
#sd
1 Like