Gursel Karacor
Gursel Karacor

Reputation: 1167

Eccentricity of a 2D convex hull in Python

How can I calculate the eccentricity of a 2D convex hull in Python?

Eccentricity: a parameter of an ellipse (or a closed shape) indicating its deviation from the circularity whose value ranging from 0 (circle) to 1 (line).

Upvotes: -1

Views: 2223

Answers (1)

Gursel Karacor
Gursel Karacor

Reputation: 1167

Well, for those who still would like to see an answer to this question: if we assume the closed shape is an ellipse or similar to an ellipse, the eccentricity is defined as sqrt(square(major_axis_length/2)-square(minor_axis_length/2)) where the major and minor axis's are shown in the figure. enter image description here

The end points of the axis's are taken as min and max valued points along each axis. With 4 sample geo-points (these also could be Cartesian coordinates), we can write something like this:

import numpy as np
from scipy.spatial.distance import euclidean

points = np.array([[50.6636778,5.0939791], [50.7674881,5.4663611], [50.94594, 5.48977], [51.0380754,5.4012648]])

small_latwise = np.min(points[points[:, 0] == np.min(points[:, 0])], 0)
small_lonwise = np.min(points[points[:, 1] == np.min(points[:, 1])], 0)
big_latwise = np.max(points[points[:, 0] == np.max(points[:, 0])], 0)
big_lonwise = np.max(points[points[:, 1] == np.max(points[:, 1])], 0)
distance_lat = euclidean(big_latwise, small_latwise)
distance_lon = euclidean(big_lonwise, small_lonwise)
if distance_lat >= distance_lon:
    major_axis_length = distance_lat
    minor_axis_length = distance_lon
else:
    major_axis_length = distance_lon
    minor_axis_length = distance_lat
a = major_axis_length/2
b = minor_axis_length/2
ecc = np.sqrt(np.square(a)-np.square(b))/a
print(ecc)

Which yields 0.0854194406287

Upvotes: 2

Related Questions