Area and centroid calculations

103
0
3 weeks ago
DanPatterson
MVP Esteemed Contributor
1 0 103

area_centroid_example.pngIt is easy to get arcpy shapes, geojson geometry and various relatives to arrays.

You can add this to your geometry arsenal.

It can be used for convex/concave/multipart (including parts with holes).

Clockwise (cw) geometry returns a positive area, holes (ccw) a negative one.

The centroids for both parts can be weighted or proportioned if needed (let me know, if so and I can provide another example).

 

def _area_centroid_(a):
    r"""Calculate area and centroid for a singlepart polygon, `a

    This is also used to calculate area and centroid for a Geo array's parts.

    Notes
    -----
    For multipart shapes (a0), just use this syntax:
    >>> [_area_centroid_(i) for i in a0]
    """
    a = _get_base_(a)
    x0, y1 = (a.T)[:, 1:]
    x1, y0 = (a.T)[:, :-1]
    e0 = np.einsum('...i,...i->...i', x0, y0)
    e1 = np.einsum('...i,...i->...i', x1, y1)
    t = e1 - e0
    area = np.sum((e0 - e1) * 0.5)
    x_c = np.sum((x1 + x0) * t, axis=0) / (area * 6.0)
    y_c = np.sum((y1 + y0) * t, axis=0) / (area * 6.0)
    return area, np.asarray([-x_c, -y_c])

 

Tags (3)
About the Author
Retired Geomatics Instructor (also DanPatterson_Retired). Currently working on geometry projects (various) as they relate to GIS and spatial analysis. I use NumPy, python and kin and interface with ArcGIS Pro.