Repository navigation
Expand file tree
/
Copy pathosm_import.sh
More file actions
executable file
·80 lines (72 loc) · 2.53 KB
/
Copy pathosm_import.sh
File metadata and controls
executable file
·80 lines (72 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env bash
#
# osm_import.sh — fetch the UK postbox dataset from Overpass and run the
# Firestore importer. Pass-through: any unrecognised arg is forwarded to
# functions/import_postboxes.js (e.g. --dry-run, --prune, --no-manifest,
# --limit N, --overwrite-corrections, --project ...).
#
# Script-level flag:
# --skip-download Reuse the existing ./postboxes.json instead of fetching.
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
OUT="postboxes.json"
TMP="${OUT}.tmp"
ENDPOINT="https://overpass-api.de/api/interpreter"
USER_AGENT="postbox-game-import/1.0 (+richard@agilepixel.io)"
skip_download=0
forward_args=()
for arg in "$@"; do
case "$arg" in
--skip-download) skip_download=1 ;;
*) forward_args+=("$arg") ;;
esac
done
if [ "$skip_download" -eq 0 ]; then
echo "Fetching UK postboxes from Overpass…"
start=$(date +%s)
# Heredoc query — readable, easy to tweak. [timeout:300] is the server-side
# budget for the query itself; curl's --max-time is the client-side cap.
curl \
--fail-with-body \
--retry 5 --retry-delay 10 --retry-all-errors \
--connect-timeout 30 --max-time 600 \
-sS \
-A "$USER_AGENT" \
--data-urlencode 'data@-' \
-o "$TMP" \
"$ENDPOINT" <<'OVERPASS'
[out:json][timeout:300];
area["name"="United Kingdom"]->.uk;
node[amenity=post_box](area.uk);
out;
OVERPASS
# Sanity-check the response before clobbering the last good file. node is
# already a project dependency — no extra tooling. A throttled/HTML error
# response or a near-empty payload fails fast and leaves the tmp file in
# place for inspection.
node -e '
const fs = require("fs");
const data = JSON.parse(fs.readFileSync(process.argv[1], "utf8"));
if (!Array.isArray(data.elements) || data.elements.length < 1000) {
console.error("Unexpected Overpass response: elements=" +
(data.elements?.length ?? "missing"));
process.exit(1);
}
' "$TMP"
mv "$TMP" "$OUT"
size=$(wc -c < "$OUT" | tr -d ' ')
elapsed=$(( $(date +%s) - start ))
echo "Downloaded $OUT ($(numfmt --to=iec "$size" 2>/dev/null || echo "${size}B")) in ${elapsed}s."
else
if [ ! -f "$OUT" ]; then
echo "Error: --skip-download set but $SCRIPT_DIR/$OUT does not exist." >&2
exit 1
fi
echo "Reusing existing $OUT (--skip-download)."
fi
echo "Running importer…"
# import_postboxes.js wants to run from functions/ so node_modules resolve.
cd functions
exec node import_postboxes.js "../$OUT" --project the-postbox-game "${forward_args[@]}"