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

fix: processing _lidar_data.json files

parent ed5fc52f
No related branches found
No related tags found
1 merge request!16Clhp map into main
This diff is collapsed.
No preview for this file type
...@@ -12,29 +12,33 @@ def get_measurements(self, lake_name): ...@@ -12,29 +12,33 @@ def get_measurements(self, lake_name):
Parameters: Parameters:
self (BaseHTTPRequestHandler): A instance of a BaseHTTPRequestHandler self (BaseHTTPRequestHandler): A instance of a BaseHTTPRequestHandler
cursor (cursor): An Sqlite3 cursor object that points to the database
lake_name (str): The name of the requested file/lake lake_name (str): The name of the requested file/lake
""" """
try: try:
file_path = os.path.join(LAKE_RELATIONS_PATH, lake_name + '_measurements.json') print("Getting measurements...")
file_path = os.path.join(LAKE_RELATIONS_PATH, lake_name + '_lidar_data.json')
# Lists to store processed data
sub_div_ids = [] sub_div_ids = []
measurements = [] measurements = []
# Check if the file exists # Check if the file exists
if os.path.exists(file_path): if os.path.exists(file_path):
# Read the newest lidar data from JSON file # Read the newest lidar data from JSON file
with open(LAKE_RELATIONS_PATH + lake_name + '_measurements.json', 'r') as file: with open(file_path, 'r') as file:
measurements = json.load(file) lidar_data = json.load(file)
# Iterate over all fetched rows # Iterate over all fetched rows
for measurement in measurements: for measurement in lidar_data:
processed_subdivs = []
print("Processing measurement ", measurement['MeasurementID'])
for sub_division in measurement['Subdivisions']: for sub_division in measurement['Subdivisions']:
measurement_id = measurement['MeasurementID']
subdiv_id = sub_division['SubdivID'] subdiv_id = sub_division['SubdivID']
center_lat = sub_division['CenLatitude'] center_lat = sub_division['CenLatitude']
center_lng = sub_division['CenLongitude'] center_lng = sub_division['CenLongitude']
print("Processing subdivision ", subdiv_id)
# Create new subdivision object # Create new subdivision object
sub_division = { sub_division = {
'SubdivID': subdiv_id, 'SubdivID': subdiv_id,
...@@ -52,7 +56,12 @@ def get_measurements(self, lake_name): ...@@ -52,7 +56,12 @@ def get_measurements(self, lake_name):
} }
sub_div_ids.append(subdiv_id) sub_div_ids.append(subdiv_id)
measurements[measurement_id]['Subdivisions'].append(sub_division) # Append processed subdivision data
processed_subdivs.append(sub_division)
# Append processed measurement and subdivisions
measurement['Subdivisions'].append(processed_subdivs)
measurements.append(measurement)
# Populate remaining subdivisions and create "invalid" or "proxy" measurement to store them # Populate remaining subdivisions and create "invalid" or "proxy" measurement to store them
remaining_sub_divs = fill_remaining_subdivisions(lake_name, sub_div_ids) remaining_sub_divs = fill_remaining_subdivisions(lake_name, sub_div_ids)
...@@ -65,18 +74,15 @@ def get_measurements(self, lake_name): ...@@ -65,18 +74,15 @@ def get_measurements(self, lake_name):
'Subdivisions': remaining_sub_divs 'Subdivisions': remaining_sub_divs
} }
# Convert dictionary values to list of measurements
data = list(measurements.values())
# Write the newest measurements to file # Write the newest measurements to file
with open(LAKE_RELATIONS_PATH + lake_name.lower() + '_measurements.json', 'w') as f: with open(LAKE_RELATIONS_PATH + lake_name.lower() + '_measurements.json', 'w') as f:
json.dump(data, f) json.dump(measurements, f)
if len(data) == 0: if len(measurements) == 0:
response_data = json.dumps(['no measurements']) response_data = json.dumps(['no measurements'])
else: else:
# Convert list of dictionaries to JSON # Convert list of dictionaries to JSON
response_data = json.dumps(data, indent=4) response_data = json.dumps(measurements, indent=4)
except Exception as e: except Exception as e:
print(f"Error in getting measurements: {e}") print(f"Error in getting measurements: {e}")
......
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