代码之家  ›  专栏  ›  技术社区  ›  Jellyse

如何以公里为单位设置标记?

  •  0
  • Jellyse  · 技术社区  · 7 年前

    我想把某一点包围起来。圆的半径需要是5公里,但如何设置我的标记,使圆在地图上是5公里?

    enter image description here

    import matplotlib.pyplot as plt
    import numpy as np
    from mpl_toolkits.basemap import Basemap
    
    width, height = 400000, 320000
    ax=plt.figure(figsize=(20,10))
    
    lonA =[2.631547,2.861595,2.931014]
    latA =[51.120983,51.209122,51.238868]
    
    m= Basemap(width=width,height=height,projection='lcc',
            resolution='h',lat_0=52.35,lon_0=4.5)
    
    m.drawmapboundary(fill_color='turquoise')
    m.fillcontinents(color='white',lake_color='aqua')
    m.drawcountries(linestyle='--')
    
    scatter2=m.scatter([], [], s=100, c='white', marker='o', label = 'Aurelia aurita', zorder=3, alpha=0.5,  edgecolor='steelblue')
    z,a = m(lonA[0:3], latA[0:3])
    scatter2.set_offsets(np.c_[z,a])
    
    plt.show()
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   swatchai    7 年前

    为了以地图单位(米)为单位绘制具有指定半径的圆,首先,我创建了一个函数(gencircle2),它接受圆的输入参数,并返回沿圆周长的点数组。在下面的代码中,命令 m(lon,lat) 用于计算(mx,my)以米为单位的地图投影坐标。 命令 genCircle2(cx=mx, cy=my, rad=5000.) 计算圆绘制的点。这是工作代码。

    import matplotlib.pyplot as plt
    import numpy as np
    from mpl_toolkits.basemap import Basemap
    
    def genCircle2(cx=0, cy=0, rad=1):
        """Generate points along perimeters of a circle"""
        points = []
        segs = 20
        for ea in range(segs+1):
            xi = cx + rad*np.cos(ea*2.*np.pi/segs)
            yi = cy + rad*np.sin(ea*2.*np.pi/segs)
            points.append([xi,yi])
        return np.array(points)
    
    width, height = 400000, 320000
    ax = plt.figure(figsize=(12,10))
    
    # long, lat
    lonA = [2.631547, 2.861595, 2.931014]
    latA = [51.120983, 51.209122, 51.238868]
    
    # accompanying attributes, colors and ...
    clrs = ['r', 'g', 'b']
    
    m = Basemap(width=width, height=height, projection='lcc', \
            resolution='i', lat_0=52.35, lon_0=4.5)
    
    m.drawmapboundary(fill_color='turquoise')
    m.fillcontinents(color='white', lake_color='aqua')
    m.drawcountries(linestyle='--')
    
    # plot circles at points defined by (lonA,latA)
    for lon,lat,clr in zip(lonA, latA, clrs):
        mx,my = m(lon,lat)                            # get map coordinates from (lon,lat)
        cclpnts = genCircle2(cx=mx, cy=my, rad=5000.) # get points along circle's perimeter
        m.plot(cclpnts[:,0], cclpnts[:,1], \
                   label='Aurelia aurita', color=clr, \
                   linewidth=0.75)           # plot circle
    
    plt.show()
    

    结果图:

    enter image description here