Skip to content

MultiPolygon

Bases: Spatial

Represents a collection of polygons.

Follows the GeoJSON specification (RFC 7946).

Parameters:

Name Type Description Default
value List[List[List[Tuple[float, float]]]]

A list of polygons.

required

Attributes:

Name Type Description
area
polygons

Examples:

>>> MultiPolygon(
...     [
...         [
...             [(0,0), (0,1), (1,1), (0,0)]
...         ],
...         [
...             [(0,0), (0,1), (1,1), (0,0)],
...             [(0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.1, 0.1)],
...             [(0.6, 0.6), (0.6, 0.7), (0.7, 0.7), (0.6, 0.6)],
...         ],
...     ]
... )
Source code in valor/schemas/symbolic/types.py
class MultiPolygon(Spatial):
    """
    Represents a collection of polygons.

    Follows the GeoJSON specification (RFC 7946).

    Parameters
    ----------
    value : List[List[List[Tuple[float, float]]]], optional
        A list of polygons.

    Attributes
    ----------
    area
    polygons

    Examples
    --------
    >>> MultiPolygon(
    ...     [
    ...         [
    ...             [(0,0), (0,1), (1,1), (0,0)]
    ...         ],
    ...         [
    ...             [(0,0), (0,1), (1,1), (0,0)],
    ...             [(0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.1, 0.1)],
    ...             [(0.6, 0.6), (0.6, 0.7), (0.7, 0.7), (0.6, 0.6)],
    ...         ],
    ...     ]
    ... )
    """

    def __init__(
        self,
        value: typing.List[
            typing.List[typing.List[typing.Tuple[float, float]]]
        ],
    ):
        super().__init__(value=value)

    @classmethod
    def __validate__(cls, value: typing.Any):
        if not isinstance(value, list):
            raise TypeError(
                f"Expected type 'List[List[List[Tuple[float, float]]]]' received type '{type(value).__name__}'"
            )
        for poly in value:
            Polygon.__validate__(poly)

    @classmethod
    def decode_value(
        cls,
        value: typing.Optional[
            typing.List[typing.List[typing.List[typing.List[float]]]]
        ],
    ):
        """Decode object from JSON compatible dictionary."""
        if value is None:
            return None
        return cls(
            [
                [
                    [(point[0], point[1]) for point in subpolygon]
                    for subpolygon in polygon
                ]
                for polygon in value
            ]
        )

    @property
    def area(self):
        """
        Symbolic representation of area.
        """
        if not isinstance(self._value, Symbol):
            raise ValueError(
                "attribute 'area' is reserved for symbolic variables."
            )
        return Float.symbolic(
            name=self._value._name,
            key=self._value._key,
            attribute="area",
        )

    def to_polygons(self) -> typing.List[Polygon]:
        """
        Converts multipolygon to a list of Polygon instances.

        Returns
        -------
        List[Polygon]
        """
        return [Polygon(poly) for poly in self.get_value()]

    @classmethod
    def from_polygons(self, polygons: typing.List[Polygon]) -> "MultiPolygon":
        """
        Converts a list of Polygon instances to a MultiPolygon.

        Parameters
        ----------
        polygons : List[Polygon]
            A list of Polygon instances.

        Returns
        -------
        MultiPolygon
            A MultiPolygon instance.
        """
        return MultiPolygon([poly.get_value() for poly in polygons])

Attributes

valor.schemas.MultiPolygon.area property

Symbolic representation of area.

Functions

valor.schemas.MultiPolygon.decode_value(value) classmethod

Decode object from JSON compatible dictionary.

Source code in valor/schemas/symbolic/types.py
@classmethod
def decode_value(
    cls,
    value: typing.Optional[
        typing.List[typing.List[typing.List[typing.List[float]]]]
    ],
):
    """Decode object from JSON compatible dictionary."""
    if value is None:
        return None
    return cls(
        [
            [
                [(point[0], point[1]) for point in subpolygon]
                for subpolygon in polygon
            ]
            for polygon in value
        ]
    )

valor.schemas.MultiPolygon.from_polygons(polygons) classmethod

Converts a list of Polygon instances to a MultiPolygon.

Parameters:

Name Type Description Default
polygons List[Polygon]

A list of Polygon instances.

required

Returns:

Type Description
MultiPolygon

A MultiPolygon instance.

Source code in valor/schemas/symbolic/types.py
@classmethod
def from_polygons(self, polygons: typing.List[Polygon]) -> "MultiPolygon":
    """
    Converts a list of Polygon instances to a MultiPolygon.

    Parameters
    ----------
    polygons : List[Polygon]
        A list of Polygon instances.

    Returns
    -------
    MultiPolygon
        A MultiPolygon instance.
    """
    return MultiPolygon([poly.get_value() for poly in polygons])

valor.schemas.MultiPolygon.to_polygons()

Converts multipolygon to a list of Polygon instances.

Returns:

Type Description
List[Polygon]
Source code in valor/schemas/symbolic/types.py
def to_polygons(self) -> typing.List[Polygon]:
    """
    Converts multipolygon to a list of Polygon instances.

    Returns
    -------
    List[Polygon]
    """
    return [Polygon(poly) for poly in self.get_value()]