paste_zayo

Inscribed_rectangle_3

Feb 27th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. import geopandas as gpd
  2. import matplotlib.pyplot as plt
  3. import shapely.geometry as sg
  4. import shapely.affinity as sa
  5. import pandas as pd
  6.  
  7.  
  8. def main():
  9.     # Load your polygon data
  10.     data = gpd.read_file(r"C:\Working_desktop\GATE_stuff\Location_allocation\New_data_07022024\Test_processes\test_set_singlepart.gpkg")
  11.  
  12.     # Create an empty GeoDataFrame to store the inscribed rectangles
  13.     inscribed_rectangles = pd.DataFrame(columns=['geometry'])
  14.  
  15.     # Iterate over each polygon in the dataset
  16.     for index, row in data.iterrows():
  17.         polygon = row['geometry']
  18.         inscribed_rectangle = find_inscribed_rectangle(polygon)
  19.  
  20.         # Check if an inscribed rectangle is found for the current polygon
  21.         if inscribed_rectangle:
  22.             # Append the inscribed rectangle to the GeoDataFrame
  23.             inscribed_rectangles = pd.concat([inscribed_rectangles, pd.DataFrame({'geometry': [inscribed_rectangle]})],
  24.                                              ignore_index=True)
  25.  
  26.     # Convert DataFrame to GeoDataFrame
  27.     inscribed_rectangles = gpd.GeoDataFrame(inscribed_rectangles, geometry='geometry')
  28.  
  29.     # Check if the GeoDataFrame is not empty before trying to export it
  30.     if not inscribed_rectangles.empty:
  31.         # Export the GeoDataFrame containing inscribed rectangles to a new GeoPackage file
  32.         inscribed_rectangles.to_file("inscribed_rectangles.gpkg", driver="GPKG")
  33.  
  34.         # Visualize the result
  35.         fig, ax = plt.subplots()
  36.         data.plot(ax=ax, color='gray', edgecolor='black', alpha=0.3)  # Plot all polygons in semi-transparent gray
  37.         inscribed_rectangles.plot(ax=ax, color='blue', edgecolor='black')  # Plot inscribed rectangles in blue
  38.         plt.title("Inscribed Rectangles")
  39.         plt.xlabel("X-Coordinate")
  40.         plt.ylabel("Y-Coordinate")
  41.         plt.show()
  42.     else:
  43.         print("No inscribed rectangles found in the dataset.")
  44.  
  45.  
  46. def find_inscribed_rectangle(polygon):
  47.     max_area = 0
  48.     max_rectangle = None
  49.  
  50.     # Get all the vertices of the polygon
  51.     vertices = list(polygon.exterior.coords)
  52.  
  53.     # Iterate over all possible pairs of non-adjacent vertices
  54.     num_vertices = len(vertices)
  55.     for i in range(num_vertices):
  56.         for j in range(i + 2, num_vertices):
  57.             # Create a rectangle using the current pair of vertices
  58.             rectangle = sg.Polygon([vertices[i], vertices[j],
  59.                                     vertices[j - 1], vertices[i - 1]])
  60.  
  61.             # Check if the rectangle is completely inside the polygon
  62.             if polygon.contains(rectangle):
  63.                 # Calculate the area of the rectangle
  64.                 area = rectangle.area
  65.  
  66.                 # Update the maximum area and corresponding rectangle if necessary
  67.                 if area > max_area:
  68.                     max_area = area
  69.                     max_rectangle = rectangle
  70.  
  71.     return max_rectangle
  72.  
  73.  
  74. if __name__ == '__main__':
  75.     main()
  76.  
Advertisement
Add Comment
Please, Sign In to add comment