I am using GDAL in Python to create a tiff raster of data that is originally from a shapefile. I have an auxillary data channel, which I would like to use to color the pixels of the tif. I have made sure that my data (yield_grid) is the same size and shape as my tif raster will be, but so far I am unable to get anything besides an all black square out of my raster.
Does anyone have a suggestion for what I might be doing wrong in assigning my data to my raster band?
# Define pixel_size and NoData value of new raster
pixel_size = 0.00005
NoData_value = -99
# Filename of input OGR file
vector_fn = inShapeFile
# Filename of the raster Tiff that will be created
raster_fn = 'test.tif'
# Open the data source and read in the extent
source_ds = ogr.Open(vector_fn)
source_layer = source_ds.GetLayer()
source_srs = source_layer.GetSpatialRef()
x_min, x_max, y_min, y_max = source_layer.GetExtent()
# Create the destination data source
x_res = int((x_max - x_min) / pixel_size)
y_res = int((y_max - y_min) / pixel_size)
# Get Driver and Create output target
target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn, x_res, y_res,1,gdal.GDT_Byte)
# Set Geo Transform
target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
# Write array into raster band, normalize by max, and scale to 1024
target_ds.GetRasterBand(1).WriteArray(np.transpose(yield_grid/np.max(yield_grid))*1024)
# Set Spatial reference and projection
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
target_ds.SetProjection(outRasterSRS.ExportToWkt())
#Flush Cache for raster band
target_ds.GetRasterBand(1).FlushCache()
# Printing raster band to make sure that I'm not crazy
print target_ds.GetRasterBand(1).ReadAsArray()
[[ 0 0 0 ..., 39 2 0][ 0 0 0 ..., 80 37 0]
[ 0 0 0 ..., 85 32 0]
...,
[ 0 58 119 ..., 0 0 0]
[ 0 0 82 ..., 0 0 0]
[ 0 0 0 ..., 0 0 0]]
The output at the end of the code shows that I am in fact putting values into the raster band, but I'm not sure why the tiff isn't showing any colors.