forked from DLu/ros_map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_kml.py
executable file
·112 lines (90 loc) · 3.54 KB
/
generate_kml.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
#!/usr/bin/python
from xml.dom.minidom import parse, Document
from yaml import load
from os.path import split, splitext
import sys, urllib2
dom = Document()
STYLES = {'school': '44AF62', 'company': 'F08641', 'other': '5EDDFF', None: 'FFFFFF', 'research institute': '3644DB'}
REGIONS = ['america', 'asia', 'australia', 'europe', 'africa']
PATTERN = 'https://raw.githubusercontent.com/DLu/ros_map/master/data/%s.yaml'
def text_element(name, text, cdata=False):
if text is None:
text = ''
d = dom.createElement(name)
if cdata:
d.appendChild( dom.createCDATASection(text) )
else:
d.appendChild( dom.createTextNode(text.encode('utf-8')) )
return d
def data_element(name, value):
data = dom.createElement('Data')
data.setAttribute('name', name)
data.appendChild( text_element('value', value) )
return data
def create_folder(name, data):
f = dom.createElement('Folder')
f.appendChild( text_element('name', name))
for place in data:
p = dom.createElement('Placemark')
tipo = place.get('type', None)
if tipo == 'null' or tipo not in STYLES:
tipo = None
p.appendChild( text_element('styleUrl', '#%s'%str(tipo)))
p.appendChild( text_element('name', place['name']))
edata = dom.createElement('ExtendedData')
edata.appendChild( data_element('type', place['type']) )
if 'link' in place:
edata.appendChild( data_element('gx_media_links', place['link']) )
p.appendChild(edata)
if 'address' in place:
p.appendChild( text_element('address', place['address']) )
if 'description' in place:
d = dom.createElement('description')
d.appendChild( dom.createCDATASection(place['description'].encode('utf-8')) )
p.appendChild(d)
point = dom.createElement('Point')
p.appendChild(point)
if 'lat' in place:
coordinates = '%f,%f,0.0'%( place['long'], place['lat'])
point.appendChild( text_element('coordinates', coordinates))
f.appendChild(p)
return f
def create_style(name, color, url='http://maps.google.com/mapfiles/kml/pal2/icon18.png'):
f = dom.createElement('Style')
f.setAttribute('id', str(name))
p = dom.createElement('IconStyle')
f.appendChild(p)
p.appendChild( text_element('color', 'ff' + color) )
p.appendChild( text_element('scale', '1.1') )
i = dom.createElement('Icon')
p.appendChild( i )
i.appendChild( text_element('href', url) )
return f
root = dom.createElement("kml")
root.setAttribute('xmlns', 'http://www.opengis.net/kml/2.2')
dom.appendChild(root)
document = dom.createElement("Document")
root.appendChild(document)
document.appendChild( text_element('name', 'ROS Users of the World') )
document.appendChild( text_element('description', 'ROS Users of the World', True) )
files = [arg for arg in sys.argv[1:] if 'yaml' in arg]
kml = [arg for arg in sys.argv[1:] if 'kml' in arg]
if len(files)==0:
for region in REGIONS:
key = region.capitalize()
url = PATTERN % region
stream = urllib2.urlopen(url)
document.appendChild( create_folder('ROS Users (%s)'%key, load(stream)) )
else:
for fn in files:
document.appendChild( create_folder(fn, load(open(fn))))
for style, color in STYLES.iteritems():
document.appendChild( create_style(style, color) )
if len(kml)>0:
f = open(kml[0], 'w')
f.write(dom.toprettyxml())
f.close()
else:
print "Content-type: text/xml"
print
print dom.toprettyxml()