Skip to content

Fixing longitude values when they cross +/-180 #1

@aivanni

Description

@aivanni

The function convert_to_latlon gives wrong longitude values that can be above 180 or below -180 degrees when the reference longitude is close to +/-180. This should fix it:

def convert_to_latlon(y, x, latitude, longitude):

    pi = 3.14159265359

    r_earth = 6371000

   lat_diff = (y / r_earth) * (180 / pi)
   lon_diff = (x / r_earth) * (180 / pi) / math.cos(latitude * pi/180)

   final_lat = latitude+lat_diff
   final_lon = longitude+lon_diff

   final_lon = final_lon - 360 if final_lon > 180 else final_lon
   final_lon = 360 + final_lon if final_lon < -180 else final_lon

   return (final_lat, final_lon)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions