diff --git a/etc/.gitignore b/etc/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..94a2dd146a22340832c88013e9fe92663bb9f2cc --- /dev/null +++ b/etc/.gitignore @@ -0,0 +1 @@ +*.json \ No newline at end of file diff --git a/etc/poly2json.sh b/etc/poly2json.sh new file mode 100755 index 0000000000000000000000000000000000000000..1a743cd715d1cc2d6cd65f72f8dc4fa54578ce71 --- /dev/null +++ b/etc/poly2json.sh @@ -0,0 +1,67 @@ +#! /bin/sh +####################################################################################################### +# # +# This convertes a .poly file (http://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Format) # +# to a json file (which then can easily be loaded by leaflet's L.Polygon constructor) # +# # +####################################################################################################### + +# Variable definition: +###################### + +POLY_FILE="$1" +OUTPUT_FILE="${POLY_FILE%%.json}" + +# Startup checks: +################# + +if [ "$#" -lt 1 ] || [ "$#" -gt 2 ] || ! [ -f "$1" ]; then + echo "Usage: $0 POLYGON_IN_FILE [JSON_OUT_FILE]" >&2 + exit 1 +fi + +if [ "$#" -eq 2 ]; then + OUTPUT_FILE="$2" +fi + +if [ -f $OUTPUT_FILE ]; then + echo "Output file exists... not doing anything!" + exit 1 +fi + +# Main logic (copy from poly to json): +###################################### + +# Create output file +touch $OUTPUT_FILE + +# Set the field seperator to a newline +IFS=" +" + +printf "[\n" >> $OUTPUT_FILE + +# Loop through the .poly file +LC_NUMERIC="en_US.UTF-8" +IS_FIRST_ENTRY=true +for LINE in $(cat $POLY_FILE); do + if [[ $LINE != *\.* ]]; then + # Strip lines that do not contain numbers (Header, End-Markers, ...) + continue + fi + + if $IS_FIRST_ENTRY; then + IS_FIRST_ENTRY=false + else + printf ",\n" >> $OUTPUT_FILE + fi + + LINE=$(echo "$LINE" | sed -e 's/^ *//' -e 's/ *$//' -e 's/ */ /g') + IFS=" " + LINE_ARR=($LINE) + + printf "[%0f, %0f]" "${LINE_ARR[0]}" "${LINE_ARR[1]}" >> $OUTPUT_FILE +done + +printf "\n]\n" >> $OUTPUT_FILE +exit 0