coords are now geojson

This commit is contained in:
Matteo Rosati
2026-01-15 11:59:15 +01:00
parent 49c5225027
commit 4af1996fb4
2 changed files with 234 additions and 3 deletions

View File

@@ -17,6 +17,78 @@ COLLECTION_NAME = os.getenv("COLLECTION_NAME")
TIMEZONE = ZoneInfo("Europe/Rome")
def convert_e6_to_degrees(e6_value):
"""
Convert E6 format to degrees.
Args:
e6_value: Integer value in E6 format (multiply by 1e-6 to get degrees)
Returns:
Float value in degrees
"""
return e6_value / 1_000_000 if e6_value else 0.0
def create_geojson_point(lat_e6, lng_e6):
"""
Create a GeoJSON Point from E6 coordinates.
Args:
lat_e6: Latitude in E6 format
lng_e6: Longitude in E6 format
Returns:
GeoJSON Point object or None if coordinates are invalid
"""
if lat_e6 == 0 and lng_e6 == 0:
return None
lat = convert_e6_to_degrees(lat_e6)
lng = convert_e6_to_degrees(lng_e6)
# GeoJSON Point format: [longitude, latitude]
return {
"type": "Point",
"coordinates": [lng, lat]
}
def transform_plext(plext):
"""
Transform a plext to use GeoJSON format for coordinates.
Args:
plext: Plext dictionary to transform
Returns:
Transformed plext with GeoJSON coordinates
"""
transformed = plext.copy()
# Transform coordinates field
if "coordinates" in transformed and isinstance(transformed["coordinates"], dict):
coords = transformed["coordinates"]
if "lat" in coords and "lng" in coords:
geojson = create_geojson_point(coords["lat"], coords["lng"])
if geojson:
transformed["coordinates"] = geojson
# Transform markup array
if "markup" in transformed and isinstance(transformed["markup"], list):
new_markup = []
for item in transformed["markup"]:
new_item = item.copy()
if "latE6" in item and "lngE6" in item:
geojson = create_geojson_point(item["latE6"], item["lngE6"])
if geojson:
new_item["location"] = geojson
new_markup.append(new_item)
transformed["markup"] = new_markup
return transformed
def get_time_range():
"""
Calculate the time range for the current execution.
@@ -67,7 +139,7 @@ def fetch_plexts(min_timestamp, max_timestamp):
def save_to_mongodb(plexts):
"""
Save plexts to MongoDB.
Save plexts to MongoDB with GeoJSON coordinates.
Args:
plexts: List of plext dictionaries to save
@@ -84,8 +156,11 @@ def save_to_mongodb(plexts):
db = client[DB_NAME]
collection = db[COLLECTION_NAME]
# Insert all plexts
result = collection.insert_many(plexts)
# Transform plexts to use GeoJSON format
transformed_plexts = [transform_plext(plext) for plext in plexts]
# Insert all transformed plexts
result = collection.insert_many(transformed_plexts)
print(f"Inserted {len(result.inserted_ids)} plexts to MongoDB")
return True
except PyMongoError as e: