pysal / esda

statistics and classes for exploratory spatial data analysis
https://pysal.org/esda
BSD 3-Clause "New" or "Revised" License
215 stars 55 forks source link

Moment of area correctness issues #260

Closed ljwolf closed 1 year ago

ljwolf commented 1 year ago

Reported by email, our shape.second_areal_moment and nmi() functions need a few corrections:

  1. the input coordinate arrays need to be centered on the centroid (i.e. center of mass, not mean center)
  2. we need to use the sum of x and y moments, rather than the cross-xy moment.
  3. we need to use the parallel axis theorem to merge the inertial contributions from parts of multi-part polygons.

The hole + winding direction code should be correct as written using the correct moment of inertia calculation:

@njit
def _second_moa_ring_xplusy(points):
    """
    implementation of the moment of area for a single ring
    """
    moi = 0
    for i in prange(len(points[:-1])):
        x_tail, y_tail = points[i]
        x_head, y_head = points[i + 1]
        xtyh = x_tail * y_head
        xhyt = x_head * y_tail
        xtyt = x_tail * y_tail
        xhyh = x_head * y_head
        moi += (xtyh-xhyt)*(
            x_head**2 + x_head*x_tail + x_tail**2 
            + y_head**2 + y_head*y_tail + y_tail**2)
    return moi/12

I should deal with this within a week or so, and this plus a few others (number fix, non-constructive minimum bounding circle index, correlogram) should make enough for a release!