Reputation: 167
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 the single quotes are getting passed to the ogr2ogr command and I can't figure out how to strip them off:
My script:
#!/bin/bash
for f in *.shp; do
projsrs=$(gdalsrsinfo -o proj4 $f)
ogr2ogr -f "ESRI Shapefile" -s_srs "$projsrs" -t_srs EPSG:3857 ${f%}3857.shp $f
done
running the gdalsrsinfo
command by itself returns:
username:shpfrm username$ gdalsrsinfo -o proj4 filename.shp
'+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs '
When I use bash -x
to test the output, I see that ''\'
is at the beginning of the string and \'''
is on the end.
+ for f in '*.shp'
++ gdalsrsinfo -o proj4 filename.shp
+ projsrs=''\''+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 PC_Sec05_Frm64.shp
Failed to process SRS definition: '+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs '
what I need is:
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
Upvotes: 1
Views: 155
Reputation: 42688
You haven't shown the output from the command, but if it does have stray quotes that you don't want, you could just remove them. Reference: http://wiki.bash-hackers.org/syntax/pe#search_and_replace
#!/bin/bash
for f in *.shp; do
projsrs=$(gdalsrsinfo -o proj4 "$f")
ogr2ogr -f "ESRI Shapefile" -s_srs "${projsrs//\'/}" -t_srs "EPSG:3857" "${f}3857.shp" "$f"
done
Also, you should always quote filenames, as they can contain spaces.
Upvotes: 2