Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created July 2, 2013 19:04
Show Gist options
  • Save TimSC/5912105 to your computer and use it in GitHub Desktop.
Save TimSC/5912105 to your computer and use it in GitHub Desktop.
Filter KML by way type by Tim Sheerman-Chase, 2013 This file may be used under CC0, http://creativecommons.org/publicdomain/zero/1.0/
#Filter KML by way type
#by Tim Sheerman-Chase, 2013
#This file may be used under CC0, http://creativecommons.org/publicdomain/zero/1.0/
import xml.etree.ElementTree as ET
import sys
def GetMetaData(place):
out = {}
for placedat in place:
if placedat.tag == "{http://www.opengis.net/kml/2.2}ExtendedData":
for schemaData in placedat:
if schemaData.tag == "{http://www.opengis.net/kml/2.2}SchemaData":
for simpledata in schemaData:
#print simpledata.attrib['name'], simpledata.text
out[simpledata.attrib['name']] = simpledata.text
return out
if __name__=="__main__":
fina = "WestSussex.kml"
if len(sys.argv) >= 2:
fina = sys.argv[1]
doc = ET.parse(fina)
root = doc.getroot()
ty = "FOOTPATH"
if len(sys.argv) >= 3:
ty = sys.argv[2]
for kml in root:
for folder in kml:
cursor = 0
while cursor < len(folder):
place = folder[cursor]
if place.tag != "{http://www.opengis.net/kml/2.2}Placemark":
cursor += 1
continue
metadata = GetMetaData(place)
print place.tag, metadata['PROWCLASS']
if metadata['PROWCLASS'] != ty:
del folder[cursor]
else:
cursor += 1
doc.write(ty+".kml", encoding="utf-8")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment