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

add: txt file with center coordinates and subidvision ids

parent e6917e12
No related branches found
No related tags found
1 merge request!12Clhp map
import ssl
import json
import sqlite3
from flask import Flask from flask import Flask
from urllib.parse import urlparse, parse_qs
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_handler.get_measurements import get_all_markers
from map_handler.add_lake import cut_map from map_handler.add_lake import cut_map
from map_handler.process_lake import fetch_divided_map from map_handler.get_measurements import get_all_markers
from map_handler.input_new_data import input_new_Lidar_data from map_handler.input_new_data import input_new_Lidar_data
from urllib.parse import urlparse, parse_qs from map_handler.process_lake import get_divided_map, get_ids_and_centers
import ssl
import json
import sqlite3
app = Flask(__name__) app = Flask(__name__)
terminate_server = 0 terminate_server = 0
...@@ -60,9 +61,11 @@ class IceHTTP(BaseHTTPRequestHandler): ...@@ -60,9 +61,11 @@ class IceHTTP(BaseHTTPRequestHandler):
elif self.path.startswith('/get_relation'): elif self.path.startswith('/get_relation'):
parsed_path = urlparse(self.path) parsed_path = urlparse(self.path)
query_params = parse_qs(parsed_path.query) query_params = parse_qs(parsed_path.query)
fetch_divided_map(self, 'Mjosa') # NB temp hardcoded value get_divided_map(self, 'Mjosa') # NB temp hardcoded value
elif self.path == '/divide_new_relation': elif self.path == '/divide_new_relation':
cut_map(self, 'Mjosa') cut_map(self, 'Mjosa')
elif self.path == '/test':
get_ids_and_centers('server/map_handler/lake_relations/mjosa_div.json')
def do_POST(self): def do_POST(self):
if self.path == '/new_lidar_data': if self.path == '/new_lidar_data':
......
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -8,7 +8,7 @@ import os ...@@ -8,7 +8,7 @@ import os
# Read a json file with relation data and send to response object # Read a json file with relation data and send to response object
def cut_map(self, body_of_water: str): # NB: implement body_of_water def cut_map(self, body_of_water: str): # NB: implement body_of_water
# Read relation from GeoJson file and extract all polygons # Read relation from GeoJson file and extract all polygons
geo_data = gpd.read_file("server/lake_relations/mjosa.geojson") geo_data = gpd.read_file("server/map_handler/lake_relations/mjosa.geojson")
polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon'] polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
polygons = [Polygon(polygon.exterior) for polygon in polygon_data['geometry']] polygons = [Polygon(polygon.exterior) for polygon in polygon_data['geometry']]
...@@ -17,6 +17,10 @@ def cut_map(self, body_of_water: str): # NB: implement body_of_water ...@@ -17,6 +17,10 @@ def cut_map(self, body_of_water: str): # NB: implement body_of_water
divided_map = [] divided_map = []
# List of all subdivisions with their center coordinates
# Expected format: [(sub_div_id, [x,y]), (sub_div_id, [x,y]), ...]
sub_div_center_list = []
for polygon in polygons: for polygon in polygons:
cell_width = 0.04 cell_width = 0.04
cell_height = 0.02 # NB could be calculated based on cell_width and distance from equator cell_height = 0.02 # NB could be calculated based on cell_width and distance from equator
...@@ -65,6 +69,8 @@ def cut_map(self, body_of_water: str): # NB: implement body_of_water ...@@ -65,6 +69,8 @@ def cut_map(self, body_of_water: str): # NB: implement body_of_water
rounded_coordinates.append(rounded_coords) rounded_coordinates.append(rounded_coords)
rounded_tile = Polygon(rounded_coordinates) rounded_tile = Polygon(rounded_coordinates)
sub_div_center_list.append((sub_div_id, center[0], center[1]))
tile_feature = { tile_feature = {
'type': 'Feature', 'type': 'Feature',
'properties': { 'properties': {
...@@ -82,7 +88,13 @@ def cut_map(self, body_of_water: str): # NB: implement body_of_water ...@@ -82,7 +88,13 @@ def cut_map(self, body_of_water: str): # NB: implement body_of_water
'tile_count': sub_div_id, # Add the last subdivision ID as number of tiles 'tile_count': sub_div_id, # Add the last subdivision ID as number of tiles
} }
#write_json_to_file("server/lake_relations", "mjosa", feature_collection) # NB currently hardcoded file path
# Create the txt file specified by the path
with open("server/map_handler/lake_relations/mjosa_centers.txt", 'w') as file:
# Iterate over the list and write each element to the file
for sub_div_id, x, y in sub_div_center_list:
file.write(f"{sub_div_id}, {x}, {y}\n") # Write each list entry on a new line
self.send_response(200) self.send_response(200)
self.send_header("Content-type", "application/json") self.send_header("Content-type", "application/json")
self.end_headers() self.end_headers()
......
import json import json
from datetime import datetime from datetime import datetime
import random import random
from random import randint
# 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
...@@ -26,7 +25,7 @@ def get_all_markers(self, cursor, waterBodyName): ...@@ -26,7 +25,7 @@ def get_all_markers(self, cursor, waterBodyName):
rows = cursor.fetchall() rows = cursor.fetchall()
# Container for all fetched measurement objects # List of all fetched measurement objects
measurement_data = {} measurement_data = {}
''' '''
...@@ -37,25 +36,28 @@ def get_all_markers(self, cursor, waterBodyName): ...@@ -37,25 +36,28 @@ def get_all_markers(self, cursor, waterBodyName):
# Iterate over all fetched rows # Iterate over all fetched rows
for row in rows: for row in rows:
measurement_id = row[0] measurement_id = row[0]
sub_div_id = row[8]
center_lat = row[12]
center_lng = row[13]
''' '''
curr_stat = [] curr_stat = []
# Find matching ice stat # Find matching ice stat
for stat in data['stats'] for stat in data['stats']
if ice_stats.sub_div_id == row[8] if ice_stats.sub_div_id == sub_div_id
curr_stat = stat curr_stat = stat
break break
''' '''
# Create subdivision new object # Create subdivision new object
sub_division = { sub_division = {
'SubdivID': row[8], 'SubdivID': sub_div_id,
'GroupID': row[9], 'GroupID': row[9],
'MinThickness': row[10], 'MinThickness': row[10],
'AvgThickness': row[11], 'AvgThickness': row[11],
'CenLatitude': row[12], 'CenLatitude': center_lat,
'CenLongitude': row[13], 'CenLongitude': center_lng,
'Accuracy': row[14], 'Accuracy': row[14],
'Color': calculateColor(row[11]), # NB color calculated based on average thickness, should be minimum 'Color': calculateColor(row[11]), # NB color calculated based on average thickness, should be minimum
'IceStats': () # NB temp empty, add ice stats later 'IceStats': () # NB temp empty, add ice stats later
...@@ -151,7 +153,7 @@ def get_all_markers(self, cursor, waterBodyName): ...@@ -151,7 +153,7 @@ def get_all_markers(self, cursor, waterBodyName):
self.wfile.write(marker_data.encode('utf-8')) self.wfile.write(marker_data.encode('utf-8'))
def calculateColor(thickness: float): # NB not final colors nor ranges def calculateColor(thickness: float): # NB neither final colors nor ranges
if 0 < thickness <= 4: if 0 < thickness <= 4:
return 0xFFff0000 # Red return 0xFFff0000 # Red
elif 4 < thickness <= 6: elif 4 < thickness <= 6:
......
0, 0.0345, 0.02
1, 0.0277, 0.02
2, 0.0371, 0.02
3, 0.0284, 0.0171
4, 0.0293, 0.02
5, 0.0022, 0.0014
6, 0.04, 0.0186
7, 0.04, 0.02
8, 0.0134, 0.006
9, 0.04, 0.02
10, 0.0008, 0.0007
11, 0.0361, 0.0073
12, 0.04, 0.0173
13, 0.0388, 0.0122
14, 0.04, 0.0198
15, 0.034, 0.0182
16, 0.04, 0.02
17, 0.0029, 0.0029
18, 0.0003, 0.0007
19, 0.0364, 0.02
20, 0.0215, 0.0059
21, 0.0222, 0.02
22, 0.008, 0.02
23, 0.006, 0.0047
24, 0.04, 0.02
25, 0.04, 0.02
26, 0.0256, 0.02
27, 0.0152, 0.0113
28, 0.0072, 0.0049
29, 0.0318, 0.02
30, 0.0334, 0.02
31, 0.0255, 0.02
32, 0.0154, 0.02
33, 0.0121, 0.02
34, 0.0018, 0.0009
35, 0.0033, 0.0015
36, 0.04, 0.02
37, 0.04, 0.02
38, 0.0336, 0.02
39, 0.0136, 0.0063
40, 0.0, 0.0001
41, 0.0002, 0.0004
42, 0.0022, 0.0023
43, 0.025, 0.02
44, 0.0359, 0.02
45, 0.0308, 0.02
46, 0.0169, 0.007
47, 0.0, 0.0
48, 0.0001, 0.0
49, 0.0018, 0.0007
50, 0.04, 0.0084
51, 0.04, 0.02
52, 0.04, 0.0124
53, 0.04, 0.0168
54, 0.04, 0.02
55, 0.0062, 0.0018
56, 0.04, 0.02
57, 0.04, 0.0144
58, 0.0363, 0.02
59, 0.0086, 0.0024
60, 0.04, 0.02
61, 0.04, 0.02
62, 0.04, 0.0126
63, 0.04, 0.0163
64, 0.0052, 0.006
65, 0.037, 0.0141
66, 0.0148, 0.0077
67, 0.04, 0.02
68, 0.04, 0.02
69, 0.04, 0.02
70, 0.04, 0.0091
71, 0.04, 0.02
72, 0.0011, 0.0036
73, 0.0354, 0.02
74, 0.0152, 0.0065
75, 0.0184, 0.0065
76, 0.0346, 0.0169
77, 0.0044, 0.0015
78, 0.0235, 0.0074
79, 0.0006, 0.0001
80, 0.04, 0.02
81, 0.04, 0.02
82, 0.04, 0.02
83, 0.04, 0.02
84, 0.04, 0.02
85, 0.04, 0.02
86, 0.0213, 0.0174
87, 0.0328, 0.02
88, 0.0258, 0.02
89, 0.0217, 0.02
90, 0.0189, 0.0035
91, 0.04, 0.0089
92, 0.04, 0.02
93, 0.04, 0.02
94, 0.04, 0.02
95, 0.04, 0.02
96, 0.04, 0.02
97, 0.04, 0.02
98, 0.04, 0.02
99, 0.0368, 0.02
100, 0.0169, 0.0095
101, 0.0025, 0.0006
102, 0.04, 0.0174
103, 0.04, 0.02
104, 0.04, 0.02
105, 0.04, 0.02
106, 0.04, 0.02
107, 0.04, 0.02
108, 0.04, 0.02
109, 0.04, 0.02
110, 0.0017, 0.0002
111, 0.0002, 0.0
112, 0.04, 0.02
113, 0.04, 0.02
114, 0.04, 0.02
115, 0.0387, 0.02
116, 0.0344, 0.02
117, 0.0156, 0.02
118, 0.0163, 0.02
119, 0.04, 0.02
120, 0.0057, 0.0043
121, 0.0023, 0.0017
122, 0.0341, 0.0142
123, 0.0005, 0.0001
124, 0.0074, 0.006
125, 0.0001, 0.0003
126, 0.0002, 0.0001
127, 0.04, 0.02
128, 0.04, 0.02
129, 0.0362, 0.02
130, 0.0063, 0.0155
131, 0.0337, 0.0087
132, 0.0002, 0.0001
133, 0.0048, 0.0052
134, 0.0013, 0.0006
135, 0.0342, 0.02
136, 0.0002, 0.0002
137, 0.0018, 0.0006
138, 0.0175, 0.0114
139, 0.04, 0.02
140, 0.04, 0.02
141, 0.0148, 0.02
142, 0.0027, 0.0126
143, 0.0302, 0.0191
144, 0.0289, 0.02
145, 0.0023, 0.0004
146, 0.0206, 0.0173
147, 0.0027, 0.0006
148, 0.0171, 0.0156
149, 0.0026, 0.0027
150, 0.0041, 0.0132
151, 0.002, 0.0053
152, 0.0018, 0.0029
153, 0.04, 0.02
154, 0.04, 0.02
155, 0.04, 0.02
156, 0.04, 0.02
157, 0.04, 0.02
158, 0.0006, 0.0005
159, 0.04, 0.02
160, 0.04, 0.02
161, 0.0105, 0.0092
162, 0.0381, 0.02
163, 0.0339, 0.0188
164, 0.0355, 0.02
165, 0.0, 0.0
166, 0.001, 0.0002
167, 0.0022, 0.001
168, 0.0075, 0.02
169, 0.0087, 0.02
170, 0.011, 0.02
171, 0.0107, 0.02
172, 0.0078, 0.02
173, 0.0117, 0.02
174, 0.0119, 0.02
175, 0.0188, 0.02
176, 0.0361, 0.02
177, 0.04, 0.0169
178, 0.0165, 0.02
179, 0.0014, 0.0003
180, 0.0026, 0.0027
181, 0.0186, 0.0144
182, 0.0204, 0.0139
183, 0.0035, 0.0029
184, 0.0011, 0.0007
185, 0.0036, 0.0018
186, 0.0029, 0.0014
187, 0.003, 0.0011
188, 0.0005, 0.0001
189, 0.0018, 0.0005
190, 0.0004, 0.0004
191, 0.0027, 0.0016
192, 0.0005, 0.0004
193, 0.0003, 0.0003
194, 0.0009, 0.0006
195, 0.0004, 0.0006
196, 0.0024, 0.0014
197, 0.0012, 0.0009
198, 0.0003, 0.0001
199, 0.0002, 0.0001
200, 0.0003, 0.0003
201, 0.0004, 0.0004
202, 0.0002, 0.0002
203, 0.0002, 0.0003
204, 0.0009, 0.0003
205, 0.0011, 0.0006
206, 0.0012, 0.0004
207, 0.0003, 0.0003
208, 0.0001, 0.0001
209, 0.0001, 0.0001
210, 0.0013, 0.0006
211, 0.0013, 0.0005
212, 0.0002, 0.0001
213, 0.001, 0.0002
214, 0.0002, 0.0001
215, 0.0001, 0.0
216, 0.0001, 0.0
217, 0.0, 0.0
218, 0.0001, 0.0
219, 0.0, 0.0
220, 0.0001, 0.0001
221, 0.0001, 0.0001
222, 0.0001, 0.0
223, 0.0, 0.0
224, 0.0001, 0.0001
225, 0.0001, 0.0
226, 0.0007, 0.0002
227, 0.0001, 0.0001
228, 0.0007, 0.0009
229, 0.0002, 0.0001
230, 0.0, 0.0
231, 0.0, 0.0
232, 0.0002, 0.0
233, 0.0, 0.0
234, 0.0001, 0.0
235, 0.0, 0.0
236, 0.0, 0.0
237, 0.0, 0.0
238, 0.0, 0.0
239, 0.0001, 0.0001
240, 0.0, 0.0
241, 0.0, 0.0
242, 0.0, 0.0
243, 0.0, 0.0
244, 0.0, 0.0
245, 0.0, 0.0
246, 0.0, 0.0
247, 0.0, 0.0
248, 0.0, 0.0
249, 0.0, 0.0
250, 0.0002, 0.0001
251, 0.0001, 0.0
252, 0.0, 0.0
253, 0.0, 0.0
254, 0.0, 0.0
255, 0.0001, 0.0
256, 0.0, 0.0
257, 0.0, 0.0
258, 0.0, 0.0
259, 0.0, 0.0
260, 0.0, 0.0
261, 0.0001, 0.0001
262, 0.0, 0.0
263, 0.0001, 0.0001
264, 0.0, 0.0
265, 0.0, 0.0
266, 0.0001, 0.0
267, 0.0001, 0.0
268, 0.0001, 0.0
269, 0.0, 0.0
270, 0.0, 0.0
271, 0.0, 0.0
272, 0.0, 0.0
273, 0.0, 0.0
274, 0.0, 0.0
275, 0.0, 0.0
276, 0.0, 0.0
277, 0.0, 0.0
278, 0.0, 0.0
279, 0.0, 0.0
280, 0.0, 0.0
281, 0.0, 0.0
282, 0.0, 0.0
283, 0.0, 0.0
284, 0.0002, 0.0
285, 0.0001, 0.0001
286, 0.0001, 0.0
287, 0.0001, 0.0
288, 0.0009, 0.0002
289, 0.0015, 0.0002
290, 0.0013, 0.0007
291, 0.0011, 0.0005
292, 0.0009, 0.0004
293, 0.0002, 0.0002
294, 0.0003, 0.0002
295, 0.0001, 0.0001
296, 0.0002, 0.0001
297, 0.0003, 0.0001
298, 0.0009, 0.0002
299, 0.0007, 0.0004
300, 0.0001, 0.0001
301, 0.0003, 0.0005
302, 0.0004, 0.0003
303, 0.001, 0.0001
304, 0.0005, 0.0001
305, 0.0004, 0.0001
306, 0.0001, 0.0
307, 0.0004, 0.0002
import geopandas as gpd import json
# Writes contents of a map_handler json file to the response # Writes contents of a lake json file to the response
def fetch_divided_map(self, file_name): def get_divided_map(self, file_name):
self.send_response(200) self.send_response(200)
self.send_header("Content-type", "application/json") self.send_header("Content-type", "application/json")
self.end_headers() self.end_headers()
...@@ -15,20 +15,16 @@ def fetch_divided_map(self, file_name): ...@@ -15,20 +15,16 @@ def fetch_divided_map(self, file_name):
self.wfile.write(data.encode('utf-8')) self.wfile.write(data.encode('utf-8'))
# Returns a list of [(sub_div_id, sub_div_center)] # Returns a list of all sub_division ids and their center coordinates for a given lake
def get_ids_and_centers(file_name): # NB buggy def get_ids_and_centers(file_path): # NB buggy
# Expected format: [(id, [x,y]), (id, [x,y])] # Expected format: [(sub_div_id, [x,y]), (sub_div_id, [x,y]), ...]
geo_data = gpd.read_file("server/map_handler/lake_relations/" + file_name + "_div.json") return_list = []
subdivisions = []
for index, row in geo_data.iterrows():
sub_div_id = row['sub_div_id']
sub_div_center = row['sub_div_center']
print("sub_div_id: ", sub_div_id) f = open(file_path)
data = json.load(f)
subdivision = { for measurement in data['measurement']:
'sub_div_id': sub_div_id, for sub_div in measurement['Subdivisions']:
'sub_div_center': sub_div_center return_list.append((sub_div['SubdivID'], (sub_div['CenLatitude'], sub_div['CenLongitude'])))
}
subdivisions.append(subdivision) return return_list
return subdivisions
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