Created
November 21, 2022 10:05
-
-
Save deangrant/70bc9ac36f8feee2266f5c9b7b0f48d4 to your computer and use it in GitHub Desktop.
Template script to create keyhole markup language file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a template script, you can add additional simple fields to the schema, | |
# and replace the method to iterate over the original datasource. | |
import simplekml | |
# Initializes class to represent a KML file. | |
kml = simplekml.Kml() | |
# Creates a custom KML schema to be used to add custom data. | |
schema = kml.newschema( | |
name='properties' | |
) | |
# Creates new simple fields and attaches it to the schema. | |
schema.newsimplefield( | |
name='{{ name }}', | |
type='{{ type }}' | |
) | |
# !! Add additional simple fields to attach to the schema. | |
# Iterates over dateframe rows as index pairs to add point feature and | |
# extended data to the schema. | |
# !! replace iteration condition with input data source. | |
for index, row in dataframe.iterrows(): | |
# Creates a point feature using the geometry coordinates | |
point = kml.newpoint( | |
coords=[({{ longitude }}, {{ latitude }})] | |
) | |
# Attaches the extended data schema to the point feature. | |
point.extendeddata.schemadata.schemaurl = schema.name | |
# Adds custom data to the KML features. | |
point.extendeddata.schemadata.newsimpledata( | |
'{{ name }}', | |
row['{{ name }}'] | |
) | |
# !! Add additional custom data to the point feature | |
# Creates keyhole markup language (.KML) file | |
kml.save( | |
{{ filename }} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
simplekml>=1.3.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment