Skip to content

Commit 903cd8b

Browse files
committed
fix: load chicago_drive.graphml from GCS when not bundled in Docker
1 parent 81257bc commit 903cd8b

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

backend/risk_cache.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,33 @@ def get_graph():
278278
if _graph is not None:
279279
return _graph
280280

281-
# Try local cached GraphML first (bundled in Docker image)
281+
import osmnx as ox
282+
283+
# 1. Try local cached GraphML (present in local dev, absent in Docker — gitignored)
282284
local_graphml = OUTPUT_DIR / "chicago_drive.graphml"
283285
if local_graphml.exists():
284-
import osmnx as ox
285286
_graph = ox.load_graphml(local_graphml)
286287
print(f"[risk_cache] loaded graph from local file ({_graph.number_of_nodes()} nodes)", flush=True)
287288
return _graph
288289

289-
# Fallback: download from OSM (slow, for local dev without cached file)
290-
print("[risk_cache] no local GraphML, downloading from OSM...", flush=True)
290+
# 2. Try GCS (uploaded once; persists across Cloud Run revisions)
291+
try:
292+
import tempfile
293+
fs = _get_gcs_fs()
294+
print("[risk_cache] downloading chicago_drive.graphml from GCS...", flush=True)
295+
with tempfile.NamedTemporaryFile(suffix=".graphml", delete=False) as tmp:
296+
with fs.open("safeway-data/chicago_drive.graphml", "rb") as f:
297+
tmp.write(f.read())
298+
tmp_path = tmp.name
299+
_graph = ox.load_graphml(tmp_path)
300+
os.unlink(tmp_path)
301+
print(f"[risk_cache] loaded graph from GCS ({_graph.number_of_nodes()} nodes)", flush=True)
302+
return _graph
303+
except Exception as e:
304+
print(f"[risk_cache] GCS graph load failed: {e}", flush=True)
305+
306+
# 3. Last resort: download from OSM (very slow, dev-only)
307+
print("[risk_cache] falling back to OSM download (slow)...", flush=True)
291308
import sys
292309
backend_dir = str(Path(__file__).resolve().parent)
293310
if backend_dir not in sys.path:

0 commit comments

Comments
 (0)