how do I get all the data of the transformed line of the "handle" - Line2D object in the following code:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61
handle = plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
color='blue', linewidth=2, marker='o',
transform=ccrs.Geodetic(),
)
plt.show()
To be more clear: I'm not looking for the output of "handle[0].get_data()", since this just prints my original longitude and latitude, but im looking for the the data of the geodetic line drawn on the map.
I found the answer! According to this question , you can access the data of the transformation via the following code snippet:
[handle] = plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat], color='blue', linewidth=2, marker='o', transform=ccrs.Geodetic())
t_path = handle._get_transformed_path()
path_in_data_coords, _ = t_path.get_transformed_path_and_affine()
print(path_in_data_coords.vertices)
In the answer to this question there is also a second approach.
Let me do some computation and plot checks on the code provided by the OP.
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61
# Plot geodetic path in thick 'blue' line
handle = plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
color='blue', linewidth=10, marker='o',
transform=ccrs.Geodetic(),
)
# Get the geodetic path's coordinates to plot on top in 'red'
t_path = handle[0]._get_transformed_path()
path_in_data_coords, _ = t_path.get_transformed_path_and_affine()
ax.plot(path_in_data_coords.vertices[:,0],
path_in_data_coords.vertices[:,1],
color='red', lw=2)
plt.show()
And, the output plot is:
Congratulations to the OP.
(Extension part 1)
Now, let us compute the length of the geodesic path using the coordinates obtained above. My proposed code is:
# (*** Continued from the code above ***)
import cartopy.geodesic as geodesic
import numpy as np
# defining the earth shape on which to make calculations
myGeod = geodesic.Geodesic(6378137.0, 1/298.257223563)
# get (lat,long) lists from (long,lat) of the geodesic path
latlonlists = []
[latlonlists.append([lat,lon]) for lon,lat in zip(path_in_data_coords.vertices[:,0], path_in_data_coords.vertices[:,1])]
#print(latlonlists)
# compute length of the geodesic
geodesic_in_meters = myGeod.geometry_length(np.array(latlonlists))
print(geodesic_in_meters) # output: 17554975.077432975
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.