Skip to content

Instantly share code, notes, and snippets.

@rgamez
Created June 18, 2015 09:40
Show Gist options
  • Save rgamez/4a147f9baff2b63882db to your computer and use it in GitHub Desktop.
Save rgamez/4a147f9baff2b63882db to your computer and use it in GitHub Desktop.
FieldtripGB Records to KMZ
from __future__ import print_function
import simplekml
import json
BASE_PATH = 'wales-observations/records'
def convertToKML(records):
kml = simplekml.Kml()
for full_record in records:
(name, record), = full_record.iteritems()
longitude = record['geometry']['coordinates'][0]
latitude = record['geometry']['coordinates'][1]
editor = record['properties']['editor']
timestamp = record['properties']['timestamp']
# Create the description
description = '<![CDATA[\n'
description += '<h3>{0}</h3><br />\n'.format(editor)
description += '<span><strong>Date</strong>: {0}</span><br />\n'.format(timestamp)
for field in record["properties"]["fields"]:
value = str(field["val"])
label = str(field["label"])
if "fieldcontain-image" in str(field["id"]):
path = kml.addfile('{0}/{1}/{2}'.format(BASE_PATH, name, value))
description += '<img src="{0}" /><br />\n'.format(path)
else:
description += "<strong>{0}</strong>:<br />\n".format(label)
description += "<span>{0}</span><br />\n".format(value)
description += ']]>\n'
# Create the point
point = kml.newpoint(name=record['name'], coords=[(longitude, latitude)])
point.description = description
# point.style.balloonstyle.text = description
return kml
if __name__ == '__main__':
with open('wales-observations/records/records_exported.json', 'r') as f:
records = json.loads(f.read())
kml = convertToKML(records['records'])
# print(kml.kml())
kml.savekmz("wales-observations.kmz", format=False)
kml.save("wales-observations.kml")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment