# libpysal.cg.Polygon

class libpysal.cg.Polygon(vertices, holes=None)

  Geometric representation of polygon objects. Returns a polygon created from the objects specified.

  Parameters:
    vertices `python:list`

      A list of vertices or a list of lists of vertices.

    holes `python:list`

      A list of sub-polygons to be considered as holes. Default is `None`.

  Attributes:
    `vertices` `python:list`

      Returns the vertices of the polygon in clockwise order.

    `len` `python:int`

      Returns the number of vertices in the polygon.

    `perimeter` `python:float`

      Returns the perimeter of the polygon.

    `bounding_box` `libpysal.cg.Rectangle`

      Returns the bounding box of the polygon.

    `bbox` `python:list`

      Returns the bounding box of the polygon as a list.

    `area` `python:float`

      Returns the area of the polygon.

    `centroid` `python:tuple`

      Returns the centroid of the polygon.

  Examples

      >>> p1 = Polygon([Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((0, 1))])

  __init__(vertices, holes=None)

  Methods

  `__init__`(vertices[, holes]) |
  `build_quad_tree_structure`() | Build the quad tree structure for this polygon.
  `contains_point`(point) | Test if a polygon contains a point.

  Attributes

  `area` | Returns the area of the polygon.
  `bbox` | Returns the bounding box of the polygon as a list.
  `bounding_box` | Returns the bounding box of the polygon.
  `centroid` | Returns the centroid of the polygon.
  `holes` | Returns the holes of the polygon in clockwise order.
  `len` | Returns the number of vertices in the polygon.
  `parts` | Returns the parts of the polygon in clockwise order.
  `perimeter` | Returns the perimeter of the polygon.
  `vertices` | Returns the vertices of the polygon in clockwise order.

  property area: float

    Returns the area of the polygon.

    Examples

        >>> p = Polygon([Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((0, 1))])
        >>> p.area
        1.0

        >>> p = Polygon(
        ...     [Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
        ...     [Point((2, 1)), Point((2, 2)), Point((1, 2)), Point((1, 1))]
        ... )
        >>> p.area
        99.0

  property bbox

    Returns the bounding box of the polygon as a list.

    Returns:
      self._bbox `python:list`

        The bounding box of the polygon as a list.

    See also

    `libpysal.cg.bounding_box`
  property bounding_box

    Returns the bounding box of the polygon.

    Returns:
      self._bounding_box `libpysal.cg.Rectangle`

        The bounding box of the polygon.

    Examples

        >>> p = Polygon([Point((0, 0)), Point((2, 0)), Point((2, 1)), Point((0, 1))])
        >>> p.bounding_box.left
        0.0

        >>> p.bounding_box.lower
        0.0

        >>> p.bounding_box.right
        2.0

        >>> p.bounding_box.upper
        1.0

  build_quad_tree_structure()

    Build the quad tree structure for this polygon. Once the structure is built, speed for testing if a point is inside the ring will be increased significantly.

  property centroid: tuple

    Returns the centroid of the polygon.

    Notes

    The centroid returned by this method is the geometric centroid and respects multipart polygons with holes. Also known as the ‘center of gravity’ or ‘center of mass’.

    Examples

        >>> p = Polygon(
        ...     [Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
        ...     [Point((1, 1)), Point((1, 2)), Point((2, 2)), Point((2, 1))]
        ... )
        >>> p.centroid
        (5.0353535353535355, 5.0353535353535355)

  contains_point(point)

    Test if a polygon contains a point.

    Parameters:
      point `libpysal.cg.Point`

        A point to test for containment.

    Returns:
      contains bool

        `True` if the polygon contains `point` otherwise `False`.

    Notes

    Points falling exactly on polygon edges may yield unpredictable results.

    Examples

        >>> p = Polygon(
        ...     [Point((0,0)), Point((4,0)), Point((4,5)), Point((2,3)), Point((0,5))]
        ... )
        >>> p.contains_point((3,3))
        1

        >>> p.contains_point((0,6))
        0

        >>> p.contains_point((2,2.9))
        1

        >>> p.contains_point((4,5))
        0

        >>> p.contains_point((4,0))
        0

    Handles holes.

        >>> p = Polygon(
        ...     [Point((0, 0)), Point((0, 10)), Point((10, 10)), Point((10, 0))],
        ...     [Point((2, 2)), Point((4, 2)), Point((4, 4)), Point((2, 4))]
        ... )
        >>> p.contains_point((3.0, 3.0))
        False

        >>> p.contains_point((1.0, 1.0))
        True

  property holes: list

    Returns the holes of the polygon in clockwise order.

    Examples

        >>> p = Polygon(
        ...     [Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],
        ...     [Point((1, 2)), Point((2, 2)), Point((2, 1)), Point((1, 1))]
        ... )
        >>> len(p.holes)
        1

  property len: int

    Returns the number of vertices in the polygon.

    Examples

        >>> p1 = Polygon([Point((0, 0)), Point((0, 1)), Point((1, 1)), Point((1, 0))])
        >>> p1.len
        4

        >>> len(p1)
        4

  property parts: list

    Returns the parts of the polygon in clockwise order.

    Examples

        >>> p = Polygon(
        ...     [
        ...         [Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((0, 1))],
        ...         [Point((2, 1)), Point((2, 2)), Point((1, 2)), Point((1, 1))]
        ...     ]
        ... )
        >>> len(p.parts)
        2

  property perimeter: int | float

    Returns the perimeter of the polygon.

    Examples

        >>> p = Polygon([Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((0, 1))])
        >>> p.perimeter
        4.0

  property vertices: list

    Returns the vertices of the polygon in clockwise order.

    Examples

        >>> p1 = Polygon([Point((0, 0)), Point((0, 1)), Point((1, 1)), Point((1, 0))])
        >>> len(p1.vertices)
        4