Python底图drawgreatcircle在路径上有空隙

时间:2018-11-24 19:04:49

标签: python matplotlib-basemap

我看到一些奇怪的底图行为。它画了一个有空隙的大圆圈。

以下是从温哥华到伦敦画一个大圆圈的代码:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

def plot_map():
    bg_color = (0.0, 0.0, 0.0, 1.0)
    coast_color = (204/255.0, 0.8, 153/255.0, 0.7)
    country_color = (204/255.0, 0.8, 153/255.0, 0.2)
    path_color = (204/255.0, 0.2, 153/255.0, 0.6)

    dep_lat, dep_lon = 51.470020, -0.454295
    arr_lat, arr_lon = 49.193901, -123.183998

    plt.figure()

    m = Basemap(llcrnrlon=-130.,llcrnrlat=40.,urcrnrlon=10.,urcrnrlat=70.,\
            rsphere=(6378137.00,6356752.3142),\
            resolution='l',projection='merc',\
            lat_0=40.,lon_0=-20.,lat_ts=20.)
    m.drawcoastlines(color=coast_color, linewidth=1.0)
    m.fillcontinents(color=bg_color, lake_color=bg_color)
    m.drawmapboundary(fill_color=bg_color)
    m.drawgreatcircle(dep_lon, dep_lat,arr_lon, arr_lat,
                      linewidth=2.0, color=path_color)

    plt.savefig('routes.png', format='png', bbox_inches='tight')

if __name__ == '__main__':
    plot_map()

结果如下:

drawgreatcircle返回的路径对象包含许多NAN,其差距为:

...
[10665759.64101299,  4404955.44856027],
[10474119.01096945,  4511711.30524047],
[              nan,               nan],
[              nan,               nan],
...
[              nan,               nan],
[              nan,               nan],
[ 4228670.53408886,  4599991.37541622],
[ 4031731.93039374,  4496703.65196268],
[ 3840974.7826069 ,  4389405.80623971],
...

有人可以帮助我吗? 谢谢

1 个答案:

答案 0 :(得分:1)

解决使用merc(墨卡托)投影在高纬度点时遇到的问题的一种方法是使用其他有效的投影。但是,如果您坚持使用Mercator绘制此类高纬度点,则可能需要结合两种方法。在这里,我使用cyl(无投影)绘制大圆弧。然后,提取其坐标,最后使用坐标在merc投影上绘制大圆路径。

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

fig=plt.figure()

# use 'cyl' projection to get greatcircle path
# some of the parameters are ignored dummies
m0 = Basemap(llcrnrlon=-130.,llcrnrlat=40.,urcrnrlon=10.,urcrnrlat=70., \
        rsphere=(6378137.00, 6356752.3142), \
        resolution='l', projection='cyl', \
        lat_0=40., lon_0=-20., lat_ts=20.)

dep_lat, dep_lon = 51.470020, -0.454295
arr_lat, arr_lon = 49.193901, -123.183998
# draw greatcircle and grab it
gcc = m0.drawgreatcircle(dep_lon, dep_lat,arr_lon, arr_lat, del_s=100.0, \
                      linewidth=2.0, color='None')
ax0 = fig.gca()
ax0.set_visible(False)   # suppress the plot of 'cyl' projection

# get list of long-lat for greatcircle plot later
xys = gcc[0].get_xydata()

def plot_map():
    bg_color = (0.0, 0.0, 0.0, 1.0)
    coast_color = (204/255.0, 0.8, 153/255.0, 0.7)
    country_color = (204/255.0, 0.8, 153/255.0, 0.2)
    path_color = (204/255.0, 0.2, 153/255.0, 0.6)

    plt.figure(figsize=[10, 6])

    m = Basemap(llcrnrlon=-130.,llcrnrlat=40.,urcrnrlon=10.,urcrnrlat=70., \
            rsphere=(6378137.00, 6356752.3142), \
            resolution='l', projection='merc', \
            lat_0=40., lon_0=-20., lat_ts=20.)

    m.drawcoastlines(color=coast_color, linewidth=1.0)
    m.fillcontinents(color=bg_color, lake_color=bg_color)
    m.drawmapboundary(fill_color=bg_color)

    # problematic code ...
    # m.drawgreatcircle(dep_lon, dep_lat,arr_lon, arr_lat, del_s=100.0, \
    #                 linewidth=2.0, color=path_color)

    # plot path on the map with coordinates obtained earlier
    m.plot(xys[:,0], xys[:,1], latlon=True)

    plt.savefig('routes.png', format='png', bbox_inches='tight')

if __name__ == '__main__':
    plot_map()

结果图:

enter image description here