-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmlparser.py
executable file
·247 lines (225 loc) · 7.22 KB
/
kmlparser.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# eventually, this will be dynamically called from the web. for now, let's use this to parse data files/queries and
# output to files which we can upload to google maps.
import contours
#def add_kmlstyle(filepath, stylename):
def add_kmlstyle(stylename="redsies"):
strStyle=""
if stylename=='transBluePoly':
strStyle= \
'<Style id="transBluePoly">\n'\
' <LineStyle>\n'\
' <width>1</width>\n'\
' <color>5dff0000</color>\n'\
' </LineStyle>\n'\
' <PolyStyle>\n'\
' <color>7dff0000</color>\n'\
' </PolyStyle>\n'\
'</Style>\n'
elif stylename=='redsies':
print("doing redsies.")
strStyle= \
'<Style id="redsies">\n'\
' <LineStyle>\n'\
' <width>1</width>\n'\
' <color>ff0000ff</color>\n'\
' </LineStyle>\n'\
' <PolyStyle>\n'\
' <color>dd0000ff</color>\n'\
' </PolyStyle>\n'\
'</Style>\n'
elif stylename=='redbg':
print("doing redbg")
strStyle= \
'<Style id="redbg">\n'\
' <LineStyle>\n'\
' <width>1</width>\n'\
' <color>3d0000ff</color>\n'\
' </LineStyle>\n'\
' <PolyStyle>\n'\
' <color>4d0000ff</color>\n'\
' </PolyStyle>\n'\
'</Style>\n'
elif stylename=='yellowsies':
strStyle= \
'<Style id="yellowsies">\n'\
' <LineStyle>\n'\
' <width>1</width>\n'\
' <color>5d007d7d</color>\n'\
' </LineStyle>\n'\
' <PolyStyle>\n'\
' <color>7d007d7d</color>\n'\
' </PolyStyle>\n'\
'</Style>\n'
elif stylename=='orangsies':
strStyle= \
'<Style id="orangsies">\n'\
' <LineStyle>\n'\
' <width>1</width>\n'\
' <color>ff25708d</color>\n' \
' </LineStyle>\n'\
' <PolyStyle>\n'\
' <color>7d2525ee</color>\n'\
' </PolyStyle>\n'\
'</Style>\n'
#else: strStyle=add_kmlstyle()
if strStyle=="": strStyle=add_kmlstyle()
return strStyle
def add_placemarker(latitude, longitude, altitude = 0.0, description = " ", name = " ", range = 6000, tilt = 45, heading = 0, styleString='redsies'):
#"adds the point to a kml file"
#file = open(filepath,"a")
# style: normalPlaceMarker
kmlstr = \
"<Placemark>\n"\
" <description>" + description + "</description>\n"\
" <name>" + name + "</name>\n"\
" <styleUrl>#normalPlaceMarker</styleUrl>" + \
" <LookAt>\n"\
" <longitude>" + str(longitude) + "</longitude>\n"\
" <latitude>" + str(latitude) + "</latitude>\n"\
" <range>" + str(range) + "</range>\n"\
" <tilt>" + str(tilt) + "</tilt>\n"\
" <heading>" + str(heading) + "</heading>\n"\
" </LookAt>\n"\
" <visibility>0</visibility>\n"\
" <Point>\n"\
" <extrude>1</extrude>\n"\
" <altitudeMode>relativeToGround</altitudeMode>\n"\
" <coordinates>" + str(longitude) + "," + str(latitude) +", " + str(altitude) + "</coordinates>\n"\
" </Point>\n"\
" </Placemark>\n"
#file.write(kmlstr)
#file.close()
#
return kmlstr
def add_polygon2ring(outcoords, incoords, altitude = 0.0, description = " ", name = " ", range = 6000, tilt = 45, heading = 0):
#file = open(filepath,"a")
#file.write(
kmlStr = \
"<Placemark>\n"\
" <description>" + description + "</description>\n"\
" <name>" + name + "</name>\n"\
" <styleUrl>#transBluePoly</styleUrl>\n"\
" <visibility>0</visibility>\n"\
"<Polygon>\n"\
" <extrude>1</extrude>\n"\
" <altitudeMode>relativeToGround</altitudeMode>\n"\
" <outerBoundaryIs>\n"\
" <LinearRing>\n"\
" <coordinates>" + outcoords + "</coordinates>\n"\
" </LinearRing>\n"\
" </outerBoundaryIs>\n"\
" <innerBoundaryIs>\n"\
" <LinearRing>\n"\
" <coordinates>" + inoutcoords + "</coordinates>\n"\
" </LinearRing>\n"\
" </innerBoundaryIs>\n"\
" </Polygon>\n"\
" </Placemark>\n"
#file.close()
return kmlStr
# if we want to plat a placemark somewhere in the polygon?
# " <styleUrl>#normalPlaceMarker</styleUrl>" +
# " <LookAt>\n"\
# " <longitude>" + str(longitude) + "</longitude>\n"\
# " <latitude>" + str(latitude) + "</latitude>\n"\
# " <range>" + str(range) + "</range>\n"\
# " <tilt>" + str(tilt) + "</tilt>\n"\
# " <heading>" + str(heading) + "</heading>\n"\
# " </LookAt>\n"\
def getCoordsString(coords):
# should be like [[lat, lon], [lat, lon]...[]]
strCoords='\n'
for rw in coords:
strCoords+='%f, %f\n' % (rw[1], rw[0])
return strCoords
def add_simplePoly(coords, altitude = 0.0, description = " ", name = " ", range = 6000, tilt = 45, heading = 0, styleStr='redsies'):
#file = open(filepath,"a")
#file.write(
kmlStr = \
"<Placemark>\n"\
" <description>" + description + "</description>\n"\
" <name>" + name + "</name>\n"\
" <styleUrl>#" + styleStr + "</styleUrl>\n"\
" <visibility>0</visibility>\n"\
"<Polygon>\n"\
" <extrude>1</extrude>\n"\
" <altitudeMode>relativeToGround</altitudeMode>\n"\
" <outerBoundaryIs>\n"\
" <LinearRing>\n"\
" <coordinates>" + getCoordsString(coords) + "</coordinates>\n"\
" </LinearRing>\n"\
" </outerBoundaryIs>\n"\
" </Polygon>\n"\
" </Placemark>\n"
#file.close()
return kmlStr
def add_square(lat0, lon0, sidelen, altitude = 0.0, description = " ", name = " ", style="redsies"):
#def add_square(lat1, lon1, sidelen):
# provide square centers.
#"adds the point to a kml file"
lat1 = eval(lat0)-sidelen/2
lon1 = eval(lon0)-sidelen/2
#print str(5)
#
strCoords = str(lon1) + ", " + str(lat1) + "\n" +\
str(lon1+sidelen) + ", " + str(lat1) + "\n" +\
str(lon1+sidelen) + ", " + str(lat1+sidelen) + "\n" +\
str(lon1) + ", " + str(lat1+sidelen) + "\n" +\
str(lon1) + ", " + str(lat1) + "\n"
#print strCoords
#file = open(filepath,"a")
#file.write(
strKml = \
"<Placemark>\n"\
" <description>" + description + "</description>\n"\
" <name>" + name + "</name>\n"\
" <styleUrl>#" + style + "</styleUrl>\n"\
" <visibility>0</visibility>\n"\
"<Polygon>\n"\
" <extrude>1</extrude>\n"\
" <altitudeMode>relativeToGround</altitudeMode>\n"\
" <outerBoundaryIs>\n"\
" <LinearRing>\n"\
" <coordinates>" + strCoords + "</coordinates>\n"\
" </LinearRing>\n"\
" </outerBoundaryIs>\n"\
" </Polygon>\n"\
" </Placemark>\n"
#file.close()
#
return strKml
def PI2KMLpolys(data, z0=0, ix=0, iy=1, iz=2, dx=.1, dy=.1, strStyle='orangsies', altitude = 0.0, description = " ", name = " ", range = 6000, tilt = 45, heading = 0):
# take in a wad of PI (pattern informatics) data points, spit out KML polygons (huge string-o-text)
#
# get a tuple of poly-tuples.
mypolys = contours.boxyContour(data, z0, ix, iy, iz, dx, dy)
#
# each entry is a polygon (with several point-elements)
kmlStr=''
for poly in mypolys:
kmlStr = kmlStr +\
"<Placemark>\n"\
" <description>" + description + "</description>\n"\
" <name>" + name + "</name>\n"\
" <styleUrl>#" + strStyle + "</styleUrl>\n"\
" <visibility>0</visibility>\n"\
"<Polygon>\n"\
" <extrude>1</extrude>\n"\
" <altitudeMode>relativeToGround</altitudeMode>\n"\
" <outerBoundaryIs>\n"\
" <LinearRing>\n"\
" <coordinates>"
for corner in poly:
#
kmlStr = kmlStr + str(corner[0]) + ', ' + str(corner[1]) + '\n'
#
#
kmlStr = kmlStr + \
"</coordinates>\n"\
" </LinearRing>\n"\
" </outerBoundaryIs>\n"\
" </Polygon>\n"\
" </Placemark>\n"
#
#
return kmlStr