SetGeoReference it is not working with this code. (I think that I should need some more explanation on how this works...)


Retrieve Georef & Projection from a reference image


ReferenceTif = gdal.Open(img_path_tif)
GeoRef = ReferenceTif.GetGeoTransform() #I take the geo ref of the ref image
projRef = ReferenceTif.GetProjection()  #I take the proj as of the ref image

(654000.0, 0.3, 0.0, 4203000.0, 0.0, -0.3)


Set the Georef & Projection of a image


test = gdal.Open('test.tif')            #Open a new image
test.SetProjection(proj)                #Set the projection like the ref image
test.SetGeoTransform(GeoRef)            #Set the GeoRef like the ref image


Check if the work is done


result = test.GetGeoTransform()         #Get the GeoRef of the test image
print(result==GeoRef)                   #Check if the work is done correctly

False (0.0, 1.0, 0.0, 0.0, 0.0, 1.0)

Any ideas ?

  • Test.SetProjection(proj) doesn't look right, should it be Test.SetProjection(projRef ) as projRef is the object retrieved from ReferenceTif.GetProjection(). If this line is failing the next line isn't executed, which is the one you use to set the GeoTransform, change the object for SetProjection and see if that works. – Michael Stimson Feb 13 at 0:05
  • Oh yes he should, I just change it but still the output is "False". Still it's not working :/ .. Any other idea ? – BasileG Feb 14 at 9:33
  • Maybe this can help: gis.stackexchange.com/questions/165950/… – Frankwin Feb 14 at 14:28

It sounds very similar to this problem How do I change a raster's geotransform with GDAL? however I was getting the same error with the current version of GDAL on two workstations. The return code was 3 which I was unable to find a reference for, however GDALDatasets opened or open shared without GDALAccess set to GDALAccess::GA_Update implicitly are opened read only so the SetGeoTransform shouldn't have been able to change the geotransform of the dataset - this is likely a bug in GDAL.

If you change your code to:

test = gdal.OpenShared('test.tif',gdal.GA_Update))  #Open a new image for updating
test.SetProjection(proj)                            #Set the projection like the ref image
test.SetGeoTransform(GeoRef)                        #Set the GeoRef like the ref image

You will be able to apply the projection and spatial reference then set test=None to close the dataset properly, this then worked for me on both workstations.

| improve this answer | |

Thank you so much for your help. I apply your advice @MichaelStimson and that's working perfectly. See you

| improve this answer | |

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.