1

I'm trying to rastrize a shapefile that I have. I read the shape using GeoPandas. I would like to give the new raster values based on the shape column called "class" which has numerical class value based on another field : enter image description here

I have tried to do this process using rastrize with the following function:

def rasterise_me(raster, vector, attribute,
                fname_out="", format="MEM"):
    """Rasterises a vector dataset by attribute to match a given
    raster dataset. This functions allows for the raster and vector
    to have different projections, and will ensure that the output
    is consistent with the input raster.
    
    By default, it returns a handle to an open GDAL dataset that you
    can e.g. `ReadAsArray`. If you want to generate a  GTiff on disk,
    set format to `GTiff` and `fname_out` to a sensible filename.
    
    Parameters
    ----------
    raster: str
        The raster filaname used as input. It will not be overwritten.
    vector: str
        The vector filename
    attribute: str
        The attribute that you want to rasterize. Ideally, this is
        numeric.
    fname_out: str, optional
        The output filename.
    format: str, optional
        The output file format, such as GTiff, or whatever else GDAL
        understands
    """
    # Open input raster file. Need to do this to figure out
    # extent, projection & resolution.
    g = gdal.Open(raster) 
    geoT = g.GetGeoTransform()
    nx, ny = g.RasterXSize, g.RasterYSize 
    srs = g.GetProjection()
    min_x = min(geoT[0], geoT[0]+nx*geoT[1])
    max_x = max(geoT[0], geoT[0]+nx*geoT[1])
    min_y = min(geoT[3], geoT[3] + geoT[-1]*ny)
    max_y = max(geoT[3], geoT[3] + geoT[-1]*ny)
    # Reproject vector to match raster file
    vector_tmp = gdal.VectorTranslate("", vector, format="Memory",
                                    dstSRS=srs)
    # Do the magic
    ds_dst= gdal.Rasterize(fname_out, vector_tmp, attribute=attribute,
                        outputSRS=srs, xRes=geoT[1], yRes=geoT[-1],
                        outputBounds=[min_x, min_y, max_x, max_y],
                        format=format, outputType=gdal.GDT_Int32)
    return ds_dst

but I always get the same error:

AttributeError Traceback (most recent call last) in ----> 1 rasterise_me("path\to\tiff.tif", "path\to shape.shp", "class") in rasterise_me(raster, vector, attribute, fname_out, format) 28 # extent, projection & resolution. 29 g = gdal.Open(raster) ---> 30 geoT = g.GetGeoTransform() 31 nx, ny = g.RasterXSize, g.RasterYSize 32 srs = g.GetProjection() AttributeError: 'NoneType' object has no attribute 'GetGeoTransform'

I don't really understand why I get this error and how can I solve it

My end goal: To create new raster based on my shapefile class values. If any more information regard what I did before is needed I would love to give it, but unfortunatly can't share the shapefile.

  • 1
    The problem is in g = gdal.Open(raster) : the raster is not open (path of the raster ?) -> g is None -> AttributeError: 'NoneType' object has no attribute 'GetGeoTransform' – gene Jun 28 at 15:39
  • 1
    Make sure you set gdal.UseExceptions() before opening the raster so GDAL raises an exception if data can not be read instead of just returning None – user2856 Jun 28 at 23:43
  • 1
    Echoing Gene; I get this error from time to time and it's always an error in my path. – Jon Jun 29 at 15:27
1

In the end what solved it was to add r before the name of the path

e.g :

r"this/is/my/path/image.tif"

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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