Skip to content

xy_to_lonlat

cellseg_gsontools.xy_to_lonlat(x, y)

Converts x, y coordinates to lon, lat coordinates.

Parameters:

Name Type Description Default
x Union[float, Sequence]

x coordinate(s).

required
y Union[float, Sequence]

y coordinate(s).

required

Returns:

Type Description
Tuple[Union[float, Sequence], Union[float, Sequence]]

Tuple[Union[float, Sequence], Union[float, Sequence]]: lon, lat coordinates.

Examples:

>>> from cellseg_gsontools import xy_to_lonlat
>>> from shapely.geometry import Polygon
>>> poly = Polygon([(0, 0), (0, 1), (1, 1)])
>>> x, y = poly.exterior.coords.xy
>>> lon, lat = xy_to_lonlat(x, y)
>>> lon, lat
(array('d', [10.511, 10.511, 10.511, 10.511]),
 array('d', [0.0, 9.019e-06, 9.019e-06, 0.0]))
Source code in cellseg_gsontools/utils.py
def xy_to_lonlat(
    x: Union[float, Sequence], y: Union[float, Sequence]
) -> Tuple[Union[float, Sequence], Union[float, Sequence]]:
    """Converts x, y coordinates to lon, lat coordinates.

    Parameters:
        x (Union[float, Sequence]):
            x coordinate(s).
        y (Union[float, Sequence]):
            y coordinate(s).

    Returns:
        Tuple[Union[float, Sequence], Union[float, Sequence]]:
            lon, lat coordinates.

    Examples:
        >>> from cellseg_gsontools import xy_to_lonlat
        >>> from shapely.geometry import Polygon
        >>> poly = Polygon([(0, 0), (0, 1), (1, 1)])
        >>> x, y = poly.exterior.coords.xy
        >>> lon, lat = xy_to_lonlat(x, y)
        >>> lon, lat
        (array('d', [10.511, 10.511, 10.511, 10.511]),
         array('d', [0.0, 9.019e-06, 9.019e-06, 0.0]))
    """
    crs_utm = CRS(proj="utm", zone=33, ellps="WGS84")
    crs_latlon = CRS(proj="latlong", zone=33, ellps="WGS84")
    transformer = Transformer.from_crs(crs_utm, crs_latlon, always_xy=True)
    lonlat = transformer.transform(x, y)

    return lonlat[0], lonlat[1]