1

I am trying to convert data from vector to raster with GDAL rasterize using by Python. I want to output raster format as TIFF, but the result TIFF opens completely black.

I am writing code benefit from this recipe: Convert an OGR File to a Raster.

I tried different combinations some part of code but I don't have any solution.

from osgeo import gdal, ogr

# Define pixel_size and NoData value of new raster
pixel_size = 25 # I tried different combinations (0.001 etc.)
NoData_value = -9999

# Filename of input OGR file
vector_fn = 'test.shp'

# 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()
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)
target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn, x_res, y_res, 1, gdal.GDT_Byte)
target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size)) # I tried different combinations 
band = target_ds.GetRasterBand(1)
band.SetNoDataValue(NoData_value)

# Rasterize
gdal.RasterizeLayer(target_ds, [1], source_layer, burn_values=[0]) # I tried different combinations
2
  • 1
    Hye, welcome to GIS SE. What happens if you add target_ds = None at the end of your code? Mar 28, 2020 at 1:57
  • What is the burn value that your code is setting?
    – user30184
    Mar 28, 2020 at 11:03

1 Answer 1

1

That recipe works changed in this way:

from osgeo import gdal, ogr

#Define pixel_size and NoData value of new raster
pixel_size = 25 # I tried different combinations (0.001 etc.)
NoData_value = 0

#Filename of input OGR file
vector_fn = 'test.shp'

#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()
x_min, x_max, y_min, y_max = source_layer.GetExtent()
spatialRef = source_layer.GetSpatialRef()

# Create the destination data source
x_res = int((x_max - x_min) / pixel_size)
y_res = int((y_max - y_min) / pixel_size)

target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn, x_res, y_res, 1, gdal.GDT_Byte)

target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size)) # I tried different combinations 
projection = spatialRef.ExportToWkt()
target_ds.SetProjection(projection)
band = target_ds.GetRasterBand(1)
band.SetNoDataValue(NoData_value)

# Rasterize
gdal.RasterizeLayer(target_ds, [1], source_layer, burn_values=[1] ) # I tried different combinations

source_ds = None
target_ds = None

After running it, I got rasterized vector layer (polygon1) as follows:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.