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?

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
ChanDon
  • 771
  • 3
  • 8
  • 13

3 Answers3

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.

Dave X
  • 1,639
  • 13
  • 25
Mike T
  • 42,095
  • 10
  • 126
  • 187
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".

Adrian
  • 1,131
  • 1
  • 14
  • 20
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

cndnflyr
  • 3,392
  • 2
  • 25
  • 43