6

I try to use the gdal.Warp function with options :

# align GFC tree cover with segments 
glad_raw = pm.getDwnDir() + aoi_name + '_treecover2010.tif'
glad_aligned = pm.getDwnDir() + aoi_name + '_treecover2010_aligned.tif'
    
if not os.path.isfile(mask): 
    return 'No treecover file'
    
options = gdal.WarpOptions(
    xRes             = res['x'], 
    yRes             = res['y'], 
    creationOptions  = "COMPRESS=LZW", 
    outputBounds     = [int(v) for v,i in enumerate(bb)],
    dstSRS           = proj
    #options          = 'overwrite'
)
ds = gdal.Warp(glad_aligned, glad_raw, options)
ds = None

but I get the following error :

TypeError: Warp() takes 2 positional arguments but 3 were given

strange because the help(gdal.warp) gave me Warp(destNameOrDestDS, srcDSOrSrcDSTab, **kwargs)

2 Answers 2

6

This means that the options argument is keyword argument and not positional. Keyword arguments are given to a function as gdal.Warp(positional, positional, keyword=keyword). So input and output are positional and have to be prvovided only in correct order, and keyword has to be provided using keyword.

However you do not have to provide options as gdal.WarpOptions. You can provide each argument as individual keyword argument like below:

gdal.Warp(OutDS, inFile, dstSRS = outSrs, dstNodata = nodata, xRes=1000, yRes=1000)
2
  • yes but I wanted to use gdal.WarpOption object. So for me the solution is ds = gdal.Warp(glad_aligned, glad_raw, options=options) Commented Jul 17, 2020 at 12:07
  • 2
    Yes, options is a keyword to be used. Also if you use options all other kwargs will be ignored.
    – Alešinar
    Commented Jul 17, 2020 at 12:11
0

You could also do the following which personally I think is cleaner..

options = {
  # "format": "GTiff",
    "xRes": res['x'],
    "yRes": res['y'],
    "compressionOptions": "COMPRESS=LZW",
    "outputBounds": [int(v) for v,i in enumerate(bb)],
    "dstSRS": proj,
  # "resampleAlg": "near",
  # "outputType": gdal.GDT_Int32
}

ds = gdal.Warp(glad_aligned, glad_raw, **options)
del ds

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.