Skip to content

Point

Bases: Spatial, Equatable

Represents a point in 2D space.

Follows the GeoJSON specification (RFC 7946).

Parameters:

Name Type Description Default
value Tuple[float, float]

A point.

required

Examples:

>>> Point((1,2))
Source code in valor/schemas/symbolic/types.py
class Point(Spatial, Equatable):
    """
    Represents a point in 2D space.

    Follows the GeoJSON specification (RFC 7946).

    Parameters
    ----------
    value : Tuple[float, float], optional
        A point.

    Examples
    --------
    >>> Point((1,2))
    """

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

    @classmethod
    def __validate__(cls, value: typing.Any):
        if not isinstance(value, (tuple, list)):
            raise TypeError(
                f"Expected type 'typing.Tuple[float, float]' received type '{type(value).__name__}'"
            )
        elif len(value) != 2:
            raise ValueError(
                "A point should contain only two x-y coordinates."
            )
        for item in value:
            if not isinstance(item, (int, float, np.floating)):
                raise TypeError(
                    f"Expected type '{float.__name__}' received type '{type(item).__name__}'"
                )

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

    def encode_value(self) -> typing.Any:
        """Encode object to JSON compatible dictionary."""
        value = self.get_value()
        if value is None:
            return None
        return (float(value[0]), float(value[1]))

    def tuple(self):
        return self.get_value()

    def resize(
        self,
        og_img_h=10,
        og_img_w=10,
        new_img_h=100,
        new_img_w=100,
    ):
        value = self.get_value()
        h_ratio = new_img_h / og_img_h
        w_ratio = new_img_w / og_img_w
        return Point((value[0] * h_ratio, value[1] * w_ratio))

    @property
    def x(self):
        return self.get_value()[0]

    @property
    def y(self):
        return self.get_value()[1]

Functions

valor.schemas.Point.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[float]]):
    """Decode object from JSON compatible dictionary."""
    if value is None:
        return None
    return cls((value[0], value[1]))

valor.schemas.Point.encode_value()

Encode object to JSON compatible dictionary.

Source code in valor/schemas/symbolic/types.py
def encode_value(self) -> typing.Any:
    """Encode object to JSON compatible dictionary."""
    value = self.get_value()
    if value is None:
        return None
    return (float(value[0]), float(value[1]))