I'm trying to export a matplotlib figure using a kml format. The question is identical to the following:
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")
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")
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:
The code that you provided:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With