-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_kmz.py
35 lines (31 loc) · 1.16 KB
/
parse_kmz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from zipfile import ZipFile
import requests
base_url = 'https://www.nhc.noaa.gov/gis/best_track/'
file_name = 'al012019_best_track.kmz'
fileobject = requests.get(base_url + file_name)
open(file_name, 'wb').write(fileobject.content)
kmz = ZipFile(file_name, 'r')
kml = kmz.open(file_name.replace('_best_track.kmz', '.kml'), 'r').read()
from lxml import html
doc = html.fromstring(kml)
for pm in doc.cssselect('Folder Placemark Polygon coordinates'):
tmp = pm.cssselect('track')
name = pm.cssselect('name')[0].text_content()
if len(tmp):
# Track Placemark
tmp = tmp[0] # always one element by definition
for desc in tmp.iterdescendants():
content = desc.text_content()
if desc.tag == 'when':
print(content)
#do_timestamp_stuff(content)
elif desc.tag == 'coord':
print(content)
#do_coordinate_stuff(content)
else:
print("Skipping empty tag %s" % desc.tag)
else:
# Reference point Placemark
coord = pm.cssselect('Point coordinates')[0].text_content()
print(coord)
#do_reference_stuff(coord)