Replace your arg installation code with:
lat1, lon1 = p1 lat2, lon2 = p2 assert -90 <= lat1 <= 90 assert -90 <= lat2 <= 90 assert -180 <= lon1 <= 180 assert -180 <= lon2 <= 180 lat1, lon1, lat2, lon2 = map(math.radians, (lat1, lon1, lat2, lon2))
and run your code again.
Update A few encouraging and helpful guidelines for latitude / longitude calculations:
- Lat / lon input in degrees or radian?
- Check lat / lon input for valid range
- Check the OUTPUT lat / lon value for a valid range. Longitude has a gap on the international line.
The last part of the midpoint subroutine can be usefully modified to avoid a potential long-distance usage problem:
lon3 = lon1 + math.atan2(dy, math.cos(lat1) + dx) # replacement code follows: lon3d = math.degrees(lon3) if lon3d < -180: print "oops1", lon3d lon3d += 360 elif lon3d > 180: print "oops2", lon3d lon3d -= 360 return(math.degrees(lat3), lon3d)
For example, looking for an intermediate point between Auckland, New Zealand (-36.9, 174.8) and Papeete, Tahiti (-17.5, -149.5) gives oops2 194.270430902
on the way to a valid answer (-28.355951246746923, -165.72956909809082)
source share