Python inaccurate lat/lon calculations?

Viewed 23

I have a code that takes the starting lat/lon, a bearing (direction), and then distance (in km) to find the new lat lon on a spherical earth. The code looks like:

get_new_lat_lon_from_distance_bearing_lat_lon(lat0,lon0,bearing,d):

    import math

    # Quick constant
    R = 6378.1

    # Convert to radians
    lat1 = math.radians(lat0)
    lon1 = math.radians(lon0)
    brng = math.radians(bearing)

    # Do some math for lat and lon
    lat2 = math.asin( math.sin(lat1)*math.cos(d/R) + math.cos(lat1)*math.sin(d/R)*math.cos(brng))
    lon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),math.cos(d/R)-math.sin(lat1)*math.sin(lat2))

    # Reconvert to degrees
    lat2 = math.degrees(lat2)
    lon2 = math.degrees(lon2)

    return lat2,lon2

I can then call this such that:

lat_s,lon_s = get_new_lat_lon_from_distance_bearing_lat_lon(yll,xll,180,cellsize*r)
lat_e,lon_e = get_new_lat_lon_from_distance_bearing_lat_lon(yll,xll,90,cellsize*c)   

where yll = 130, and xll = 55

I want to move every 500m from this starting lat/lon position south (bearing = 180) and then also to the east (bearing = 90). The East direction should be 14,000 times, and then the South should be traversed 7000 times. In other words, we can loop through these such that:

nrows = 7000
ncols = 14000
c = 0.5 (0.5km)
# Pre-allocate
biglats = []
biglons = []

# Traverse south
for r in range(0,nrows):
    lat_s,lon_s = get_new_lat_lon_from_distance_bearing_lat_lon(yll,xll,180,cellsize*r)  
    biglats.append(lat_s)

# Traverse East
for c in range(0,ncols):
    lat_e,lon_e = get_new_lat_lon_from_distance_bearing_lat_lon(yll,xll,90,cellsize*c)   
    biglons.append(lon_e)

However, when I print the first and last values of each:

55.000000000147004
23.56327426583246
-130.00000000007
-56.372396480687385

23.56 should be 20, and -56.37 should be -60. The end goal would be to create a meshgrid of lat/lon with a [14000,7000] array. However, the calculations are wrong. What could be done to get a more correct lat/lon and/or some sort of 'meshgrid' of 14000,7000 values of lat/lon equally spaced 500m apart given the starting lat/lon provided?

0 Answers
Related