Guest User

Untitled

a guest
Oct 19th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. latlon2cartesian.py
  5.  
  6. !! These are parameterized differently than stuff listed in textbooks...
  7. """
  8.  
  9. import numpy as np
  10.  
  11. EARTH_RADIUS = 6371
  12. def haversine_distance(point1, point2, radius=EARTH_RADIUS):
  13. latitude1, longitude1 = np.radians(point1)
  14. latitude2, longitude2 = np.radians(point2)
  15.  
  16. dlongitude = longitude2 - longitude1
  17. dlatitude = latitude2 - latitude1
  18. a = np.sin(dlatitude/2)**2 + np.cos(latitude1) * np.cos(latitude2) * np.sin(dlongitude/2)**2
  19. c = 2 * np.arcsin(np.sqrt(a))
  20. km = radius * c
  21. return km
  22.  
  23.  
  24. def latlon2cartesian(latlon, d=1):
  25. lat, lon = np.radians(latlon)
  26. return np.array([
  27. d * np.cos(lat) * np.cos(-lon), # x
  28. d * np.cos(lat) * np.sin(-lon), # y
  29. d * np.sin(lat), # z
  30. ])
  31.  
  32. def latlon2cartesian_vec(latlon, d=1):
  33. latlon = np.radians(latlon)
  34. return np.array([
  35. d * np.cos(latlon[:,0]) * np.cos(-latlon[:,1]), # x
  36. d * np.cos(latlon[:,0]) * np.sin(-latlon[:,1]), # y
  37. d * np.sin(latlon[:,0]), # z
  38. ]).T
  39.  
  40.  
  41. def cartesian2latlon(xyz):
  42. d = (xyz ** 2).sum()
  43. x, y, z = xyz
  44. latlon = np.array([
  45. np.pi / 2 - np.arccos(z / d), # lat
  46. - np.arctan2(y, x), # lon
  47. ])
  48. return np.degrees(latlon)
  49.  
  50.  
  51. if __name__ == "__main__":
  52. # Some points
  53. chicago = np.array([41.8781, -87.6298])
  54. auckland = np.array([-36.8485, 174.7633])
  55. beijing = np.array([39.9042, 116.4074])
  56.  
  57. # Unit norm
  58. assert (latlon2cartesian(chicago) ** 2).sum() == 1, '(chicago) norm != 1'
  59. assert (latlon2cartesian(auckland) ** 2).sum() == 1, '(auckland) norm != 1'
  60. assert (latlon2cartesian(beijing) ** 2).sum() == 1, '(beijing) norm != 1'
  61.  
  62. # Reversible
  63. assert np.allclose(cartesian2latlon(latlon2cartesian(chicago)), chicago), '(chicago) reversal failed'
  64. assert np.allclose(cartesian2latlon(latlon2cartesian(auckland)), auckland), '(auckland) reversal failed'
  65. assert np.allclose(cartesian2latlon(latlon2cartesian(beijing)), beijing), '(beijing) reversal failed'
  66.  
  67. # Can compute distance
  68. assert np.allclose(
  69. haversine_distance(chicago, beijing),
  70. EARTH_RADIUS * np.arccos((latlon2cartesian(chicago) * latlon2cartesian(beijing)).sum()),
  71. ), '(chicago -> beijing) bad distance'
  72.  
  73. assert np.allclose(
  74. haversine_distance(chicago, auckland),
  75. EARTH_RADIUS * np.arccos((latlon2cartesian(chicago) * latlon2cartesian(auckland)).sum()),
  76. ), '(chicago -> auckland) bad distance'
  77.  
  78. assert np.allclose(
  79. haversine_distance(beijing, auckland),
  80. EARTH_RADIUS * np.arccos((latlon2cartesian(beijing) * latlon2cartesian(auckland)).sum()),
  81. ), '(beijing -> auckland) bad distance'
  82.  
  83. print('pass')
Add Comment
Please, Sign In to add comment