1

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

print(GeoRef)
(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
print(result)

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

Any ideas ?

3
  • 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. Commented Feb 13, 2020 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
    Commented Feb 14, 2020 at 9:33
  • Maybe this can help: gis.stackexchange.com/questions/165950/…
    – Frankwin
    Commented Feb 14, 2020 at 14:28

2 Answers 2

1

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.