For rasterizing a vector layer, I have tried gdal.RasterizeLayer method for converting vector(shapefile) to raster(tiff). But its giving raster output with NAN values(complete black image). I need some help in which where I am doing wrong. And I want to print one attribute value from shapefile to raster & remaining values should be zero in outRaster.
My code ( using Gdal )is
NoDataVal = -9999
# Open the data source and read in the extent
inPolygonShp = r"E:\polygons.shp"
outputRaster=r"E:\OutRaster.tif"
inGridSize=float(2)/110575 # nearly I am converting to 2 meters gridsize since its in GCS coordinates (vector layer)
shpDS = ogr.Open(inPolygonShp)
shpLayer = shpDS.GetLayer()
# Create the destination data source
xMin, xMax, yMin, yMax = shpLayer.GetExtent()
xRes = int((xMax - xMin) / inGridSize)
yRes = int((yMax - yMin) / inGridSize)
rasterDS = gdal.GetDriverByName('GTiff').Create(outputRaster, xRes, yRes, 1, gdal.GDT_Byte)
# Define spatial reference
rasterDS.SetProjection(shpLayer.GetSpatialRef().ExportToWkt())
rasterDS.SetGeoTransform((xMin, inGridSize, 0, yMax, 0, -inGridSize))
rBand = rasterDS.GetRasterBand(1)
rBand.SetNoDataValue(NoDataVal)
rBand.Fill(NoDataVal)
# Rasterize
err = gdal.RasterizeLayer(rasterDS, [1], shpLayer, burn_values=[200], options = ["ALL_TOUCHED=TRUE"])
# for rasterizing with a attribute value of polygon
# err = gdal.RasterizeLayer(rasterDS, [1], shpLayer, burn_values=[0], options = ["ATTRIBUTE= Height"])
Here I feel its generating a raster file but its failed to overwrite with shapefile values. I need some help in this regard.
No comments:
Post a Comment