2

I need to reproject a raster into the same extent and resolution as an existing raster, and want to use the gdal command-line utilities to do it.

How do I extract these values in a format I can use as input for gdalwarp?

Here's what I've tried:

function gdal_extent_gdalwarp_te() {
    if [ -z "$1" ]; then
        echo "Missing arguments. Syntax:"
        echo "  gdal_extent <input_raster>"
        return
    fi
    EXTENT=$(gdalinfo "$1" |\
        grep "Lower Left\|Upper Right" |\
        sed "s/Lower Left  //g;s/Upper Right //g;s/).*//g" |\
        #tr "\n" " " |\
        sed 's/ *$//g' |\
        tr -d "\n[(,]'")
    echo -n "$EXTENT"
}

function gdal_pixelsize_gdalwarp_tr() {
    if [ -z "$1" ]; then
        echo "Missing arguments. Syntax:"
        echo "  gdal_pixelsize_gdalwarp_tr <input_raster>"
        return
    fi
    EXTENT=$(gdalinfo "$1" |\
        grep "Pixel Size" |\
        sed "s/Pixel Size =//g; s/,/ /g" |\
        tr "\n" " " |\
        tr -d "[(,])-")
    echo -n "$EXTENT"
}

ExtractedSRS=$(gdalsrsinfo -o proj4 "SRSraster.tif" | tr -d "'")
echo "d"
ExtractedExtent=$(gdal_extent_gdalwarp_te "SRSraster.tif")
echo "e"
ExtractedResolution=$(gdal_pixelsize_gdalwarp_tr "SRSraster.tif")

#in this case I'm trying to tile the input rasters into a new output raster
gdalwarp -t_srs "$ExtractedSRS" -te "$ExtractedExtent" -tr "$ExtractedResolution" inputrasters*.tif outputraster.tif

This gives the error:

Creating output file that is -124997P x 0L.
ERROR 1: Attempt to create -124997x0 dataset is illegal,sizes must be larger than zero.

If I try leaving out the -te option, like this,

gdalwarp -t_srs "$ExtractedSRS" -tr "$ExtractedResolution" inputrasters*.tif outputraster.tif

I get:

Usage: gdalwarp [--help-general] [--formats]
    [-s_srs srs_def] [-t_srs srs_def] [-to "NAME=VALUE"]
    [-order n | -tps | -rpc | -geoloc] [-et err_threshold]
    [-refine_gcps tolerance [minimum_gcps]]
    [-te xmin ymin xmax ymax] [-tr xres yres] [-tap] [-ts width height]
    [-wo "NAME=VALUE"] [-ot Byte/Int16/...] [-wt Byte/Int16]
    [-srcnodata "value [value...]"] [-dstnodata "value [value...]"] -dstalpha
    [-r resampling_method] [-wm memory_in_mb] [-multi] [-q]
    [-cutline datasource] [-cl layer] [-cwhere expression]
    [-csql statement] [-cblend dist_in_pixels] [-crop_to_cutline]
    [-of format] [-co "NAME=VALUE"]* [-overwrite]
    [-nomd] [-cvmd meta_conflict_value]
    srcfile* dstfile

Available resampling methods:
    near (default), bilinear, cubic, cubicspline, lanczos, average, mode.

FAILURE: Wrong value for -tr parameters.
  • If I understant your question well, you should make a copy of the world file from the georeferenced raster and rename it to the new raster. – Zoltan Dec 22 '16 at 17:30
  • How do I do that? – user1521655 Dec 22 '16 at 17:31
  • Your old file probably has georeference in an external file (e.g. .tfw or jgw) or a GeoTiff (georeference inside the tif file). In the later case you can gdal_translate -co TFW=YES source.tif destination.tif to get deatination.tfw file. But notice that this is not reprojection at all. – Zoltan Dec 22 '16 at 18:04
  • If it's not a reprojection, then I don't think it will do what I need. I need to take a raster in UTM and reproject it to match the dimensions, extent, resolution, and projection of a raster in Albers. – user1521655 Dec 22 '16 at 18:19
  • Personally, if I had to stick with BASH, I'd use gdalinfo -json etc... to output JSON, then pipe that to a cli JSON parser (like JSON.sh, jq or a simple python script but that isn't really sticking with BASH :) – user2856 Dec 22 '16 at 22:38
0

The gdal command line utility to get the extent and the resolution of a raster is called gdalinfo. You can then parse those results if you want. Personnally, I prefer to use a Python script to get the information. See this answer here

  • I need help parsing the output into a format that will work as input for gdalwarp. I'll post code showing what I've tried. – user1521655 Dec 22 '16 at 18:21
0

It appears that my code was actually extracting the values correctly, but that I needed to drop the quotation marks when I actually ran gdalwarp.

This seems to work:

gdalwarp -t_srs "$ExtractedSRS" -te $ExtractedExtent -tr $ExtractedResolution -r bilinear inputrasters*.tif outputraster.tif

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.