From c4b0dadf297aae1dcc3f1c2227737260f2f035e1 Mon Sep 17 00:00:00 2001 From: Sara <sarasdj@stud.ntnu.no> Date: Wed, 13 Mar 2024 12:24:07 +0100 Subject: [PATCH] update: small improvement on circle --- .../__pycache__/get_relation.cpython-311.pyc | Bin 7317 -> 6792 bytes server/map/get_relation.py | 108 +++++++++--------- 2 files changed, 56 insertions(+), 52 deletions(-) diff --git a/server/map/__pycache__/get_relation.cpython-311.pyc b/server/map/__pycache__/get_relation.cpython-311.pyc index 744144e33606421cf5d36ed1c24d8c6e2ddab9df..e35a7a0bbb24aae68e2c04bf6f3fd9fadf1b0f4e 100644 GIT binary patch delta 2429 zcmZuyU2Gdg5Z?9O*%v#u&+%WAv}x?p1XD-|YAESXXak`IL<_1~L}=@}xx;mO@f~}Y z(!!mRib~`mDv1_UKnVE(l|n%jRYC<J0TPIc2V{d0;RI3z52!B?R3QNm!0b6TNnqst zc6WAmZvJ-s{J?a7=(S+bkAPmA`Bgcyb}ppj+}vb(ouF{CPl-Op%T1^Gk+|$XjYblT zBYRzL;50w?$^N7WH#r#BbsnL!D35H9i4mfExWr^0+nmMSjShZp^D?#tlL;=8#aqHT z4?-7U)X4T)UfI)H-y+)wdv3{Iv_#N##%?l&h7bZhO`r)hUubBz{gxjz?QKZe0bndu z#ajU)x<U;Z1JCqWKKQ^syV>+L*vJZufoC-u6DZ~r8)g2OyC)uuiv7#130TeYN^63a zr{tv-A<hM54=sh;ZnT2StO?BQUuoaWv-L~#vv7DTMi`mlx)}(Kv9P;jdEWG%gMeRv zQ6oEKM(b;k^g}9AI6xog;wf&qB<TD%yvPd4KCo_;1TiQcfV9{VNSjy+!qr%(xn_yZ zh9$B`ZmMOG1xB)4EPhF}BzlUE;s|qnP1K6XemU@F^Ers-g+@d%vZPWAlgNOC)gp8M zm6*|%ITU5J<%Nr(QjDgAZX9R6#e*w-vrs9)PLd%xyu8xjE-EGAZdzi?u~l`~O08f? z61TBySfUlKV?aiGQ6{oG|I&+$zP*#M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wzd7Fg1zn9yImMFD`Y)zmNB-%!@1TE-j+_mFH92k0OdLL(Aer<P^XPaY zDUfp@YI)oBn9meu7c8MjG6j>^UW%ClG$|8&{2p+U3k<M$lT-9q_n`<YOLDdi-Uh@F zF#TEg<g;2WO_~V3fsZ@>h0qmP9)R(aj(^(_D1g(yb??R=>W*cTdy(8%lX`23G5G8x zr?xm|w=&!7uj!dUzr*C{4+BG(qaOr@ysXV_jx0z26xj8|i4+{&$y?%OeZHU?7ME8R zEJvYei3(8)X)Wn$Wp9EOg6EUAKmDMMFz^vfqYPjPZ4B=DRlLr}m!__dzO!pJLLUTk zIQZ$Mt|C5m|GU7Fbo?ElJxCFj`>Y7b4;WyMBqd9*?;|SgEJrb8aVZ_lF){_HszCVI z)XR(@fK55gj=f!R2Fv$mB_$HATIz!%!oWIA<1+wD=w3LwboyQ}L~n+Fi-UAI_WjBr z5UgNcF^f6f%w;YQx7rkW4$jH*jK~A=iX{~0^TkEmE;7T8IjDcTyI8_pPFKuU@!DCo zLu23XW?EQv+BQ9sC-S;FpVN+zx8RI5V>;9G<H`w)N2;kNV(b5bmmGWXc=bRN!E*b8 zz1*3%mnXqx9K$cH0}YnnwH6!S0uH)yyedKO!SUNu_R@TTZCa>}#l85MCVCp!j{FbT C;iJL; diff --git a/server/map/get_relation.py b/server/map/get_relation.py index 52b037aa..b80b3bf2 100644 --- a/server/map/get_relation.py +++ b/server/map/get_relation.py @@ -11,21 +11,6 @@ polygon_min_x = None # The left most point of the entire polygon # 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 - # Load GeoJSON data using geopandas - geo_data = gpd.read_file("server/map/mjosa.geojson") - - # Filter only polygons, exclude points and other feature types to reduce response size - polygon_data = geo_data[geo_data['geometry'].geom_type == 'Polygon'] - - if polygon_data.empty: - raise ValueError("Failed to extract polygon data from file") - - # Extract coordinates from polygons and create polygon objects - polygons = [Polygon(polygon.exterior) for polygon in polygon_data['geometry']] - - if not polygons: - raise ValueError("Failed to convert to polygons") - divided_map = [] # List to store map shapes while splitting # NB: test polygon, remove after testing @@ -53,7 +38,20 @@ def get_relation(self, body_of_water: str): # NB: implement body_of_water if not horizontal_section or not divided_poly[0]: continue - divided_map.append(horizontal_section) + # Cut each horizontal section into vertical sections, right to left + for vrt_line in vrt_lines: + if len(horizontal_section.exterior.coords) < 3: + break # Break from loop im remaining section has no coordinates + + # Split the horizontal section into two vertical parts + vertical_parts = cut_polygon_by_points(horizontal_section, vrt_line, -0.1) + + divided_map.append(vertical_parts[0]) # Append split vertical sections to final list of shapes + + # Set horizontal_section to the remaining, un-split, horizontal section for next iteration + horizontal_section = vertical_parts[1] + + polygon = divided_poly[1] # Set polygon to the remaining, un-split shape for next iteration divided_map.append(polygon) @@ -63,8 +61,8 @@ def get_relation(self, body_of_water: str): # NB: implement body_of_water # NB test plot fig, ax = plt.subplots() - ax.set_aspect(1.5) - ax.xaxis.set_major_locator(ticker.MultipleLocator(0.2)) + ax.set_aspect(1) + # ax.xaxis.set_major_locator(ticker.MultipleLocator(0.2)) for i, polygon in enumerate(tiles['geometry']): random_color = "#{:06x}".format(random.randint(0, 0xFFFFFF)) @@ -106,10 +104,11 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float): else: remaining_shape.append(point) - # Add points to the newly created edges of split_shape and remaining_shape - populate_new_edge(split_shape, remaining_shape, cell_size, divisor) - #split_shape = shape_w_edges[0] - #remaining_shape = shape_w_edges[1] + # Populate newly created edges with points to facilitate vertical cutting + populated_shapes = populate_new_edge(split_shape, remaining_shape, cell_size, divisor) + if populated_shapes is not None: + split_shape = populated_shapes[0] + remaining_shape = populated_shapes[1] else: # Vertical split for point in exterior_coords: @@ -137,39 +136,14 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float): return Polygon(split_shape), Polygon(remaining_shape) -# Adds equally spaced points along an edge that is created after a polygon is cut. -def populate_new_edge(split_shape, remaining_shape, cell_size: float, divisor: float): - # Prepend new points onto the newly created edge to facilitate vertical splitting - if len(split_shape) > 2 and polygon_min_x is not None: - # Define starting point with an x-value that will be common for all polygons - starting_point = polygon_min_x - - # Get corners of the newly created shape which make up the new edge - right_corner = split_shape[-1] - left_corner = split_shape[0] - - print("right_corner: ", right_corner, " left_corner: ", left_corner, " left == right?: ", left_corner == right_corner) - - while starting_point < left_corner.x: # Increment starting point until it is withing the polygons bounds - starting_point += cell_size - # if starting_point < left_corner.x: # NB: optimised substitute for previous while loop, requires testing - # starting_point += cell_size * math.floor(starting_point - left_corner.x) - - # Insert new points with cell_size spacing while starting_point is within bounds - while starting_point < right_corner.x: - split_shape.insert(0, (starting_point, divisor)) # NB may have to add/subtract small offset of 0.00001 - remaining_shape.insert(0, (starting_point, divisor)) # Prepend new point to shape - - starting_point += cell_size - - return split_shape, remaining_shape - - # Generate grid of equally spaced x and y coordinates where the grid size is determined by cell_size def create_grid_coords(polygon: Polygon, cell_size: float): # Define boundaries of grid min_x, min_y, max_x, max_y = polygon.bounds + global polygon_min_x + polygon_min_x = min_x + # Divide grid into sections of size *cell_size x_step = max((max_x - min_x) / 10, cell_size) y_step = max((max_y - min_y) / 10, cell_size) @@ -183,7 +157,7 @@ def create_grid_coords(polygon: Polygon, cell_size: float): # NB: only for testing def circle_polygon(): circle_points = [] - num_points = 80 + num_points = 200 center_x, center_y = 0.0, 0.0 radius = 6 @@ -194,4 +168,34 @@ def circle_polygon(): circle_points.append((x, y)) circle_points.append(circle_points[0]) # Close the circle - return Polygon(circle_points) \ No newline at end of file + return Polygon(circle_points) + + +# Adds equally spaced points along an edge that is created after a polygon is cut. +def populate_new_edge(split_shape, remaining_shape, cell_size: float, divisor: float): + # Prepend new points onto the newly created edge to facilitate vertical splitting + if len(split_shape) > 2 and polygon_min_x is not None: + # Define starting point with an x-value that will be common for all polygons + starting_point = polygon_min_x + + # Get corners of the newly created shape which make up the new edge + right_corner = split_shape[-1] + left_corner = split_shape[0] + + print("right_corner: ", right_corner, " left_corner: ", left_corner, " cell_size: ", cell_size) + + while starting_point < left_corner.x: # Increment starting point until it is withing the polygons bounds + starting_point += cell_size + + # if starting_point < left_corner.x: # NB: optimised substitute for previous while loop, requires testing + # starting_point += cell_size * math.floor(starting_point - left_corner.x) + + # Insert new points with cell_size spacing while starting_point is within bounds + while starting_point < right_corner.x: + split_shape.insert(0, (starting_point, divisor)) # NB may have to add/subtract small offset of 0.00001 + remaining_shape.insert(0, (starting_point, divisor)) # Prepend new point to shape + + starting_point += cell_size + + return split_shape, remaining_shape + -- GitLab