Skip to content

Instantly share code, notes, and snippets.

@angoca
Last active June 4, 2024 04:50
Show Gist options
  • Save angoca/dff941220631dace54901a3fec3de9df to your computer and use it in GitHub Desktop.
Save angoca/dff941220631dace54901a3fec3de9df to your computer and use it in GitHub Desktop.
#!/bin/bash
# Downloads all public traces on OpenStreetMap for a given user.
# This requires gunzip and bzip2 to extract the compressed traces.
# Also, wget to retrieve the traces from OSM.
#
# Arguments.
# 1) Username. If the username has spaces, you should provide the name between quotes.
#
# Version: 2024-05-26
# Author: Andres Gomez (AngocA)
set -e
set -u
# Prepares the environment.
USER="${1}"
DIR="./${USER}"
TEMP_TRACES_PAGE="${DIR}/tracesPage"
TEMP_LIST_TRACE="${DIR}/tracesList"
mkdir -p "${DIR}"
set +e
rm -f "${DIR}/*" 2> /dev/null
set -e
# Get base page.
echo "Retrieving base page for user ${USER}..."
wget "https://www.openstreetmap.org/user/${USER}/traces" -O "${TEMP_TRACES_PAGE}" 2> /dev/null
grep '/traces/' "${TEMP_TRACES_PAGE}" | grep gpx | awk -F/ '{print $5}' | awk -F'"' '{print $1}' > "${TEMP_LIST_TRACE}"
for i in $(cat "${TEMP_LIST_TRACE}"); do
GPX_TEMP_FILE="${DIR}/${i}.gpx"
wget "http://www.openstreetmap.org/trace/${i}/data" -O "${GPX_TEMP_FILE}" 2> /dev/null
done
# Get recursive pages.
BEFORE=$(grep before "${TEMP_TRACES_PAGE}" | grep -v referer | awk -F'?' '{print $2}' | awk -F= '{print $2}' | awk -F'"' '{print $1}' | tail -1)
while [[ -n "${BEFORE}" ]] ; do
echo "Retrieving traces before ${BEFORE} for user ${USER}..."
wget "https://www.openstreetmap.org/user/${USER}/traces?before=${BEFORE}" -O "${TEMP_TRACES_PAGE}" 2> /dev/null
grep '/traces/' "${TEMP_TRACES_PAGE}" | grep gpx | awk -F/ '{print $5}' | awk -F'"' '{print $1}' > "${TEMP_LIST_TRACE}"
for i in $(cat "${TEMP_LIST_TRACE}"); do
GPX_TEMP_FILE="${DIR}/${i}.gpx"
wget "http://www.openstreetmap.org/trace/${i}/data" -O "${GPX_TEMP_FILE}" 2> /dev/null
done
unset BEFORE
BEFORE=$(grep before "${TEMP_TRACES_PAGE}" | grep -v referer | awk -F'?' '{print $2}' | awk -F= '{print $2}' | awk -F'"' '{print $1}' | tail -1)
done
# Cleans temporal files.
rm -f "${TEMP_TRACES_PAGE}" "${TEMP_LIST_TRACE}"
cd "${DIR}"
# Extracting compressed traces.
echo "Extracting compressed traces..."
for i in $(ls -1) ; do
TYPE=$(file "${i}")
set +e
BZIP2=$(echo "${TYPE}" | grep bzip2)
GZIP=$(echo "${TYPE}" | grep gzip)
set -e
if [[ -n "${BZIP2:-}" ]] ; then
mv "${i}" "${i}.bz2"
bzip2 -d "${i}.bz2"
elif [[ -n "${GZIP:-}" ]] ; then
mv "${i}" "${i}.gz"
gunzip "${i}.gz"
fi
unset BZIP2
unset GZIP
done
set +e
COUNT=$(ls -1 | wc -l)
set -e
# Creating tar file.
tar -cvf "${USER}.tar" *.gpx
echo "${COUNT} GPX traces retrieved."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment