Skip to content

Instantly share code, notes, and snippets.

@Mause
Created November 4, 2012 14:53
Show Gist options
  • Save Mause/4012181 to your computer and use it in GitHub Desktop.
Save Mause/4012181 to your computer and use it in GitHub Desktop.
Requires unidecode, can be got from the python package index
#!/usr/bin/env python
# -*- coding: LATIN-1 -*-
import sys
import csv
from xml.dom import minidom
from unidecode import unidecode
from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement, Comment
def create_kml(mapping, folder_name, formatted=False):
top = Element('kml', xmlns="http://www.opengis.net/kml/2.2")
top.append(Comment('Generated for Hurricane Hackers'))
document = SubElement(top, 'Document')
folder = SubElement(document, 'Folder')
name = SubElement(folder, 'name')
name.text = folder_name
objectid = 0
for location in mapping:
placemark = SubElement(folder, 'Placemark', id=unicode(objectid))
name = SubElement(placemark, 'name')
name.text = unidecode(location['INCIDENT TITLE'])
description = SubElement(placemark, 'description')
description.text = unidecode(location['LOCATION'])
point = SubElement(placemark, 'Point')
coords = SubElement(point, 'coordinates')
coords.text = '%s,%s' % (location['LONGITUDE'], location['LATITUDE'])
objectid += 1
pure = ElementTree.tostring(top)
return (minidom.parseString(pure)).toprettyxml(indent="\t")
def main():
if len(sys.argv) > 1:
fh = open(sys.argv[1], 'r')
data = csv.DictReader(fh)
print create_kml(
data,
'Paid WiFi points',
formatted=True)
fh.close()
else:
print 'Please provide a filename'
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment