Preparing the next post, I have found a file with a wrong geotransform, but not an easy tool to do it.
Coding it is as easy as opening the datasource with the update option and setting the new geotransform as follows:
I have created a small program to make it even easier, called changeGeotransform.py:
To execute, just type:Coding it is as easy as opening the datasource with the update option and setting the new geotransform as follows:
ds = gdal.Open( fileIn, GA_Update ) ds.SetGeoTransform(geotransform) ds = NoneWhere the geotransform must be a tuple like (-180.5, 1.0, 0.0, 90.5, 0.0, -1.0). Take a look to the documentation for more information.
I have created a small program to make it even easier, called changeGeotransform.py:
from osgeo import gdal
from osgeo.gdalconst import *
import sys
def changeGeotransform(fileIn, geotransform):
"""
Takes a dataset, and changes its original geotransform to an arbitrary geotransform
"""
ds = gdal.Open( fileIn, GA_Update )
#Exit if there is an error opening the dataset. GDAL will output the error message
if ds is None:
sys.exit()
ds.SetGeoTransform(geotransform)
ds = None
def helpText():
print "To change the file Geotransform, the command is python changeGeotransform , where the new geotransform must be like '(left_value,delta_x,rotation_x,top_value,rotation_y,delta_y)'"
sys.exit()
def geotransformError(inputGeotransform):
print "Error reading the geotransform: " + inputGeotransform + " it must be like (-180.5, 1.0, 0.0, 90.5, 0.0, -1.0)"
sys.exit()
if __name__ == "__main__":
"""
Program mainline
"""
# Parse command line arguments.
argv = gdal.GeneralCmdLineProcessor( sys.argv )
if len(argv) != 3:
helpText()
fileIn = argv[1]
try:
geotransform = eval(argv[2])
except Exception, ex:
geotransformError(argv[2])
if type(geotransform) != type(()):
geotransformError(argv[2])
changeGeotransform(fileIn, geotransform)
python changeGeotransform file_name new_geotransform
where the new geotransform must be like:
(left_value,delta_x,rotation_x,top_value,rotation_y,delta_y)
The program basically tests if the inputs are correct and executes the code I have commented before.
Of course, only the files with drivers able to write the format can be changed. Type gdalinfo --formats to know which drivers do you have installed.
Many thanks! This was incredibly useful, even though some 2to3 refactoring was required.
ReplyDelete