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

update: split responses

parent 358f2326
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
...@@ -2,7 +2,7 @@ from flask import Flask ...@@ -2,7 +2,7 @@ from flask import Flask
from http.server import HTTPServer, BaseHTTPRequestHandler from http.server import HTTPServer, BaseHTTPRequestHandler
from consts import SSL_CERT_PATH, SSL_KEY_PATH, HOST, PORT from consts import SSL_CERT_PATH, SSL_KEY_PATH, HOST, PORT
from map.get_measurements import get_all_markers from map.get_measurements import get_all_markers
from map.add_lake import cut_map from map.add_lake import fetch_divided_map
from APIs.get_weather import get_weather from APIs.get_weather import get_weather
from map.input_new_data import input_new_Lidar_data from map.input_new_data import input_new_Lidar_data
import ssl import ssl
...@@ -40,7 +40,7 @@ class IceHTTP(BaseHTTPRequestHandler): ...@@ -40,7 +40,7 @@ class IceHTTP(BaseHTTPRequestHandler):
get_all_markers(self, self.cursor, 'mjosa') # Get all markers get_all_markers(self, self.cursor, 'mjosa') # Get all markers
# NB: temporary hardcoded waterBodyName # NB: temporary hardcoded waterBodyName
elif self.path == '/get_relation': elif self.path == '/get_relation':
cut_map(self, 'Mjosa') # NB temp hardcoded value fetch_divided_map(self, 'mjosa') # NB temp hardcoded value
def do_POST(self): def do_POST(self):
if self.path == '/get_weather_data': if self.path == '/get_weather_data':
......
No preview for this file type
No preview for this file type
...@@ -69,7 +69,7 @@ def cut_map(self, body_of_water: str): # NB: implement body_of_water ...@@ -69,7 +69,7 @@ def cut_map(self, body_of_water: str): # NB: implement body_of_water
'type': 'Feature', 'type': 'Feature',
'properties': { 'properties': {
'sub_div_id': str(sub_div_id), 'sub_div_id': str(sub_div_id),
'group_id': '', # Initialised empty, will be set upon requesting the relation 'group_id': '', # Initialised empty, will be set upon requesting the relation
'measurement_id': '', 'measurement_id': '',
'sub_div_center': center, 'sub_div_center': center,
...@@ -143,14 +143,21 @@ def write_json_to_file(path: str, file_name: str, json_data: dict): ...@@ -143,14 +143,21 @@ def write_json_to_file(path: str, file_name: str, json_data: dict):
json.dump(json_data, f) json.dump(json_data, f)
def get_divided_map(file_name): def fetch_divided_map(self, file_name):
geo_data = gpd.read_file("server/map/" + file_name + ".geojson") self.send_response(200)
polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon'] self.send_header("Content-type", "application/json")
polygons = [Polygon(polygon.exterior) for polygon in polygon_data['geometry']] self.end_headers()
# Extract contents from JSON file
with open("server/lake_relations/" + file_name + "_div.json", "r") as file:
data = file.read()
# Write contents of the JSON file to response
self.wfile.write(data.encode('utf-8'))
# Returns a list of [(sub_div_id, sub_div_center)] # Returns a list of [(sub_div_id, sub_div_center)]
def get_id_and_center(file_name): # NB buggy def get_id_and_center(file_name): # NB buggy
# Expected format: [(id, [x,y]), (id, [x,y])] # Expected format: [(id, [x,y]), (id, [x,y])]
geo_data = gpd.read_file("server/lake_relations/" + file_name + "_div.json") geo_data = gpd.read_file("server/lake_relations/" + file_name + "_div.json")
subdivisions = [] subdivisions = []
...@@ -166,4 +173,3 @@ def get_id_and_center(file_name): # NB buggy ...@@ -166,4 +173,3 @@ def get_id_and_center(file_name): # NB buggy
} }
subdivisions.append(subdivision) subdivisions.append(subdivision)
return subdivisions return subdivisions
...@@ -2,6 +2,7 @@ import json ...@@ -2,6 +2,7 @@ import json
from datetime import datetime from datetime import datetime
import random import random
import geopandas as gpd import geopandas as gpd
from server.map.add_lake import write_json_to_file
# get_markers requests all marker data or valid markers, converts the data to json, and writes # get_markers requests all marker data or valid markers, converts the data to json, and writes
...@@ -66,7 +67,7 @@ def get_all_markers(self, cursor, waterBodyName): ...@@ -66,7 +67,7 @@ def get_all_markers(self, cursor, waterBodyName):
'Subdivisions': [sub_division], # Array of sub_division objects 'Subdivisions': [sub_division], # Array of sub_division objects
} }
########################### TEST DATA ########################################### ##################################### TEST DATA ###########################################
# Temporary test data # Temporary test data
test_measurements = [] test_measurements = []
subdiv_id = 17 subdiv_id = 17
...@@ -106,10 +107,10 @@ def get_all_markers(self, cursor, waterBodyName): ...@@ -106,10 +107,10 @@ def get_all_markers(self, cursor, waterBodyName):
} }
test_measurements.append(measurement) test_measurements.append(measurement)
########################### TEST DATA ########################################### ##################################### TEST DATA ###########################################
# Convert dictionary values to list of measurements # Convert dictionary values to list of measurements
measurements_list = list(measurement_data.values()) + test_measurements data = list(measurement_data.values()) + test_measurements
######################### ADD GROUP_IDS TO RELATION DATA ################################## ######################### ADD GROUP_IDS TO RELATION DATA ##################################
...@@ -118,7 +119,7 @@ def get_all_markers(self, cursor, waterBodyName): ...@@ -118,7 +119,7 @@ def get_all_markers(self, cursor, waterBodyName):
relation_data = geo_data[geo_data['geometry'].geom_type == 'Polygon'] relation_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
# Add group IDs to lake relation # Add group IDs to lake relation
for measurement in measurements_list: for measurement in data:
measurement_id = str(measurement['MeasurementID']) # Extract measurement ID measurement_id = str(measurement['MeasurementID']) # Extract measurement ID
for subdivision in measurement['Subdivisions']: for subdivision in measurement['Subdivisions']:
subDivID = str(subdivision['SubdivID']) # Convert to string to match format in feature subDivID = str(subdivision['SubdivID']) # Convert to string to match format in feature
...@@ -135,14 +136,13 @@ def get_all_markers(self, cursor, waterBodyName): ...@@ -135,14 +136,13 @@ def get_all_markers(self, cursor, waterBodyName):
relation_data.at[index, 'measurement_id'] = measurement_id relation_data.at[index, 'measurement_id'] = measurement_id
# relation_data.at[index, 'sub_div_center'] = feature['sub_div_center'] # relation_data.at[index, 'sub_div_center'] = feature['sub_div_center']
#################################################################################### # Convert GeoDataFrame to JSON and update json file
# Convert GeoDataFrame to JSON
relation_data_json = json.loads(relation_data.to_json()) relation_data_json = json.loads(relation_data.to_json())
# Combine measurements_list and relation_data write_json_to_file("server/lake_relations", "mjosa", relation_data_json)
data = [measurements_list, relation_data_json]
####################################################################################
if len(rows) == 0 or len(data) == 0: # Return 500 and empty list if no data is found if len(rows) == 0 or len(data) == 0: # Return 500 and empty list if no data is found
print(f"No data which meets the condition found") print(f"No data which meets the condition found")
marker_data = '[]' marker_data = '[]'
......
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