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

merge:

parents 104b221e ea862781
No related branches found
No related tags found
No related merge requests found
import json
# get_markers requests all marker data or valid markers, converts the data to json, and writes
# the data to the response object
def get_all_markers(self, cursor, waterBodyName):
try:
sql_query = '''
SELECT m.MeasurementID, m.SensorID, m.TimeMeasured, m.CenterLat, m.CenterLon,
s.SensorType, s.Active,
b.Name,
d.SubDivisionID, d.GroupID, d.MinimumThickness,
d.AverageThickness, d.CenterLatitude, d.CenterLongitude,
d.Accuracy
FROM Measurement m
INNER JOIN Sensor s ON m.SensorID = s.SensorID
INNER JOIN BodyOfWater b ON m.WaterBodyName = b.Name
LEFT JOIN SubDivision d ON m.MeasurementID = d.MeasurementID
WHERE b.Name = 'Mjosa'
'''
cursor.execute(sql_query)
rows = cursor.fetchall()
# Container for all fetched measurement objects
measurement_data = {}
# Iterate over all fetched rows
for row in rows:
measurement_id = row[0]
# Create subdivision new object
sub_division = {
'SubdivID': row[8],
'GroupID': row[9],
'MinThickness': row[10],
'AvgThickness': row[11],
'CenLatitude': row[12],
'CenLongitude': row[13],
'Accuracy': row[14]
}
# Check if measurement ID already exists in measurement_data
if measurement_id in measurement_data:
# Create new subdivision within measurement if it does not already exist
if sub_division not in measurement_data[measurement_id]['Subdivisions']:
measurement_data[measurement_id]['Subdivisions'].append(sub_division)
else:
# Create a new entry for measurement_id if it does not already exist in the list
measurement_data[measurement_id] = {
'MeasurementID': measurement_id,
'TimeMeasured': row[2],
'CenterLat': row[3],
'CenterLon': row[4],
'Sensor': { # Each measurement only has one related sensor
'SensorID': row[1],
'SensorType': row[5],
'Active': bool(row[6])
},
'Subdivisions': [sub_division], # Array of sub_division objects
'Corners': [], # NB remove value if not used
}
# Convert dictionary values to list of measurements
data = list(measurement_data.values())
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")
marker_data = '[]'
else:
# Convert list of dictionaries to JSON
marker_data = json.dumps(data, indent=4)
except Exception as e:
print(f"Error in querying database: {e}")
marker_data = '[]'
# Set headers
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
# Write marker data to response object
self.wfile.write(marker_data.encode('utf-8'))
import geopandas as gpd
from shapely.geometry import Polygon, LineString, MultiLineString
from shapely.ops import linemerge, unary_union, polygonize
import matplotlib.pyplot as plt
import random
import json
import os
# Read a json file with relation data and send to response object
def get_relation(self, body_of_water: str): # NB: implement body_of_water
# Read relation from GeoJson file and extract all polygons
geo_data = gpd.read_file("server/map/mjosa.geojson")
polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
polygons = [Polygon(polygon.exterior) for polygon in polygon_data['geometry']]
if len(polygons) <= 1:
raise Exception("Failed to convert JSON object to Shapely Polygons")
divided_map = []
for polygon in polygons:
cell_size = 0.04
lines = create_grid(polygon, cell_size)
lines.append(polygon.boundary)
lines = unary_union(lines)
lines = linemerge(lines)
lines = list(polygonize(lines))
divided_map.extend(combine_grid_with_poly(polygon, lines))
'''
####################### PLOTTING ############################
tiles = [gpd.GeoDataFrame(geometry=[tile]) for tile in divided_map]
print("Plotting... This may take some time...")
# NB test plot
fig, ax = plt.subplots()
ax.set_aspect(1.5)
# Plot each tile
for tile in tiles: # NB temporarily limited to 5 tiles
random_color = "#{:06x}".format(random.randint(0, 0xFFFFFF))
gpd.GeoSeries(tile.geometry).plot(ax=ax, facecolor=random_color, edgecolor='none')
plt.show()
##################### PLOTTIND END ###########################
'''
features = []
sub_div_id = 0
for tile in divided_map: # NB temporarily limited to 5 tiles
# Round coordinates to 4 decimals
center = round(tile.centroid.coords[0][0], 4), round(tile.centroid.coords[0][1], 4)
rounded_coordinates = []
if isinstance(tile, Polygon):
for coords in tile.exterior.coords:
rounded_coords = (round(coords[0], 4), round(coords[1], 4))
rounded_coordinates.append(rounded_coords)
rounded_tile = Polygon(rounded_coordinates)
tile_feature = {
'type': 'Feature',
'properties': {
'sub_div_id': str(sub_div_id),
'sub_div_center': center
},
'geometry': rounded_tile.__geo_interface__
}
features.append(tile_feature)
sub_div_id += 1
feature_collection = {
'type': 'FeatureCollection',
'features': features
}
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(feature_collection).encode('utf-8'))
def create_grid(poly: Polygon, cell_size):
# Retrieve bounds of the entire polygon
bounds = poly.bounds
min_x, min_y, max_x, max_y = bounds
grid_lines = []
# Horizontal lines
y = min_y
while y <= max_y:
line = LineString([(min_x, y), (max_x, y)])
grid_lines.append(line)
y += cell_size
# Vertical lines
x = min_x
while x <= max_x:
line = LineString([(x, min_y), (x, max_y)])
grid_lines.append(line)
x += cell_size
return grid_lines
def combine_grid_with_poly(polygon, grid):
intersecting_tiles = []
for line in grid:
if line.intersects(polygon):
intersection = line.intersection(polygon)
# Check if intersection is a MultiLineString
if isinstance(intersection, MultiLineString):
# Extend the intersecting tiles with the polygonized results
intersecting_tiles.extend(list(polygonize(intersection)))
else:
intersecting_tiles.append(intersection)
return intersecting_tiles
def write_json_to_file(path: str, file_name: str, json_data: dict):
print("Writing to file...")
if not os.path.exists(path):
raise Exception("Directory from path does not exist")
with open(path + '/' + file_name + '_div.json', 'w') as f:
json.dump(json_data, f)
'''
def get_divided_map(file_name):
geo_data = gpd.read_file("server/map/mjosa.geojson")
polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon']
polygons = [Polygon(polygon.exterior) for polygon in polygon_data['geometry']]
'''
This diff is collapsed.
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment