-
Notifications
You must be signed in to change notification settings - Fork 3
/
kml2muxp.py
114 lines (108 loc) · 5.19 KB
/
kml2muxp.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
filename = "insert filename"
muxp = [] #will at the end contain the muxp file data
area_lon = []
area_lat = []
elev_3d = None #used to store elevation of 3d coordiontes coming before coordinates
context = None #current context we are in kml file
with open(filename, encoding="utf8", errors="ignore") as f:
for line in f:
if line.find('<Document>') >= 0:
context = "Document"
line = line[line.find('<Document>')+10:]
if context == "Document":
if line.find('<description>') >= 0:
context = "muxp_header"
line = line[line.find('<description>')+13:]
if line.find('<Placemark>') >= 0:
context = "Document_Placemark"
if line.find('</Placemark>') >= 0:
context = "Document"
if line.find('<Folder>') >= 0:
context = "Folder"
continue ###### Assume no other tag than folder in one line; by this have chance to detect subfolder below
if line.find('</Folder>') >= 0:
context = "Document"
if context == "muxp_header":
if line.find('</description>') >= 0:
line = line[:line.find('</description>')]
context = "Document"
muxp.append(line.strip())
if context == "Document_Placemark":
if line.find('<name>') >= 0 and line.find('area:') >= 0:
context = "Area_Definition"
if context == "Area_Definition":
if line.find('<coordinates>') >= 0:
line = line[line.find('<coordinates>')+13:]
context = "Area_Coordinates"
if context == "Area_Coordinates":
if line.find('</coordinates>') >= 0:
line = line[:line.find('</coordinates>')]
muxp.append("area: {} {} {} {}".format(min(area_lon), max(area_lon), min(area_lat), max(area_lat)))
context = "Document"
line = line.strip()
line = line.split()
for v in line:
v = v.split(',')
area_lat.append(float(v[0]))
area_lon.append(float(v[1]))
if context == "Folder":
if line.find('<name>') >= 0 and line.find(':') >= 0:
muxp.append("") #empty line for new starting command
muxp.append(line[line.find('<name>')+6:line.find('</name>')])
if line.find('<description>') >= 0:
line = line[line.find('<description>')+13:]
if line.find('</description>') >= 0:
muxp.append(" {}".format(line[:line.find('</description>')]))
else:
context = "Command_Parameters"
if line.find('<Placemark>') >= 0:
context = "Folder_Placemark"
if line.find("</Folder>") >= 0:
context = "Document"
if line.find("<Folder>") >= 0:
context = "Subfolder"
if context == "Command_Parameters":
if line.find('</description>') >= 0:
line = line[:line.find('</description>')]
context = "Folder"
line = line.strip()
muxp.append(" {}".format(line))
if context == "Folder_Placemark":
if line.find('</Placemark>') >= 0:
context = "Folder"
if line.find('<name>') >= 0 and line.find(':') >= 0:
muxp.append(" {}".format(line[line.find('<name>')+6:line.find('</name>')]))
context = "Coords_Definition"
if context == "Coords_Definition":
if line.find('<coordinates>') >= 0:
line = line[line.find('<coordinates>')+13:]
context = "Coordinates"
if context == "Coordinates":
if line.find('</coordinates>') >= 0:
line = line[:line.find('</coordinates>')]
context = "Folder_Placemark"
line = line.strip()
line = line.split()
for v in line:
v = v.split(',')
muxp.append(" - {} {}".format(v[1], v[0]))
if context == "Subfolder":
if line.find('<name>') >= 0 and line.find('3d_coordinates:') >= 0:
muxp.append(" {}".format(line[line.find('<name>')+6:line.find('</name>')]))
context = "3d_coordinates"
continue #### assume name for folder is in separat line, no conflict with name of 3d elevation points below
if line.find("</Folder>") >= 0:
context = "Folder"
if context =="3d_coordinates": #### expected only Pins wiht elevation in this subfolder, nothing else
if line.find('<name>') >= 0:
line = line[line.find('<name>')+6:line.find('</name>')]
elev_3d = float(line)
if line.find('<coordinates>') >= 0:
line = line[line.find('<coordinates>')+13:line.find('</coordinates>')]
line = line.strip()
v = line.split(',')
muxp.append(" - {} {} {}".format(v[1], v[0], elev_3d))
if line.find('</Folder>') >= 0:
context = "Folder" #Subfoler ends here
for line in muxp:
print(line)