Followed the steps from this link to create raster from shapefile using GDAL
Convert an OGR File to a Raster
but it is returning an empty raster. Can someone tell how to define a particular column from the shapefile data to burn?
The code is as follows
import gdal
from osgeo import osr
from osgeo import ogr
raster_path = 'D:\SWAT_HRU_Test\SWAT_Bihar\Bihar_shapefile_python\Full_HRU_GCS_output.tif'
shapefile = 'D:\SWAT_HRU_Test\SWAT_Bihar\Bihar_shapefile_python\Full_HRU_GCS.shp'
source_ds = ogr.Open(shapefile)
source_layer = source_ds.GetLayer()
pixelWidth = pixelHeight = 0.01
x_min, x_max, y_min, y_max = source_layer.GetExtent()
cols = int((x_max - x_min) / pixelHeight)
rows = int((y_max - y_min) / pixelWidth)
target_ds = gdal.GetDriverByName('GTiff').Create(raster_path, cols, rows, 1, gdal.GDT_Float32)
target_ds.SetGeoTransform((x_min, pixelWidth, 0, y_max, 0, -pixelHeight))
target_dsSRS = osr.SpatialReference()
target_dsSRS.ImportFromEPSG(4326)
target_ds.SetProjection(target_dsSRS.ExportToWkt())
band = target_ds.GetRasterBand(1)
band.SetNoDataValue(-9999)
gdal.RasterizeLayer(target_ds, [1], source_layer, options=["ATTRIBUTE = HRUGIS"])
this HRUGIS is a column in the shapefile on the basis of which i want to create the raster but it is returning a raster with all values 255
target_ds = None
conda create GeoCube --strict-channel-priority -c conda-forge geocube <others, like gdal>and activate the environment, it should work (since Geocube depends on rasterio, xarray and geopandas, those will be installed as well) – Fee Dec 02 '20 at 16:45conda activate <env-name>; jupyter-notebookalternatively, follow this guide for jupyter and conda. good luck! – Fee Dec 07 '20 at 17:07