5

How do I automatically reproject a map in GDAL (or using gdalwarp/gdaltransform etc.) to match the projection of another map? I do not want to read the projection manually and give it as input.

Basically, I would like to do something like:

gdalwarp -source map1.tif -get_projection_from map2.tif -output out.tif
  • Can you write scripts in any language? I am sure that someone can help you with a Python solution. – user30184 May 3 '17 at 8:38
  • Yes python would be good – fmonegaglia May 3 '17 at 8:41
8

http://www.gdal.org/gdalwarp.html says

-t_srs srs_def: target spatial reference set. The coordinate systems that can be passed are anything supported by the OGRSpatialReference.SetFromUserInput() call, which includes EPSG PCS and GCSes (i.e. EPSG:4296), PROJ.4 declarations (as above), or the name of a .prj file containing well known text.

http://www.gdal.org/gdalsrsinfo.html says it

Lists info about a given SRS in number of formats (WKT, PROJ.4, etc.)

So you can write the SRS of your other raster to a file via

gdalsrsinfo -o wkt other.tif > target.wkt

and read it for gdalwarp via

gdalwarp -t_srs target.wkt source.tif output.tif

You can combine this with a scripting language of your likings. You did not mention which OS you are on, so options might vary.

A simple, mostly untested, ugly proof-of-concept example Bash script without any error handling would be:

#!/bin/bash

targetfile=$1
infile=$2
outfile=$3

echo "Warping ${infile} using the SRS of ${targetfile} to ${outfile}"

projectionfile=$(mktemp /tmp/tif2targetsrs.XXXXXX)

gdalsrsinfo -o wkt "${targetfile}" > "${projectionfile}"
gdalwarp -t_srs "${projectionfile}" "${infile}" "${outfile}"
rm "${projectionfile}"
  • I tried, but when I call gdal.GetGeoTransform() on the new image I still get the one corresponding to the initial image. – fmonegaglia May 3 '17 at 8:58
  • It works for me. I added a Bash script above. – bugmenot123 May 3 '17 at 9:06
  • 5
    Oneliner: gdalwarp -t_srs <(gdalsrsinfo -o wkt other.tif) source.tif output.tif – pLumo May 3 '17 at 9:21

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.