Skip to content

lonlat_to_xy

cellseg_gsontools.lonlat_to_xy(lon, lat)

Converts lon, lat coordinates to x, y coordinates.

Parameters:

Name Type Description Default
lon Union[float, Sequence]

Longitude coordinate(s).

required
lat Union[float, Sequence]

Latitude coordinate(s).

required

Returns:

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

Tuple[Union[float, Sequence], Union[float, Sequence]]: x, y coordinates.

Examples:

>>> from shapely.geometry import Polygon
>>> from cellseg_gsontools import lonlat_to_xy
>>> poly = Polygon([(10, 10), (10, 0), (20, 10)])
>>> lon, lat = poly.exterior.coords.xy
>>> x, y = lonlat_to_xy(lon, lat)
>>> x, y
(array('d', [-48636.648, -57087.120, 1048636.648, -48636.648]),
 array('d', [1109577.311, 0.0, 1109577.311, 1109577.311]))
Source code in cellseg_gsontools/utils.py
def lonlat_to_xy(
    lon: Union[float, Sequence], lat: Union[float, Sequence]
) -> Tuple[Union[float, Sequence], Union[float, Sequence]]:
    """Converts lon, lat coordinates to x, y coordinates.

    Parameters:
        lon (Union[float, Sequence]):
            Longitude coordinate(s).
        lat (Union[float, Sequence]):
            Latitude coordinate(s).

    Returns:
        Tuple[Union[float, Sequence], Union[float, Sequence]]:
            x, y coordinates.

    Examples:
        >>> from shapely.geometry import Polygon
        >>> from cellseg_gsontools import lonlat_to_xy
        >>> poly = Polygon([(10, 10), (10, 0), (20, 10)])
        >>> lon, lat = poly.exterior.coords.xy
        >>> x, y = lonlat_to_xy(lon, lat)
        >>> x, y
        (array('d', [-48636.648, -57087.120, 1048636.648, -48636.648]),
         array('d', [1109577.311, 0.0, 1109577.311, 1109577.311]))
    """
    crs_utm = CRS(proj="utm", zone=33, ellps="WGS84")
    crs_latlon = CRS(proj="latlong", zone=33, ellps="WGS84")
    transformer = Transformer.from_crs(crs_latlon, crs_utm, always_xy=True)
    xy = transformer.transform(lon, lat)

    return xy[0], xy[1]