13

I'm starting to use QGIS for my thesis (environmental physics) and have to admit I'm pretty ignorant about it - I've always used Mathematica or Matlab.

My problem is: I have a raster layer, in .tif format, which is misplaced (I need to move it south and west by 100m). I need to translate (reposition) it, and I have absolutely no idea on how I can do that.

I've tried Affine transform but it doesn't work (doesn't let me select an input layer); I've also read this How to translate (reposition) a raster in Python? but I have to admit I haven't understood much of it, I tried several times but am no good with python.

3
  • I assume the .tif layer is georeferenced in some way? Which coordinate reference system is stated in the metadata? Quite likely, you only need to correct the CRS settings and the layer will align.
    – underdark
    Commented Dec 17, 2013 at 20:43
  • thanks for the comment! yes, it is georeferenced, and the coordinate system is the correct one (WGS 84/UTM zone 32N) ..unfortunately the displacement comes from a (badly-written) plugin I need to use, and that I can't change/correct.. hence the need to reposition the layer...
    – user25031
    Commented Dec 17, 2013 at 22:30
  • The Affine Transform plugin is for vector layers only.
    – AndreJ
    Commented Jan 26, 2015 at 12:22

5 Answers 5

15

ah! I managed to do it, following what's been said in the linked discussion.... (How to translate (reposition) a raster in Python?) apparently it just took me a while to figure how to use the python console! ;) thanks anyways! :)

I used GDAL/Python, from the QGIS Python console! :)

what I wrote was:

from osgeo import gdal
gdal.AllRegister()
rast_src = gdal.Open('filename.tif', 1 )
gt = rast_src.GetGeoTransform()
gtl = list(gt)
gtl[0] -= 200
gtl[3] -= 100
rast_src.SetGeoTransform(tuple(gtl))
rast_src = None

EDIT: switched "gdall.AllRegister()" to "gdal.AllRegister()".

2
  • There is a one letter typo in the code: gdall.AllRegister is not supposed to be spelled with two "L". (It generats: "'gdall' is not defined") Unfortunately I can't edit it as an edit is required to be at least 6 characters long.
    – Smerla
    Commented May 15, 2018 at 13:52
  • 1
    This answer really helped me out! Only thing I would like to add is to make sure and check your units before doing a transform by typing a print(gtl) function. For instance, my raster was in dd mm ss, and so to figure out how many meters are in a decimal degree, I used the table available here (easier for latitude than longitude!): table for converting meters to decimal degrees Commented Feb 1, 2019 at 8:45
2

The Rasmover plugin should do what you want.

You have to allow for experimental plugins to get it in the plugin list.

The result is a virtual raster file, which you can edit with a text editor to adjust the parameters if needed.

2
1

In the special case where the raster already has a UTM projection, and you want to move it longitudinally to another UTM zone, I found that gdal_translate can do it. For example, the input raster is in any UTM zone Z, and you want to move it to UTM zone 26 (i.e., the Atlantic Ocean at most latitudes):

gdal_translate -a_srs '+proj=utm +zone=26 +datum=WGS84' input.tif output.tif

The origin and corner coordinates in model space units as reported by gdalinfo remain the same. But the corner coordinates in latitude/longitude change, and the raster really did move to another part of the world.

1
  • Great, that's what I was looking for! Can it be done moving the raster origin in latitude direction within the same zone as well? gis.stackexchange.com/q/408259/44978
    – ulrich
    Commented Aug 13, 2021 at 12:59
0

Here is an example on how you do that with rasterio Reference: An issue I opened and dissuaded a while ago.

  • Example 1: read, reposition/shift, and write in a new tif file
import rastrio
# specify the needed x and y for the reposition/shift
x = -7.247 
y = 35.08
with rasterio.open('./orbview_data/orbview_3.tif', 'r') as data:
    img = data.read()`
        
    with rasterio.open('./orbview_data/orbview_3_shift', 'w',
                       driver='GTiff',
                       height=data.shape[0], width=data.shape[1],
                       count=data.count,
                       dtype='uint16',
                       crs=data.crs,
                       transform=Affine.translation(-7.247, 35.08) * data.transform) as dst:
        dst.write(img)
  • Example 2: read, reposition/shift, and update in the same tif file
import rasterio
with rasterio.open("./orbview_data/orbview_3.tif", mode="r+") as dataset:
    dataset.transform = Affine.translation(-7.247, 35.08) * dataset.transform

0

In QGIS

If you want to do this in QGIS, the "Freehand raster georeferencer" might work. You can add it via "Plugins -> Manage and Install Plugins...". It allows you to move rasters around, using the mouse. It did work for me for some GeoTiffs, but failed to open others.

From command line

The other option is to use GDAL functions from the command line. You need to have GDAL with OSGEO installed on your computer for this to work. You can move a GeoTiff by setting (overwriting) new upper left and lower right coordinates of your geotiff using gdal_edit.py -a_ullr. I recommend you make a backup of your GeoTiff before you start, as the command diretly modifies the file.
If you do not know the current coordinates of the corners of your geotiff, run gdalinfo your_map.tiff and look for the "Upper Left" and "Lower Right" cordinates (first pair of numbers, which are in metres, if you geotiff's LENGTHUNIT is metre). Keep a note of those numbers, and add however much you want to move your raster towards east (subtract 100 to shift 100m westward) to the first number of both corners and do the same for shifting north with the second number of both corners. Then call gdal_edit.py -a_ullr new_ulx new_uly new_lrx new_lry your_map.tiff.
Import the GeoTiff into QGIS again and you should see it in its new place.

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.