根据 Shapely 多边形的边界以及给定的半径和距离计算行数和列数

问题描述 投票:0回答:1

我想找到形状多边形内可能的行数和列数。目的是我需要在每个单元格内创建圆圈。这些圆之间还会有一个半径和距离值。

import numpy as np
from shapely.geometry import Polygon

def generate_circles(pointsdata, radius, distance):
    
    points_data = np.array(pointsdata['DataPoints']).astype(float)
    x_coords, y_coords = points_data[:, 0], points_data[:, 1]
    shapely_polygon = Polygon(list(zip(x_coords, y_coords)))
    if shapely_polygon.is_empty or not shapely_polygon.exterior:
            pass
    else : 
           #I want to find number of rows and columns possible 
           #inside this polygon considering the radius and distance


pointsdata = { 'NumberofDataPoints': 8, "DataPoints": [ [0, 0], [1, 0.5], [1.5, 1.5], [1, 2.5], [0, 3], [-1, 2.5], [-1.5, 1.5], [-1, 0.5] ] } 
radius = 1.5
distance = 2
generate_circles(pointsdata,radius,distance)

如何找到可能的行数和列数?

python-3.x geometry shapely
1个回答
0
投票

要在由 Shapely 多边形、半径和圆之间的距离定义的网格的每个单元格内创建圆,您可以迭代每个单元格并将圆放置在其中。以下是修改函数以实现此目的的方法:

import numpy as np
from shapely.geometry import Polygon, Point
import matplotlib.pyplot as plt

def generate_circles(pointsdata, radius, distance):
    points_data = np.array(pointsdata['DataPoints']).astype(float)
    x_coords, y_coords = points_data[:, 0], points_data[:, 1]
    shapely_polygon = Polygon(list(zip(x_coords, y_coords)))
    
    if shapely_polygon.is_empty or not shapely_polygon.exterior:
        pass
    else:
        # Calculate the bounding box of the polygon
        min_x, min_y, max_x, max_y = shapely_polygon.bounds
        
        # Calculate the size of each cell
        cell_size = radius * 2 + distance
        
        # Calculate the number of rows and columns
        num_rows = int(np.ceil((max_y - min_y) / cell_size))
        num_cols = int(np.ceil((max_x - min_x) / cell_size))
        
        # Create a list to store circle centers
        circle_centers = []
        
        # Iterate over each cell and place circles inside it
        for i in range(num_rows):
            for j in range(num_cols):
                # Calculate the center of the current cell
                center_x = min_x + j * cell_size + radius + distance / 2
                center_y = min_y + i * cell_size + radius + distance / 2
                
                # Check if the center is inside the polygon
                if shapely_polygon.contains(Point(center_x, center_y)):
                    circle_centers.append((center_x, center_y))
        
        return circle_centers

pointsdata = { 'NumberofDataPoints': 8, "DataPoints": [ [0, 0], [1, 0.5], [1.5, 1.5], [1, 2.5], [0, 3], [-1, 2.5], [-1.5, 1.5], [-1, 0.5] ] }
radius = 1.5
distance = 2
circle_centers = generate_circles(pointsdata, radius, distance)

# Plotting
x_coords = [point[0] for point in circle_centers]
y_coords = [point[1] for point in circle_centers]

plt.figure(figsize=(8, 8))
plt.scatter(x_coords, y_coords, color='red')
plt.gca().set_aspect('equal', adjustable='box')
plt.gca().set_xlim(min_x, max_x)
plt.gca().set_ylim(min_y, max_y)
plt.gca().set_xlabel('X')
plt.gca().set_ylabel('Y')
plt.title('Circles inside each cell')
plt.show()

此函数迭代网格中的每个单元格,计算单元格的中心,检查中心是否位于多边形内,如果是,则将其添加到圆心列表中。最后,它使用 matplotlib 绘制圆圈。

© www.soinside.com 2019 - 2024. All rights reserved.