[GIS] Reprojecting a raster to match another raster using GDAL

coordinate systemgdalgdalwarp

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

Best Answer

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}"
Related Question