将Matplotlib文件导出为KML。

huangapple go评论70阅读模式
英文:

How to export matplotlib file to kml

问题

Output 1:

import simplekml
kml = simplekml.Kml()
kml.newpoint(name="Kirstenbosch", coords=[(18.432314,33.988862)])
kml.save("botanicalgarden.kml")

Output 2:

import matplotlib
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as ppl
from pylab import rcParams
import simplekml
rcParams['figure.figsize'] = (8,8)

# 在0到10度经度和0到10度纬度上创建矩形
x = [0, 10, 10, 0, 0]
y = [10, 10, 0, 0, 10]
x1 = range(0,11)    # 画对角线

fig = ppl.figure(1)
ax = fig.add_axes([0,0,1,1])
ax.axis('off')
fig.patch.set_facecolor('blue')  # 以便看到真实范围

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()

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")
英文:

I'm trying to export a matplotlib figure using a kml format. The question is identical to the following:

https://stackoverflow.com/questions/31709437/export-python-plot-to-kml

I've outlined the exact function but I can't get the kml output to work. Conversely, if I export a simplekml function, it's working fine.

I've attached both outputs below. Output 1 one works but 2 doesn't.

Output 1:

import simplekml
kml = simplekml.Kml()
kml.newpoint(name="Kirstenbosch", coords=[(18.432314,33.988862)])
kml.save("botanicalgarden.kml")

将Matplotlib文件导出为KML。

But when trying to pass a matplotlib function to simplekml function, I'm returning the following output. What am I doing wrong?

Output 2:

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()

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")

将Matplotlib文件导出为KML。

答案1

得分: 2

确保您已完成以下操作:

pip 安装 matplotlib
pip 安装 basemap
pip 安装 simplekml

我已安装的版本:

Matplotlib 版本:3.7.1
Basemap 版本:1.3.7
Simplekml 版本:1.3.2

如何从命令行查找您已安装的版本:

python -c "import matplotlib; import mpl_toolkits.basemap; import simplekml; print('Matplotlib 版本:', matplotlib.__version__); print('Basemap 版本:', mpl_toolkits.basemap.__version__); print('Simplekml 版本:', simplekml.__version__)"

此外,请检查您是否位于正确的工作目录中。另外,请确保您在目标目录中具有必要的权限。如果程序没有写入权限,它将无法保存 KML 文件。此外,请检查兼容性问题:您使用的 mpl_toolkits.basemapmatplotlib 版本可能存在兼容性问题。

最后,我能够成功运行您的代码。请参见下面的图片链接:

将Matplotlib文件导出为KML。

您提供的代码:将Matplotlib文件导出为KML。

英文:

Ensure you have done the following:

pip install matplotlib
pip install basemap
pip install simplekml

Versions I have installed:

Matplotlib version: 3.7.1
Basemap version: 1.3.7  
Simplekml version: 1.3.2

How to find the versions you have installed from command line:

python -c "import matplotlib; import mpl_toolkits.basemap; import simplekml; print('Matplotlib version:', matplotlib.__version__); print('Basemap version:', mpl_toolkits.basemap.__version__); print('Simplekml version:', simplekml.__version__)"

Moreover, Check that you are in the correct working directory. Additionally, ensure you have the necessary permissions in the target directory. If the program does not have write permissions, it will not be able to save the KML file. Furthermore, check for compatibility issues: It's possible that there may be compatibility issues between the versions of mpl_toolkits.basemap and matplotlib you are using.

Lastly, I was able to run your code just fine. See image below:
将Matplotlib文件导出为KML。

The code that you provided: 将Matplotlib文件导出为KML。

huangapple
  • 本文由 发表于 2023年6月26日 12:58:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定