Python – How to calculate area of polygon from list of points with python

geometrypolygonpython

I have a list/array of 2D points which form a non-convex non-self-intersecting shape.
I want to calculate the area enclosed by that polygon.

First I need to form a polygon with the points that I get provided as a 'curve-walk' (non self intersecting). If I can triangulate that polygon I can calculate the area ( I can tolerate a small relative error).

Using scipy's scipy.spatial.ConvexHull() results in the wrong shape and area , obviously, but I have not found a available algorithm in the big math packages which does this.

Can anyone tell me how to do that ?

Best Answer

use the shapely module available for both Python 2.7 and 3

In [41]: from shapely.geometry import Polygon

In [48]: coords  = ((-1, 0), (-1, 1), (0, 0.5), (1, 1), (1, 0), (-1, 0))

In [49]: polygon = Polygon(coords)

In [50]: polygon.area
Out[50]: 1.5