pat*_*eau 1 python angle shapely geopandas
我有一个简单的问题,但我找不到答案我正在寻找“最小旋转矩形”多边形相对于纬度或经度的主轴角
df4.minimum_rotated_rectangle
Run Code Online (Sandbox Code Playgroud)
有人有这个库存吗 提前致谢
这是一个函数,它采用minimum_rotated_rectangle多边形并根据较长的边计算其方位角(0-180)。
def _azimuth(point1, point2):
"""azimuth between 2 points (interval 0 - 180)"""
import numpy as np
angle = np.arctan2(point2[0] - point1[0], point2[1] - point1[1])
return np.degrees(angle) if angle > 0 else np.degrees(angle) + 180
def _dist(a, b):
"""distance between points"""
import math
return math.hypot(b[0] - a[0], b[1] - a[1])
def azimuth(mrr):
"""azimuth of minimum_rotated_rectangle"""
bbox = list(mrr.exterior.coords)
axis1 = _dist(bbox[0], bbox[3])
axis2 = _dist(bbox[0], bbox[1])
if axis1 <= axis2:
az = _azimuth(bbox[0], bbox[1])
else:
az = _azimuth(bbox[0], bbox[3])
return az
Run Code Online (Sandbox Code Playgroud)
例子:
import geopandas as gpd
df = gpd.read_file(gpd.datasets.get_path('nybb'))
# single geometry
mrr = df.geometry.iloc[0].minimum_rotated_rectangle
azimuth(mrr)
# 66.65508762854085
# whole dataframe
mrrs = df.geometry.apply(lambda geom: geom.minimum_rotated_rectangle)
df['az'] = mrrs.apply(azimuth)
ax = df.plot('az', legend=True)
mrrs.boundary.plot(ax=ax)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2674 次 |
| 最近记录: |