Simple-KML is a lightweight Python library for creating and manipulating KML (Keyhole Markup Language) files. KML is an XML-based format used for displaying geographic data in web-based mapping systems.
To use Simple-KML, follow the installation instructions below:
pip install simplekml
Follow the steps below to get started with Simple-KML:
- Create a new Python script or open an existing one.
- Import the SimpleKML module:
- Create a new KML object:
- Start building your KML file using the available methods and properties.
- Save the KML file:
import simplekml
kml = simplekml.Kml()
kml.save("path/to/save/file.kml")
Below is a basic example illustrating the usage of Simple-KML:
import simplekml
kml = simplekml.Kml()
placemark = kml.newpoint(name="My Point")
placemark.coords = [(1.0, 2.0)]
kml.save("path/to/save/file.kml")
Simple-KML provides a range of features to create and manipulate KML files:
Placemarks
Create point, line, and polygon placemarks in your KML file. Customize icons, color, and style properties.
import simplekml
kml = simplekml.Kml()
point = kml.newpoint(name="My Point")
point.coords = [(1.0, 2.0)]
line = kml.newlinestring(name="My Line")
line.coords = [(1.0, 2.0), (3.0, 4.0)]
polygon = kml.newpolygon(name="My Polygon")
polygon.outerboundaryis = [(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)]
kml.save("path/to/save/file.kml")
Styles
Create custom styles for placemarks, lines, and polygons. Define icon, color, and other visual properties.
import simplekml
kml = simplekml.Kml()
style = kml.newstyle()
style.iconstyle.icon.href = "http://example.com/icon.png"
point = kml.newpoint(name="My Point")
point.coords = [(1.0, 2.0)]
point.style = style
kml.save("path/to/save/file.kml")
Advanced examples of Simple-KML usage:
Creating Folders
Create folders to organize your KML content.
import simplekml
kml = simplekml.Kml()
folder = kml.newfolder(name="My Folder")
point = folder.newpoint(name="My Point")
point.coords = [(1.0, 2.0)]
kml.save("path/to/save/file.kml")
Adding Extended Data
Add extended data fields to your KML placemarks.
import simplekml
kml = simplekml.Kml()
placemark = kml.newpoint(name="My Point")
placemark.coords = [(1.0, 2.0)]
placemark.extendeddata.newdata(name="Population", value=10000)
placemark.extendeddata.newdata(name="Region", value="North America")
kml.save("path/to/save/file.kml")
If you would like to contribute to Simple-KML, please follow the guidelines below:
- Fork the Simple-KML repository on GitHub.
- Create a new branch for your contribution.
- Make the necessary changes and additions.
- Submit a pull request for review.
For additional information and resources, refer to the following links: