Skip to content

Instantly share code, notes, and snippets.

@shiftkey
Last active December 12, 2015 03:19
Show Gist options
  • Save shiftkey/4706249 to your computer and use it in GitHub Desktop.
Save shiftkey/4706249 to your computer and use it in GitHub Desktop.
A bit rusty but here's a script to parse a collection of KML files and pull out all the interesting data
from xml.dom.minidom import parse
def get_label(node):
name = node.getElementsByTagName('name')[0]
label = name.firstChild.nodeValue
return label
def get_coords(node):
coordinates = node.getElementsByTagName('coordinates')[0]
coords = coordinates.firstChild.nodeValue
split = coords.split(" ")
return split
def parse_file(f):
dom = parse(f)
for node in dom.getElementsByTagName('Placemark'):
label = get_label(node)
split = get_coords(node)
count = len(split)
if (count > 1):
print '{0} has {1} nodes'.format(label,count)
print '\n'
from os import listdir, path
current_dir = '.'
kml_files = [ f for f in listdir(current_dir) if path.splitext(f)[1] == '.kml' ]
for f in kml_files:
print 'Opening file {0}'.format(f)
parse_file(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment