简体   繁体   中英

Alternative methods to cartopy functions (manipulating shapely linestrings - Geodetic)

Long story short I can't get cartopy to install in my environment so I'm looking for alternative ways of doing things it might be used for.

I've recently been following this tutorial which uses cartopy to alter the path of shapely linestrings to take into account the curvature of the earth:

"Cartopy can be used to manipulate the way that lines are plotted. The transform=ccrs.Geodetic() method transforms the LineStrings to account for the earths curvature"

assuming I can just google the actual value that is the curvature of the earth, are there any ways I could manually manipulate the linestrings to achieve roughly the same effect?

It's probably feasible to implement the great circle algorithms yourself, but there are also other options. If you manage the install pyproj for example, you can use the example below, it samples a given amount of points between two locations on earth.

Note that although I still use Cartopy to show the coastlines (for reference), the actual plotting of the line (great circle) is fully independent of Cartopy and can be used with a plain Matplotlib axes as well.

And you can always read the coastlines or other borders/annotation etc for your map without Cartopy as well, as long as you pay attention to the projection. Matplotlib has all the primatives to do this (lines, PathCollections etc), it's just that Cartopy makes it a lot more convenient.

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from pyproj import Geod

# from
lat1 = 55.
lon1 = -65

# to
lat2 = 30
lon2 = 80

n_samples = 1000

g = Geod(ellps='WGS84')
coords = g.npts(
    lon1, lat1, lon2, lat2, n_samples, initial_idx=0, terminus_idx=0,
)
lons, lats = zip(*coords)

fig, ax = plt.subplots(
    figsize=(10,5), dpi=86, facecolor="w", 
    subplot_kw=dict(projection=ccrs.PlateCarree(), xlim=(-180,180), ylim=(-90, 90)),
)
ax.axis("off")
ax.coastlines(lw=.3)

ax.plot(lons, lats, "r-") # <- no cartopy, just x/y points! 

在此处输入图像描述

The amount of points you sample should probably depend on the distance between both points, which can also be calculated using the g.inv(...) function. And the amount is a tradeoff between accuracy and performance. For most applications you probably want to keep it as low as possible, just above where you start to visibly see the effect.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM