-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkml_to_sjx.py
193 lines (169 loc) · 6.51 KB
/
kml_to_sjx.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import sys
import getopt
import urllib2
import re
from pykml import parser
from pykml import helpers
from lxml import objectify
from lxml import etree
namespace = 'http://earth.google.com/kml/2.2'
scriptTemplate = "'// Refer to newly created entity with ''self''\n\
\n\
logger.info(\"Created entity \" + self.name);\n\
{0}\n\
'"
defaultScript = scriptTemplate.format('')
reportScriptTemplate = scriptTemplate.format('sumet.createReport({0});')
entityScriptTemplate = scriptTemplate.format('sumet.createEntity({0});')
#this is mostly just here for documentation at this point
typeMap = {'Point':'waypoint', 'LineString':'route',
'RPGReport':'threat-rpg', 'RPG':'rpg',
'SAFReport':'threat-saf', 'SAF':'saf',
'IEDReport':'ied-undetonated', 'IED':'ied'
}
def get_tree_from_url(url):
fileobject = urllib2.urlopen(url)
tree = parser.parse(fileobject)
return tree.getroot()
def create_entity_element(name, prototype, location, force="friendly", visible='true', orientation=0.0, initScript=defaultScript, points=['[]'], outFile=None):
output_lines = ['',
'- alt: ' + location[2],
' force: ' + force,
' heading: ' + '0.0',
' initScript: ' + initScript,
' lat: ' + location[1],
' lng: ' + location[0],
' name: ' + name,
' pitch: ' + '0.0',
' points: ' + '\n - '.join(points),
' prototype: ' + prototype,
' roll: ' + '0.0',
' visible: ' + visible]
if prototype == 'script':
outFile = open(name + '.yaml', 'w')
outFile.write('events:\n')
outFile.write('\n'.join(output_lines))
outFile.close()
else:
outFile.write('\n'.join(output_lines))
return
# Future version using a data object to hold the data rather than passing all the parameters in separately.
# Not yet used.
def write_element(elem):
output_lines = ['',
'- alt: ' + elem.alt,
' force: ' + elem.force,
' heading: ' + '0.0',
' initScript: ' + elem.initScript,
' lat: ' + elem.lat,
' lng: ' + elem.lng,
' name: ' + elem.name,
' pitch: ' + '0.0',
' points: ' + '\n - '.join(elem.points),
' prototype: ' + elem.prototype,
' roll: ' + '0.0',
' visible: ' + elem.visible]
if prototype == 'script':
outFile = open(name + '.yaml', 'w')
outFile.write('events:\n')
outFile.write('\n'.join(output_lines))
outFile.close()
else:
outFile.write('\n'.join(output_lines))
return
def parse_name(name):
name_parts = re.split(':?\s+', name)
ret_name = '-'.join(name_parts)
prototype = 'unknown'
script = defaultScript
while len(name_parts) < 3:
name_parts.append('n/a')
(obj_name, obj_type1, obj_type2) = name_parts[:3]
if obj_type1 == 'IED':
if obj_type2 == 'Report':
prototype = 'script'
script = reportScriptTemplate.format('"ied-undetonated", null, 0')
else:
prototype = 'script'
script = entityScriptTemplate.format('"ied", 0')
elif obj_type1 == 'RPG':
if obj_type2 == 'Report':
prototype = 'script'
script = reportScriptTemplate.format('"threat-rpg", null, 0')
else:
prototype = 'script'
script = entityScriptTemplate.format('"rpg", 0')
elif obj_type1 == 'SAF':
if obj_type2 == 'Report':
prototype = 'script'
script = reportScriptTemplate.format('"threat-saf", null, 0')
else:
prototype = 'script'
script = entityScriptTemplate.format('"saf", 0')
elif obj_type1 == 'Traffic':
if obj_type2 == 'Report':
prototype = 'script'
script = reportScriptTemplate.format('"traffic-heavy", null, 0')
else:
prototype = 'script'
script = entityScriptTemplate.format('"automobile", 0')
elif obj_type1 == 'Obstacle' or obj_type1 == 'Obstacles' or obj_type1 == 'Blockage':
if obj_type2 == 'Report':
prototype = 'script'
script = reportScriptTemplate.format('"obstacle-report", null, 0')
else:
prototype = 'script'
script = entityScriptTemplate.format('"obstacle", 0')
elif obj_type1 == 'Bridge-Out':
if obj_type2 == 'Report':
prototype = 'script'
script = reportScriptTemplate.format('"bridge-disabled", null, 0')
else:
prototype = 'script'
script = entityScriptTemplate.format('"obstacle", 0')
else:
if obj_name == 'Line' or obj_type1 == 'Route':
prototype = 'route'
else:
prototype = 'waypoint'
return ret_name, prototype, script
def convert_placemark(placemark, wp_count=0, outFile=None):
name = None
prototype = None
location = None
points = []
for elem in placemark.iterchildren():
tag = helpers.separate_namespace(elem.tag)[1]
if tag == 'name':
name, prototype, script = parse_name(elem.text)
elif tag == 'Point':
location = str.split(elem.coordinates.text, ',')
points.append('[]')
elif tag == 'LineString':
location = ['0', '0', '0']
points.append('')
for coordinate in re.split('\s+', elem.coordinates.text)[1:-1]:
wp_name = 'wp'+str(wp_count)
wp_loc = str.split(coordinate, ',')
create_entity_element(wp_name, 'waypoint', wp_loc, outFile=outFile)
wp_count += 1
points.append(wp_name)
create_entity_element(name, prototype, location, points=points, initScript=script, outFile=outFile)
return wp_count
def process_kml(kml_root, outFile=None):
placemarks = kml_root.Document.findall('{'+namespace+'}Placemark')
wp_count = 0
for placemark in placemarks:
wp_count = convert_placemark(placemark, wp_count, outFile)
return
def main(argv=None):
if argv is None:
argv = sys.argv
url = argv[1]
outFileName = argv[2]
tree = get_tree_from_url(url)
outFile = open(outFileName+'.yaml', 'w')
process_kml(tree, outFile)
outFile.close()
if __name__ == "__main__":
sys.exit(main())