Skip to content
Snippets Groups Projects
Commit a4f2701e authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

update: start to add back centers, not working

parent 61bd0932
No related branches found
No related tags found
1 merge request!19Clhp map, license agreement
Showing
with 776 additions and 28 deletions
No preview for this file type
This diff is collapsed.
sentinel_config.py
\ No newline at end of file
File added
File added
File added
File added
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -140,4 +140,4 @@ def calculate_area_data(center, body_of_water, path): ...@@ -140,4 +140,4 @@ def calculate_area_data(center, body_of_water, path):
return [] # return [0] if no data collected from lidar return [] # return [0] if no data collected from lidar
print(calculate_area_data((61, 11), 'mj\u00f8sa', "server\\lidar_data\\mj\u00f8sa\\measurement_id_2.laz")) #print(calculate_area_data((61, 11), 'mj\u00f8sa', "server\\lidar_data\\mj\u00f8sa\\measurement_id_2.laz"))
\ No newline at end of file \ No newline at end of file
...@@ -126,7 +126,7 @@ class IceHTTP(BaseHTTPRequestHandler): ...@@ -126,7 +126,7 @@ class IceHTTP(BaseHTTPRequestHandler):
addTestData(self, lake_name) addTestData(self, lake_name)
elif self.path.startswith('/new_lidar_data'): elif self.path.startswith('/new_lidar_data'):
parsed_path = urlparse(self.path) parsed_path = urlparse(self.path)
query_params = parse_qs(parsed_path.query) query_params = parse_qs(parsed_path.query)
...@@ -140,10 +140,9 @@ class IceHTTP(BaseHTTPRequestHandler): ...@@ -140,10 +140,9 @@ class IceHTTP(BaseHTTPRequestHandler):
self.send_header('Content-type', 'application/json') self.send_header('Content-type', 'application/json')
self.end_headers() self.end_headers()
#def do_POST(self):
# Start a server on port 8443 using self defined HTTP class
if __name__ == "__main__": if __name__ == "__main__":
"""Start a server on port 8443 using self defined HTTP class"""
try: try:
# Initialize database connection # Initialize database connection
conn = sqlite3.connect('server/database/icedb') conn = sqlite3.connect('server/database/icedb')
......
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -52,6 +52,7 @@ def cut_map(cursor, lake_name: str, cell_size_in_km: float = 0.5) -> (int, str): ...@@ -52,6 +52,7 @@ def cut_map(cursor, lake_name: str, cell_size_in_km: float = 0.5) -> (int, str):
features = [] # List to store new GeoJSON feature objects features = [] # List to store new GeoJSON feature objects
sub_div_id = 0 # Tracker to create unique subdivision ids sub_div_id = 0 # Tracker to create unique subdivision ids
divided_map = [] # Object for plotting the tiles divided_map = [] # Object for plotting the tiles
centers = [] # Object to store subdivision ids and their center coordinates
# Process all polygons # Process all polygons
for polygon in polygons: for polygon in polygons:
...@@ -82,11 +83,11 @@ def cut_map(cursor, lake_name: str, cell_size_in_km: float = 0.5) -> (int, str): ...@@ -82,11 +83,11 @@ def cut_map(cursor, lake_name: str, cell_size_in_km: float = 0.5) -> (int, str):
rounded_tile = Polygon(rounded_coordinates) rounded_tile = Polygon(rounded_coordinates)
geometry = rounded_tile.__geo_interface__ geometry = rounded_tile.__geo_interface__
if not geometry['coordinates']: if not geometry['coordinates'] or len(geometry['coordinates']) < 3:
continue # Skip empty tiles continue # Skip empty tiles and tiles with fewer than 3 coordinates
# Create new feature object # Create new feature object
tile_feature = { cell_feature = {
'type': 'Feature', 'type': 'Feature',
'properties': { 'properties': {
'sub_div_id': str(sub_div_id), 'sub_div_id': str(sub_div_id),
...@@ -94,8 +95,11 @@ def cut_map(cursor, lake_name: str, cell_size_in_km: float = 0.5) -> (int, str): ...@@ -94,8 +95,11 @@ def cut_map(cursor, lake_name: str, cell_size_in_km: float = 0.5) -> (int, str):
}, },
'geometry': geometry 'geometry': geometry
} }
centers.append((sub_div_id, (center[0], center[1]))) # Add the new subdivision to the centers list
# Append new feature object to list, and increment sub_div_id for next iteration # Append new feature object to list, and increment sub_div_id for next iteration
features.append(tile_feature) features.append(cell_feature)
print("len features = ", len(features))
sub_div_id += 1 sub_div_id += 1
break # NB test break break # NB test break
...@@ -123,7 +127,7 @@ def cut_map(cursor, lake_name: str, cell_size_in_km: float = 0.5) -> (int, str): ...@@ -123,7 +127,7 @@ def cut_map(cursor, lake_name: str, cell_size_in_km: float = 0.5) -> (int, str):
# Plot the newly created map and save it to a new file # Plot the newly created map and save it to a new file
plot_map(divided_map) plot_map(divided_map)
write_json_to_file(lake_name, feature_collection) save_all_data(lake_name, feature_collection, centers)
# Return OK and the newly divided map # Return OK and the newly divided map
return 200, feature_collection return 200, feature_collection
...@@ -170,33 +174,43 @@ def create_grid(poly: Polygon, cell_width: float, cell_height: float): ...@@ -170,33 +174,43 @@ def create_grid(poly: Polygon, cell_width: float, cell_height: float):
return grid_lines return grid_lines
def write_json_to_file(lake_name: str, map_data: dict): def save_all_data(lake_name: str, map_data: dict, centers: list):
""" """
Writes a divided map to a JSON file and updates all_lake_names.json Writes a divided map to a JSON file and updates all_lake_names.json
Parameters: Parameters:
lake_name (str): Name of the lake and file to write to lake_name (str): Name of the lake and file to write to
map_data (dict): List of map polygons converted to a JSON dictionary map_data (dict): List of map polygons converted to a JSON dictionary
centers (list): List of all subdivisions and their center coordiantes
""" """
# Create and write divided map to new file try:
print("Writing to file...") # Create and write divided map to new file
if not os.path.exists(LAKE_RELATIONS_PATH): print("Writing to file...")
raise Exception("Directory from path does not exist") if not os.path.exists(LAKE_RELATIONS_PATH):
raise Exception("Directory from path does not exist")
with open(LAKE_RELATIONS_PATH + '/' + lake_name + '_div.json', 'w') as f:
json.dump(map_data, f)
with open(LAKE_RELATIONS_PATH + '/' + lake_name + '_div.json', 'w') as f: # Read all_system_lakes.json
json.dump(map_data, f) with open(LAKE_RELATIONS_PATH + 'all_lake_names.json', 'r', encoding='utf-8') as f:
data = json.load(f)
# Read all_system_lakes.json # Check if the lake name exists in the list
with open(LAKE_RELATIONS_PATH + 'all_lake_names.json', 'r', encoding='utf-8') as f: if lake_name not in data:
data = json.load(f) data.append(lake_name) # Only append to list if it does not already exist in the file
# Check if the lake name exists in the list # Update all_lake_names.json with new lake name
if lake_name not in data: with open(LAKE_RELATIONS_PATH + 'all_lake_names.json', 'w', encoding='utf-8') as f:
data.append(lake_name) # Only append to list if it does not already exist json.dump(data, f, ensure_ascii=False, indent=2)
# Update all_lake_names.json with new lake name # Save the file of all subdivisions and center coordinates
with open(LAKE_RELATIONS_PATH + 'all_lake_names.json', 'w', encoding='utf-8') as f: with open(LAKE_RELATIONS_PATH + lake_name + "_centers.txt", "w") as f:
json.dump(data, f, ensure_ascii=False, indent=2) for sub_div_id, (cen_x, cen_y) in centers:
f.write(f"{sub_div_id}, {cen_x}, {cen_y}\n") # Format each line
except Exception as e:
print("Failed to save the map data: ", e)
# Plotting the map can take a considerable amount of time, especially when creating maps with many # Plotting the map can take a considerable amount of time, especially when creating maps with many
......
This diff is collapsed.
...@@ -3,7 +3,7 @@ import json ...@@ -3,7 +3,7 @@ import json
from shapely.geometry import Polygon, LineString from shapely.geometry import Polygon, LineString
from server.consts import LAKE_RELATIONS_PATH from server.consts import LAKE_RELATIONS_PATH
from server.map_handler.add_new_lake import create_grid, write_json_to_file, cut_map from server.map_handler.add_new_lake import create_grid, save_all_data, cut_map
def test_cut_map() -> None: def test_cut_map() -> None:
...@@ -41,7 +41,7 @@ def test_write_json_to_file() -> None: ...@@ -41,7 +41,7 @@ def test_write_json_to_file() -> None:
test_path = LAKE_RELATIONS_PATH + '/' + test_lake_name + '_div.json' test_path = LAKE_RELATIONS_PATH + '/' + test_lake_name + '_div.json'
# Call the function that is being tested # Call the function that is being tested
write_json_to_file(test_lake_name, test_data) save_all_data(test_lake_name, test_data)
# Try to read the data from the newly created file # Try to read the data from the newly created file
with open(test_path, 'r') as f: with open(test_path, 'r') as f:
......
...@@ -71,7 +71,6 @@ def update_measurements(lake_name: str) -> (int, str): ...@@ -71,7 +71,6 @@ def update_measurements(lake_name: str) -> (int, str):
# Read from json file # Read from json file
# lakName_sub_div.json # lakName_sub_div.json
# Add center coord txt file # Add center coord txt file
# Call update function once daily
# Retrieve ice statistics for current subdivision # Retrieve ice statistics for current subdivision
ice_stats = get_raw_dates(ice_prognosis_raw_data(sub_div_id=subdiv_id, x=center_lat, y=center_lng)) ice_stats = get_raw_dates(ice_prognosis_raw_data(sub_div_id=subdiv_id, x=center_lat, y=center_lng))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment