KML.js
51.1 KB
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the Clear BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/BaseTypes/Date.js
* @requires OpenLayers/Format/XML.js
* @requires OpenLayers/Feature/Vector.js
* @requires OpenLayers/Geometry/Point.js
* @requires OpenLayers/Geometry/LineString.js
* @requires OpenLayers/Geometry/Polygon.js
* @requires OpenLayers/Geometry/Collection.js
* @requires OpenLayers/Request/XMLHttpRequest.js
* @requires OpenLayers/Console.js
* @requires OpenLayers/Lang.js
* @requires OpenLayers/Projection.js
*/
/**
* Class: OpenLayers.Format.KML
* Read/Write KML. Create a new instance with the <OpenLayers.Format.KML>
* constructor.
*
* Inherits from:
* - <OpenLayers.Format.XML>
*/
OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {
/**
* Property: namespaces
* {Object} Mapping of namespace aliases to namespace URIs.
*/
namespaces: {
kml: "http://www.opengis.net/kml/2.2",
gx: "http://www.google.com/kml/ext/2.2"
},
/**
* APIProperty: kmlns
* {String} KML Namespace to use. Defaults to 2.0 namespace.
*/
kmlns: "http://earth.google.com/kml/2.0",
/**
* APIProperty: placemarksDesc
* {String} Name of the placemarks. Default is "No description available".
*/
placemarksDesc: "No description available",
/**
* APIProperty: foldersName
* {String} Name of the folders. Default is "OpenLayers export".
* If set to null, no name element will be created.
*/
foldersName: "OpenLayers export",
/**
* APIProperty: foldersDesc
* {String} Description of the folders. Default is "Exported on [date]."
* If set to null, no description element will be created.
*/
foldersDesc: "Exported on " + new Date(),
/**
* APIProperty: extractAttributes
* {Boolean} Extract attributes from KML. Default is true.
* Extracting styleUrls requires this to be set to true
*/
extractAttributes: true,
/**
* Property: extractStyles
* {Boolean} Extract styles from KML. Default is false.
* Extracting styleUrls also requires extractAttributes to be
* set to true
*/
extractStyles: false,
/**
* APIProperty: extractTracks
* {Boolean} Extract gx:Track elements from Placemark elements. Default
* is false. If true, features will be generated for all points in
* all gx:Track elements. Features will have a when (Date) attribute
* based on when elements in the track. If tracks include angle
* elements, features will have heading, tilt, and roll attributes.
* If track point coordinates have three values, features will have
* an altitude attribute with the third coordinate value.
*/
extractTracks: false,
/**
* APIProperty: trackAttributes
* {Array} If <extractTracks> is true, points within gx:Track elements will
* be parsed as features with when, heading, tilt, and roll attributes.
* Any additional attribute names can be provided in <trackAttributes>.
*/
trackAttributes: null,
/**
* Property: internalns
* {String} KML Namespace to use -- defaults to the namespace of the
* Placemark node being parsed, but falls back to kmlns.
*/
internalns: null,
/**
* Property: features
* {Array} Array of features
*
*/
features: null,
/**
* Property: styles
* {Object} Storage of style objects
*
*/
styles: null,
/**
* Property: styleBaseUrl
* {String}
*/
styleBaseUrl: "",
/**
* Property: fetched
* {Object} Storage of KML URLs that have been fetched before
* in order to prevent reloading them.
*/
fetched: null,
/**
* APIProperty: maxDepth
* {Integer} Maximum depth for recursive loading external KML URLs
* Defaults to 0: do no external fetching
*/
maxDepth: 0,
/**
* Constructor: OpenLayers.Format.KML
* Create a new parser for KML.
*
* Parameters:
* options - {Object} An optional object whose properties will be set on
* this instance.
*/
initialize: function(options) {
// compile regular expressions once instead of every time they are used
this.regExes = {
trimSpace: (/^\s*|\s*$/g),
removeSpace: (/\s*/g),
splitSpace: (/\s+/),
trimComma: (/\s*,\s*/g),
kmlColor: (/(\w{2})(\w{2})(\w{2})(\w{2})/),
kmlIconPalette: (/root:\/\/icons\/palette-(\d+)(\.\w+)/),
straightBracket: (/\$\[(.*?)\]/g)
};
// KML coordinates are always in longlat WGS84
this.externalProjection = new OpenLayers.Projection("EPSG:4326");
OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
},
/**
* APIMethod: read
* Read data from a string, and return a list of features.
*
* Parameters:
* data - {String} or {DOMElement} data to read/parse.
*
* Returns:
* {Array(<OpenLayers.Feature.Vector>)} List of features.
*/
read: function(data) {
this.features = [];
this.styles = {};
this.fetched = {};
// Set default options
var options = {
depth: 0,
styleBaseUrl: this.styleBaseUrl
};
return this.parseData(data, options);
},
/**
* Method: parseData
* Read data from a string, and return a list of features.
*
* Parameters:
* data - {String} or {DOMElement} data to read/parse.
* options - {Object} Hash of options
*
* Returns:
* {Array(<OpenLayers.Feature.Vector>)} List of features.
*/
parseData: function(data, options) {
if(typeof data == "string") {
data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
}
// Loop throught the following node types in this order and
// process the nodes found
var types = ["Link", "NetworkLink", "Style", "StyleMap", "Placemark"];
for(var i=0, len=types.length; i<len; ++i) {
var type = types[i];
var nodes = this.getElementsByTagNameNS(data, "*", type);
// skip to next type if no nodes are found
if(nodes.length == 0) {
continue;
}
switch (type.toLowerCase()) {
// Fetch external links
case "link":
case "networklink":
this.parseLinks(nodes, options);
break;
// parse style information
case "style":
if (this.extractStyles) {
this.parseStyles(nodes, options);
}
break;
case "stylemap":
if (this.extractStyles) {
this.parseStyleMaps(nodes, options);
}
break;
// parse features
case "placemark":
this.parseFeatures(nodes, options);
break;
}
}
return this.features;
},
/**
* Method: parseLinks
* Finds URLs of linked KML documents and fetches them
*
* Parameters:
* nodes - {Array} of {DOMElement} data to read/parse.
* options - {Object} Hash of options
*
*/
parseLinks: function(nodes, options) {
// Fetch external links <NetworkLink> and <Link>
// Don't do anything if we have reached our maximum depth for recursion
if (options.depth >= this.maxDepth) {
return false;
}
// increase depth
var newOptions = OpenLayers.Util.extend({}, options);
newOptions.depth++;
for(var i=0, len=nodes.length; i<len; i++) {
var href = this.parseProperty(nodes[i], "*", "href");
if(href && !this.fetched[href]) {
this.fetched[href] = true; // prevent reloading the same urls
var data = this.fetchLink(href);
if (data) {
this.parseData(data, newOptions);
}
}
}
},
/**
* Method: fetchLink
* Fetches a URL and returns the result
*
* Parameters:
* href - {String} url to be fetched
*
*/
fetchLink: function(href) {
var request = OpenLayers.Request.GET({url: href, async: false});
if (request) {
return request.responseText;
}
},
/**
* Method: parseStyles
* Looks for <Style> nodes in the data and parses them
* Also parses <StyleMap> nodes, but only uses the 'normal' key
*
* Parameters:
* nodes - {Array} of {DOMElement} data to read/parse.
* options - {Object} Hash of options
*
*/
parseStyles: function(nodes, options) {
for(var i=0, len=nodes.length; i<len; i++) {
var style = this.parseStyle(nodes[i]);
if(style) {
var styleName = (options.styleBaseUrl || "") + "#" + style.id;
this.styles[styleName] = style;
}
}
},
/**
* Method: parseKmlColor
* Parses a kml color (in 'aabbggrr' format) and returns the corresponding
* color and opacity or null if the color is invalid.
*
* Parameters:
* kmlColor - {String} a kml formated color
*
* Returns:
* {Object}
*/
parseKmlColor: function(kmlColor) {
var color = null;
if (kmlColor) {
var matches = kmlColor.match(this.regExes.kmlColor);
if (matches) {
color = {
color: '#' + matches[4] + matches[3] + matches[2],
opacity: parseInt(matches[1], 16) / 255
};
}
}
return color;
},
/**
* Method: parseStyle
* Parses the children of a <Style> node and builds the style hash
* accordingly
*
* Parameters:
* node - {DOMElement} <Style> node
*
*/
parseStyle: function(node) {
var style = {};
var types = ["LineStyle", "PolyStyle", "IconStyle", "BalloonStyle",
"LabelStyle"];
var type, styleTypeNode, nodeList, geometry, parser;
for(var i=0, len=types.length; i<len; ++i) {
type = types[i];
styleTypeNode = this.getElementsByTagNameNS(node, "*", type)[0];
if(!styleTypeNode) {
continue;
}
// only deal with first geometry of this type
switch (type.toLowerCase()) {
case "linestyle":
var kmlColor = this.parseProperty(styleTypeNode, "*", "color");
var color = this.parseKmlColor(kmlColor);
if (color) {
style["strokeColor"] = color.color;
style["strokeOpacity"] = color.opacity;
}
var width = this.parseProperty(styleTypeNode, "*", "width");
if (width) {
style["strokeWidth"] = width;
}
break;
case "polystyle":
var kmlColor = this.parseProperty(styleTypeNode, "*", "color");
var color = this.parseKmlColor(kmlColor);
if (color) {
style["fillOpacity"] = color.opacity;
style["fillColor"] = color.color;
}
// Check if fill is disabled
var fill = this.parseProperty(styleTypeNode, "*", "fill");
if (fill == "0") {
style["fillColor"] = "none";
}
// Check if outline is disabled
var outline = this.parseProperty(styleTypeNode, "*", "outline");
if (outline == "0") {
style["strokeWidth"] = "0";
}
break;
case "iconstyle":
// set scale
var scale = parseFloat(this.parseProperty(styleTypeNode,
"*", "scale") || 1);
// set default width and height of icon
var width = 32 * scale;
var height = 32 * scale;
var iconNode = this.getElementsByTagNameNS(styleTypeNode,
"*",
"Icon")[0];
if (iconNode) {
var href = this.parseProperty(iconNode, "*", "href");
if (href) {
var w = this.parseProperty(iconNode, "*", "w");
var h = this.parseProperty(iconNode, "*", "h");
// Settings for Google specific icons that are 64x64
// We set the width and height to 64 and halve the
// scale to prevent icons from being too big
var google = "http://maps.google.com/mapfiles/kml";
if (OpenLayers.String.startsWith(
href, google) && !w && !h) {
w = 64;
h = 64;
scale = scale / 2;
}
// if only dimension is defined, make sure the
// other one has the same value
w = w || h;
h = h || w;
if (w) {
width = parseInt(w) * scale;
}
if (h) {
height = parseInt(h) * scale;
}
// support for internal icons
// (/root://icons/palette-x.png)
// x and y tell the position on the palette:
// - in pixels
// - starting from the left bottom
// We translate that to a position in the list
// and request the appropriate icon from the
// google maps website
var matches = href.match(this.regExes.kmlIconPalette);
if (matches) {
var palette = matches[1];
var file_extension = matches[2];
var x = this.parseProperty(iconNode, "*", "x");
var y = this.parseProperty(iconNode, "*", "y");
var posX = x ? x/32 : 0;
var posY = y ? (7 - y/32) : 7;
var pos = posY * 8 + posX;
href = "http://maps.google.com/mapfiles/kml/pal"
+ palette + "/icon" + pos + file_extension;
}
style["graphicOpacity"] = 1; // fully opaque
style["externalGraphic"] = href;
}
}
// hotSpots define the offset for an Icon
var hotSpotNode = this.getElementsByTagNameNS(styleTypeNode,
"*",
"hotSpot")[0];
if (hotSpotNode) {
var x = parseFloat(hotSpotNode.getAttribute("x"));
var y = parseFloat(hotSpotNode.getAttribute("y"));
var xUnits = hotSpotNode.getAttribute("xunits");
if (xUnits == "pixels") {
style["graphicXOffset"] = -x * scale;
}
else if (xUnits == "insetPixels") {
style["graphicXOffset"] = -width + (x * scale);
}
else if (xUnits == "fraction") {
style["graphicXOffset"] = -width * x;
}
var yUnits = hotSpotNode.getAttribute("yunits");
if (yUnits == "pixels") {
style["graphicYOffset"] = -height + (y * scale) + 1;
}
else if (yUnits == "insetPixels") {
style["graphicYOffset"] = -(y * scale) + 1;
}
else if (yUnits == "fraction") {
style["graphicYOffset"] = -height * (1 - y) + 1;
}
}
style["graphicWidth"] = width;
style["graphicHeight"] = height;
break;
case "balloonstyle":
var balloonStyle = OpenLayers.Util.getXmlNodeValue(
styleTypeNode);
if (balloonStyle) {
style["balloonStyle"] = balloonStyle.replace(
this.regExes.straightBracket, "${$1}");
}
break;
case "labelstyle":
var kmlColor = this.parseProperty(styleTypeNode, "*", "color");
var color = this.parseKmlColor(kmlColor);
if (color) {
style["fontColor"] = color.color;
style["fontOpacity"] = color.opacity;
}
break;
default:
}
}
// Some polygons have no line color, so we use the fillColor for that
if (!style["strokeColor"] && style["fillColor"]) {
style["strokeColor"] = style["fillColor"];
}
var id = node.getAttribute("id");
if (id && style) {
style.id = id;
}
return style;
},
/**
* Method: parseStyleMaps
* Looks for <Style> nodes in the data and parses them
* Also parses <StyleMap> nodes, but only uses the 'normal' key
*
* Parameters:
* nodes - {Array} of {DOMElement} data to read/parse.
* options - {Object} Hash of options
*
*/
parseStyleMaps: function(nodes, options) {
// Only the default or "normal" part of the StyleMap is processed now
// To do the select or "highlight" bit, we'd need to change lots more
for(var i=0, len=nodes.length; i<len; i++) {
var node = nodes[i];
var pairs = this.getElementsByTagNameNS(node, "*",
"Pair");
var id = node.getAttribute("id");
for (var j=0, jlen=pairs.length; j<jlen; j++) {
var pair = pairs[j];
// Use the shortcut in the SLD format to quickly retrieve the
// value of a node. Maybe it's good to have a method in
// Format.XML to do this
var key = this.parseProperty(pair, "*", "key");
var styleUrl = this.parseProperty(pair, "*", "styleUrl");
if (styleUrl && key == "normal") {
this.styles[(options.styleBaseUrl || "") + "#" + id] =
this.styles[(options.styleBaseUrl || "") + styleUrl];
}
if (styleUrl && key == "highlight") {
// TODO: implement the "select" part
}
}
}
},
/**
* Method: parseFeatures
* Loop through all Placemark nodes and parse them.
* Will create a list of features
*
* Parameters:
* nodes - {Array} of {DOMElement} data to read/parse.
* options - {Object} Hash of options
*
*/
parseFeatures: function(nodes, options) {
var features = [];
for(var i=0, len=nodes.length; i<len; i++) {
var featureNode = nodes[i];
var feature = this.parseFeature.apply(this,[featureNode]) ;
if(feature) {
// Create reference to styleUrl
if (this.extractStyles && feature.attributes &&
feature.attributes.styleUrl) {
feature.style = this.getStyle(feature.attributes.styleUrl, options);
}
if (this.extractStyles) {
// Make sure that <Style> nodes within a placemark are
// processed as well
var inlineStyleNode = this.getElementsByTagNameNS(featureNode,
"*",
"Style")[0];
if (inlineStyleNode) {
var inlineStyle= this.parseStyle(inlineStyleNode);
if (inlineStyle) {
feature.style = OpenLayers.Util.extend(
feature.style, inlineStyle
);
}
}
}
// check if gx:Track elements should be parsed
if (this.extractTracks) {
var tracks = this.getElementsByTagNameNS(
featureNode, this.namespaces.gx, "Track"
);
if (tracks && tracks.length > 0) {
var track = tracks[0];
var container = {
features: [],
feature: feature
};
this.readNode(track, container);
if (container.features.length > 0) {
features.push.apply(features, container.features);
}
}
} else {
// add feature to list of features
features.push(feature);
}
} else {
throw "Bad Placemark: " + i;
}
}
// add new features to existing feature list
this.features = this.features.concat(features);
},
/**
* Property: readers
* Contains public functions, grouped by namespace prefix, that will
* be applied when a namespaced node is found matching the function
* name. The function will be applied in the scope of this parser
* with two arguments: the node being read and a context object passed
* from the parent.
*/
readers: {
"kml": {
"when": function(node, container) {
container.whens.push(OpenLayers.Date.parse(
this.getChildValue(node)
));
},
"_trackPointAttribute": function(node, container) {
var name = node.nodeName.split(":").pop();
container.attributes[name].push(this.getChildValue(node));
}
},
"gx": {
"Track": function(node, container) {
var obj = {
whens: [],
points: [],
angles: []
};
if (this.trackAttributes) {
var name;
obj.attributes = {};
for (var i=0, ii=this.trackAttributes.length; i<ii; ++i) {
name = this.trackAttributes[i];
obj.attributes[name] = [];
if (!(name in this.readers.kml)) {
this.readers.kml[name] = this.readers.kml._trackPointAttribute;
}
}
}
this.readChildNodes(node, obj);
if (obj.whens.length !== obj.points.length) {
throw new Error("gx:Track with unequal number of when (" + obj.whens.length + ") and gx:coord (" + obj.points.length + ") elements.");
}
var hasAngles = obj.angles.length > 0;
if (hasAngles && obj.whens.length !== obj.angles.length) {
throw new Error("gx:Track with unequal number of when (" + obj.whens.length + ") and gx:angles (" + obj.angles.length + ") elements.");
}
var feature, point, angles;
for (var i=0, ii=obj.whens.length; i<ii; ++i) {
feature = container.feature.clone();
feature.fid = container.feature.fid || container.feature.id;
point = obj.points[i];
feature.geometry = point;
if ("z" in point) {
feature.attributes.altitude = point.z;
}
if (this.internalProjection && this.externalProjection) {
feature.geometry.transform(
this.externalProjection, this.internalProjection
);
}
if (this.trackAttributes) {
for (var j=0, jj=this.trackAttributes.length; j<jj; ++j) {
feature.attributes[name] = obj.attributes[this.trackAttributes[j]][i];
}
}
feature.attributes.when = obj.whens[i];
feature.attributes.trackId = container.feature.id;
if (hasAngles) {
angles = obj.angles[i];
feature.attributes.heading = parseFloat(angles[0]);
feature.attributes.tilt = parseFloat(angles[1]);
feature.attributes.roll = parseFloat(angles[2]);
}
container.features.push(feature);
}
},
"coord": function(node, container) {
var str = this.getChildValue(node);
var coords = str.replace(this.regExes.trimSpace, "").split(/\s+/);
var point = new OpenLayers.Geometry.Point(coords[0], coords[1]);
if (coords.length > 2) {
point.z = parseFloat(coords[2]);
}
container.points.push(point);
},
"angles": function(node, container) {
var str = this.getChildValue(node);
var parts = str.replace(this.regExes.trimSpace, "").split(/\s+/);
container.angles.push(parts);
}
}
},
/**
* Method: parseFeature
* This function is the core of the KML parsing code in OpenLayers.
* It creates the geometries that are then attached to the returned
* feature, and calls parseAttributes() to get attribute data out.
*
* Parameters:
* node - {DOMElement}
*
* Returns:
* {<OpenLayers.Feature.Vector>} A vector feature.
*/
parseFeature: function(node) {
// only accept one geometry per feature - look for highest "order"
var order = ["MultiGeometry", "Polygon", "LineString", "Point"];
var type, nodeList, geometry, parser;
for(var i=0, len=order.length; i<len; ++i) {
type = order[i];
this.internalns = node.namespaceURI ?
node.namespaceURI : this.kmlns;
nodeList = this.getElementsByTagNameNS(node,
this.internalns, type);
if(nodeList.length > 0) {
// only deal with first geometry of this type
var parser = this.parseGeometry[type.toLowerCase()];
if(parser) {
geometry = parser.apply(this, [nodeList[0]]);
if (this.internalProjection && this.externalProjection) {
geometry.transform(this.externalProjection,
this.internalProjection);
}
} else {
OpenLayers.Console.error(OpenLayers.i18n(
"unsupportedGeometryType", {'geomType':type}));
}
// stop looking for different geometry types
break;
}
}
// construct feature (optionally with attributes)
var attributes;
if(this.extractAttributes) {
attributes = this.parseAttributes(node);
}
var feature = new OpenLayers.Feature.Vector(geometry, attributes);
var fid = node.getAttribute("id") || node.getAttribute("name");
if(fid != null) {
feature.fid = fid;
}
return feature;
},
/**
* Method: getStyle
* Retrieves a style from a style hash using styleUrl as the key
* If the styleUrl doesn't exist yet, we try to fetch it
* Internet
*
* Parameters:
* styleUrl - {String} URL of style
* options - {Object} Hash of options
*
* Returns:
* {Object} - (reference to) Style hash
*/
getStyle: function(styleUrl, options) {
var styleBaseUrl = OpenLayers.Util.removeTail(styleUrl);
var newOptions = OpenLayers.Util.extend({}, options);
newOptions.depth++;
newOptions.styleBaseUrl = styleBaseUrl;
// Fetch remote Style URLs (if not fetched before)
if (!this.styles[styleUrl]
&& !OpenLayers.String.startsWith(styleUrl, "#")
&& newOptions.depth <= this.maxDepth
&& !this.fetched[styleBaseUrl] ) {
var data = this.fetchLink(styleBaseUrl);
if (data) {
this.parseData(data, newOptions);
}
}
// return requested style
var style = OpenLayers.Util.extend({}, this.styles[styleUrl]);
return style;
},
/**
* Property: parseGeometry
* Properties of this object are the functions that parse geometries based
* on their type.
*/
parseGeometry: {
/**
* Method: parseGeometry.point
* Given a KML node representing a point geometry, create an OpenLayers
* point geometry.
*
* Parameters:
* node - {DOMElement} A KML Point node.
*
* Returns:
* {<OpenLayers.Geometry.Point>} A point geometry.
*/
point: function(node) {
var nodeList = this.getElementsByTagNameNS(node, this.internalns,
"coordinates");
var coords = [];
if(nodeList.length > 0) {
var coordString = nodeList[0].firstChild.nodeValue;
coordString = coordString.replace(this.regExes.removeSpace, "");
coords = coordString.split(",");
}
var point = null;
if(coords.length > 1) {
// preserve third dimension
if(coords.length == 2) {
coords[2] = null;
}
point = new OpenLayers.Geometry.Point(coords[0], coords[1],
coords[2]);
} else {
throw "Bad coordinate string: " + coordString;
}
return point;
},
/**
* Method: parseGeometry.linestring
* Given a KML node representing a linestring geometry, create an
* OpenLayers linestring geometry.
*
* Parameters:
* node - {DOMElement} A KML LineString node.
*
* Returns:
* {<OpenLayers.Geometry.LineString>} A linestring geometry.
*/
linestring: function(node, ring) {
var nodeList = this.getElementsByTagNameNS(node, this.internalns,
"coordinates");
var line = null;
if(nodeList.length > 0) {
var coordString = this.getChildValue(nodeList[0]);
coordString = coordString.replace(this.regExes.trimSpace,
"");
coordString = coordString.replace(this.regExes.trimComma,
",");
var pointList = coordString.split(this.regExes.splitSpace);
var numPoints = pointList.length;
var points = new Array(numPoints);
var coords, numCoords;
for(var i=0; i<numPoints; ++i) {
coords = pointList[i].split(",");
numCoords = coords.length;
if(numCoords > 1) {
if(coords.length == 2) {
coords[2] = null;
}
points[i] = new OpenLayers.Geometry.Point(coords[0],
coords[1],
coords[2]);
} else {
throw "Bad LineString point coordinates: " +
pointList[i];
}
}
if(numPoints) {
if(ring) {
line = new OpenLayers.Geometry.LinearRing(points);
} else {
line = new OpenLayers.Geometry.LineString(points);
}
} else {
throw "Bad LineString coordinates: " + coordString;
}
}
return line;
},
/**
* Method: parseGeometry.polygon
* Given a KML node representing a polygon geometry, create an
* OpenLayers polygon geometry.
*
* Parameters:
* node - {DOMElement} A KML Polygon node.
*
* Returns:
* {<OpenLayers.Geometry.Polygon>} A polygon geometry.
*/
polygon: function(node) {
var nodeList = this.getElementsByTagNameNS(node, this.internalns,
"LinearRing");
var numRings = nodeList.length;
var components = new Array(numRings);
if(numRings > 0) {
// this assumes exterior ring first, inner rings after
var ring;
for(var i=0, len=nodeList.length; i<len; ++i) {
ring = this.parseGeometry.linestring.apply(this,
[nodeList[i], true]);
if(ring) {
components[i] = ring;
} else {
throw "Bad LinearRing geometry: " + i;
}
}
}
return new OpenLayers.Geometry.Polygon(components);
},
/**
* Method: parseGeometry.multigeometry
* Given a KML node representing a multigeometry, create an
* OpenLayers geometry collection.
*
* Parameters:
* node - {DOMElement} A KML MultiGeometry node.
*
* Returns:
* {<OpenLayers.Geometry.Collection>} A geometry collection.
*/
multigeometry: function(node) {
var child, parser;
var parts = [];
var children = node.childNodes;
for(var i=0, len=children.length; i<len; ++i ) {
child = children[i];
if(child.nodeType == 1) {
var type = (child.prefix) ?
child.nodeName.split(":")[1] :
child.nodeName;
var parser = this.parseGeometry[type.toLowerCase()];
if(parser) {
parts.push(parser.apply(this, [child]));
}
}
}
return new OpenLayers.Geometry.Collection(parts);
}
},
/**
* Method: parseAttributes
*
* Parameters:
* node - {DOMElement}
*
* Returns:
* {Object} An attributes object.
*/
parseAttributes: function(node) {
var attributes = {};
// Extended Data is parsed first.
var edNodes = node.getElementsByTagName("ExtendedData");
if (edNodes.length) {
attributes = this.parseExtendedData(edNodes[0]);
}
// assume attribute nodes are type 1 children with a type 3 or 4 child
var child, grandchildren, grandchild;
var children = node.childNodes;
for(var i=0, len=children.length; i<len; ++i) {
child = children[i];
if(child.nodeType == 1) {
grandchildren = child.childNodes;
if(grandchildren.length >= 1 && grandchildren.length <= 3) {
var grandchild;
switch (grandchildren.length) {
case 1:
grandchild = grandchildren[0];
break;
case 2:
var c1 = grandchildren[0];
var c2 = grandchildren[1];
grandchild = (c1.nodeType == 3 || c1.nodeType == 4) ?
c1 : c2;
break;
case 3:
default:
grandchild = grandchildren[1];
break;
}
if(grandchild.nodeType == 3 || grandchild.nodeType == 4) {
var name = (child.prefix) ?
child.nodeName.split(":")[1] :
child.nodeName;
var value = OpenLayers.Util.getXmlNodeValue(grandchild);
if (value) {
value = value.replace(this.regExes.trimSpace, "");
attributes[name] = value;
}
}
}
}
}
return attributes;
},
/**
* Method: parseExtendedData
* Parse ExtendedData from KML. Limited support for schemas/datatypes.
* See http://code.google.com/apis/kml/documentation/kmlreference.html#extendeddata
* for more information on extendeddata.
*/
parseExtendedData: function(node) {
var attributes = {};
var i, len, data, key;
var dataNodes = node.getElementsByTagName("Data");
for (i = 0, len = dataNodes.length; i < len; i++) {
data = dataNodes[i];
key = data.getAttribute("name");
var ed = {};
var valueNode = data.getElementsByTagName("value");
if (valueNode.length) {
ed['value'] = this.getChildValue(valueNode[0]);
}
var nameNode = data.getElementsByTagName("displayName");
if (nameNode.length) {
ed['displayName'] = this.getChildValue(nameNode[0]);
}
attributes[key] = ed;
}
var simpleDataNodes = node.getElementsByTagName("SimpleData");
for (i = 0, len = simpleDataNodes.length; i < len; i++) {
var ed = {};
data = simpleDataNodes[i];
key = data.getAttribute("name");
ed['value'] = this.getChildValue(data);
ed['displayName'] = key;
attributes[key] = ed;
}
return attributes;
},
/**
* Method: parseProperty
* Convenience method to find a node and return its value
*
* Parameters:
* xmlNode - {<DOMElement>}
* namespace - {String} namespace of the node to find
* tagName - {String} name of the property to parse
*
* Returns:
* {String} The value for the requested property (defaults to null)
*/
parseProperty: function(xmlNode, namespace, tagName) {
var value;
var nodeList = this.getElementsByTagNameNS(xmlNode, namespace, tagName);
try {
value = OpenLayers.Util.getXmlNodeValue(nodeList[0]);
} catch(e) {
value = null;
}
return value;
},
/**
* APIMethod: write
* Accept Feature Collection, and return a string.
*
* Parameters:
* features - {Array(<OpenLayers.Feature.Vector>)} An array of features.
*
* Returns:
* {String} A KML string.
*/
write: function(features) {
if(!(OpenLayers.Util.isArray(features))) {
features = [features];
}
var kml = this.createElementNS(this.kmlns, "kml");
var folder = this.createFolderXML();
for(var i=0, len=features.length; i<len; ++i) {
folder.appendChild(this.createPlacemarkXML(features[i]));
}
kml.appendChild(folder);
return OpenLayers.Format.XML.prototype.write.apply(this, [kml]);
},
/**
* Method: createFolderXML
* Creates and returns a KML folder node
*
* Returns:
* {DOMElement}
*/
createFolderXML: function() {
// Folder
var folder = this.createElementNS(this.kmlns, "Folder");
// Folder name
if (this.foldersName) {
var folderName = this.createElementNS(this.kmlns, "name");
var folderNameText = this.createTextNode(this.foldersName);
folderName.appendChild(folderNameText);
folder.appendChild(folderName);
}
// Folder description
if (this.foldersDesc) {
var folderDesc = this.createElementNS(this.kmlns, "description");
var folderDescText = this.createTextNode(this.foldersDesc);
folderDesc.appendChild(folderDescText);
folder.appendChild(folderDesc);
}
return folder;
},
/**
* Method: createPlacemarkXML
* Creates and returns a KML placemark node representing the given feature.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>}
*
* Returns:
* {DOMElement}
*/
createPlacemarkXML: function(feature) {
// Placemark name
var placemarkName = this.createElementNS(this.kmlns, "name");
var name = feature.style && feature.style.label ? feature.style.label :
feature.attributes.name || feature.id;
placemarkName.appendChild(this.createTextNode(name));
// Placemark description
var placemarkDesc = this.createElementNS(this.kmlns, "description");
var desc = feature.attributes.description || this.placemarksDesc;
placemarkDesc.appendChild(this.createTextNode(desc));
// Placemark
var placemarkNode = this.createElementNS(this.kmlns, "Placemark");
if(feature.fid != null) {
placemarkNode.setAttribute("id", feature.fid);
}
placemarkNode.appendChild(placemarkName);
placemarkNode.appendChild(placemarkDesc);
// Geometry node (Point, LineString, etc. nodes)
var geometryNode = this.buildGeometryNode(feature.geometry);
placemarkNode.appendChild(geometryNode);
// TBD - deal with remaining (non name/description) attributes.
return placemarkNode;
},
/**
* Method: buildGeometryNode
* Builds and returns a KML geometry node with the given geometry.
*
* Parameters:
* geometry - {<OpenLayers.Geometry>}
*
* Returns:
* {DOMElement}
*/
buildGeometryNode: function(geometry) {
var className = geometry.CLASS_NAME;
var type = className.substring(className.lastIndexOf(".") + 1);
var builder = this.buildGeometry[type.toLowerCase()];
var node = null;
if(builder) {
node = builder.apply(this, [geometry]);
}
return node;
},
/**
* Property: buildGeometry
* Object containing methods to do the actual geometry node building
* based on geometry type.
*/
buildGeometry: {
// TBD: Anybody care about namespace aliases here (these nodes have
// no prefixes)?
/**
* Method: buildGeometry.point
* Given an OpenLayers point geometry, create a KML point.
*
* Parameters:
* geometry - {<OpenLayers.Geometry.Point>} A point geometry.
*
* Returns:
* {DOMElement} A KML point node.
*/
point: function(geometry) {
var kml = this.createElementNS(this.kmlns, "Point");
kml.appendChild(this.buildCoordinatesNode(geometry));
return kml;
},
/**
* Method: buildGeometry.multipoint
* Given an OpenLayers multipoint geometry, create a KML
* GeometryCollection.
*
* Parameters:
* geometry - {<OpenLayers.Geometry.Point>} A multipoint geometry.
*
* Returns:
* {DOMElement} A KML GeometryCollection node.
*/
multipoint: function(geometry) {
return this.buildGeometry.collection.apply(this, [geometry]);
},
/**
* Method: buildGeometry.linestring
* Given an OpenLayers linestring geometry, create a KML linestring.
*
* Parameters:
* geometry - {<OpenLayers.Geometry.LineString>} A linestring geometry.
*
* Returns:
* {DOMElement} A KML linestring node.
*/
linestring: function(geometry) {
var kml = this.createElementNS(this.kmlns, "LineString");
kml.appendChild(this.buildCoordinatesNode(geometry));
return kml;
},
/**
* Method: buildGeometry.multilinestring
* Given an OpenLayers multilinestring geometry, create a KML
* GeometryCollection.
*
* Parameters:
* geometry - {<OpenLayers.Geometry.Point>} A multilinestring geometry.
*
* Returns:
* {DOMElement} A KML GeometryCollection node.
*/
multilinestring: function(geometry) {
return this.buildGeometry.collection.apply(this, [geometry]);
},
/**
* Method: buildGeometry.linearring
* Given an OpenLayers linearring geometry, create a KML linearring.
*
* Parameters:
* geometry - {<OpenLayers.Geometry.LinearRing>} A linearring geometry.
*
* Returns:
* {DOMElement} A KML linearring node.
*/
linearring: function(geometry) {
var kml = this.createElementNS(this.kmlns, "LinearRing");
kml.appendChild(this.buildCoordinatesNode(geometry));
return kml;
},
/**
* Method: buildGeometry.polygon
* Given an OpenLayers polygon geometry, create a KML polygon.
*
* Parameters:
* geometry - {<OpenLayers.Geometry.Polygon>} A polygon geometry.
*
* Returns:
* {DOMElement} A KML polygon node.
*/
polygon: function(geometry) {
var kml = this.createElementNS(this.kmlns, "Polygon");
var rings = geometry.components;
var ringMember, ringGeom, type;
for(var i=0, len=rings.length; i<len; ++i) {
type = (i==0) ? "outerBoundaryIs" : "innerBoundaryIs";
ringMember = this.createElementNS(this.kmlns, type);
ringGeom = this.buildGeometry.linearring.apply(this,
[rings[i]]);
ringMember.appendChild(ringGeom);
kml.appendChild(ringMember);
}
return kml;
},
/**
* Method: buildGeometry.multipolygon
* Given an OpenLayers multipolygon geometry, create a KML
* GeometryCollection.
*
* Parameters:
* geometry - {<OpenLayers.Geometry.Point>} A multipolygon geometry.
*
* Returns:
* {DOMElement} A KML GeometryCollection node.
*/
multipolygon: function(geometry) {
return this.buildGeometry.collection.apply(this, [geometry]);
},
/**
* Method: buildGeometry.collection
* Given an OpenLayers geometry collection, create a KML MultiGeometry.
*
* Parameters:
* geometry - {<OpenLayers.Geometry.Collection>} A geometry collection.
*
* Returns:
* {DOMElement} A KML MultiGeometry node.
*/
collection: function(geometry) {
var kml = this.createElementNS(this.kmlns, "MultiGeometry");
var child;
for(var i=0, len=geometry.components.length; i<len; ++i) {
child = this.buildGeometryNode.apply(this,
[geometry.components[i]]);
if(child) {
kml.appendChild(child);
}
}
return kml;
}
},
/**
* Method: buildCoordinatesNode
* Builds and returns the KML coordinates node with the given geometry
* <coordinates>...</coordinates>
*
* Parameters:
* geometry - {<OpenLayers.Geometry>}
*
* Return:
* {DOMElement}
*/
buildCoordinatesNode: function(geometry) {
var coordinatesNode = this.createElementNS(this.kmlns, "coordinates");
var path;
var points = geometry.components;
if(points) {
// LineString or LinearRing
var point;
var numPoints = points.length;
var parts = new Array(numPoints);
for(var i=0; i<numPoints; ++i) {
point = points[i];
parts[i] = this.buildCoordinates(point);
}
path = parts.join(" ");
} else {
// Point
path = this.buildCoordinates(geometry);
}
var txtNode = this.createTextNode(path);
coordinatesNode.appendChild(txtNode);
return coordinatesNode;
},
/**
* Method: buildCoordinates
*
* Parameters:
* point - {<OpenLayers.Geometry.Point>}
*
* Returns
* {String} a coordinate pair
*/
buildCoordinates: function(point) {
if (this.internalProjection && this.externalProjection) {
point = point.clone();
point.transform(this.internalProjection,
this.externalProjection);
}
return point.x + "," + point.y;
},
CLASS_NAME: "OpenLayers.Format.KML"
});