Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions API/fireguard_app/Fireguard_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 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"]
location = Location(latitude=coordinates["latitude"], longitude=coordinates["longitude"])
Expand All @@ -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
Expand Down Expand Up @@ -126,3 +139,42 @@ 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
8 changes: 4 additions & 4 deletions API/fireguard_app/db_locations/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ 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

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

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

Expand Down