Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import geopandas as gpd
- import numpy as np
- import cv2 as cv
- import largestinteriorrectangle as lir
- # Read the dataset containing the polygons
- data = gpd.read_file(
- r"C:\Working_desktop\GATE_stuff\Location_allocation\New_data_07022024\Test_processes\test_set_singlepart.gpkg")
- # Initialize an empty image
- img = np.zeros((1000, 1000, 3), dtype="uint8")
- # Loop through each polygon in the dataset
- for index, row in data.iterrows():
- # Extract the polygon coordinates and convert to float64
- polygon = np.array(row['geometry'].exterior.coords, dtype=np.float64)
- # Calculate the largest inscribed rectangle for the polygon
- rectangle = lir.lir(polygon)
- # Draw the polygon on the image
- cv.polylines(img, [polygon.astype(int)], True, (0, 255, 0), 1)
- # Draw the largest inscribed rectangle on the image
- cv.rectangle(img, tuple(rectangle[0]), tuple(rectangle[1]), (0, 0, 255), 1)
- # Display the image
- cv.imshow('Polygons with Inscribed Rectangles', img)
- cv.waitKey(0)
- cv.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment