将Python Plot导出为KML

时间:2015-07-29 19:27:25

标签: python matplotlib kml

如何使用Matplotlib创建一个lon / lat图,可以将其导出到Google地球,并在G.E.上显示点正确。图片可以在这里看到:http://imgur.com/4szLzNF Google Earth view   好像在我导出的图形周围总是有一个小边框,这样我在绘图中定义的点在G.E中是关闭的。

x = [0, 10, 10, 0, 0]
y = [10, 10, 0, 0, 10]
x1=[0,1,2,3,4,5,6,7,8,9,10]

fig = Figure(facecolor=None, frameon=False)
ax = fig.add_axes([0,0,1,1])
ax.axis('off')

ppl.plot(x, y, 'r',  axes=ax)
ppl.plot(x, y, '.b', axes=ax)
ppl.plot(x1, x1, 'g', axes=ax)

ppl.axis('off')
ppl.tight_layout(0,h_pad=0, w_pad=0)
border1 = ppl.axis(bbox_inches='tight')
ppl.show()

pngName = 'temp.png'
py.savefig(pngName, bbox_inches='tight', pad_inches=0, transparent=True)

bottomleft  = (border1[0],border1[2])
bottomright = (border1[1],border1[2])
topright    = (border1[1],border1[3])
topleft     = (border1[0],border1[3])

kml = simplekml.Kml()
ground = kml.newgroundoverlay(name='GroundOverlay')
ground.icon.href = pngName
ground.gxlatlonquad.coords =[bottomleft, bottomright, topright, topleft]
kml.save("GroundOverlay_temp.kml")

1 个答案:

答案 0 :(得分:2)

我有一个解决方案,但我对图和艺术家类不太熟悉,无法清楚地解释为什么它是正确的。

你需要做这两件事:

  1. 使用fig = ppl.figure()代替matplotlib.figure.Figure()
  2. 使用fig.savefig()代替ppl.savefig()保存。
  3. 也不需要使用tight_layout和填充。我还为该图设置了facecolor,以便我可以看到真正的边界。使用图像查看器查看输出图像 temp.png ,您应该看到矩形边线位于图形边缘;当我运行原始代码并查看图像时,矩形和图形边界之间总是有一些空间。

    这是固定代码:

    import matplotlib
    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as ppl
    from pylab import rcParams
    import simplekml
    rcParams['figure.figsize'] = (8,8)
    # create rectangle over 0 to 10 degrees longitude and 0 to 10 degrees latitude
    x = [0, 10, 10, 0, 0]
    y = [10, 10, 0, 0, 10]
    x1=range(0,11)    # to draw a diagonal line
    
    fig = ppl.figure(1)
    ax = fig.add_axes([0,0,1,1])
    ax.axis('off')
    fig.patch.set_facecolor('blue')  # so we can see the true extent
    
    ppl.plot(x, y, 'r', linewidth=3)
    ppl.plot(x, y, '.b', linewidth=3)
    ppl.plot(x1, x1, 'g', linewidth=3)
    
    ppl.axis('off')
    border1 = ppl.axis()
    print 'Border = %s' %(str(border1))
    if False:
        ppl.show()
    else:
        pngName = 'Overlay.png'
        fig.savefig(pngName, facecolor=fig.get_facecolor(), transparent=False)
    
    bottomleft  = (border1[0],border1[2])
    bottomright = (border1[1],border1[2])
    topright    = (border1[1],border1[3])
    topleft     = (border1[0],border1[3])
    
    kml = simplekml.Kml()
    ground = kml.newgroundoverlay(name='GroundOverlay')
    ground.icon.href = pngName
    ground.gxlatlonquad.coords =[bottomleft, bottomright, topright, topleft]
    kml.save("GroundOverlay.kml")