From ea3fa8d50a66b67e65a27856273c09283a8a9c5b Mon Sep 17 00:00:00 2001 From: Susanne <599032@stud.hvl.no> Date: Tue, 6 May 2025 20:12:47 +0200 Subject: [PATCH 1/3] added the option to add new location when not in database --- API/fireguard_app/Fireguard_API.py | 61 +++++++++++++++++++++++--- API/fireguard_app/db_locations/crud.py | 8 ++-- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/API/fireguard_app/Fireguard_API.py b/API/fireguard_app/Fireguard_API.py index 35c09df..d80b7a4 100644 --- a/API/fireguard_app/Fireguard_API.py +++ b/API/fireguard_app/Fireguard_API.py @@ -7,6 +7,7 @@ from .db_locations.crud import LocationOperations import math import numpy as np +import requests # Initialize the Fire Risk API frc = METFireRiskAPI() @@ -24,12 +25,25 @@ def get_fire_risk(loc: str, days_past: int = 7, weatherdata: bool = False): """ Fetches weather data and fire risk predictions for a given location. + Adds the location to the database if it doesn't exist. + :param loc: The location name (e.g., "Oslo") """ loc = loc.capitalize() # Define the location with their latitude and longitude location_db = operator_db.get_location_by_name(loc) if location_db is None: - return {"error": "Location not found in the database."} + try: + coordinates = get_coordinates_from_StedsnavnAPI(loc) + new_location = { + "name": loc, + "coordinates": coordinates, + "fireRiskPrediction": None, + "lastModified": None, + } + operator_db.create_location(new_location) + except: + return {"error": f"Location {loc} not found in StedsnavnAPI."} + location_db = operator_db.get_location_by_name(loc) coordinates = location_db["coordinates"] location = Location(latitude=coordinates["latitude"], longitude=coordinates["longitude"]) @@ -45,11 +59,10 @@ def get_fire_risk(loc: str, days_past: int = 7, weatherdata: bool = False): } try: - fire_risk = frc.compute_now(location, obs_delta) - - fire_risk_dict = serialize_fire_risk_prediction(fire_risk) - operator_db.update_location_firerisk(loc, fire_risk_dict) if not beenModified: + fire_risk = frc.compute_now(location, obs_delta) + fire_risk_dict = serialize_fire_risk_prediction(fire_risk) + operator_db.update_location_firerisk(loc, fire_risk_dict) data = { "location": {"name": loc, "latitude": location.latitude, "longitude": location.longitude}, "fireRiskPrediction": fire_risk @@ -126,3 +139,41 @@ def trenddetector(list_of_index, array_of_data, order=1): } return trends + +def get_coordinates_from_StedsnavnAPI(location_name, kommunenavn=None): + + url = "https://ws.geonorge.no/stedsnavn/v1/navn" + params = { + "sok": location_name, + "treffPerSide": 10, + } + response = requests.get(url, params=params) + if response.status_code == 200: + data = response.json() + places =data.get("navn", []) + if kommunenavn: + kommunenavn.capitalize() + for place in places: + # Check if any kommune in the place has the matching kommunenavn + if any(kommune.get("kommunenavn") == kommunenavn for kommune in place.get("kommuner", [])): + place = place + break + else: + place = None + else: + place = places[0] if places else None + + if place: + representasjonspunkt = place.get("representasjonspunkt", {}) + if representasjonspunkt: + latitude = representasjonspunkt.get("nord") + longitude = representasjonspunkt.get("øst") + if latitude is not None and longitude is not None: + return { + "latitude": latitude, + "longitude": longitude, + } + else: + return None + else: + return None diff --git a/API/fireguard_app/db_locations/crud.py b/API/fireguard_app/db_locations/crud.py index 9272a9b..b955788 100644 --- a/API/fireguard_app/db_locations/crud.py +++ b/API/fireguard_app/db_locations/crud.py @@ -14,14 +14,14 @@ def collection_exists(self, collection_name: str): collections = self.collection.database.list_collection_names() return collection_name in collections except Exception as e: - print(f"An error occurred: {e}") + print(f"An error occurred when checking if the collection exists: {e}") return False def create_location(self, location_data: dict): try: result = self.collection.insert_one(location_data) except Exception as e: - print(f"An error occurred: {e}") + print(f"An error occurred when trying to create a location: {e}") return None return result.inserted_id @@ -29,7 +29,7 @@ def get_location_by_name(self, location_name: str): try: location = self.collection.find_one({"name": location_name}) except Exception as e: - print(f"An error occurred: {e}") + print(f"An error occurred when retriving a location by name: {e}") return None return location @@ -37,7 +37,7 @@ def get_location(self, location_id: str): try: location = self.collection.find_one({"_id": ObjectId(location_id)}) except Exception as e: - print(f"An error occurred: {e}") + print(f"An error occurred when retrieving a location by id: {e}") return None return location From 996e59c162ada037a8b51ec9b9c95c65c1cd819a Mon Sep 17 00:00:00 2001 From: Susanne <599032@stud.hvl.no> Date: Thu, 8 May 2025 23:35:23 +0200 Subject: [PATCH 2/3] fixed style stuff --- API/fireguard_app/Fireguard_API.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/API/fireguard_app/Fireguard_API.py b/API/fireguard_app/Fireguard_API.py index d80b7a4..f04c735 100644 --- a/API/fireguard_app/Fireguard_API.py +++ b/API/fireguard_app/Fireguard_API.py @@ -41,8 +41,8 @@ def get_fire_risk(loc: str, days_past: int = 7, weatherdata: bool = False): "lastModified": None, } operator_db.create_location(new_location) - except: - return {"error": f"Location {loc} not found in StedsnavnAPI."} + except Exception as e: + return {"error": f"Location {loc} not found in StedsnavnAPI. Error: {str(e)}"} location_db = operator_db.get_location_by_name(loc) coordinates = location_db["coordinates"] @@ -140,21 +140,22 @@ def trenddetector(list_of_index, array_of_data, order=1): return trends + def get_coordinates_from_StedsnavnAPI(location_name, kommunenavn=None): url = "https://ws.geonorge.no/stedsnavn/v1/navn" params = { "sok": location_name, - "treffPerSide": 10, + "treffPerSide": 10, } response = requests.get(url, params=params) if response.status_code == 200: - data = response.json() + data = response.json() places =data.get("navn", []) if kommunenavn: kommunenavn.capitalize() for place in places: - # Check if any kommune in the place has the matching kommunenavn + # Check if any kommune in the place has the matching kommunenavn if any(kommune.get("kommunenavn") == kommunenavn for kommune in place.get("kommuner", [])): place = place break @@ -163,7 +164,7 @@ def get_coordinates_from_StedsnavnAPI(location_name, kommunenavn=None): else: place = places[0] if places else None - if place: + if place: representasjonspunkt = place.get("representasjonspunkt", {}) if representasjonspunkt: latitude = representasjonspunkt.get("nord") @@ -175,5 +176,5 @@ def get_coordinates_from_StedsnavnAPI(location_name, kommunenavn=None): } else: return None - else: + else: return None From 47e05553c523dc92113a017b72125684be44bc1e Mon Sep 17 00:00:00 2001 From: Susanne <599032@stud.hvl.no> Date: Thu, 8 May 2025 23:38:50 +0200 Subject: [PATCH 3/3] fix style stuff2 --- API/fireguard_app/Fireguard_API.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/API/fireguard_app/Fireguard_API.py b/API/fireguard_app/Fireguard_API.py index f04c735..1cc546c 100644 --- a/API/fireguard_app/Fireguard_API.py +++ b/API/fireguard_app/Fireguard_API.py @@ -150,8 +150,8 @@ def get_coordinates_from_StedsnavnAPI(location_name, kommunenavn=None): } response = requests.get(url, params=params) if response.status_code == 200: - data = response.json() - places =data.get("navn", []) + data = response.json() + places = data.get("navn", []) if kommunenavn: kommunenavn.capitalize() for place in places: