geo.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. import numpy as np
  2. import matplotlib as mpl
  3. from matplotlib import _api
  4. from matplotlib.axes import Axes
  5. import matplotlib.axis as maxis
  6. from matplotlib.patches import Circle
  7. from matplotlib.path import Path
  8. import matplotlib.spines as mspines
  9. from matplotlib.ticker import (
  10. Formatter, NullLocator, FixedLocator, NullFormatter)
  11. from matplotlib.transforms import Affine2D, BboxTransformTo, Transform
  12. class GeoAxes(Axes):
  13. """An abstract base class for geographic projections."""
  14. class ThetaFormatter(Formatter):
  15. """
  16. Used to format the theta tick labels. Converts the native
  17. unit of radians into degrees and adds a degree symbol.
  18. """
  19. def __init__(self, round_to=1.0):
  20. self._round_to = round_to
  21. def __call__(self, x, pos=None):
  22. degrees = round(np.rad2deg(x) / self._round_to) * self._round_to
  23. return f"{degrees:0.0f}\N{DEGREE SIGN}"
  24. RESOLUTION = 75
  25. def _init_axis(self):
  26. self.xaxis = maxis.XAxis(self, clear=False)
  27. self.yaxis = maxis.YAxis(self, clear=False)
  28. self.spines['geo'].register_axis(self.yaxis)
  29. def clear(self):
  30. # docstring inherited
  31. super().clear()
  32. self.set_longitude_grid(30)
  33. self.set_latitude_grid(15)
  34. self.set_longitude_grid_ends(75)
  35. self.xaxis.set_minor_locator(NullLocator())
  36. self.yaxis.set_minor_locator(NullLocator())
  37. self.xaxis.set_ticks_position('none')
  38. self.yaxis.set_ticks_position('none')
  39. self.yaxis.set_tick_params(label1On=True)
  40. # Why do we need to turn on yaxis tick labels, but
  41. # xaxis tick labels are already on?
  42. self.grid(mpl.rcParams['axes.grid'])
  43. Axes.set_xlim(self, -np.pi, np.pi)
  44. Axes.set_ylim(self, -np.pi / 2.0, np.pi / 2.0)
  45. def _set_lim_and_transforms(self):
  46. # A (possibly non-linear) projection on the (already scaled) data
  47. self.transProjection = self._get_core_transform(self.RESOLUTION)
  48. self.transAffine = self._get_affine_transform()
  49. self.transAxes = BboxTransformTo(self.bbox)
  50. # The complete data transformation stack -- from data all the
  51. # way to display coordinates
  52. self.transData = \
  53. self.transProjection + \
  54. self.transAffine + \
  55. self.transAxes
  56. # This is the transform for longitude ticks.
  57. self._xaxis_pretransform = \
  58. Affine2D() \
  59. .scale(1, self._longitude_cap * 2) \
  60. .translate(0, -self._longitude_cap)
  61. self._xaxis_transform = \
  62. self._xaxis_pretransform + \
  63. self.transData
  64. self._xaxis_text1_transform = \
  65. Affine2D().scale(1, 0) + \
  66. self.transData + \
  67. Affine2D().translate(0, 4)
  68. self._xaxis_text2_transform = \
  69. Affine2D().scale(1, 0) + \
  70. self.transData + \
  71. Affine2D().translate(0, -4)
  72. # This is the transform for latitude ticks.
  73. yaxis_stretch = Affine2D().scale(np.pi * 2, 1).translate(-np.pi, 0)
  74. yaxis_space = Affine2D().scale(1, 1.1)
  75. self._yaxis_transform = \
  76. yaxis_stretch + \
  77. self.transData
  78. yaxis_text_base = \
  79. yaxis_stretch + \
  80. self.transProjection + \
  81. (yaxis_space +
  82. self.transAffine +
  83. self.transAxes)
  84. self._yaxis_text1_transform = \
  85. yaxis_text_base + \
  86. Affine2D().translate(-8, 0)
  87. self._yaxis_text2_transform = \
  88. yaxis_text_base + \
  89. Affine2D().translate(8, 0)
  90. def _get_affine_transform(self):
  91. transform = self._get_core_transform(1)
  92. xscale, _ = transform.transform((np.pi, 0))
  93. _, yscale = transform.transform((0, np.pi/2))
  94. return Affine2D() \
  95. .scale(0.5 / xscale, 0.5 / yscale) \
  96. .translate(0.5, 0.5)
  97. def get_xaxis_transform(self, which='grid'):
  98. _api.check_in_list(['tick1', 'tick2', 'grid'], which=which)
  99. return self._xaxis_transform
  100. def get_xaxis_text1_transform(self, pad):
  101. return self._xaxis_text1_transform, 'bottom', 'center'
  102. def get_xaxis_text2_transform(self, pad):
  103. return self._xaxis_text2_transform, 'top', 'center'
  104. def get_yaxis_transform(self, which='grid'):
  105. _api.check_in_list(['tick1', 'tick2', 'grid'], which=which)
  106. return self._yaxis_transform
  107. def get_yaxis_text1_transform(self, pad):
  108. return self._yaxis_text1_transform, 'center', 'right'
  109. def get_yaxis_text2_transform(self, pad):
  110. return self._yaxis_text2_transform, 'center', 'left'
  111. def _gen_axes_patch(self):
  112. return Circle((0.5, 0.5), 0.5)
  113. def _gen_axes_spines(self):
  114. return {'geo': mspines.Spine.circular_spine(self, (0.5, 0.5), 0.5)}
  115. def set_yscale(self, *args, **kwargs):
  116. if args[0] != 'linear':
  117. raise NotImplementedError
  118. set_xscale = set_yscale
  119. def set_xlim(self, *args, **kwargs):
  120. """Not supported. Please consider using Cartopy."""
  121. raise TypeError("Changing axes limits of a geographic projection is "
  122. "not supported. Please consider using Cartopy.")
  123. set_ylim = set_xlim
  124. def format_coord(self, lon, lat):
  125. """Return a format string formatting the coordinate."""
  126. lon, lat = np.rad2deg([lon, lat])
  127. ns = 'N' if lat >= 0.0 else 'S'
  128. ew = 'E' if lon >= 0.0 else 'W'
  129. return ('%f\N{DEGREE SIGN}%s, %f\N{DEGREE SIGN}%s'
  130. % (abs(lat), ns, abs(lon), ew))
  131. def set_longitude_grid(self, degrees):
  132. """
  133. Set the number of degrees between each longitude grid.
  134. """
  135. # Skip -180 and 180, which are the fixed limits.
  136. grid = np.arange(-180 + degrees, 180, degrees)
  137. self.xaxis.set_major_locator(FixedLocator(np.deg2rad(grid)))
  138. self.xaxis.set_major_formatter(self.ThetaFormatter(degrees))
  139. def set_latitude_grid(self, degrees):
  140. """
  141. Set the number of degrees between each latitude grid.
  142. """
  143. # Skip -90 and 90, which are the fixed limits.
  144. grid = np.arange(-90 + degrees, 90, degrees)
  145. self.yaxis.set_major_locator(FixedLocator(np.deg2rad(grid)))
  146. self.yaxis.set_major_formatter(self.ThetaFormatter(degrees))
  147. def set_longitude_grid_ends(self, degrees):
  148. """
  149. Set the latitude(s) at which to stop drawing the longitude grids.
  150. """
  151. self._longitude_cap = np.deg2rad(degrees)
  152. self._xaxis_pretransform \
  153. .clear() \
  154. .scale(1.0, self._longitude_cap * 2.0) \
  155. .translate(0.0, -self._longitude_cap)
  156. def get_data_ratio(self):
  157. """Return the aspect ratio of the data itself."""
  158. return 1.0
  159. ### Interactive panning
  160. def can_zoom(self):
  161. """
  162. Return whether this Axes supports the zoom box button functionality.
  163. This Axes object does not support interactive zoom box.
  164. """
  165. return False
  166. def can_pan(self):
  167. """
  168. Return whether this Axes supports the pan/zoom button functionality.
  169. This Axes object does not support interactive pan/zoom.
  170. """
  171. return False
  172. def start_pan(self, x, y, button):
  173. pass
  174. def end_pan(self):
  175. pass
  176. def drag_pan(self, button, key, x, y):
  177. pass
  178. class _GeoTransform(Transform):
  179. # Factoring out some common functionality.
  180. input_dims = output_dims = 2
  181. def __init__(self, resolution):
  182. """
  183. Create a new geographical transform.
  184. Resolution is the number of steps to interpolate between each input
  185. line segment to approximate its path in curved space.
  186. """
  187. super().__init__()
  188. self._resolution = resolution
  189. def __str__(self):
  190. return f"{type(self).__name__}({self._resolution})"
  191. def transform_path_non_affine(self, path):
  192. # docstring inherited
  193. ipath = path.interpolated(self._resolution)
  194. return Path(self.transform(ipath.vertices), ipath.codes)
  195. class AitoffAxes(GeoAxes):
  196. name = 'aitoff'
  197. class AitoffTransform(_GeoTransform):
  198. """The base Aitoff transform."""
  199. @_api.rename_parameter("3.8", "ll", "values")
  200. def transform_non_affine(self, values):
  201. # docstring inherited
  202. longitude, latitude = values.T
  203. # Pre-compute some values
  204. half_long = longitude / 2.0
  205. cos_latitude = np.cos(latitude)
  206. alpha = np.arccos(cos_latitude * np.cos(half_long))
  207. sinc_alpha = np.sinc(alpha / np.pi) # np.sinc is sin(pi*x)/(pi*x).
  208. x = (cos_latitude * np.sin(half_long)) / sinc_alpha
  209. y = np.sin(latitude) / sinc_alpha
  210. return np.column_stack([x, y])
  211. def inverted(self):
  212. # docstring inherited
  213. return AitoffAxes.InvertedAitoffTransform(self._resolution)
  214. class InvertedAitoffTransform(_GeoTransform):
  215. @_api.rename_parameter("3.8", "xy", "values")
  216. def transform_non_affine(self, values):
  217. # docstring inherited
  218. # MGDTODO: Math is hard ;(
  219. return np.full_like(values, np.nan)
  220. def inverted(self):
  221. # docstring inherited
  222. return AitoffAxes.AitoffTransform(self._resolution)
  223. def __init__(self, *args, **kwargs):
  224. self._longitude_cap = np.pi / 2.0
  225. super().__init__(*args, **kwargs)
  226. self.set_aspect(0.5, adjustable='box', anchor='C')
  227. self.clear()
  228. def _get_core_transform(self, resolution):
  229. return self.AitoffTransform(resolution)
  230. class HammerAxes(GeoAxes):
  231. name = 'hammer'
  232. class HammerTransform(_GeoTransform):
  233. """The base Hammer transform."""
  234. @_api.rename_parameter("3.8", "ll", "values")
  235. def transform_non_affine(self, values):
  236. # docstring inherited
  237. longitude, latitude = values.T
  238. half_long = longitude / 2.0
  239. cos_latitude = np.cos(latitude)
  240. sqrt2 = np.sqrt(2.0)
  241. alpha = np.sqrt(1.0 + cos_latitude * np.cos(half_long))
  242. x = (2.0 * sqrt2) * (cos_latitude * np.sin(half_long)) / alpha
  243. y = (sqrt2 * np.sin(latitude)) / alpha
  244. return np.column_stack([x, y])
  245. def inverted(self):
  246. # docstring inherited
  247. return HammerAxes.InvertedHammerTransform(self._resolution)
  248. class InvertedHammerTransform(_GeoTransform):
  249. @_api.rename_parameter("3.8", "xy", "values")
  250. def transform_non_affine(self, values):
  251. # docstring inherited
  252. x, y = values.T
  253. z = np.sqrt(1 - (x / 4) ** 2 - (y / 2) ** 2)
  254. longitude = 2 * np.arctan((z * x) / (2 * (2 * z ** 2 - 1)))
  255. latitude = np.arcsin(y*z)
  256. return np.column_stack([longitude, latitude])
  257. def inverted(self):
  258. # docstring inherited
  259. return HammerAxes.HammerTransform(self._resolution)
  260. def __init__(self, *args, **kwargs):
  261. self._longitude_cap = np.pi / 2.0
  262. super().__init__(*args, **kwargs)
  263. self.set_aspect(0.5, adjustable='box', anchor='C')
  264. self.clear()
  265. def _get_core_transform(self, resolution):
  266. return self.HammerTransform(resolution)
  267. class MollweideAxes(GeoAxes):
  268. name = 'mollweide'
  269. class MollweideTransform(_GeoTransform):
  270. """The base Mollweide transform."""
  271. @_api.rename_parameter("3.8", "ll", "values")
  272. def transform_non_affine(self, values):
  273. # docstring inherited
  274. def d(theta):
  275. delta = (-(theta + np.sin(theta) - pi_sin_l)
  276. / (1 + np.cos(theta)))
  277. return delta, np.abs(delta) > 0.001
  278. longitude, latitude = values.T
  279. clat = np.pi/2 - np.abs(latitude)
  280. ihigh = clat < 0.087 # within 5 degrees of the poles
  281. ilow = ~ihigh
  282. aux = np.empty(latitude.shape, dtype=float)
  283. if ilow.any(): # Newton-Raphson iteration
  284. pi_sin_l = np.pi * np.sin(latitude[ilow])
  285. theta = 2.0 * latitude[ilow]
  286. delta, large_delta = d(theta)
  287. while np.any(large_delta):
  288. theta[large_delta] += delta[large_delta]
  289. delta, large_delta = d(theta)
  290. aux[ilow] = theta / 2
  291. if ihigh.any(): # Taylor series-based approx. solution
  292. e = clat[ihigh]
  293. d = 0.5 * (3 * np.pi * e**2) ** (1.0/3)
  294. aux[ihigh] = (np.pi/2 - d) * np.sign(latitude[ihigh])
  295. xy = np.empty(values.shape, dtype=float)
  296. xy[:, 0] = (2.0 * np.sqrt(2.0) / np.pi) * longitude * np.cos(aux)
  297. xy[:, 1] = np.sqrt(2.0) * np.sin(aux)
  298. return xy
  299. def inverted(self):
  300. # docstring inherited
  301. return MollweideAxes.InvertedMollweideTransform(self._resolution)
  302. class InvertedMollweideTransform(_GeoTransform):
  303. @_api.rename_parameter("3.8", "xy", "values")
  304. def transform_non_affine(self, values):
  305. # docstring inherited
  306. x, y = values.T
  307. # from Equations (7, 8) of
  308. # https://mathworld.wolfram.com/MollweideProjection.html
  309. theta = np.arcsin(y / np.sqrt(2))
  310. longitude = (np.pi / (2 * np.sqrt(2))) * x / np.cos(theta)
  311. latitude = np.arcsin((2 * theta + np.sin(2 * theta)) / np.pi)
  312. return np.column_stack([longitude, latitude])
  313. def inverted(self):
  314. # docstring inherited
  315. return MollweideAxes.MollweideTransform(self._resolution)
  316. def __init__(self, *args, **kwargs):
  317. self._longitude_cap = np.pi / 2.0
  318. super().__init__(*args, **kwargs)
  319. self.set_aspect(0.5, adjustable='box', anchor='C')
  320. self.clear()
  321. def _get_core_transform(self, resolution):
  322. return self.MollweideTransform(resolution)
  323. class LambertAxes(GeoAxes):
  324. name = 'lambert'
  325. class LambertTransform(_GeoTransform):
  326. """The base Lambert transform."""
  327. def __init__(self, center_longitude, center_latitude, resolution):
  328. """
  329. Create a new Lambert transform. Resolution is the number of steps
  330. to interpolate between each input line segment to approximate its
  331. path in curved Lambert space.
  332. """
  333. _GeoTransform.__init__(self, resolution)
  334. self._center_longitude = center_longitude
  335. self._center_latitude = center_latitude
  336. @_api.rename_parameter("3.8", "ll", "values")
  337. def transform_non_affine(self, values):
  338. # docstring inherited
  339. longitude, latitude = values.T
  340. clong = self._center_longitude
  341. clat = self._center_latitude
  342. cos_lat = np.cos(latitude)
  343. sin_lat = np.sin(latitude)
  344. diff_long = longitude - clong
  345. cos_diff_long = np.cos(diff_long)
  346. inner_k = np.maximum( # Prevent divide-by-zero problems
  347. 1 + np.sin(clat)*sin_lat + np.cos(clat)*cos_lat*cos_diff_long,
  348. 1e-15)
  349. k = np.sqrt(2 / inner_k)
  350. x = k * cos_lat*np.sin(diff_long)
  351. y = k * (np.cos(clat)*sin_lat - np.sin(clat)*cos_lat*cos_diff_long)
  352. return np.column_stack([x, y])
  353. def inverted(self):
  354. # docstring inherited
  355. return LambertAxes.InvertedLambertTransform(
  356. self._center_longitude,
  357. self._center_latitude,
  358. self._resolution)
  359. class InvertedLambertTransform(_GeoTransform):
  360. def __init__(self, center_longitude, center_latitude, resolution):
  361. _GeoTransform.__init__(self, resolution)
  362. self._center_longitude = center_longitude
  363. self._center_latitude = center_latitude
  364. @_api.rename_parameter("3.8", "xy", "values")
  365. def transform_non_affine(self, values):
  366. # docstring inherited
  367. x, y = values.T
  368. clong = self._center_longitude
  369. clat = self._center_latitude
  370. p = np.maximum(np.hypot(x, y), 1e-9)
  371. c = 2 * np.arcsin(0.5 * p)
  372. sin_c = np.sin(c)
  373. cos_c = np.cos(c)
  374. latitude = np.arcsin(cos_c*np.sin(clat) +
  375. ((y*sin_c*np.cos(clat)) / p))
  376. longitude = clong + np.arctan(
  377. (x*sin_c) / (p*np.cos(clat)*cos_c - y*np.sin(clat)*sin_c))
  378. return np.column_stack([longitude, latitude])
  379. def inverted(self):
  380. # docstring inherited
  381. return LambertAxes.LambertTransform(
  382. self._center_longitude,
  383. self._center_latitude,
  384. self._resolution)
  385. def __init__(self, *args, center_longitude=0, center_latitude=0, **kwargs):
  386. self._longitude_cap = np.pi / 2
  387. self._center_longitude = center_longitude
  388. self._center_latitude = center_latitude
  389. super().__init__(*args, **kwargs)
  390. self.set_aspect('equal', adjustable='box', anchor='C')
  391. self.clear()
  392. def clear(self):
  393. # docstring inherited
  394. super().clear()
  395. self.yaxis.set_major_formatter(NullFormatter())
  396. def _get_core_transform(self, resolution):
  397. return self.LambertTransform(
  398. self._center_longitude,
  399. self._center_latitude,
  400. resolution)
  401. def _get_affine_transform(self):
  402. return Affine2D() \
  403. .scale(0.25) \
  404. .translate(0.5, 0.5)