I've written a function to get the longitude of a point of known latitude between two known points (might not be working correctly yet), but I feel like there must be a better way of using pyproj to do this.
There isn't a built in method for doing this, but doing this binary search method I'm using seems possibly unnecessary as opposed to just solving for the other half of the coordinate directly.
Any suggestions? Ideally I'd like to generalize this to solve for latitude when the longitude is known, too.
My function thing (maybe not working, will update once I'm sure):
import pyproj
import random
def unknownlon(lon1,lat1,lon2,lat2,lat_known):
''' Binary search by distance to find unknown longitude between points
latitude and azimuth are known'''
# Get positive for north movement and negative for south
vert_change = (lat2 - lat1) / abs(lat2 - lat1)
horz_change = (lon2 - lon1) / abs(lon2 - lon1)
# Calculate angle and distance between known points
fwd_azimuth,back_azimuth,dist = g.inv(lon1,lat1,lon2,lon2)
# Random distance between known points
dist_result = random.uniform(0.0, dist)
# Loop while difference between desired lat and result is high
lat_diff = 1.0
while lat_diff > 0.00000001:
lon_result,lat_result,back_azimuth = g.fwd(lon1,lat1,
fwd_azimuth,dist_result)
# Calc difference between desired lat and result
lat_diff = abs(lat_known - lat_result)
# Determine whether to use a distance higher or lower than last
if (lat_result * vert_change) > (lat_known * vert_change):
dist_result = random.uniform(0.0, dist_result)
else:
dist_result = random.uniform(0.0, dist - dist_result)
lon1 = lon_result
lat1 = lat_result
return lon_result, dist_result, lat_diff
if __name__ == '__main__':
lat_known = 65.0
lat1 = 68.0
lon1 = 3.0
lat2 = 63.0
lon2 = -12.0
g = pyproj.Geod(ellps='WGS84') # Use WGS84 ellipsoid
# Calculate an unknown longitude
lon_result, dist_result, lat_diff = unknownlon(lon1,lat1,
lon2,lat2,
lat_known)
pyproj
package can use standard ellipsoid definitions for it's calculation, which is why I was thinking it would be a good option. I'll add the class/function call to my question for clarity. Thanks!