paste_zayo

Inscribed_rectangle_2

Feb 27th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. import geopandas as gpd
  2. import numpy as np
  3. import cv2 as cv
  4. from shapely.geometry import Polygon
  5.  
  6. def largest_interior_rectangle(polygon):
  7.     max_area = 0
  8.     max_rect = None
  9.  
  10.     for i in range(len(polygon)):
  11.         for j in range(i, len(polygon)):
  12.             for k in range(j, len(polygon)):
  13.                 for l in range(k, len(polygon)):
  14.                     if is_convex_quad(polygon[i], polygon[j], polygon[k], polygon[l]):
  15.                         area = (polygon[k][0] - polygon[i][0]) * (polygon[l][1] - polygon[j][1])
  16.                         if area > max_area:
  17.                             max_area = area
  18.                             max_rect = [polygon[i], polygon[j], polygon[k], polygon[l]]
  19.  
  20.     return max_rect
  21.  
  22.  
  23. def is_convex_quad(p1, p2, p3, p4):
  24.     # Compute the convex hull of the quadrilateral
  25.     hull = cv.convexHull(np.array([p1, p2, p3, p4], dtype=np.float32))
  26.  
  27.     # Check if the midpoint of the diagonal lies inside the convex hull
  28.     return cv.pointPolygonTest(hull, ((p1[0] + p3[0]) / 2, (p1[1] + p3[1]) / 2), False) >= 0
  29.  
  30.  
  31. # Read the GeoPackage file
  32. data = gpd.read_file(
  33.      r"C:\Working_desktop\GATE_stuff\Location_allocation\New_data_07022024\Test_processes\test_set_singlepart.gpkg")
  34.  
  35. # Initialize an empty list to store the largest interior rectangles for each polygon
  36. largest_rectangles = []
  37.  
  38. # Iterate over each polygon in the GeoDataFrame
  39. for index, row in data.iterrows():
  40.     # Extract the polygon coordinates
  41.     polygon = np.array(row.geometry.exterior.coords)
  42.  
  43.     # Calculate the largest interior rectangle for the polygon
  44.     rectangle = largest_interior_rectangle(polygon)
  45.  
  46.     # Append the largest rectangle to the list
  47.     largest_rectangles.append(rectangle)
  48.  
  49.     # Visualize the polygon and its largest interior rectangle
  50.     img = np.zeros((160, 240, 3), dtype="uint8")
  51.     cv.polylines(img, [polygon.astype(int)], True, (0, 0, 255), 1)
  52.     cv.rectangle(img, (int(rectangle[0][0]), int(rectangle[0][1])), (int(rectangle[2][0]), int(rectangle[2][1])),
  53.                  (255, 0, 0), 1)
  54.  
  55.     cv.imshow('Largest Interior Rectangle', img)
  56.     cv.waitKey(0)
  57.     cv.destroyAllWindows()
  58.  
  59. # Print the largest rectangles for each polygon
  60. print("Largest Rectangles:")
  61. for idx, rectangle in enumerate(largest_rectangles):
  62.     print(f"Polygon {idx + 1}: {rectangle}")
  63.  
  64. # Export the largest inscribed rectangles as a GeoPackage
  65. # Create a list to store the largest rectangles as Polygon objects
  66. rect_polygons = []
  67.  
  68. # Iterate over the largest rectangles and convert them to Shapely Polygon objects
  69. for idx, rectangle in enumerate(largest_rectangles):
  70.     rect_coords = [(point[0], point[1]) for point in rectangle]
  71.     rect_poly = Polygon(rect_coords)
  72.     rect_polygons.append(rect_poly)
  73.  
  74. # Create a GeoDataFrame from the list of Polygon objects
  75. gdf_rectangles = gpd.GeoDataFrame(geometry=rect_polygons, crs=data.crs)
  76.  
  77. # Define the output GeoPackage file path
  78. output_gpkg = (r"C:\Working_desktop\GATE_stuff\Location_allocation\New_data_07022024\Test_processes\rect.gpkg")
  79. # Save the GeoDataFrame to a GeoPackage file
  80. gdf_rectangles.to_file(output_gpkg, driver="GPKG")
  81.  
Advertisement
Add Comment
Please, Sign In to add comment