2

I'm trying to shift a georeferenced raster by 0.5m N and 0.5m W in QGIS using the python console:

from osgeo import gdal

# Open in read/write mode
rast_src = gdal.Open('test_raster.tif', 1)

# Get affine transform coefficients
gt = rast_src.GetGeoTransform()

# Convert tuple to list, so we can modify it
gtl = list(gt)
gtl[0] -= 0.5  # Move west 0.5 m
gtl[3] += 0.5 # Move north 0.5 m

# Save the geotransform to the raster
rast_src.SetGeoTransform(tuple(gtl))
rast_src = None # equivalent to save/close

However line #7 (gt = rast_src.GetGeoTransform()) throws an AttributeError: 'NoneType' object has no attribute 'GetGeoTransform'. The installed GDAL version is 1.11.0. The raster seems fine. So what's wrong?

  • 2
    Clearly the Open() call failed (returning None). Can you check if it works with a full path? Probably the issue is that the directory that you think you're working in isn't the one you're actually looking in. You can check this by adding import os and print os.getcwd() before the failing Open() call. – BradHards May 27 '15 at 9:34
  • Same error with rast_src = gdal.Open('C:\temp\raster1.tif', 1). I tried another file too. Can't I pick just a loaded layer? – MapEngine May 27 '15 at 13:21
  • 1
    Maybe you could try escaping the backslashes in that path. BTW: What did the working directory show? – BradHards May 28 '15 at 6:38
  • Escaping the backslashes did the trick: rast_src = gdal.Open('C:\\temp\\raster1.tif', 1). The working directory is C:\Program Files\QGIS Chugiak\bin. If you change your comment to an answer I can tick it :) – MapEngine May 28 '15 at 13:44
1

Note: this is just a slightly expanded version of the comments above.

Clearly the Open() call failed (returning None).

This can occur for a range of reasons (e.g. that you don't have permissions to read the file because of operating system permissions like ACLs), but as identified in the comments, issue in this case is that the directory that you think you're working in isn't the one you're actually looking in. In particular, python from QGIS on Windows is "homed" wherever the QGIS.exe binary lives (note: you can change runtime directories on Windows applications if you have Administrator rights - create a shortcut and set "Start in").

The easiest way to move forward with this is just to provide the full path:

rast_src = gdal.Open('C:\\temp\\raster1.tif', 1)

Note that the \ character is special in python, so you need to double them to escape it.

If you don't want to provide a hard-coded path, you can construct it from an environment variable, or from other source (whatever the code should assume as the parent directory). That isn't really GIS specific though.

0

This same issue happened to me and had nothing to do with the path name. I am able to open the raster OK, but then I find out that the methods that were previously available to me are no longer there . . . enter image description here

0

Try in a console:

gdalinfo yourfile.tif

If everything seems normal (geotransform is present), then you can try to rewrite your file with gdal_translate.

I had a similar problem, with a large array, and could access it with python by rewriting the file as:

gdal_translate -co "COMPRESS=LZW" -co "TILED=YES" yourfile.tif newname.tif

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.