Debian Codegraph @

examples/02-plot/flight_paths.py

raw
1 """
2 .. _flight_paths_example:
3
4 Plot Curved Flight Paths on a Globe
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 Connect cities with elevated circular arcs from :func:`pyvista.CircularArc` to
8 create a simple route map.
9 """
10
11 import numpy as np
12 import pyvista as pv
13
14 # sphinx_gallery_start_ignore
15 # point labels are static-only in the docs
16 PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True
17 # sphinx_gallery_end_ignore
18
19
20 def latlon_to_xyz(latitude: float, longitude: float, radius: float = 1.0) -> np.ndarray:
21 """Convert latitude and longitude in degrees to Cartesian coordinates."""
22 lat = np.deg2rad(latitude)
23 lon = np.deg2rad(longitude)
24 return np.array(
25 [
26 radius * np.cos(lat) * np.cos(lon),
27 radius * np.cos(lat) * np.sin(lon),
28 radius * np.sin(lat),
29 ],
30 )
31
32
33 # %%
34 # Define city locations and routes
35 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36 # The routes are lifted slightly above the globe to keep them visible.
37
38 cities = {
39 'New York': latlon_to_xyz(40.7128, -74.0060, radius=1.02),
40 'London': latlon_to_xyz(51.5074, -0.1278, radius=1.02),
41 'Tokyo': latlon_to_xyz(35.6762, 139.6503, radius=1.02),
42 'Sydney': latlon_to_xyz(-33.8688, 151.2093, radius=1.02),
43 'Cape Town': latlon_to_xyz(-33.9249, 18.4241, radius=1.02),
44 }
45 routes = [
46 ('New York', 'London'),
47 ('London', 'Tokyo'),
48 ('Tokyo', 'Sydney'),
49 ('Cape Town', 'London'),
50 ]
51
52 arcs = []
53 for start, stop in routes:
54 arc = pv.CircularArc(
55 pointa=cities[start], pointb=cities[stop], center=(0, 0, 0), resolution=150
56 )
57 scale = 1.0 + 0.18 * np.sin(np.linspace(0, np.pi, arc.n_points))
58 directions = arc.points / np.linalg.norm(arc.points, axis=1)[:, None]
59 arc.points = directions * scale[:, None]
60 arcs.append(arc.tube(radius=0.01))
61
62
63 # %%
64 # Render the route map
65 # ~~~~~~~~~~~~~~~~~~~~
66 # Great-circle arcs follow the curvature of the sphere instead of cutting
67 # straight through it.
68
69 earth = pv.Sphere(theta_resolution=90, phi_resolution=90)
70 route_mesh = pv.merge(arcs)
71
72 pl = pv.Plotter()
73 pl.add_mesh(earth, color='lightsteelblue', smooth_shading=True, opacity=0.9)
74 pl.add_mesh(route_mesh, color='tomato')
75 pl.add_points(
76 np.array(list(cities.values())),
77 color='gold',
78 point_size=18,
79 render_points_as_spheres=True,
80 )
81 pl.add_point_labels(
82 np.array(list(cities.values())),
83 list(cities.keys()),
84 font_size=20,
85 point_size=0,
86 fill_shape=False,
87 )
88 pl.show()
89 # %%
90 # .. tags:: plot