-
Notifications
You must be signed in to change notification settings - Fork 0
/
KmlHelpers.cs
958 lines (850 loc) · 37.3 KB
/
KmlHelpers.cs
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
// <copyright file="KmlHelpers.cs" company="FC">
// Copyright (c) 2008 Fraser Chapman
// </copyright>
// <author>Fraser Chapman</author>
// <email>[email protected]</email>
// <date>2009-10-04</date>
// <summary>This file is part of FC.GEPluginCtrls
// FC.GEPluginCtrls is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
// </summary>
namespace FC.GEPluginCtrls
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Xml;
using FC.GEPluginCtrls.Geo;
using Microsoft.CSharp.RuntimeBinder;
/// <summary>
/// This class provides basic Kml helper methods
/// </summary>
public static class KmlHelpers
{
/// <summary>
/// Computes the bounding box for the given object.
/// Note that this method walks the object's DOM, so may have poor performance for large objects.
/// </summary>
/// <param name="kmlFeature">{KmlFeature|KmlGeometry} object The feature or geometry whose bounds should be computed</param>
/// <returns>A bounds object based on the <paramref name="kmlFeature"/> (or an empty bounds object)</returns>
/// <remarks>
/// Based on the methods at:
/// http://code.google.com/p/earth-api-utility-library/source/browse/trunk/extensions/src/dom/utils.js
/// </remarks>
public static Bounds ComputeBounds(dynamic kmlFeature)
{
Bounds bounds = new Bounds();
Action<dynamic> eachNode = feature =>
{
ApiType type = GEHelpers.GetApiType(feature);
switch (type)
{
case ApiType.KmlGroundOverlay:
dynamic llb = feature.getLatLonBox();
if (llb != null)
{
double alt = feature.getAltitude();
bounds.Extend(new Coordinate(llb.getNorth(), llb.getEast(), alt));
bounds.Extend(new Coordinate(llb.getNorth(), llb.getWest(), alt));
bounds.Extend(new Coordinate(llb.getSouth(), llb.getEast(), alt));
bounds.Extend(new Coordinate(llb.getSouth(), llb.getWest(), alt));
}
break;
case ApiType.KmlModel:
bounds.Extend(new Coordinate(feature.getLocation()));
break;
case ApiType.KmlLinearRing:
case ApiType.KmlLineString:
dynamic coords = feature.getCoordinates();
if (coords != null)
{
int count = coords.getLength();
for (int i = 0; i < count; i++)
{
bounds.Extend(new Coordinate(coords.get(i)));
}
}
break;
case ApiType.KmlCoord:
case ApiType.KmlLocation:
case ApiType.KmlPoint:
bounds.Extend(new Coordinate(feature));
break;
case ApiType.KmlPlacemark:
dynamic geometry = feature.getGeometry();
if (GEHelpers.IsApiType(geometry, ApiType.KmlPoint))
{
bounds.Extend(new Coordinate(geometry));
}
break;
}
};
KmlHelpers.WalkKmlDom(kmlFeature, eachNode, true, true);
return bounds;
}
/// <summary>
/// Creates a kmlLookAt from a Bounds object.
/// </summary>
/// <param name="ge">the plug-in</param>
/// <param name="bounds">the bounds object to create the view of</param>
/// <param name="aspectRatio">Optional aspect ratio</param>
/// <param name="defaultRange">Optional default range</param>
/// <param name="scaleRange">Optional scale range</param>
/// <returns>A KmlLookAt based on the <paramref name="bounds"/> (or null)</returns>
public static dynamic CreateBoundsView(
dynamic ge,
Bounds bounds,
double aspectRatio = 1.0,
double defaultRange = 1000,
double scaleRange = 1.5)
{
Coordinate center = bounds.Center();
Coordinate boundsSpan = bounds.Span();
double lookAtRange = defaultRange;
dynamic lookat = null;
if (Convert.ToBoolean(boundsSpan.Latitude) || Convert.ToBoolean(boundsSpan.Longitude))
{
// Distance - using law of cosines for speed...
double distEW = new Coordinate(center.Latitude, bounds.East)
.Distance(new Coordinate(center.Latitude, bounds.West));
double distNS = new Coordinate(bounds.North, center.Longitude)
.Distance(new Coordinate(bounds.South, center.Longitude));
aspectRatio = Math.Min(Math.Max(aspectRatio, distEW / distNS), 1.0);
// Create a LookAt using the experimentally derived distance formula.
double alpha = Maths.ConvertDegreesToRadians(45.0 / (aspectRatio + 0.4) - 2.0);
double expandToDistance = Math.Max(distNS, distEW);
double beta = Math.Min(
Maths.ConvertDegreesToRadians(90),
alpha + expandToDistance / (2 * Maths.EarthMeanRadiusKilometres));
lookAtRange = scaleRange * Maths.EarthMeanRadiusKilometres *
(Math.Sin(beta) * Math.Sqrt(1 + 1 / Math.Pow(Math.Tan(alpha), 2)) - 1);
}
try
{
lookat = ge.createLookAt(string.Empty);
lookat.set(center.Latitude, center.Longitude, bounds.Top, bounds.Northeast.AltitudeMode, 0, 0, lookAtRange);
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("CreateBoundsView: " + rbex.Message, "KmlHelpers");
}
return lookat;
}
/// <summary>
/// Creates a KML placemark
/// </summary>
/// <param name="ge">The plug-in instance</param>
/// <param name="id">Optional placemark Id. Default is empty</param>
/// <param name="latitude">The placemark latitude in decimal degrees</param>
/// <param name="longitude">The placemark longitude in decimal degrees</param>
/// <param name="altitude">Optional placemark altitude in metres. Default is 0</param>
/// <param name="altitudeMode">Optional altitudeMode. Default is AltitudeMode.RelativeToGround</param>
/// <param name="name">Optional name of the placemark. Default is empty</param>
/// <param name="description">Optional placemark description text. Default is empty</param>
/// <param name="addFeature">Optionally adds the placemark directly to the plug-in. Default is true</param>
/// <returns>A placemark (or null)</returns>
public static dynamic CreatePlacemark(
dynamic ge,
string id = "",
double latitude = 0,
double longitude = 0,
double altitude = 0,
AltitudeMode altitudeMode = AltitudeMode.RelativeToGround,
string name = "",
string description = "",
bool addFeature = true)
{
if (!GEHelpers.IsGE(ge))
{
throw new ArgumentException("ge is not of the type GEPlugin");
}
dynamic placemark = null;
try
{
dynamic point = CreatePoint(
ge,
string.Empty,
Maths.FixLatitude(latitude),
Maths.FixLongitude(longitude),
altitude,
altitudeMode);
placemark = ge.createPlacemark(id);
placemark.setGeometry(point);
placemark.setName(name);
placemark.setDescription(description);
if (addFeature)
{
GEHelpers.AddFeaturesToPlugin(ge, placemark);
}
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("CreatePlacemark: " + rbex.Message, "KmlHelpers");
}
catch (COMException cex)
{
Debug.WriteLine("CreatePlacemark: " + cex.Message, "KmlHelpers");
}
return placemark;
}
/// <summary>
/// Creates a KML placemark
/// </summary>
/// <param name="ge">The plug-in instance</param>
/// <param name="coordinate">A Coordinate to use as the place marks location</param>
/// <param name="id">Optional placemark Id. Default is empty</param>
/// <param name="name">Optional name of the placemark. Default is empty</param>
/// <param name="description">Optional placemark description text. Default is empty</param>
/// <param name="addFeature">Optionally adds the placemark directly to the plug-in. Default is true</param>
/// <returns>A placemark (or null)</returns>
public static dynamic CreatePlacemark(
dynamic ge,
Coordinate coordinate,
string id = "",
string name = "",
string description = "",
bool addFeature = true)
{
return CreatePlacemark(
ge: ge,
latitude: coordinate.Latitude,
longitude: coordinate.Longitude,
altitude: coordinate.Altitude,
altitudeMode: coordinate.AltitudeMode,
name: name,
id: id,
description: description,
addFeature: addFeature);
}
/// <summary>
/// Creates a KML point
/// </summary>
/// <param name="ge">The plug-in instance</param>
/// <param name="id">Optional placemark Id. Default is empty</param>
/// <param name="latitude">The placemark latitude in decimal degrees</param>
/// <param name="longitude">The placemark longitude in decimal degrees</param>
/// <param name="altitude">Optional placemark altitude in metres. Default is 0</param>
/// <param name="altitudeMode">Optional altitudeMode. Default is AltitudeMode.RelativeToGround</param>
/// <returns>A Kml point (or null)</returns>
public static dynamic CreatePoint(
dynamic ge,
string id = "",
double latitude = 0,
double longitude = 0,
double altitude = 0,
AltitudeMode altitudeMode = AltitudeMode.RelativeToGround)
{
if (!GEHelpers.IsGE(ge))
{
throw new ArgumentException("ge is not of the type GEPlugin");
}
dynamic point = null;
try
{
point = ge.createPoint(id);
point.setLatitude(latitude);
point.setLongitude(longitude);
point.setAltitude(altitude);
point.setAltitudeMode(altitudeMode);
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("CreatePoint: " + rbex.Message, "KmlHelpers");
}
return point;
}
/// <summary>
/// Creates a KML point from a Coordinate
/// </summary>
/// <param name="ge">The plug-in instance</param>
/// <param name="coordinate">The Coordinate to base the point on</param>
/// <param name="id">Optional point Id. Default is empty</param>
/// <returns>A KML point (or null)</returns>
public static dynamic CreatePoint(
dynamic ge,
Coordinate coordinate,
string id = "")
{
return CreatePoint(
ge: ge,
id: id,
latitude: coordinate.Latitude,
longitude: coordinate.Longitude,
altitude: coordinate.Altitude,
altitudeMode: coordinate.AltitudeMode);
}
/// <summary>
/// Creates an Html String Balloon
/// </summary>
/// <param name="ge">The plug-in instance</param>
/// <param name="html">The balloon content html string</param>
/// <param name="minWidth">Optional minimum balloon width, default is 100</param>
/// <param name="minHeight">Optional minimum balloon height, default is 100</param>
/// <param name="maxWidth">Optional maximum balloon width, default is 800</param>
/// <param name="maxHeight">Optional maximum balloon height, default is 600</param>
/// <param name="setBalloon">Optionally set the balloon to be the current in the plug-in</param>
/// <param name="feature">Optionally associate the balloon with a feature</param>
/// <param name="closeButtonEnabled">Optionally display a closed button on the balloon, default is true</param>
/// <param name="backgroundColor">Optionally set the balloon backgroundColor in the #rrggbb format, default #FFFFFF</param>
/// <param name="foregroundColor">Optionally set the balloon backgroundColor in the #rrggbb format, default #000000</param>
/// <returns>A HtmlStringBalloon object (or null)</returns>
public static dynamic CreateHtmlStringBalloon(
dynamic ge,
string html = "",
int minWidth = 0,
int minHeight = 0,
int maxWidth = 800,
int maxHeight = 600,
bool setBalloon = true,
dynamic feature = null,
bool closeButtonEnabled = true,
string backgroundColor = "#FFFFFF",
string foregroundColor = "#000000")
{
dynamic balloon = null;
try
{
balloon = ge.createHtmlStringBalloon(string.Empty);
balloon.setMinHeight(minHeight);
balloon.setMaxHeight(maxHeight);
balloon.setMinWidth(minWidth);
balloon.setMaxWidth(maxWidth);
balloon.setCloseButtonEnabled(Convert.ToInt16(closeButtonEnabled));
if (!string.IsNullOrEmpty(backgroundColor))
{
balloon.setBackgroundColor(backgroundColor);
}
if (!string.IsNullOrEmpty(foregroundColor))
{
balloon.setForegroundColor(foregroundColor);
}
balloon.setContentString(html);
if (feature != null)
{
balloon.setFeature(feature);
}
if (setBalloon)
{
ge.setBalloon(balloon);
}
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("OpenFeatureBalloon: " + rbex.Message, "KmlHelpers");
}
catch (ArgumentOutOfRangeException)
{
}
return balloon;
}
/// <summary>
/// Draws a line string between the given place marks or points
/// </summary>
/// <param name="ge">The plug-in instance</param>
/// <param name="start">The first placemark or point</param>
/// <param name="end">The second placemark or point</param>
/// <param name="id">Optional ID of the line string placemark. Default is empty</param>
/// <param name="tessellate">Optionally sets tessellation for the line string. Default is true</param>
/// <param name="addFeature">Optionally adds the line string directly to the plug-in. Default is true</param>
/// <param name="width">Optional line string width, default is 1</param>
/// <param name="color">Optional KmlColor, default is white/opaque</param>
/// <returns>A line string placemark (or null)</returns>
public static dynamic CreateLineString(
dynamic ge,
dynamic start,
dynamic end,
string id = "",
bool tessellate = true,
bool addFeature = true,
int width = 1,
KmlColor color = new KmlColor())
{
if (!GEHelpers.IsGE(ge))
{
throw new ArgumentException("ge is not of the type GEPlugin");
}
if (GEHelpers.IsApiType(start, ApiType.KmlPlacemark))
{
start = start.getGeometry();
}
if (GEHelpers.IsApiType(end, ApiType.KmlPlacemark))
{
end = end.getGeometry();
}
dynamic placemark = null;
try
{
placemark = CreatePlacemark(ge);
dynamic lineString = ge.createLineString(id);
lineString.setTessellate(Convert.ToUInt16(tessellate));
lineString.getCoordinates().pushLatLngAlt(start.getLatitude(), start.getLongitude(), start.getAltitude());
lineString.getCoordinates().pushLatLngAlt(end.getLatitude(), end.getLongitude(), end.getAltitude());
if (placemark.getStyleSelector() == null)
{
placemark.setStyleSelector(ge.createStyle(string.Empty));
}
dynamic lineStyle = placemark.getStyleSelector().getLineStyle();
lineStyle.setWidth(width);
lineStyle.getColor().set(color.ToString());
placemark.setGeometry(lineString);
if (addFeature)
{
GEHelpers.AddFeaturesToPlugin(ge, placemark);
}
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("CreateLineString: " + rbex.Message, "KmlHelpers");
}
return placemark;
}
/// <summary>
/// Draws a line string between the given coordinates
/// </summary>
/// <param name="ge">The plug-in instance</param>
/// <param name="coordinates">List of points</param>
/// <param name="id">Optional ID of the line string placemark. Default is empty</param>
/// <param name="tessellate">Optionally sets tessellation for the line string. Default is true</param>
/// <param name="addFeature">Optionally adds the line string directly to the plug-in. Default is true</param>
/// <param name="width">Optional line string-width, default is 1</param>
/// <param name="color">Optional KmlColor, default is white/opaque</param>
/// <returns>A line string placemark (or null)</returns>
public static dynamic CreateLineString(
dynamic ge,
IList<Coordinate> coordinates,
string id = "",
bool tessellate = true,
bool addFeature = true,
int width = 1,
KmlColor color = new KmlColor())
{
if (!GEHelpers.IsGE(ge))
{
throw new ArgumentException("ge is not of the type GEPlugin");
}
dynamic placemark = null;
try
{
placemark = CreatePlacemark(ge, addFeature: addFeature);
dynamic lineString = ge.createLineString(id);
lineString.setTessellate(Convert.ToUInt16(tessellate));
foreach (Coordinate c in coordinates)
{
lineString.getCoordinates().pushLatLngAlt(c.Latitude, c.Longitude, c.Altitude);
}
if (placemark.getStyleSelector() == null)
{
placemark.setStyleSelector(ge.createStyle(string.Empty));
}
dynamic lineStyle = placemark.getStyleSelector().getLineStyle();
lineStyle.setWidth(width);
lineStyle.getColor().set(color.ToString());
placemark.setGeometry(lineString);
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("CreateLineString: " + rbex, "KmlHelpers");
}
return placemark;
}
/// <summary>
/// Converts a System.Drawing.Color into a KmlColor
/// </summary>
/// <param name="color">A colour to base the KmlColor on</param>
/// <param name="alpha">Optional alpha value in the range [0-1].
/// Where 0 is fully transparent and 1 is fully opaque. Default value is 1</param>
/// <returns>A Kml colour object</returns>
public static KmlColor ToKmlColor(this Color color, double alpha = 1.0)
{
return new KmlColor(color, alpha);
}
/// <summary>
/// Converts a System.Drawing.Color into a KmlColor string
/// </summary>
/// <param name="color">A colour to base the KmlColor on</param>
/// <param name="alpha">Optional alpha value in the range [0-1].
/// Where 0 is fully transparent and 1 is fully opaque. Default value is 1</param>
/// <returns>A Kml colour string in the aabbggrr format</returns>
public static string ToKmlColorString(this Color color, double alpha = 1.0)
{
return new KmlColor(color, alpha).ToString();
}
/// <summary>
/// Look at the given coordinates
/// </summary>
/// <param name="ge">the plug-in</param>
/// <param name="latitude">latitude in decimal degrees</param>
/// <param name="longitude">longitude in decimal degrees</param>
/// <param name="id">Optional LookAt Id. Default is empty</param>
/// <param name="altitude">Optional altitude. Default is 0</param>
/// <param name="altitudeMode">Optional altitudeMode. Default is AltitudeMode.RelativeToGround</param>
/// <param name="heading">Optional heading in degrees. Default is 0 (north)</param>
/// <param name="tilt">Optional tilt in degrees. Default is 0</param>
/// <param name="range">Optional range in metres. Default is 1000</param>
/// <param name="setView">Optional set the current view to the lookAt</param>
/// <returns>a look at object (or null)</returns>
public static dynamic CreateLookAt(
dynamic ge,
double latitude,
double longitude,
string id = "",
double altitude = 0,
AltitudeMode altitudeMode = AltitudeMode.RelativeToGround,
double heading = 0,
double tilt = 0,
double range = 1000,
bool setView = true)
{
if (!GEHelpers.IsGE(ge))
{
throw new ArgumentException("ge is not of the type GEPlugin");
}
dynamic lookat = null;
try
{
lookat = ge.createLookAt(id);
lookat.set(latitude, longitude, altitude, altitudeMode, heading, tilt, range);
if (setView)
{
ge.getView().setAbstractView(lookat);
}
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("CreateLookAt: " + rbex.Message, "KmlHelpers");
}
return lookat;
}
/// <summary>
/// Creates a style that can be referenced by StyleMaps and features.
/// </summary>
/// <param name="ge">The plug-in instance</param>
/// <param name="id">Optional style Id. Default is empty</param>
/// <returns>A Google API KmlStyle object</returns>
public static dynamic CreateStyle(dynamic ge, string id = "")
{
dynamic style = null;
try
{
style = ge.createStyle(id);
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("CreateStyle: " + rbex.Message, "KmlHelpers");
}
return style;
}
/// <summary>
/// Gives access to un-typed data/value pairs using the basic Data element
/// See: http://code.google.com/apis/kml/documentation/kmlreference.html#extendeddata
/// </summary>
/// <param name="kmlFeature">feature to get data from</param>
/// <returns>A list of key value pairs</returns>
public static Dictionary<string, string> GetExtendedData(dynamic kmlFeature)
{
Dictionary<string, string> keyValues =
new Dictionary<string, string>();
XmlNodeList list = GetElementsByTagName(kmlFeature, "Data");
int c = list.Count;
for (int i = 0; i < c; i++)
{
XmlAttributeCollection xmlAttributeCollection = list[i].Attributes;
if (xmlAttributeCollection != null)
{
keyValues.Add(
xmlAttributeCollection["name"].InnerText,
list[i].ChildNodes[0].InnerText);
}
}
return keyValues;
}
/// <summary>
/// Returns an System.Xml.XmlNodeList containing a list of all descendant elements
/// that match the specified <paramref name="tagName"/>.
/// </summary>
/// <param name="kmlFeature">
/// The Kml feature on which to check for nodes matching the <paramref name="tagName"/>
/// </param>
/// <param name="tagName">
/// The qualified name to match.
/// It is matched against the Name property of the matching node.
/// The special value "*" matches all tags
/// </param>
/// <returns>
/// An System.Xml.XmlNodeList containing a list of all matching nodes.
/// If no nodes match name, the returned collection will be empty.
/// </returns>
public static XmlNodeList GetElementsByTagName(dynamic kmlFeature, string tagName)
{
XmlDocument doc = new XmlDocument();
string kml;
try
{
kml = kmlFeature.getKml();
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("GetElementsByTagName: " + rbex.Message, "KmlHelplers");
return doc.ChildNodes;
}
catch (COMException)
{
return doc.ChildNodes;
}
doc.InnerXml = kml;
return doc.GetElementsByTagName(tagName);
}
/// <summary>
/// Gives access to the URL element in pre KML Release 2.1 documents
/// This allows the controls to work with legacy Kml formats
/// </summary>
/// <param name="kmlFeature">The network link to look for a URL in</param>
/// <returns>A URL from the feature or null</returns>
/// <remarks>This method is used by <see cref="KmlTreeView"/> for legacy KML support</remarks>
/// <example>string URL = KmlHelpers.GetUrl(kmlObject);</example>
public static Uri GetUrl(dynamic kmlFeature)
{
Uri uri;
string link = string.Empty;
XmlNodeList list = GetElementsByTagName(kmlFeature, "href");
if (list.Count > 0)
{
link = list[0].InnerText;
}
if (string.IsNullOrEmpty(link))
{
try
{
link = kmlFeature.getUrl();
}
catch (COMException)
{
}
if (string.IsNullOrEmpty(link))
{
try
{
link = kmlFeature.getLink().getHref();
}
catch (COMException)
{
}
}
}
Uri.TryCreate(link, UriKind.RelativeOrAbsolute, out uri);
return uri;
}
/// <summary>
/// Wrapper for getOwnerDocument().getComputedStyle().getListStyle().getListItemType()
/// See:
/// </summary>
/// <param name="kmlFeature">The feature to find the list item type of</param>
/// <returns>The corresponding ListItem type <see cref="ListItemStyle"/></returns>
/// <remarks>This method is used by <see cref="KmlTreeView"/> to build the nodes</remarks>
/// <example>Example: KmlHelpers.GetListItemType(kmlFeature)</example>
public static ListItemStyle GetListItemType(dynamic kmlFeature)
{
ListItemStyle listItem = ListItemStyle.Check;
try
{
listItem = (ListItemStyle)kmlFeature.getComputedStyle().getListStyle().getListItemType();
}
catch (COMException cex)
{
Debug.WriteLine("GetListItemType: " + cex.Message, "KmlHelpers");
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("GetListItemType: " + rbex.Message, "KmlHelpers");
}
return listItem;
}
/// <summary>
/// Gets the child nodes from a KML feature.
/// Basically a wrapper for feature.getFeatures().getChildNodes();
/// </summary>
/// <param name="feature">The feature to get the children from</param>
/// <returns>A KML object containing the child nodes, or null if the feature has no children or on any error.</returns>
public static dynamic GetChildNodes(dynamic feature)
{
try
{
if (IsKmlContainer(feature))
{
feature = feature.getFeatures();
}
return feature.getChildNodes();
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("GetChildNodes: " + rbex.Message, "KmlHelplers");
}
catch (COMException)
{
}
return null;
}
/// <summary>
/// Tests if a given kml feature has child nodes
/// </summary>
/// <param name="feature">The feature to check</param>
/// <returns>True if the feature has children</returns>
public static bool HasChildNodes(dynamic feature)
{
try
{
if (IsKmlContainer(feature))
{
feature = feature.getFeatures();
}
return Convert.ToBoolean(feature.hasChildNodes());
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("HasChildNodes: " + rbex.Message, "KmlHelplers");
}
catch (COMException)
{
}
return false;
}
/// <summary>
/// Tests if a given kml feature is a Kml container
/// </summary>
/// <param name="feature">The feature to check</param>
/// <returns>True if the feature is a Kml container</returns>
public static bool IsKmlContainer(dynamic feature)
{
ApiType type = GEHelpers.GetApiType(feature);
switch (type)
{
case ApiType.KmlDocument:
case ApiType.KmlFolder:
case ApiType.KmlLayer:
case ApiType.KmlLayerRoot:
return true;
default:
return false;
}
}
/// <summary>
/// Sets a Bounds object to the current plugin view
/// </summary>
/// <param name="ge">the plugin</param>
/// <param name="bounds">the bounds object to create the view of</param>
/// <param name="aspectRatio">Optional aspect ratio</param>
/// <param name="defaultRange">Optional default range</param>
/// <param name="scaleRange">Optional scale range</param>
public static void SetBoundsView(
dynamic ge,
Bounds bounds,
double aspectRatio = 1.0,
double defaultRange = 1000,
double scaleRange = 1.5)
{
try
{
ge.getView().setAbstractView(
KmlHelpers.CreateBoundsView(
ge,
bounds,
aspectRatio,
defaultRange,
scaleRange));
}
catch (RuntimeBinderException rbex)
{
Debug.WriteLine("SetBoundsView: " + rbex.Message, "KmlHelpers");
}
}
/// <summary>
/// Based on kmldomwalk.js
/// see: http://code.google.com/p/earth-api-samples/source/browse/trunk/lib/kmldomwalk.js
/// </summary>
/// <param name="feature">The KML object to parse</param>
/// <param name="callback">A delegate action, each node visited will be passed to this as the single parameter</param>
/// <param name="walkFeatures">Optionally walk features, default is true</param>
/// <param name="walkGeometries">Optionally walk geometries, default is false</param>
/// <remarks>This method is used by <see cref="KmlTreeView"/> to build the nodes</remarks>
public static void WalkKmlDom(
dynamic feature,
Action<dynamic> callback,
bool walkFeatures = true,
bool walkGeometries = false)
{
if (feature == null)
{
return;
}
dynamic objectContainer = null;
ApiType type = GEHelpers.GetApiType(feature);
switch (type)
{
// objects that support getFeatures
case ApiType.KmlDocument:
case ApiType.KmlFolder:
case ApiType.KmlLayer:
case ApiType.KmlLayerRoot:
{
if (walkFeatures)
{
objectContainer = feature.getFeatures(); // GESchemaObjectContainer
}
}
break;
// objects that support getGeometry
case ApiType.KmlPlacemark:
{
if (walkGeometries)
{
WalkKmlDom(feature.getGeometry(), callback, walkFeatures, true);
}
}
break;
// object that support getInnerBoundaries
case ApiType.KmlPolygon:
{
if (walkGeometries)
{
WalkKmlDom(feature.getOuterBoundary(), callback, walkFeatures, true);
}
}
break;
// objects that support getGeometries
case ApiType.KmlMultiGeometry:
{
if (walkGeometries)
{
objectContainer = feature.getGeometries(); // GESchemaObjectContainer
////WalkKmlDom(feature.getOuterBoundary(), callback, walkFeatures, walkGeometries);
}
}
break;
}
callback(feature);
if (objectContainer != null && HasChildNodes(objectContainer))
{
// 'GetChildNodes' returns null in some circumstances.
// see: Issue 96
dynamic childNodes = KmlHelpers.GetChildNodes(objectContainer);
int count = childNodes == null ? 0 : childNodes.getLength();
for (int i = 0; i < count; i++)
{
dynamic node = childNodes.item(i);
WalkKmlDom(node, callback, walkFeatures, walkGeometries);
callback(node);
}
}
}
}
}