【问题标题】:Creating rectangle polygons within Multipolygons with shapely在 Multipolygons 中使用 shapely 创建矩形多边形
【发布时间】:2021-04-06 16:45:44
【问题描述】:

我正在寻找一种方法来在多多边形内沿一条线创建一组多边形(rechtangles),并像在绘图中一样均匀地间隔它们。

我尝试生成点并将它们用作多边形的中点,但问题是通过创建均匀间隔的点栅格,除了 180 度之外,它无法以任何其他方向旋转。

example

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

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

【问题讨论】:

    标签: shapely


    【解决方案1】:

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

    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')
    

    【讨论】:

    • 我感激不尽。我喜欢这个解决方案,它正是我需要的。我会自己试一试,然后回复你。非常感谢!
    猜你喜欢
    • 2015-05-13
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2023-02-17
    • 1970-01-01
    相关资源
    最近更新 更多