-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathogckml22.go
646 lines (567 loc) · 17.8 KB
/
ogckml22.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
package kml
import (
"encoding/xml"
"fmt"
"io"
"strconv"
"strings"
)
// Namespace is the default namespace.
const Namespace = "http://www.opengis.net/kml/2.2"
const defaultLinkSnippetMaxLines = 2
// A Coordinate is a single geographical coordinate.
type Coordinate struct {
Lon float64 // Longitude in degrees.
Lat float64 // Latitude in degrees.
Alt float64 // Altitude in meters.
}
// CoordinatesElement is a coordinates element composed of Coordinates.
type CoordinatesElement []Coordinate
// Coordinates returns a new CoordinatesElement.
func Coordinates(value ...Coordinate) CoordinatesElement {
return CoordinatesElement(value)
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e CoordinatesElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{Name: xml.Name{Local: "coordinates"}}
var builder strings.Builder
builder.Grow(3 * float64StringSize * len(e))
for i, c := range e {
if i != 0 {
builder.WriteByte(' ')
}
builder.WriteString(strconv.FormatFloat(c.Lon, 'f', -1, 64))
builder.WriteByte(',')
builder.WriteString(strconv.FormatFloat(c.Lat, 'f', -1, 64))
if c.Alt != 0 {
builder.WriteByte(',')
builder.WriteString(strconv.FormatFloat(c.Alt, 'f', -1, 64))
}
}
charData := xml.CharData(builder.String())
return encodeElementWithCharData(encoder, startElement, charData)
}
// CoordinatesFlatElement is a coordinates element composed of flat coordinates.
type CoordinatesFlatElement struct {
FlatCoords []float64
Offset int
End int
Stride int
Dim int
}
// CoordinatesFlat returns a new Coordinates element from flat coordinates.
func CoordinatesFlat(flatCoords []float64, offset, end, stride, dim int) *CoordinatesFlatElement {
return &CoordinatesFlatElement{
FlatCoords: flatCoords,
Offset: offset,
End: end,
Stride: stride,
Dim: dim,
}
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *CoordinatesFlatElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{Name: xml.Name{Local: "coordinates"}}
var builder strings.Builder
builder.Grow(3 * float64StringSize * (e.End - e.Offset) / e.Stride)
for i := e.Offset; i < e.End; i += e.Stride {
if i != e.Offset {
builder.WriteByte(' ')
}
builder.WriteString(strconv.FormatFloat(e.FlatCoords[i], 'f', -1, 64))
builder.WriteByte(',')
builder.WriteString(strconv.FormatFloat(e.FlatCoords[i+1], 'f', -1, 64))
if e.Dim > 2 && e.FlatCoords[i+2] != 0 {
builder.WriteByte(',')
builder.WriteString(strconv.FormatFloat(e.FlatCoords[i+2], 'f', -1, 64))
}
}
charData := xml.CharData(builder.String())
return encodeElementWithCharData(encoder, startElement, charData)
}
// CoordinatesSliceElement is a coordinates element composed of a slice of []float64s.
type CoordinatesSliceElement [][]float64
// CoordinatesSlice returns a new CoordinatesArrayElement.
func CoordinatesSlice(value ...[]float64) CoordinatesSliceElement {
return CoordinatesSliceElement(value)
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e CoordinatesSliceElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{Name: xml.Name{Local: "coordinates"}}
var builder strings.Builder
builder.Grow(3 * float64StringSize * len(e))
for i, c := range e {
if i != 0 {
builder.WriteByte(' ')
}
builder.WriteString(strconv.FormatFloat(c[0], 'f', -1, 64))
builder.WriteByte(',')
builder.WriteString(strconv.FormatFloat(c[1], 'f', -1, 64))
if len(c) > 2 && c[2] != 0 {
builder.WriteByte(',')
builder.WriteString(strconv.FormatFloat(c[2], 'f', -1, 64))
}
}
charData := xml.CharData(builder.String())
return encodeElementWithCharData(encoder, startElement, charData)
}
// A DataElement is a Data element.
type DataElement struct {
Name string
Children []Element
}
// Data returns a new DataElement.
func Data(name string, children ...Element) *DataElement {
return &DataElement{
Name: name,
Children: children,
}
}
// Add appends children to e and returns e as a ParentElement.
func (e *DataElement) Add(children ...Element) ParentElement {
return e.Append(children...)
}
// Append appends children to e and returns e.
func (e *DataElement) Append(children ...Element) *DataElement {
e.Children = append(e.Children, children...)
return e
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *DataElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{
Name: xml.Name{Local: "Data"},
Attr: []xml.Attr{
{Name: xml.Name{Local: "name"}, Value: e.Name},
},
}
return encodeElementWithChildren(encoder, startElement, e.Children)
}
// A KMLElement is a kml element.
type KMLElement struct { //nolint:revive
Child Element
}
// KML returns a new KMLElement.
func KML(child Element) *KMLElement {
return &KMLElement{
Child: child,
}
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *KMLElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{
Name: xml.Name{Space: Namespace, Local: "kml"},
}
return encodeElementWithChild(encoder, startElement, e.Child)
}
// Write writes e to w.
func (e *KMLElement) Write(w io.Writer) error {
return write(w, e)
}
// WriteIndent writes e to w with the given prefix and indent.
func (e *KMLElement) WriteIndent(w io.Writer, prefix, indent string) error {
return writeIndent(w, e, prefix, indent)
}
// A LinkSnippetElement is a LinkSnippet element.
type LinkSnippetElement struct {
MaxLines int
Value string
}
// LinkSnippet returns a new LinkSnippetElement.
func LinkSnippet(value string) *LinkSnippetElement {
return &LinkSnippetElement{
MaxLines: defaultLinkSnippetMaxLines,
Value: value,
}
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *LinkSnippetElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{Name: xml.Name{Local: "linkSnippet"}}
if e.MaxLines != defaultLinkSnippetMaxLines {
startElement.Attr = []xml.Attr{
{Name: xml.Name{Local: "maxLines"}, Value: strconv.Itoa(e.MaxLines)},
}
}
charData := xml.CharData(e.Value)
return encodeElementWithCharData(encoder, startElement, charData)
}
// WithMaxLines sets e's maxLines attribute.
func (e *LinkSnippetElement) WithMaxLines(maxLines int) *LinkSnippetElement {
e.MaxLines = maxLines
return e
}
// A ModelScaleElement is a Scale element.
type ModelScaleElement struct {
Children []Element
}
// ModelScale returns a new ModelScaleElement.
func ModelScale(children ...Element) *ModelScaleElement {
return &ModelScaleElement{
Children: children,
}
}
// Add appends children to e and returns e as a ParentElement.
func (e *ModelScaleElement) Add(children ...Element) ParentElement {
return e.Append(children...)
}
// Append appends children to e and returns e.
func (e *ModelScaleElement) Append(children ...Element) *ModelScaleElement {
e.Children = append(e.Children, children...)
return e
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *ModelScaleElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{Name: xml.Name{Local: "Scale"}}
return encodeElementWithChildren(encoder, startElement, e.Children)
}
// A SchemaElement is a Schema element.
type SchemaElement struct {
ID string
Name string
Children []Element
}
// NamedSchema returns a new SchemaElement with the given name.
func NamedSchema(id, name string, children ...Element) *SchemaElement {
return &SchemaElement{
ID: id,
Name: name,
Children: children,
}
}
// Schema returns a new SchemaElement.
func Schema(id string, children ...Element) *SchemaElement {
return &SchemaElement{
ID: id,
Children: children,
}
}
// Add appends children to e and returns e as a ParentElement.
func (e *SchemaElement) Add(children ...Element) ParentElement {
return e.Append(children...)
}
// Append appends children to e and returns e.
func (e *SchemaElement) Append(children ...Element) *SchemaElement {
e.Children = append(e.Children, children...)
return e
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *SchemaElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{
Name: xml.Name{Local: "Schema"},
Attr: []xml.Attr{
{Name: xml.Name{Local: "id"}, Value: e.ID},
},
}
if e.Name != "" {
startElement.Attr = append(startElement.Attr,
xml.Attr{Name: xml.Name{Local: "name"}, Value: e.Name},
)
}
return encodeElementWithChildren(encoder, startElement, e.Children)
}
// WithName sets e's name.
func (e *SchemaElement) WithName(name string) *SchemaElement {
e.Name = name
return e
}
// URL return e's URL.
func (e *SchemaElement) URL() string {
if e.ID == "" {
return ""
}
return "#" + e.ID
}
// A SchemaDataElement is a SchemaData element.
type SchemaDataElement struct {
SchemaURL string
Children []Element
}
// SchemaData returns a new SchemaDataElement.
func SchemaData(schemaURL string, children ...Element) *SchemaDataElement {
return &SchemaDataElement{
SchemaURL: schemaURL,
Children: children,
}
}
// Add appends children to e and returns e as a ParentElement.
func (e *SchemaDataElement) Add(children ...Element) ParentElement {
return e.Append(children...)
}
// Append appends children to e and returns e.
func (e *SchemaDataElement) Append(children ...Element) *SchemaDataElement {
e.Children = append(e.Children, children...)
return e
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *SchemaDataElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{
Name: xml.Name{Local: "SchemaData"},
Attr: []xml.Attr{
{Name: xml.Name{Local: "schemaUrl"}, Value: e.SchemaURL},
},
}
return encodeElementWithChildren(encoder, startElement, e.Children)
}
// A SimpleDataElement is a SimpleData element.
type SimpleDataElement struct {
Name string
Value string
}
// SimpleData returns a new SimpleDataElement.
func SimpleData(name, value string) *SimpleDataElement {
return &SimpleDataElement{
Name: name,
Value: value,
}
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *SimpleDataElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{
Name: xml.Name{Local: "SimpleData"},
Attr: []xml.Attr{
{Name: xml.Name{Local: "name"}, Value: e.Name},
},
}
charData := xml.CharData(e.Value)
return encodeElementWithCharData(encoder, startElement, charData)
}
// A SimpleFieldElement is a SimpleField element.
type SimpleFieldElement struct {
Name string
Type string
Children []Element
}
// SimpleField returns a new SimpleFieldElement.
func SimpleField(name, _type string, children ...Element) *SimpleFieldElement {
return &SimpleFieldElement{
Name: name,
Type: _type,
Children: children,
}
}
// Add appends children to e and returns e as a ParentElement.
func (e *SimpleFieldElement) Add(children ...Element) ParentElement {
return e.Append(children...)
}
// Append appends children to e and returns e.
func (e *SimpleFieldElement) Append(children ...Element) *SimpleFieldElement {
e.Children = append(e.Children, children...)
return e
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *SimpleFieldElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{
Name: xml.Name{Local: "SimpleField"},
Attr: []xml.Attr{
{Name: xml.Name{Local: "name"}, Value: e.Name},
{Name: xml.Name{Local: "type"}, Value: e.Type},
},
}
return encodeElementWithChildren(encoder, startElement, e.Children)
}
// A SnippetElement is a snippet element.
type SnippetElement struct {
MaxLines int
Value string
}
// Snippet returns a new SnippetElement.
func Snippet(value string) *SnippetElement {
return &SnippetElement{
MaxLines: defaultLinkSnippetMaxLines,
Value: value,
}
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *SnippetElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{Name: xml.Name{Local: "Snippet"}}
if e.MaxLines != defaultLinkSnippetMaxLines {
startElement.Attr = []xml.Attr{
{Name: xml.Name{Local: "maxLines"}, Value: strconv.Itoa(e.MaxLines)},
}
}
charData := xml.CharData(e.Value)
return encodeElementWithCharData(encoder, startElement, charData)
}
// WithMaxLines sets e's maxLines attribute.
func (e *SnippetElement) WithMaxLines(maxLines int) *SnippetElement {
e.MaxLines = maxLines
return e
}
// A StyleElement is a Style element.
type StyleElement struct {
ID string
Children []Element
}
// SharedStyle returns a new StyleElement with the given id.
func SharedStyle(id string, children ...Element) *StyleElement {
return &StyleElement{
ID: id,
Children: children,
}
}
// Style returns a new StyleElement.
func Style(children ...Element) *StyleElement {
return &StyleElement{
Children: children,
}
}
// Add appends children to e and returns e as a ParentElement.
func (e *StyleElement) Add(children ...Element) ParentElement {
return e.Append(children...)
}
// Append appends children to e and returns e.
func (e *StyleElement) Append(children ...Element) *StyleElement {
e.Children = append(e.Children, children...)
return e
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *StyleElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{Name: xml.Name{Local: "Style"}}
if e.ID != "" {
startElement.Attr = []xml.Attr{
{Name: xml.Name{Local: "id"}, Value: e.ID},
}
}
return encodeElementWithChildren(encoder, startElement, e.Children)
}
// URL return e's URL.
func (e *StyleElement) URL() string {
if e.ID == "" {
return ""
}
return "#" + e.ID
}
// WithID sets e's ID.
func (e *StyleElement) WithID(id string) *StyleElement {
e.ID = id
return e
}
// A StyleMapElement is a StyleMap element.
type StyleMapElement struct {
ID string
Children []Element
}
// SharedStyleMap returns a new StyleMapElement with the given id.
func SharedStyleMap(id string, children ...Element) *StyleMapElement {
return &StyleMapElement{
ID: id,
Children: children,
}
}
// StyleMap returns a new StyleMapElement.
func StyleMap(children ...Element) *StyleMapElement {
return &StyleMapElement{
Children: children,
}
}
// Add appends children to e and returns e as a ParentElement.
func (e *StyleMapElement) Add(children ...Element) ParentElement {
return e.Append(children...)
}
// Append appends children to e and returns e.
func (e *StyleMapElement) Append(children ...Element) *StyleMapElement {
e.Children = append(e.Children, children...)
return e
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *StyleMapElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{Name: xml.Name{Local: "StyleMap"}}
if e.ID != "" {
startElement.Attr = []xml.Attr{
{Name: xml.Name{Local: "id"}, Value: e.ID},
}
}
return encodeElementWithChildren(encoder, startElement, e.Children)
}
// URL return e's URL.
func (e *StyleMapElement) URL() string {
if e.ID == "" {
return ""
}
return "#" + e.ID
}
// WithID sets e's ID.
func (e *StyleMapElement) WithID(id string) *StyleMapElement {
e.ID = id
return e
}
// A ValueElement is a value element.
type ValueElement struct {
Value any
}
// Value returns a new ValueElement.
func Value(value any) *ValueElement {
return &ValueElement{
Value: value,
}
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *ValueElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{Name: xml.Name{Local: "value"}}
charData, err := charData(e.Value)
if err != nil {
return err
}
return encodeElementWithCharData(encoder, startElement, charData)
}
// A Vec2 is a vec2.
type Vec2 struct {
X float64
Y float64
XUnits UnitsEnum
YUnits UnitsEnum
}
// attr returns a slice of attributes populated with v's values.
func (v *Vec2) attr() []xml.Attr {
return []xml.Attr{
{Name: xml.Name{Local: "x"}, Value: strconv.FormatFloat(v.X, 'f', -1, 64)},
{Name: xml.Name{Local: "y"}, Value: strconv.FormatFloat(v.Y, 'f', -1, 64)},
{Name: xml.Name{Local: "xunits"}, Value: string(v.XUnits)},
{Name: xml.Name{Local: "yunits"}, Value: string(v.YUnits)},
}
}
func charData(value any) (xml.CharData, error) {
switch value := value.(type) {
case nil:
return nil, nil
case xml.CharData:
return value, nil
case fmt.Stringer:
return xml.CharData(value.String()), nil
case []byte:
return xml.CharData(value), nil
case bool:
return xml.CharData(strconv.FormatBool(value)), nil
case complex64:
return xml.CharData(strconv.FormatComplex(complex128(value), 'f', -1, 64)), nil
case complex128:
return xml.CharData(strconv.FormatComplex(value, 'f', -1, 128)), nil
case float32:
return xml.CharData(strconv.FormatFloat(float64(value), 'f', -1, 64)), nil
case float64:
return xml.CharData(strconv.FormatFloat(value, 'f', -1, 64)), nil
case int:
return xml.CharData(strconv.Itoa(value)), nil
case int8:
return xml.CharData(strconv.FormatInt(int64(value), 10)), nil
case int16:
return xml.CharData(strconv.FormatInt(int64(value), 10)), nil
case int32:
return xml.CharData(strconv.FormatInt(int64(value), 10)), nil
case int64:
return xml.CharData(strconv.FormatInt(value, 10)), nil
case string:
return xml.CharData(value), nil
case uint:
return xml.CharData(strconv.FormatUint(uint64(value), 10)), nil
case uint8:
return xml.CharData(strconv.FormatUint(uint64(value), 10)), nil
case uint16:
return xml.CharData(strconv.FormatUint(uint64(value), 10)), nil
case uint32:
return xml.CharData(strconv.FormatUint(uint64(value), 10)), nil
case uint64:
return xml.CharData(strconv.FormatUint(value, 10)), nil
default:
return nil, &unsupportedTypeError{value: value}
}
}