Spaces:
No application file
No application file
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
import sqlite3
|
| 5 |
+
from pyrosm import OSM
|
| 6 |
+
import numpy as np
|
| 7 |
+
from shapely.geometry import Point
|
| 8 |
+
|
| 9 |
+
# -------------------------------
|
| 10 |
+
# 1️ Download latest OSM Map
|
| 11 |
+
# -------------------------------
|
| 12 |
+
def download_osm(url, output_dir="data"):
|
| 13 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 14 |
+
filename = os.path.join(output_dir, url.split("/")[-1])
|
| 15 |
+
if os.path.exists(filename):
|
| 16 |
+
print(f"{filename} already exists. Skipping download.")
|
| 17 |
+
return filename
|
| 18 |
+
print(f"Downloading {url} ...")
|
| 19 |
+
with requests.get(url, stream=True) as r:
|
| 20 |
+
r.raise_for_status()
|
| 21 |
+
with open(filename, "wb") as f:
|
| 22 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 23 |
+
f.write(chunk)
|
| 24 |
+
print("Download complete!")
|
| 25 |
+
return filename
|
| 26 |
+
|
| 27 |
+
fl_url = "https://download.geofabrik.de/north-america/us/florida-latest.osm.pbf"
|
| 28 |
+
osm_file = download_osm(fl_url)
|
| 29 |
+
|
| 30 |
+
# ----------------------------------------
|
| 31 |
+
# 2️ Prepare SQLite database with R-tree
|
| 32 |
+
# ----------------------------------------
|
| 33 |
+
os.makedirs("output", exist_ok=True)
|
| 34 |
+
conn = sqlite3.connect("output/fl_roads.sqlite")
|
| 35 |
+
c = conn.cursor()
|
| 36 |
+
|
| 37 |
+
# Table for road attributes
|
| 38 |
+
c.execute("""
|
| 39 |
+
CREATE TABLE IF NOT EXISTS roads (
|
| 40 |
+
id INTEGER PRIMARY KEY,
|
| 41 |
+
name TEXT,
|
| 42 |
+
highway TEXT,
|
| 43 |
+
maxspeed TEXT
|
| 44 |
+
);
|
| 45 |
+
""")
|
| 46 |
+
|
| 47 |
+
# R-tree table for spatial queries
|
| 48 |
+
c.execute("""
|
| 49 |
+
CREATE VIRTUAL TABLE IF NOT EXISTS roads_index USING rtree(
|
| 50 |
+
id, minx, maxx, miny, maxy
|
| 51 |
+
);
|
| 52 |
+
""")
|
| 53 |
+
conn.commit()
|
| 54 |
+
|
| 55 |
+
# ----------------------------------------
|
| 56 |
+
# 3️ Load OSM and iterate through roads
|
| 57 |
+
# ----------------------------------------
|
| 58 |
+
print("Loading OSM...")
|
| 59 |
+
osm = OSM(osm_file)
|
| 60 |
+
|
| 61 |
+
# Use generator to avoid loading everything into memory
|
| 62 |
+
roads_generator = osm.get_data_by_custom_criteria(
|
| 63 |
+
custom_filter={"highway": True},
|
| 64 |
+
filter_type="keep",
|
| 65 |
+
as_generator=True
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
print("Processing roads and writing to database...")
|
| 69 |
+
for idx, road in enumerate(roads_generator):
|
| 70 |
+
geom = road.geometry
|
| 71 |
+
if geom is None:
|
| 72 |
+
continue
|
| 73 |
+
|
| 74 |
+
minx, miny, maxx, maxy = geom.bounds
|
| 75 |
+
road_id = int(road.id)
|
| 76 |
+
name = road.get("name", "")
|
| 77 |
+
highway = road.get("highway", "")
|
| 78 |
+
maxspeed = road.get("maxspeed", "")
|
| 79 |
+
|
| 80 |
+
# Insert into R-tree
|
| 81 |
+
c.execute("INSERT INTO roads_index VALUES (?, ?, ?, ?, ?)",
|
| 82 |
+
(road_id, minx, maxx, miny, maxy))
|
| 83 |
+
|
| 84 |
+
# Insert into main table
|
| 85 |
+
c.execute("INSERT INTO roads VALUES (?, ?, ?, ?)",
|
| 86 |
+
(road_id, name, highway, maxspeed))
|
| 87 |
+
|
| 88 |
+
# Commit periodically to avoid large transactions
|
| 89 |
+
if idx % 1000 == 0:
|
| 90 |
+
conn.commit()
|
| 91 |
+
print(f"Processed {idx} roads...")
|
| 92 |
+
|
| 93 |
+
conn.commit()
|
| 94 |
+
conn.close()
|
| 95 |
+
print("Finished! SQLite database saved at output/fl_roads.sqlite")
|