TransWikia.com

How to burn values from a shapefile into raster with gdal.Rasterize?

Geographic Information Systems Asked by Nikolay Yasinskiy on March 12, 2021

I have this code to rasterise a point shapefile into raster TIFF.

vector_ds  = ogr.Open('stations.shp')
shp_layer = vector_ds.GetLayer()
shape_file = 'stations.shp'
output_raster = os.getcwd()+'/rasterized_points.tif'
pixel_size = 0.01
xmin, xmax, ymin, ymax = shp_layer.GetExtent()
ds = gdal.Rasterize(output_raster, shape_file, xRes=pixel_size, yRes=pixel_size, 
                    burnValues='data',outputBounds=[xmin, ymin, xmax, ymax], 
                    outputType=gdal.GDT_Byte)
ds = None

It works, but the result doesn’t have values from ‘data’ column. Parameter ‘attribute’ of RasterizeOptions doesn’t help neither. Can somebody explain, how I should make the Rasterize burn the right values (0,1,2,..) from shape.

One Answer

I believe that burnValues cannot be used like that.

See the api reference https://gdal.org/python/osgeo.gdal-module.html#RasterizeOptions.

burnValues -- list of fixed values to burn into each band for all objects. Excusive with attribute.
attribute --- identifies an attribute field on the features to be used for a burn-in value. The value will be burned into all output bands. Excusive with burnValues.

Change "burnValues" into "attribute" and it should work.

I made a test by digitizing two polygons with an attribute "data" of type integer. I places values 1 and 200 as "data". I modified your code just slightly.

from osgeo import gdal,ogr,osr
vector_ds  = ogr.Open('burn.jml')
jml_layer = vector_ds.GetLayer()
jml_file = 'burn.jml'
output_raster = rasterized_points.tif
pixel_size = 0.01
xmin, xmax, ymin, ymax = jml_layer.GetExtent()
ds = gdal.Rasterize(output_raster, jml_file, xRes=pixel_size, yRes=pixel_size, 
                    attribute='data',outputBounds=[xmin, ymin, xmax, ymax], 
                    outputType=gdal.GDT_Byte)
ds = None

Shortened gdalinfo about the output raster:

gdalinfo rasterized_points.tif -stats
Driver: GTiff/GeoTIFF
Files: rasterized_points.tif
Size is 6668, 4339
Origin = (300.000000000000000,683.391841779975266)
Pixel Size = (0.010000000000000,-0.010000000000000)
...
Band 1 Block=6668x1 Type=Byte, ColorInterp=Gray
  Minimum=0.000, Maximum=200.000, Mean=54.468, StdDev=88.690
  Metadata:
    STATISTICS_MAXIMUM=200
    STATISTICS_MEAN=54.468393933566
    STATISTICS_MINIMUM=0
    STATISTICS_STDDEV=88.689808459494
    STATISTICS_VALID_PERCENT=100

Maximum value in the raster is 200, just as the maximum value in the "data" column of the vector source.

Correct answer by user30184 on March 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP