Marble/Runners/MarblePythonLoadingKML
< Marble
Loading KML files into Marble
Tutorial Series | Marble Python Tutorial |
Previous | Tutorial 3 - Basic Interactions with MarbleWidget |
What's Next | Tutorial 5 - Loading OSM files into MarbleWidget |
Further Reading | n/a |
Loading KML files into a Marble Widget
Note
the documentation for the Marble Python bindings are currently not available online. They can instead be found in the source code at the location: src/bindings/python/html/marble/Marble.html.
Marble uses so-called runners to calculate routes, do reverse geocoding, parse files and search for placemarks (cities, addresses, points of interest, ...). This tutorial shows how to open a .kml (or .gpx, .osm, ...) file and display it into the Marble Widget.
#!/usr/bin/env python
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.marble import *
import sys
import os
def main():
app = QApplication(sys.argv)
# check the file was loaded
if(len(sys.argv) < 2):
print("Usage: python " + sys.argv[0] + " file.kml")
return 1
# load file
inputFile = os.path.abspath(sys.argv[-1])
print(inputFile)
# create the marble widget
marble = Marble.MarbleWidget()
# resize the widget and add a window title
marble.resize(800, 600)
marble.setWindowTitle("loadKML")
# Load the OpenStreetMap map
marble.setMapThemeId("earth/openstreetmap/openstreetmap.dgml")
# center on Bucharest and Zoom
marble.centerOn(Marble.GeoDataCoordinates(26.0783, 44.4671, 0, Marble.GeoDataCoordinates.Degree));
marble.setZoom(2200)
# add the kml file to the model
marble.model().addGeoDataFile(inputFile)
# add the widget to the KMainWindow
marble.show()
# run the app
app.exec_()
main()
Copy and paste the code above into a text editor. Then save it as loading_kml.py and run it:
python loading_kml.py some-file.kml
You should end up with a map displaying your kml file. For an example file, download and unpack Bucharest.kml (a LinearRing representing Bucharest's city boundaries), place it in the same folder as your loading_kml.py file and run python loading_kml.py bucharest.kml. The result should be similar to this:
Note
The same method works for loading other file types too, like OpenStreetMap (.osm), GPX and Shapefiles (.shp).
Note
If you provide maps in your application please check the Terms of Use of the map material. The map material that is shipped with Marble is licensed in the spirit of Free Software. This usually means at least that the authors should be credited and that the license is mentioned.
E.g. for OpenStreetMap the license is CC-BY-SA. Other map data shipped with Marble is either public domain or licensed in the spirit of the BSD license.