-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_osm.sh
More file actions
90 lines (78 loc) · 2.68 KB
/
import_osm.sh
File metadata and controls
90 lines (78 loc) · 2.68 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
81
82
83
84
85
86
87
88
89
90
#!/bin/sh
set -e
echo "Waiting for PostGIS to be ready..."
until pg_isready -h "$PGHOST" -U "$PGUSER"; do
sleep 2
done
# -----------------------------
# Variables
# -----------------------------
OSM_URL="https://download.geofabrik.de/europe/germany-latest.osm.pbf"
OSM_FILE="/data/germany-latest.osm.pbf"
DUMP_FILE="/data/filtered_osm_data.sql.gz"
# -----------------------------
# 1. NEW: Check if SQL Dump exists (Highest Priority)
# -----------------------------
if [ -f "$DUMP_FILE" ]; then
echo "Found SQL Dump ($DUMP_FILE). Checking database..."
# Check if data is already in the DB to avoid double import
ROWS=$(psql -h "$PGHOST" -U "$PGUSER" -d "$PGDATABASE" -tAc \
"SELECT COUNT(*) FROM education_poi LIMIT 1;" 2>/dev/null || echo 0)
if [ "$ROWS" -gt 0 ]; then
echo "Data already exists in database. Skipping SQL import."
else
echo "Database is empty. Importing directly from SQL Dump (Fast mode)..."
zcat "$DUMP_FILE" | psql -h "$PGHOST" -U "$PGUSER" -d "$PGDATABASE"
echo "SQL Import finished successfully."
fi
echo "Process complete."
exit 0
fi
# -----------------------------
# 2. Check if data is already imported
# -----------------------------
ROWS=$(psql -h "$PGHOST" -U "$PGUSER" -d "$PGDATABASE" -tAc \
"SELECT COUNT(*) FROM education_poi LIMIT 1;" 2>/dev/null || echo 0)
if [ "$ROWS" -gt 0 ]; then
echo "Data already exists in database. Skipping import."
else
# -----------------------------
# 3. Download OSM data if missing (Fallback)
# -----------------------------
echo "No SQL Dump found. Starting standard OSM processing..."
if [ ! -f "$OSM_FILE" ]; then
echo "Downloading Germany OSM data from Geofabrik..."
wget -O "$OSM_FILE" "$OSM_URL"
else
echo "OSM file already exists, skipping download."
fi
echo "Cleaning up old tables..."
psql -h "$PGHOST" -U "$PGUSER" -d "$PGDATABASE" <<EOF
DROP TABLE IF EXISTS
education_poi, education_area, leisure_poi, leisure_area,
pedestrian_roads, tram_stations, landuse_areas;
EOF
echo "Starting OSM import (Flex Mode - filtering into your custom tables)..."
# --slim is required for memory management
# --drop cleans up temporary tables after finishing
osm2pgsql \
--slim \
--drop \
--output=flex \
--style /styles/osm.lua \
--cache 12000 \
--number-processes 2 \
-d "$PGDATABASE" \
-U "$PGUSER" \
-H "$PGHOST" \
"$OSM_FILE"
echo "OSM import completed successfully."
fi
# -----------------------------
# 4. Cleanup
# -----------------------------
echo "Cleaning up PBF file..."
rm -f "$OSM_FILE"
echo "--------------------------------------------------"
echo "PROCESS FINISHED!"
echo "--------------------------------------------------"