Skip to content

Instantly share code, notes, and snippets.

@jqtrde
Last active December 21, 2015 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jqtrde/6225302 to your computer and use it in GitHub Desktop.
Save jqtrde/6225302 to your computer and use it in GitHub Desktop.
Testing if ZSH parameters exist with IF & ELSE.

I was looking for a way to simplify the following process:

  1. Have a CSV projected in EPSG:26919
  2. Convert it to a GeoJSON
  3. Reproject that GeoJSON to EPSG:4326

It's a very common pattern, and I don't like retyping it. These are the 'solutions' to that problem.

#!/bin/zsh
# via http://gis.stackexchange.com/questions/68464/making-development-patterns-more-efficient-with-zsh?noredirect=1#comment93125_68464
usage(){
usage="$0 infilepath {-o originalProjection} {-n newProjection}"
echo "Usage: $usage"
exit 1
}
orig_proj="some EPSG code"
new_proj="another EPSG code"
if [ -z "$1" ];then # Is filepath zero length?
echo "Error: Filepath is required"
usage
else
case ${arg} in
o) echo "Error: Filepath is required"
usage
;;
n) echo "Error: Filepath is required"
usage
;;
esac
filepath=$1
fi
shift #Get rid of $1 so getopts picks up the other parameters
while getopts ':o:n:' arg
do
case ${arg} in
o) orig_proj=${OPTARG};;
n) new_proj=${OPTARG};;
esac
done
csv2geojon $filepath > ${filepath%.*}.geojson #the %.* bit strips off the file ext.
ogr2ogr -F geojson -s_srs epsg:$orig_proj -t_srs epsg:$new_proj \
${filepath%.*}_$new_proj.geojson ${filepath%.*}.geojson
zshtest() {
if [ -z "$1" ]; then
echo 'no parameter specified'
else
echo $1;
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment