-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkml.go
288 lines (276 loc) · 6.21 KB
/
kml.go
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"encoding/xml"
"github.com/twpayne/go-kml"
"image/color"
)
var iconScale = 2.0
var pathName = "Path"
var pathLineColor = color.RGBA{}
var pathLineWidth = 2.0
/*
Returns a KML element and its Document element.
*/
func getKmlDoc(name string) (kmlEl *kml.CompoundElement, docEl *kml.CompoundElement) {
docEl = kml.Document(
kml.Name(name),
)
kmlEl = kml.KML(docEl)
kmlEl.Attr = append(kmlEl.Attr,
xml.Attr{Name: xml.Name{Local: "xmlns:gx"}, Value: "http://www.google.com/kml/ext/2.2"},
xml.Attr{Name: xml.Name{Local: "xmlns:kml"}, Value: "http://www.opengis.net/kml/2.2"},
xml.Attr{Name: xml.Name{Local: "xmlns:atom"}, Value: "http://www.w3.org/2005/Atom"},
)
return kmlEl, docEl
}
/*
Add a image placemark into the given element (usually Document or Folder).
The description image placemark has a HTML img tag in the description.
*/
func addDescriptionImagePlacemark(el *kml.CompoundElement, img *imagePlacemark) {
el.Add(
kml.Placemark(
kml.Name(img.name),
kml.Description(`
<!DOCTYPE html>
<html>
<head></head>
<body>
<img src="`+img.pathInKml+`" style="display: block; max-width: 800px; max-height: 800px; width: auto; height: auto;" />
<p>`+img.description+`</p>
</body>
</html>
`),
kml.Point(
kml.Coordinates(kml.Coordinate{Lat: img.latitude, Lon: img.longitude}),
),
kml.Style(
kml.Scale(iconScale),
kml.IconStyle(
kml.Icon(
kml.Href(img.iconPathInKml),
),
),
),
),
)
}
/*
Add a image placemark into the given element (usually Document or Folder).
The HTML image placemark has a HTML balloon style with a img tag.
*/
func addHtmlImagePlacemark(el *kml.CompoundElement, img *imagePlacemark) {
el.Add(
kml.Placemark(
kml.Name(img.name),
kml.Description(img.description),
kml.Point(
kml.Coordinates(kml.Coordinate{Lat: img.latitude, Lon: img.longitude}),
),
kml.Style(
kml.Scale(iconScale),
kml.IconStyle(
kml.Icon(
kml.Href(img.iconPathInKml),
),
),
kml.BalloonStyle(
kml.Text(`
<!DOCTYPE html>
<html>
<head>
<style>
img {display: block; max-width: 800px; max-height: 800px; width: auto; height: auto;}
</style>
</head>
<body>
<!--<p>$[name]</p>-->
<img src="`+img.pathInKml+`"/>
<p>$[description]</p>
</body>
</html>
`),
),
),
),
)
}
/*
Almost same as addHtmlImagePlacemark, but added gx:displayMode panel (so it will be displayed as a panel - in GEW).
*/
func addGxPanelHtmlImage(el *kml.CompoundElement, img *imagePlacemark) {
el.Add(
kml.Placemark(
kml.Name(img.name),
kml.Description(img.description),
kml.Point(
kml.Coordinates(kml.Coordinate{Lat: img.latitude, Lon: img.longitude}),
),
kml.Style(
kml.Scale(iconScale),
kml.IconStyle(
kml.Icon(
kml.Href(img.iconPathInKml),
),
),
kml.BalloonStyle(
kml.Text(`
<!DOCTYPE html>
<html>
<head>
<style>
img {display: block; max-width: 100%; max-height: 100%; width: auto; height: auto;}
</style>
</head>
<body>
<!--<p>$[name]</p>-->
<img src="`+img.pathInKml+`"/>
<p>$[description]</p>
</body>
</html>
`),
newSimpleEl("gx:displayMode", "panel"),
),
),
),
)
}
/*
Add a image placemark into the given element (usually Document or Folder).
The photo overlay placemark uses PhotoOverlay - the image is not in the description/HTML, but placed above the map.
*/
func addPhotoOverlayPlacemark(el *kml.CompoundElement, img *imagePlacemark) {
w, l := img.width, img.length
if w == 0 || l == 0 { // prevent divide-by-zero exception
w = 1
l = 1
}
id := img.name // todo better id
coordinate := kml.Coordinate{Lat: img.latitude, Lon: img.longitude}
photoOverlay := kml.PhotoOverlay(
kml.Name(img.name),
kml.Description(`<!DOCTYPE html><html><head></head><body>
<a href="#`+id+`">Click here to fly into photo</a><br>
</body></html>`),
kml.Open(false),
kml.Visibility(true),
kml.Icon(
kml.Href(img.pathInKml),
//kml.Href(iconSrc),
),
// todo tilt 45 deg
kml.Camera(
kml.Latitude(coordinate.Lat),
kml.Longitude(coordinate.Lon),
kml.Altitude(10),
kml.Tilt(90),
),
kml.Point(
kml.Coordinates(coordinate),
),
kml.Rotation(0),
kml.ViewVolume(
kml.Near(10),
kml.LeftFOV(float64(w/l*-20)),
kml.RightFOV(float64(w/l*20)),
kml.BottomFOV(-20),
kml.TopFOV(20),
),
kml.Shape(kml.ShapeRectangle),
// todo ImagePyramid
kml.Style(
kml.Scale(iconScale),
kml.IconStyle(
kml.Icon(
kml.Href(img.iconPathInKml),
),
),
kml.BalloonStyle(
kml.DisplayMode(kml.DisplayModeHide),
),
),
)
photoOverlay.Attr = append(photoOverlay.Attr, xml.Attr{
Name: xml.Name{
Space: "",
Local: "id",
},
Value: id,
})
el.Add(photoOverlay)
}
/*
Add a image placemark into the given element (usually Document or Folder).
This placemark uses gx:Carousel.
fixme
*/
func addGxCarouselPlacemark(el *kml.CompoundElement, img *imagePlacemark) {
el.Add(
kml.Placemark(
kml.Name(img.name),
kml.Description(`<!DOCTYPE html><html><head></head><body>
<p>`+img.description+`</p>
</body></html>`),
kml.Point(
kml.Coordinates(kml.Coordinate{Lat: img.latitude, Lon: img.longitude}),
),
kml.Style(
kml.Scale(iconScale),
kml.IconStyle(
kml.Icon(
kml.Href(img.iconPathInKml),
),
),
),
newCompoundEl("gx:Carousel").Add(
newCompoundEl("gx:Image").Add(
newSimpleEl("gx:ImageUrl", img.pathInKml),
),
),
),
)
}
/*
Returns a new KML compound element.
*/
func newCompoundEl(name string) *kml.CompoundElement {
el := new(kml.CompoundElement)
el.StartElement = xml.StartElement{
Name: xml.Name{Local: name},
Attr: nil,
}
return el
}
/*
Returns a new KML simple element <space>:<local>
*/
func newSimpleEl(name, value string) *kml.SimpleElement {
// hack - rename value element
el := kml.Value(value)
el.StartElement = xml.StartElement{
Name: xml.Name{Local: name},
Attr: nil,
}
return el
}
/*
Creates a line connecting the given coordinates.
*/
func createLine(el *kml.CompoundElement, coordinates []kml.Coordinate) {
el.Add(
kml.Placemark(
kml.Name(pathName),
kml.Style(
kml.LineStyle(
kml.Color(pathLineColor),
kml.Width(pathLineWidth),
),
),
kml.LineString(
kml.Extrude(true),
kml.Tessellate(true),
kml.Coordinates(coordinates...),
),
),
)
}