#!/bin/bash
# $HeadURL: https://svn.pasta.freemyip.com/main/miniade/trunk/bin/nop-sh $ $LastChangedRevision: 10133 $
# Modules
. $(miniade) || { echo "${0##*/}: ERROR: miniade failed (hint: run 'miniade' to see error)" >&2; exit 1; }
# Configurable stuff
# Other globals
OSMLINK_PRESERVE_FLAG=false
DESC_SIMPLIFY_FLAG=true
# INSTRUCTIONS
#
# 1) Go to https://overpass-turbo.eu/
# 2) In the left-hand panel replace all the code there with this:
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# 3) In the right-hand panel click the little-box icon (tooltip is
# "manually select bbox")
# 4) Select the desired area.
# 5) Click Run.
# 6) Wait.
# 7) Click Export and export as GPX file.
# 8) Create a new directory.
# 9) Call this script from inside the empty directory.
#
# To deduplicate several (rectangular and possibly overlapping) files
# then use:
#
# md5sum ??/*[0-9]* | {
# echo ".width 100"
# echo "CREATE TABLE files (file, md5sum, primary key (file));"
# echo "BEGIN TRANSACTION;"
# while read MD5SUM FILE; do
# echo "INSERT INTO files VALUES ('$FILE', '$MD5SUM');"
# done
# echo "END TRANSACTION;"
# echo "SELECT GROUP_CONCAT(file,' ') FROM files GROUP BY MD5SUM HAVING COUNT(*) != 1;"
# } | sqlite3 -column -noheader | sed -r 's/^.{10}//' | xargs rm
#
# To then merge those into one file do:
#
# cat at/atpro */*[0-9]* at/atepi > all.gpx
#
# and then strip the UTF line at the top of the file (GORP won't load it
# with that present).
#
# To strip that down to a sensible number that are within 50K of the route:
#
# # This should create an arc file (see https://www.gpsbabel.org/htmldoc-development/fmt_arc.html)
# # but seems not to order the points correctly, which means the
# # arc's "corridor" is much wider than it should be, so don't
# # use this:
# #gpsbabel -i gpx -f all.gpx -o arc -F all.arc
# # What works better is just to extract the route points out of the
# # route file:
# sed -rn 's/.*/\1\t\2/p' seven-countries-route-gpswandern.gpx > seven-countries-route-gpswandern.arcman
# # and then get Gpsbabel to remove waypoints from all.gpx that are
# # not close to the arc:
# gpsbabel -i gpx -f all.gpx -x arc,file=seven-countries-route-gpswandern.arcman,distance=50K -o gpx -F all-arcman50K.gpx
main()
{
local MY_ARGS
# Process options
special_opts_handler()
{
case $1 in
*) return 1 ;;
esac
}
miniade_process_options --help-handler=help --special-opts-handler=special_opts_handler MY_ARGS "$@" && set -- "${MY_ARGS[@]}"
# Process arguments
[ $# = 1 ] || miniade_bad_usage
FILE=$1
# Sanity checks and derivations
[[ $FILE =~ \.gpx$ ]] || miniade_error "$FILE: doesn't have .gpx suffix"
OUTPUT_DIR=${FILE%.gpx}
COUNTRY_ABBR=${OUTPUT_DIR##*/}
[[ $COUNTRY_ABBR =~ ^[a-z][a-z]$ ]] || miniade_error "$COUNTRY_ABBR: doesn't look like a country abbreviation"
[ -f $FILE ] || miniade_error "$FILE: not accessible"
[ ! -e $OUTPUT_DIR ] || miniade_error "$OUTPUT_DIR: exists already"
# Guts
miniade_info "$FILE: pre-processing file ..."
TMPFILE=/tmp/$PROGNAME.$$.gpx
cp $FILE $TMPFILE
# csplit breaks on *line-boundaries* not on the specified tag, so
# ensure all tags are on separate lines.
perl -0777 -pi -e 's/\n)//g' $TMPFILE
# Squeeze blank lines
sed -i '/^ *$/d' $TMPFILE
miniade_info "$FILE: splitting into waypoints and tracks ..."
mkdir $OUTPUT_DIR
cd $OUTPUT_DIR
csplit -f $COUNTRY_ABBR -k -b %04d $TMPFILE '/\(\| /dev/null || true
miniade_info "$FILE: saving prologue and epilogue .."
FIRSTXXFILE=${COUNTRY_ABBR}0000
LASTXXFILE=$(ls $COUNTRY_ABBR* | sed -n '$p')
mv $FIRSTXXFILE ${COUNTRY_ABBR}pro
perl -0777 -pi -e 's/<\/gpx>\n//' $LASTXXFILE
echo "" > ${COUNTRY_ABBR}epi
for XXFILE in $COUNTRY_ABBR[0-9]*; do
grep -q trkseg $XXFILE || continue
miniade_info "$FILE/$XXFILE: converting track into waypoint file ..."
DESC_LINES=()
IN_DESC_LINES_FLAG=false
HAD_FIRST_TRKPT_FLAG=false
NAME_LINE=
OSMLINK_LINE=
while read LINE; do
if [[ $LINE =~ ^\$ ]]; then
:
elif [[ $LINE =~ ^\(.*)\$ ]]; then
NAME_LINE=$LINE
elif [[ $LINE =~ ^\$ ]] && $IN_DESC_LINES_FLAG; then
IN_DESC_LINES_FLAG=false
elif { ! $IN_DESC_LINES_FLAG; } && [[ $LINE =~ ^\(.+)$ ]]; then
IN_DESC_LINES_FLAG=true
DESC_LINES=( "${DESC_LINES[@]}" "${BASH_REMATCH[1]}" )
elif $IN_DESC_LINES_FLAG; then
DESC_LINES=( "${DESC_LINES[@]}" "$LINE" )
elif [[ $LINE =~ ^\$ ]]; then
:
elif [[ $LINE =~ ^\$ ]]; then
if ! $HAD_FIRST_TRKPT_FLAG; then
FIRST_TRKPT_LAT_LON=${BASH_REMATCH[1]}
HAD_FIRST_TRKPT_FLAG=true
fi
elif [[ $LINE =~ ^\$ ]]; then
:
elif [[ $LINE =~ ^\$ ]]; then
:
else
miniade_error "$XXFILE: $LINE: can't parse"
fi
done < $XXFILE
$HAD_FIRST_TRKPT_FLAG || miniade_error "$XXFILE: track has no trackpoints"
[ ${#DESC_LINES[*]} -ge 1 ] || miniade_error "$XXFILE: desc looks short"
[ "X$NAME_LINE" != X ] || miniade_error "$XXFILE: no name"
[ "X$OSMLINK_LINE" != X ] || miniade_error "$XXFILE: no link"
{
echo ""
echo "$NAME_LINE"
echo -n ""
for DESC_LINE in "${DESC_LINES[@]}"; do
echo "$DESC_LINE"
done
echo ""
echo "$OSMLINK_LINE"
echo ""
} > $XXFILE
done
if $DESC_SIMPLIFY_FLAG; then
miniade_info "simplifying description ..."
for XXFILE in $COUNTRY_ABBR[0-9]*; do
if ! [[ $(cat $XXFILE) =~ ^(.*)\(.*)\(.*)$ ]]; then
miniade_error "$XXFILE: failed to parse"
fi
PRE_DESC=${BASH_REMATCH[1]}
DESC=${BASH_REMATCH[2]}
POST_DESC=${BASH_REMATCH[3]}
FOUND_FLAG=false
for KEY in website url source ref email; do
if [[ $DESC =~ $KEY=(http[^$'\n']*).* ]]; then
URL=${BASH_REMATCH[1]}
FOUND_FLAG=true
break
fi
done
if $FOUND_FLAG; then
perl -0777 -pi -e "s@.*?@$URL@ms" $XXFILE
else
miniade_warning "$XXFILE: URL not found"
# Previously I erased the description entirely with this:
#
# perl -0777 -pi -e 's@.*?\n@@ms' $XXFILE
#
# But, given that there may be some less useful information there
# (e.g. address, telno) then now we just leave it.
fi
done
fi
}
help()
{
local PROGNAME
miniade_get_progname PROGNAME
echo "Usage: $PROGNAME [ ] "
exit 0
}
main "$@"