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"
    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.SetGeoTransform((xMin, inGridSize, 0, yMax, 0, -inGridSize))
    rBand = rasterDS.GetRasterBand(1)

    # 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.

  • to produce zero in all remaining areas of the raster, i assume the burn_values should be [0]. Also - what is the data type of the column you want to convert to raster values? Commented May 25, 2015 at 16:35
  • Data Type of the column is an Integer( Ranging from 1 to 10 only) and Burn values I will keep a zero only to get remaining values as zero but still I am not getting the proper Raster Output
    – Raam
    Commented May 26, 2015 at 6:57
  • When running the above provided script I get this error message: Traceback (most recent call last): File "D:/erosion_model/erosion_main/test_rasterizing.pyh.py", line 17, in <module> rasterDS = gdal.GetDriverByName('GTiff').Create(outputRaster, xRes, yRes, 1, gdal.GDT_Byte) File "D:\Programs\Anaconda2\lib\site-packages\osgeo\gdal.py", line 1476, in Create return _gdal.Driver_Create(self, *args, **kwargs) OverflowError: in method 'Driver_Create', argument 3 of type 'int' Can someone help me out with this please? I know you're supposed to answer and not ask again but it's too specific and redund
    – lvhk2
    Commented Mar 27, 2017 at 13:12
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – MaryBeth
    Commented Mar 27, 2017 at 13:49
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review
    – Oto Kaláb
    Commented Mar 27, 2017 at 14:02

1 Answer 1


I think you're pretty much there...but you may not need burn_values, try this:

err = gdal.RasterizeLayer(rasterDS, [1], shpLayer, options = ["ALL_TOUCHED=TRUE", "ATTRIBUTE=POLYNUMB"])

where POLYNUMB is just an example of a column name in your shapefile that you wish to rasterize. Note that all_touched=true may or may not be what you want...test it out and look at your edges.

Your Answer

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

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