4

i want to use python GDAL to convert some rasters files but i take error in the create new tiff image and specific in the line driver= gdal.GetDriverByName('GTiff')

update :

python version 2.7 64 bit

GDAL version 2020100

os: windows

i use it and before this version and all work fine code :

inputfile = 'input.tif'
input = gdal.Open(inputfile )
inputProj = input.GetProjection()
inputTrans = input.GetGeoTransform()

referencefile = 'ref.tif'
reference = gdal.Open(referencefile )
referenceProj = reference.GetProjection()
referenceTrans = reference.GetGeoTransform()
bandreference = reference.GetRasterBand(1)
x = reference.RasterXSize
y = reference.RasterYSize


outputfile = tempfile.NamedTemporaryFile(suffix='.tif').name
driver= gdal.GetDriverByName('GTiff')
output = driver.Create(outputfile,x,y,1,bandreference.DataType)
output.SetGeoTransform(referenceTrans)
output.SetProjection(referenceProj)

gdal.ReprojectImage(input,output,inputProj,referenceProj,gdalconst.GRA_Bilinear)

error :

    return _gdal.GetDriverByName(*args)
TypeError: in method 'GetDriverByName', argument 1 of type 'char const *'

update 2:

that error show when i use this limes:

from __future__ import absolute_import
from __future__ import unicode_literals

but that i need it because i use python 2.7

any idea how to fix it ?

8

Because you imported unicode_literals, you're passing a unicode literal to GDAL which is expecting a string literal.

So, explicitly cast the 'GTiff' arg to a str.

E.g.

>>> from __future__ import unicode_literals
>>> from osgeo import gdal
>>> gdal.GetDriverByName('GTiff')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Temp\conda\envs\test27\lib\site-packages\osgeo\gdal.py", line 3019, in GetDriverByName
    return _gdal.GetDriverByName(*args)
TypeError: in method 'GetDriverByName', argument 1 of type 'char const *'
>>> type('GTiff')
<type 'unicode'>
>>> gdal.GetDriverByName(str('GTiff'))
<osgeo.gdal.Driver; proxy of <Swig Object of type 'GDALDriverShadow *' at 0x0000000002A9ED80> >

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.