]> gitweb.michael.orlitzky.com - dead/census-tools.git/blobdiff - jsroute/jsroute
Added the jsroute folder with a shell script and sample Javascript file for routing.
[dead/census-tools.git] / jsroute / jsroute
diff --git a/jsroute/jsroute b/jsroute/jsroute
new file mode 100755 (executable)
index 0000000..5997e11
--- /dev/null
@@ -0,0 +1,82 @@
+#!/bin/bash
+#
+# jsroute, a utility to route via Google Maps using Envjs.
+#
+# We take a starting point and a destination as arguments, and
+# pass them off to a Javascript file (route.js) which is compiled
+# on-the-fly using Mozilla's Rhino Javascript compiler. Then, magic
+# occurs, and the JSON results are output to the command line.
+#
+# Soon the output will be piped through a Python script to convert
+# it to (usable) KML.
+#
+
+# Some exit codes.
+EXIT_MISSING_START=1
+EXIT_MISSING_DESTINATION=2
+EXIT_HELP=3
+EXIT_GETOPT_UNHAPPY=4
+EXIT_TOO_MANY_ARGS=5
+
+
+# Empty by default.
+ROUTENAME=""
+
+# A function which just displays the usage information.
+function usage {
+    echo ""
+    echo "Usage: $0 [-h] [-n \"name\"] \"<start>\" \"<destination>\""
+    echo ""
+    echo "ARGUMENTS"
+    echo "  <start>,       Your starting location. Use quotes."
+    echo "  <destination>, Your destination. Use quotes."
+    echo ""
+    echo "OPTIONS"
+    echo "  -h,            Show the usage information and bail."
+    echo "  -n <name>,     Give the route a meaningful name. Use quotes."
+    echo ""
+}
+
+
+while getopts "hn:" option; do
+  case $option in
+      n ) ROUTENAME="$OPTARG";
+         # Shift the number of remaining arguments down by two,
+         # so that the non-option parameters are accessible via
+         # $1, $2, etc.
+         shift $((OPTIND-1));;
+      h ) usage;
+         exit $EXIT_HELP;;
+      * ) usage;
+         exit $EXIT_GETOPT_UNHAPPY;;
+  esac
+done
+
+
+# The non-option parameters.
+START=$1
+DESTINATION=$2
+
+# Dollarsign-octothorpe contains the number of parameter arguments.
+if [ "$#" -gt 2 ]; then
+    echo "Error: too many arguments. Maybe you forgot to place the source and destination in quotes?"
+    usage;
+    exit $EXIT_TOO_MANY_ARGS
+fi
+
+if [ "$START" == "" ]; then
+    usage;
+    exit $EXIT_MISSING_START
+fi
+
+if [ "$DESTINATION" == "" ]; then
+    usage;
+    exit $EXIT_MISSING_DESTINATION
+fi
+
+if [ "$ROUTENAME" == "" ]; then
+    # Set it to a default of "<start> to <destination>".
+    ROUTENAME="$START to $DESTINATION"
+fi
+
+java -jar ../lib/env-js/rhino/js.jar -opt -1 route.js "$1" "$2"