问题描述
我正在寻找一种方法来沿着一条线在多多边形内创建一组多边形(rechtangles),并像在绘图中一样均匀地将它们隔开。
我尝试生成点并将它们用作多边形的中点,但问题是通过创建均匀间隔的点栅格,除了 180 度之外,它不可能以任何其他方向旋转。
给定的是一个多多边形匀称对象和由宽度和高度以及每个多边形之间的垂直和水平间距定义的多边形。 多边形应仅放置在多多边形内,不得相交。
额外问题:也许有一种方法可以将它们沿向量放置,以便可以旋转线条。
解决方法
如果您的间距和矩形尺寸以米为单位。请尝试以下操作。
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,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,height) #increase bearing to rotate rectangle
folium.Polygon(rect_coords,color='red').add_to(webmap)
webmap.save('webmap.html')