1
1
#!/usr/bin/env python3
2
2
3
+ '''
4
+ AP_FLAKE8_CLEAN
5
+ '''
6
+
7
+
3
8
import lxml .etree as etree
4
9
from io import BytesIO as SIO
5
10
from zipfile import ZipFile
6
11
import pathlib
7
12
8
13
namespaces = {'kml' : 'http://www.opengis.net/kml/2.2' }
9
14
15
+
10
16
def readkmz (filename ):
11
17
'''reads in a kmz file and returns xml nodes'''
12
- #Strip quotation marks if neccessary
18
+ # Strip quotation marks if neccessary
13
19
filename .strip ('"' )
14
- #Open the zip file (as applicable)
20
+ # Open the zip file (as applicable)
15
21
suffix = pathlib .Path (filename ).suffix
16
22
if suffix .lower () == '.kml' :
17
23
fo = open (filename , "rb" )
18
24
fstring = fo .read ()
19
25
fo .close ()
20
26
elif suffix .lower () == '.kmz' :
21
- zip = ZipFile (filename )
27
+ zip = ZipFile (filename )
22
28
fstring = None
23
29
for z in zip .filelist :
24
30
if z .filename [- 4 :] == '.kml' :
25
- fstring = zip .read (z )
31
+ fstring = zip .read (z )
26
32
break
27
33
if fstring is None :
28
34
raise Exception ("Could not find kml file in %s" % filename )
@@ -35,12 +41,14 @@ def readkmz(filename):
35
41
36
42
return tree .findall (xpath , namespaces )
37
43
44
+
38
45
def find_tag (node , tagname ):
39
46
for c in node .getchildren ():
40
47
if c .tag == "{" + namespaces ['kml' ] + "}" + tagname :
41
48
return c
42
49
return None
43
50
51
+
44
52
def find_tag_recursive (node , tagname ):
45
53
for c in node .getchildren ():
46
54
if c .tag == "{" + namespaces ['kml' ] + "}" + tagname :
@@ -54,7 +62,7 @@ def find_tag_recursive(node, tagname):
54
62
55
63
def readObject (innode ):
56
64
'''reads in a node and returns as a tuple: (type, name, points[])'''
57
- #get name
65
+ # get name
58
66
name = find_tag (innode , 'name' )
59
67
if name is None :
60
68
return None
@@ -76,6 +84,7 @@ def readObject(innode):
76
84
77
85
return ('Unknown' , None , None )
78
86
87
+
79
88
if __name__ == '__main__' :
80
89
import sys
81
90
nodes = readkmz (sys .argv [1 ])
0 commit comments