使用 shapely 在 Multipolygons 中创建矩形多边形

Creating rectangle polygons within Multipolygons with shapely

我正在寻找在多边形内沿着一条线创建一组多边形(rechtangles)的方法,并且像在绘图中一样 space 它们。

我试图生成点并将它们用作多边形的中点,但问题是通过创建一个均匀的 spaced 点栅格,除了 180 度外,它不可能在任何其他方向上旋转。

example

给定的是一个多边形形状对象和由宽度和高度以及每个多边形之间的垂直和水平间距定义的多边形。 多边形只能放置在多边形内,不能相交。

额外的问题:也许有一种方法可以将它们沿着矢量放置,这样就可以旋转线条。

如果您的间距和矩形尺寸以米为单位。请尝试以下操作。

import folium
import geopy
import numpy as np
from shapely.geometry import Polygon, MultiPoint


def get_rectangle_points(coordinates, bearing, width, height):
    start = geopy.Point(coordinates)
    hypotenuse = np.hypot(width/1000, height/1000)
    d = geopy.distance.distance(kilometers=hypotenuse/2)

    opposite_angle = np.degrees(np.arctan(width/height))
    northeast_angle = 0 - opposite_angle
    southwest_angle = 180  - opposite_angle
    northwest_angle = opposite_angle
    southeast_angle = 180 + opposite_angle

    points = []
    for angle in [northeast_angle, northwest_angle, southwest_angle, southeast_angle]:
        point = d.destination(point=start, bearing=angle+bearing)
        coords = (point.latitude, point.longitude)
        #coords = (point.longitude, point.latitude)
        points.append(coords)

    return points

lat_point_list = [50.854457, 52.518172, 50.072651, 48.853033, 50.854457]
lon_point_list = [4.377184, 13.407759, 14.435935, 2.349553, 4.377184]

polygon_geom = Polygon(zip(lon_point_list, lat_point_list))

latmin, lonmin, latmax, lonmax = polygon_geom.bounds

v = 100 #vertical spacing in meters
h = 500 #horizontal spacing in meters
height = 20000
width = 50000

vertical_spacing = geopy.units.degrees(arcminutes=geopy.units.km(meters=2*v+height)) #spacing in degrees
horizontal_spacing = geopy.units.degrees(arcminutes=geopy.units.km(meters=2*h+width)) #spacing in degrees
x, y = np.round(np.meshgrid(np.arange(latmin, latmax, horizontal_spacing), np.arange(lonmin, lonmax, vertical_spacing)),4)
points = MultiPoint(list(zip(x.flatten(),y.flatten())))
valid_points = []
valid_points.extend([i for i in points if polygon_geom.contains(i)])

map_coords = [polygon_geom.centroid.y, polygon_geom.centroid.x]

webmap = folium.Map(map_coords, zoom_start=6)
folium.GeoJson(polygon_geom).add_to(webmap)

for point in valid_points:
    coords = (point.y, point.x)
    rect_coords = get_rectangle_points(coords, 0, width, height) #increase bearing to rotate rectangle    
    folium.Polygon(rect_coords, color='red').add_to(webmap)

webmap.save('webmap.html')