Skip to content

capturegraph.data.typed_json.handlers.solar #

SolarHandler #

Bases: TypeHandler

TypeHandler for CaptureGraph SolarPosition objects.

Serializes Location to lat/lon/alt format compatible with iOS PLocation.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/solar.py
class SolarHandler(TypeHandler):
    """TypeHandler for CaptureGraph SolarPosition objects.

    Serializes Location to lat/lon/alt format compatible with iOS PLocation.
    """

    def decodable_schema(self) -> Dict[str, Tuple[type, ...]]:
        """Return schema: expects latitude, longitude, altitude keys."""
        return {
            "azimuth": float,
            "altitude": float,
        }

    def decode(self, obj: Dict[str, Any]) -> Any | None:
        """Decode JSON dict to Location.

        Args:
            obj: Dict with 'latitude', 'longitude', 'altitude' keys.

        Returns:
            Location instance, or None if schema doesn't match.
        """
        try:
            return SolarPosition.from_json(obj)
        except (KeyError, TypeError, ValueError):
            return None

    def encodable_types(self) -> Tuple[type, ...]:
        """Return tuple of types this handler can encode."""
        return (SolarPosition,)

    def encode(self, obj: Any) -> Dict[str, Any] | None:
        """Encode Location to JSON dict.

        Args:
            obj: Location instance to encode.

        Returns:
            Dict with latitude/longitude/altitude keys, or None if not a Location.
        """
        return obj.to_json()

decodable_schema() #

Return schema: expects latitude, longitude, altitude keys.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/solar.py
def decodable_schema(self) -> Dict[str, Tuple[type, ...]]:
    """Return schema: expects latitude, longitude, altitude keys."""
    return {
        "azimuth": float,
        "altitude": float,
    }

decode(obj) #

Decode JSON dict to Location.

Parameters:

Name Type Description Default
obj Dict[str, Any]

Dict with 'latitude', 'longitude', 'altitude' keys.

required

Returns:

Type Description
Any | None

Location instance, or None if schema doesn't match.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/solar.py
def decode(self, obj: Dict[str, Any]) -> Any | None:
    """Decode JSON dict to Location.

    Args:
        obj: Dict with 'latitude', 'longitude', 'altitude' keys.

    Returns:
        Location instance, or None if schema doesn't match.
    """
    try:
        return SolarPosition.from_json(obj)
    except (KeyError, TypeError, ValueError):
        return None

encodable_types() #

Return tuple of types this handler can encode.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/solar.py
def encodable_types(self) -> Tuple[type, ...]:
    """Return tuple of types this handler can encode."""
    return (SolarPosition,)

encode(obj) #

Encode Location to JSON dict.

Parameters:

Name Type Description Default
obj Any

Location instance to encode.

required

Returns:

Type Description
Dict[str, Any] | None

Dict with latitude/longitude/altitude keys, or None if not a Location.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/solar.py
def encode(self, obj: Any) -> Dict[str, Any] | None:
    """Encode Location to JSON dict.

    Args:
        obj: Location instance to encode.

    Returns:
        Dict with latitude/longitude/altitude keys, or None if not a Location.
    """
    return obj.to_json()