Created
September 13, 2018 20:36
-
-
Save cghiban/a9793203780ce857d3e1c265d741cbcf to your computer and use it in GitHub Desktop.
remove placemarks from a kml file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python 2 | |
# pip instal --user pykml | |
from pykml import parser | |
# kml source: https://www.census.gov/geo/maps-data/data/kml/kml_zcta.html | |
kml_file = 'cb_2017_us_zcta510_500k.kml' | |
# zip codes in one column text file | |
zip_file = 'our-zipcodes.txt' | |
zipcodes = {} | |
with open(zip_file) as zf: | |
for line in zf: | |
zipcodes[line.strip()] = 1 | |
removed = 0 | |
kept = 0 | |
with open(kml_file) as f: | |
doc = parser.parse(f) | |
root = doc.getroot() | |
folder = root.Document.Folder # parent of Placemark | |
for pm in folder.getchildren(): | |
#print pm.tag, pm.getparent().tag | |
if pm.tag == '{http://www.opengis.net/kml/2.2}Placemark': | |
keep = False | |
#zipcode = '' | |
for sd in pm.ExtendedData.SchemaData.getchildren(): | |
if 'ZCTA5CE10' in sd.values(): | |
if sd.text in zipcodes: | |
#zipcode = sd.text | |
keep = True | |
break | |
if not keep: | |
removed += 1 | |
folder.remove(pm) | |
else: | |
kept += 1 | |
doc.write('output.kml', xml_declaration=True, encoding='UTF-8') | |
print 'removed: ', removed | |
print 'kept: ', kept |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment