TransWikia.com

Calculate Azimuth from polygon in GeoPandas

Geographic Information Systems Asked by inneb on December 20, 2020

Is there a simple way / ready-to-go function to calculate the azimuth of 2D-polygons in a GeoPandas DataFrame? My goal is to eventually compute azimuth angles of building footprints.

Most of the functions I have found explicitly calculate the azimuth for a line, which is not what I am looking for.
I have been thinking along the following lines to calculate the azimuth of a polygon using its normal vector (2 dimensions). Is my approach going into the right directions? I am a bit unsure how to adjust for the quadrants, also my results seem a bit odd.

azimuth = 90 - math.degrees(math.atan2(normal[1], normal[0]))
if azimuth >= 360.0:
    azimuth -= 360.0
elif azimuth < 0.0:
    azimuth += 360.0

One Answer

You could try something like this (which I'm sure can be shortened) and then turn into a function:

import geopandas as gpd
import shapely
import math

df = gpd.read_file('/home/bera/GIS/Data/testdata/ak_riks.shp')

g = df.iloc[2].geometry
a = g.minimum_rotated_rectangle
l = a.boundary
coords = [c for c in l.coords]
segments = [shapely.geometry.LineString([a, b]) for a, b in zip(coords,coords[1:])]
longest_segment = max(segments, key=lambda x: x.length)

p1, p2 = [c for c in longest_segment.coords]
angle = math.degrees(math.atan2(p2[1]-p1[1], p2[0]-p1[0])) #https://stackoverflow.com/questions/42258637/how-to-know-the-angle-between-two-points

enter image description here

Correct answer by BERA on December 20, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP