-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_route_db.sh
More file actions
executable file
·80 lines (68 loc) · 2.64 KB
/
Copy pathinit_route_db.sh
File metadata and controls
executable file
·80 lines (68 loc) · 2.64 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
#!/bin/bash
set -e
# Load environment variables from scripts/.env
ENV_FILE="$(pwd)/scripts/.env"
if [ -f "$ENV_FILE" ]; then
export $(grep -v '^#' "$ENV_FILE" | xargs)
else
echo "Error: .env file not found at $ENV_FILE"
exit 1
fi
DB_PASS="$DB_PASSWORD"
PBF_FILE="$(pwd)/route-engine/data/south-korea.osm.pbf"
FILTERED_PBF_FILE="$(pwd)/route-engine/data/south-korea-highways.osm.pbf"
echo "========================================="
echo " 1. Filtering Highway Data using Osmium (Official)"
echo "========================================="
if [ ! -f "$PBF_FILE" ]; then
echo "Error: PBF file not found at $PBF_FILE"
exit 1
fi
# Extract only highway tags to drastically reduce file size (from 276MB to ~20MB) using working community image
docker run --rm \
-v "$(pwd)/route-engine/data":/data \
stefda/osmium-tool:latest \
osmium tags-filter /data/south-korea.osm.pbf w/highway -o /data/south-korea-highways.osm.pbf --overwrite
echo "========================================="
echo " 2. Loading Filtered OSM PBF to PostGIS (Official Recommended)"
echo "========================================="
# Load the filtered, much smaller PBF file to RDS using the official-recommended Docker Hub image
# The image entrypoint is 'osm2pgsql', so we only pass the arguments directly.
docker run --rm \
-e PGPASSWORD=$DB_PASS \
-v "$(pwd)/route-engine/data":/data \
iboates/osm2pgsql:latest \
--slim --drop -c -l -d $DB_NAME -U $DB_USER -H $DB_HOST -P $DB_PORT /data/south-korea-highways.osm.pbf
echo "========================================="
echo " 3. Creating Materialized View (postgres:alpine)"
echo "========================================="
SQL_CREATE_MV="
DROP MATERIALIZED VIEW IF EXISTS osm_edge_trash_scores;
CREATE MATERIALIZED VIEW osm_edge_trash_scores AS
SELECT
w.osm_id,
AVG(h.trash_score) AS trash_score
FROM planet_osm_line w
JOIN predicted_hotspots h
ON ST_DWithin(w.way, h.geometry, 0.00003)
WHERE w.highway IS NOT NULL
GROUP BY w.osm_id;
"
docker run --rm \
-e PGPASSWORD=$DB_PASS \
postgres:alpine \
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "$SQL_CREATE_MV"
echo "========================================="
echo " 4. Creating Unique Index for Fast Lookup"
echo "========================================="
SQL_CREATE_INDEX="
CREATE UNIQUE INDEX IF NOT EXISTS idx_osm_edge_trash_scores_osm_id
ON osm_edge_trash_scores(osm_id);
"
docker run --rm \
-e PGPASSWORD=$DB_PASS \
postgres:alpine \
psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "$SQL_CREATE_INDEX"
echo "========================================="
echo " DB Setup Complete! Ready for Routing."
echo "========================================="