gdalsrsinfo proj4 string: pass as a variable

by jamierob   Last Updated October 05, 2018 18:22 PM

I'm trying to use gdalsrsinfo to get the proj4 string from a .shp and then pass it to ogr2ogr for use in a batch reprojection loop. It's close to working, but i'm getting some additional formatting that is messing up the ogr2ogr command.

My script:

#!/bin/bash
    for f in *.shp; do 
        projsrs=$(gdalsrsinfo -o proj4 $f)
        echo "the proj 4 string is: $projsrs"
        ogr2ogr -f "ESRI Shapefile" -s_srs $projsrs -t_srs EPSG:3857 ${f%}3857.shp $f
    done

When I use bash -x to test the output, I'm getting and additional ''\' at the front of the proj4 string, and '\'' appended to the end and this is making the ogr2ogr command invalid. This might just be a shell/bash issue (not gdal), but why am I getting these extra characters?

    username:shpfrm username$ bash -x ./srstest.sh 
    + for f in '*.shp'
    ++ gdalsrsinfo -o proj4 filename.shp
    + projsrs=''\''+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs '\'''
    + echo 'the proj 4 string is: '\''+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs '\'''
    the proj 4 string is: '+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs '
    + ogr2ogr -f 'ESRI Shapefile' -s_srs ''\''+proj=utm' +zone=11 +datum=WGS84 +units=m +no_defs ''\''' -t_srs EPSG:3857 filename.shp3857.shp filename.shp
FAILURE:
Unable to open datasource `+datum=WGS84' with the following drivers.
Tags : gdal ogr bash


Related Questions



how to reproject all tif in a directory with gdal

Updated April 09, 2017 12:22 PM