Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angle in minimum_rotated_rectangle

I have a simple question, but I can't find the answer I am looking for the principal angle of a "minimum_rotated_rectangle" polygon in relation to a latitude or a longitude

df4.minimum_rotated_rectangle

does anyone have this in stock thanks in advance

like image 720
patrickblancseau Avatar asked Jul 13 '26 22:07

patrickblancseau


1 Answers

Here is a function which takes minimum_rotated_rectangle polygon and calculates its azimuth (0-180) based on the longer edge.

def _azimuth(point1, point2):
    """azimuth between 2 points (interval 0 - 180)"""
    import numpy as np

    angle = np.arctan2(point2[1] - point1[1], point2[0] - point1[0])
    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

Example:

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)

result

like image 98
martinfleis Avatar answered Jul 15 '26 12:07

martinfleis