3

I tried to use QGIS GeoReferencer to translate a raster layer, but somehow failed. It just goes to somewhere unexpected.

Now I decide to do it myself, with python. I think some libraries like GDAL must have done it. I only want to do an x-offset and y-offset with the tif, with no scale and rotation. Do you know what module I could use?

1
  • What did go wrong with QGIS? Are you sure you have done it the right way? what version of QGIS you are using? What platform?
    – gioman
    Commented Aug 14, 2011 at 9:20

3 Answers 3

12

Depending on the raster format, you can either edit the world file, or use GDAL/Python:

from osgeo import gdal

# Open in read/write mode
rast_src = gdal.Open(rast_fname, 1)

# Get affine transform coefficients
gt = rast_src.GetGeoTransform()
# (2776450.0, 100.0, 0.0, 6352650.0, 0.0, -100.0)

The geotransform gt object is a 6-parameter tuple described here. You want to edit the first and fourth items:

# Convert tuple to list, so we can modify it
gtl = list(gt)
gtl[0] += 1000.0  # Move east 1 km
gtl[3] -= 20000.0 # Move south 20 km
# [2777450.0, 100.0, 0.0, 6332650.0, 0.0, -100.0]

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

Note: I tested this with QGIS 1.7. You can keep the previous raster in during this whole operation, but it stays in place the whole time. To see the updated change you can either (1) close/open the QGIS project, or (2) re-add the same raster. The second option has the advantage of being able to see the differences between "before" and "after" transform operations.

2
1

If its any help, the only time I've experienced the same problem in QGis was when the co-ordinate system for the geo-referenced image was different from the current layers in QGis and I'd forgotten to switch on "on the fly CRS translation".

0

See my answer on this post. GDAL Translate has a parameter that allows you to do this, either through the QGIS user interface and manually entering that option, or with Python

1
  • If this is a duplicate of the linked question please just vote to close this question
    – Ian Turton
    Commented Aug 12, 2022 at 19:48

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.