Geographic Information Systems Asked by MapEngine on June 20, 2021
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?
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:tempraster1.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.
Correct answer by BradHards on June 20, 2021
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 . . .
Answered by Renee Cammarere on June 20, 2021
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
Answered by cgon on June 20, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP