get_rectangle_centroid

pyhelpers.geom.get_rectangle_centroid(rectangle, as_geom=False)[source]

Get coordinates of the centroid of a rectangle.

Parameters:
  • rectangle (list | tuple | numpy.ndarray | shapely.geometry.Polygon | shapely.geometry.MultiPolygon) – Variable/object representing a rectangle.

  • as_geom (bool) – Whether to return a shapely.geometry.Point object; defaults to False.

Returns:

Coordinates of the centroid of the rectangle.

Return type:

numpy.ndarray | shapely.geometry.Point

Examples:

>>> from pyhelpers.geom import get_rectangle_centroid
>>> from shapely.geometry import Polygon
>>> import numpy
>>> coords_1 = [[0, 0], [0, 1], [1, 1], [1, 0]]
>>> rect_obj = Polygon(coords_1)
>>> rect_cen = get_rectangle_centroid(rectangle=rect_obj)
>>> rect_cen
array([0.5, 0.5])
>>> rect_obj = numpy.array(coords_1)
>>> rect_cen = get_rectangle_centroid(rectangle=rect_obj)
>>> rect_cen
array([0.5, 0.5])
>>> rect_cen = get_rectangle_centroid(rectangle=rect_obj, as_geom=True)
>>> type(rect_cen)
shapely.geometry.point.Point
>>> rect_cen.wkt
'POINT (0.5 0.5)'
>>> coords_2 = [[(0, 0), (0, 1), (1, 1), (1, 0)], [(1, 1), (1, 2), (2, 2), (2, 1)]]
>>> rect_cen = get_rectangle_centroid(rectangle=coords_2)
>>> rect_cen
array([1., 1.])