Skip to content

Instantly share code, notes, and snippets.

@maxcollombin
Created May 8, 2024 06:47
Show Gist options
  • Save maxcollombin/ab8f00c2c49381ff88ff6f063829444f to your computer and use it in GitHub Desktop.
Save maxcollombin/ab8f00c2c49381ff88ff6f063829444f to your computer and use it in GitHub Desktop.
Convert JSON to KML
import json
import polyline
import simplekml
# Load the JSON file
with open('input.json') as f:
data = json.load(f)
# Create a KML object
kml = simplekml.Kml()
# Loop through each feature in the JSON file
for feature in data:
# Get the decoded path from the encoded path and invert the lat/lon coordinates
decoded_path = [(lng, lat) for lat, lng in polyline.decode(feature['geometry']['encodedpath'])]
# Create a linestring object and add the decoded path to it
linestring = kml.newlinestring(name=feature['properties']['name'])
linestring.coords = decoded_path
# Set the style of the linestring
linestring.style.linestyle.width = feature['properties']['width']
linestring.style.linestyle.color = simplekml.Color.hex(feature['properties']['color'][1:])
linestring.style.linestyle.opacity = feature['properties']['opacity']
# Save the KML file
kml.save('output.kml')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment