-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_ingestion.py
More file actions
51 lines (39 loc) · 1.74 KB
/
data_ingestion.py
File metadata and controls
51 lines (39 loc) · 1.74 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
import requests
from sqlalchemy.orm import Session
from shapely.geometry import shape, Polygon
from geoalchemy2 import WKBElement
from app.models import GeoData
from app.database import SessionLocal
from app.config import GEOJSON_URL
# Function to fetch and load GeoJSON data into the database
def fetch_and_load_geojson(db: Session, url: str):
response = requests.get(url)
if response.status_code != 200:
print(f"Failed to fetch data from {url}")
return
geojson_data = response.json()
for feature in geojson_data["features"]:
# Extract the name (or set a default if it's missing)
name = feature["properties"].get("name", "Unknown")
# Extract the geometry and convert it to a Shapely object
geometry_type = feature["geometry"]["type"]
coordinates = feature["geometry"]["coordinates"]
if geometry_type == "Polygon":
geometry = Polygon(coordinates[0]) # The first element is the array of coordinates for the polygon
else:
print(f"Unsupported geometry type: {geometry_type}")
continue
# Convert Shapely geometry to WKB (Well-Known Binary) format
wkb_geometry = geometry.wkb # This will give us a binary representation of the geometry
# Create a GeoData instance and add it to the database
db_geodata = GeoData(
name=name,
geometry=WKBElement(wkb_geometry), # Convert the WKB to GeoAlchemy2's WKBElement
properties=feature["properties"] # Store properties as JSON
)
db.add(db_geodata)
# Commit the transaction
db.commit()
print("Data ingestion completed.")
db = SessionLocal()
fetch_and_load_geojson(db, GEOJSON_URL)