Skip to content

Instantly share code, notes, and snippets.

@dmerrick
Last active August 14, 2023 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmerrick/d4dda4705f0673b1af4a7c366c317073 to your computer and use it in GitHub Desktop.
Save dmerrick/d4dda4705f0673b1af4a7c366c317073 to your computer and use it in GitHub Desktop.
kml-filter
#!/usr/bin/env python
import sys
from lxml import etree
from pykml import parser
from rich.pretty import pprint
# small point coords
small_point_lat = "-69.80"
small_point_lng = "43.75"
input_file = sys.argv[1]
output_file = input_file.replace('.kml', '.shortened.kml')
with open(input_file) as f:
doc = parser.parse(f)
for elt in doc.iter():
if elt.tag == "{http://www.opengis.net/kml/2.2}Placemark":
placemark = elt
inner_text = ''.join(placemark.itertext())
if not (small_point_lat or small_point_lng) in inner_text:
pprint(f"removing {placemark}")
placemark.getparent().remove(placemark)
with open(output_file, 'wb') as outfile:
outfile.write(etree.tostring(doc, pretty_print=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment