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.
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