Skip to content

utm conversion will break in southern hemisphere #45

@Lkruitwagen

Description

@Lkruitwagen

proj_utm = pyproj.Proj(proj='utm', zone=utm_zone, ellps='WGS84')

Learned this the hard way, projection needs to be specified with the latitude (326 -> Norther hemisphere; vs 327 -> Southern).

e.g.

def get_utm_zone(lat, lon):
    """A function to grab the UTM zone number for any lat/lon location"""
    zone_str = str(int((lon + 180) / 6) + 1)

    if (lat >= 56.0) & (lat < 64.0) & (lon >= 3.0) & (lon < 12.0):
        zone_str = '32'
    elif (lat >= 72.0) & (lat < 84.0):
        if (lon >= 0.0) & (lon < 9.0):
            zone_str = '31'
        elif (lon >= 9.0) & (lon < 21.0):
            zone_str = '33'
        elif (lon >= 21.0) & (lon < 33.0):
            zone_str = '35'
        elif (lon >= 33.0) & (lon < 42.0):
            zone_str = '37'

    return zone_str


def get_utm_epsg(lat, lon, utm_zone=None):
    """A function to combine the UTM zone number and the hemisphere into an EPSG code"""

    if utm_zone is None:
        utm_zone = get_utm_zone(lat, lon)

    if lat > 0:
        return f'EPSG:{str(326)+str(utm_zone)}'
    else:
        return f'EPSG:{str(327)+str(utm_zone)}'


def utm_reprojs(utm_epsg):

    proj_wgs = pyproj.Proj('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')

    # define the UTM projection using the utm zone
    proj_utm = pyproj.Proj(utm_epsg)

    # create reprojection functions using functools.partial
    reproj_wgs_utm = partial(pyproj.transform, proj_wgs, proj_utm)
    reproj_utm_wgs = partial(pyproj.transform, proj_utm, proj_wgs)

    return {'wgs2utm': reproj_wgs_utm, 'utm2wgs': reproj_utm_wgs}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions