Skip to content
Snippets Groups Projects
Commit a7b76e40 authored by Hoa Ben The Nguyen's avatar Hoa Ben The Nguyen
Browse files

fix: some errors, current problem unique constraint

parent 46cc40aa
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -45,13 +45,13 @@ def distance(point1, point2):
return math.sqrt(abs(x2 - x1)**2 + abs(y2 - y1)**2)
# find the closest point in json list
def closest_points(point, list):
def closest_points(point, list, coords_taken):
closest_point = None
closest_dist = float('inf')
for current_point in list:
lng, lat = current_point['properties']['sub_div_center'][0]
dist = distance(point, (lat, lng))
if dist < closest_dist:
if dist < closest_dist and current_point not in coords_taken:
closest_dist = dist
closest_point = current_point
return closest_point
......@@ -91,9 +91,11 @@ def calculate_area_data(center, cell_size, body_of_water):
# container for all the heights in area
area_heights = []
# zone coords taken
taken_coords = []
# set the limit of the area compared to local coordinates
area_limit = calculate_corners(center[0],center[1], cell_size_to_meters)
# read json data with path to data for specific water body
file_name = "server/lake_relations/" + body_of_water + "_div.json"
with open(file_name) as data:
......@@ -138,7 +140,12 @@ def calculate_area_data(center, cell_size, body_of_water):
xs, ys = start
xe, ye = end
sub_center = ((xs + xe)/2, (ys + ye)/2)
current_map_zone = closest_points(sub_center, map_data)
if len(map_data) > 0:
current_map_zone = closest_points(sub_center, map_data, taken_coords)
taken_coords.append((current_map_zone['properties']['sub_div_center'][0][1], current_map_zone['properties']['sub_div_center'][0][0]))
else:
return [0]
current_zone_id = current_map_zone['properties']['sub_div_id']
# filter data within sub-area zones
......
......@@ -47,7 +47,7 @@ class IceHTTP(BaseHTTPRequestHandler):
get_weather(self)
elif self.path == '/new_lidar_data':
input_new_Lidar_data(self,self.cursor, 'Mjosa') # hardcoded body of water must change later
input_new_Lidar_data(self, self.cursor, 'Mjosa') # hardcoded body of water must change later
# Start a server on port 8443 using self defined HTTP class
if __name__ == "__main__":
......
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -6,8 +6,8 @@ from server.data_processing.process_lidar_data import calculate_area_data, about
def input_new_Lidar_data(self, cursor, bodyOfWater):
try:
# hard coded coordinates
latitude = 60.0
longitude = 10.0
latitude = 61.0
longitude = 11.0
sensorId = 1
total_measurement_average = 0 # the total average of a measurement
......@@ -22,22 +22,22 @@ def input_new_Lidar_data(self, cursor, bodyOfWater):
INSERT INTO Measurement( SensorID, TimeMeasured, WaterBodyName,
WholeAverageThickness, CenterLat, CenterLon) VALUES
(?,?,?,?,?,?);
''', (sensorId, datetime.utcnow().replace(microsecond=0), bodyOfWater, 0, latitude, longitude, scale_factor))
''', (sensorId, datetime.utcnow().replace(microsecond=0), bodyOfWater, 0, latitude, longitude))
# auto generate new measurement id
measurement_id = cursor.lastrowid
# calculate the area of to be calculated based on the coordinates given to the calculation model
areas_data = calculate_area_data((latitude, longitude), 0.04, bodyOfWater)
lidar_json_data = {}
print(len(areas_data))
lidar_json_data = []
if(areas_data):
# store lidar data in jason formate
# calculate data for each zone within the area
for area in areas_data:
# lng and lat relative to map
map_lng, map_lat = area
map_lng, map_lat = area[2]
if(len(area[2]) != 0):
average = sum(area[2])/len(area[2])
......@@ -47,12 +47,12 @@ def input_new_Lidar_data(self, cursor, bodyOfWater):
minimum_thickness = 0
total_measurement_average += average
print("should be unique ", measurement_id, " ", area[0])
# input the data into the database
cursor.execute('''
INSERT INTO SubDivision(MeasurementID, SubDivisionID, GroupID, MinimumThickness, AverageThickness, CenterLatitude, CenterLongitude, Accuracy) VALUES
(?,?,?,?,?,?,?,?);
''',(measurement_id, area[0], area[1], float(minimum_thickness), float(average), float(map_lat), float(map_lng), float(1)))
''', (measurement_id, area[0], area[1], float(minimum_thickness), float(average), float(map_lat), float(map_lng), scale_factor))
# set up json formate
lidar_read = {
......@@ -60,7 +60,7 @@ def input_new_Lidar_data(self, cursor, bodyOfWater):
'SubId': area[0],
'GroupId': area[1],
'SubCenter': (map_lat, map_lng),
'Heights': area[2]
'Heights': area[3]
}
lidar_json_data.append(lidar_read)
......@@ -91,7 +91,7 @@ def input_new_Lidar_data(self, cursor, bodyOfWater):
content = json.dumps(lidar_json_data)
else:
print('No data found')
content = []
content = json.dumps([])
# Write content data to response object
self.wfile.write(content.encode('utf-8'))
......
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