@sswroom/sswr 1.5.1 → 1.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/parser.js CHANGED
@@ -1,15 +1,16 @@
1
- import * as geomtry from "./geometry.js";
1
+ import * as geometry from "./geometry.js";
2
2
  import * as kml from "./kml.js";
3
3
  import * as text from "./text.js";
4
+ import * as web from "./web.js";
4
5
 
5
6
  function parseKMLStyle(node)
6
7
  {
7
- var subNode;
8
- var subNode2;
9
- var subNode3;
10
- var innerStyle;
11
- var id = node.id;
12
- var style = new kml.Style(id);
8
+ let subNode;
9
+ let subNode2;
10
+ let subNode3;
11
+ let innerStyle;
12
+ let id = node.id;
13
+ let style = new kml.Style(id);
13
14
  for (subNode of node.childNodes)
14
15
  {
15
16
  switch (subNode.nodeName)
@@ -32,10 +33,10 @@ function parseKMLStyle(node)
32
33
  break;
33
34
  case "hotSpot":
34
35
  {
35
- var x;
36
- var y;
37
- var xunits;
38
- var yunits;
36
+ let x;
37
+ let y;
38
+ let xunits;
39
+ let yunits;
39
40
  for (subNode3 of subNode2.attributes)
40
41
  {
41
42
  switch (subNode3.name)
@@ -77,7 +78,7 @@ function parseKMLStyle(node)
77
78
  break;
78
79
  case "href":
79
80
  {
80
- var url = subNode3.textContent;
81
+ let url = subNode3.textContent;
81
82
  if (url.startsWith("http://") || url.startsWith("https://"))
82
83
  {
83
84
  innerStyle.setIconUrl(url);
@@ -133,6 +134,9 @@ function parseKMLStyle(node)
133
134
  {
134
135
  case "#text":
135
136
  break;
137
+ case "color":
138
+ innerStyle.setColor(subNode2.textContent);
139
+ break;
136
140
  case "scale":
137
141
  innerStyle.setScale(Number.parseFloat(subNode2.textContent));
138
142
  break;
@@ -143,6 +147,27 @@ function parseKMLStyle(node)
143
147
  }
144
148
  style.setLineStyle(innerStyle);
145
149
  break;
150
+ case "PolyStyle":
151
+ innerStyle = new kml.PolyStyle();
152
+ for (subNode2 of subNode.childNodes)
153
+ {
154
+ switch (subNode2.nodeName)
155
+ {
156
+ case "#text":
157
+ break;
158
+ case "color":
159
+ innerStyle.setColor(subNode2.textContent);
160
+ break;
161
+ case "outline":
162
+ innerStyle.setOutline(subNode2.textContent == "1");
163
+ break;
164
+ default:
165
+ console.log("Unknown node in kml PolyStyle", subNode2);
166
+ break;
167
+ }
168
+ }
169
+ style.setPolyStyle(innerStyle);
170
+ break;
146
171
  default:
147
172
  console.log("Unknown node in kml Style", subNode);
148
173
  break;
@@ -154,10 +179,10 @@ function parseKMLStyle(node)
154
179
  function parseKMLContainer(container, kmlNode, doc)
155
180
  {
156
181
  doc = doc || container;
157
- var node;
158
- var feature;
159
- var subNode;
160
- var subNode2;
182
+ let node;
183
+ let feature;
184
+ let subNode;
185
+ let subNode2;
161
186
  for (node of kmlNode.childNodes)
162
187
  {
163
188
  switch (node.nodeName)
@@ -167,9 +192,9 @@ function parseKMLContainer(container, kmlNode, doc)
167
192
  break;
168
193
  case "StyleMap":
169
194
  {
170
- var id;
171
- var normalStyle;
172
- var highlightStyle;
195
+ let id;
196
+ let normalStyle;
197
+ let highlightStyle;
173
198
  for (subNode of node.attributes)
174
199
  {
175
200
  if (subNode.name == "id")
@@ -179,7 +204,7 @@ function parseKMLContainer(container, kmlNode, doc)
179
204
  }
180
205
  for (subNode of node.childNodes)
181
206
  {
182
- var pairType;
207
+ let pairType;
183
208
  switch (subNode.nodeName)
184
209
  {
185
210
  case "#text":
@@ -238,12 +263,12 @@ function parseKMLContainer(container, kmlNode, doc)
238
263
  {
239
264
  switch (node.nodeName)
240
265
  {
241
- case "name":
242
- container.setName(node.textContent);
243
- break;
244
266
  case "description":
245
267
  container.setDescription(node.textContent);
246
268
  break;
269
+ case "name":
270
+ container.setName(node.textContent);
271
+ break;
247
272
  case "open":
248
273
  container.setOpen(node.textContent == "1");
249
274
  break;
@@ -260,6 +285,12 @@ function parseKMLContainer(container, kmlNode, doc)
260
285
  if (feature)
261
286
  container.addFeature(feature);
262
287
  break;
288
+ case "LookAt":
289
+ container.setLookAt(parseKMLLookAt(node));
290
+ break;
291
+ case "Snippet":
292
+ container.setSnippet(node.textContent);
293
+ break;
263
294
  case "#text":
264
295
  break;
265
296
  default:
@@ -272,15 +303,13 @@ function parseKMLContainer(container, kmlNode, doc)
272
303
 
273
304
  function parseKMLPlacemark(kmlNode, doc)
274
305
  {
275
- var node;
276
- var subNode;
277
- var name;
278
- var description;
279
- var snippet;
280
- var coords;
281
- var geom;
282
- var visibility;
283
- var style;
306
+ let node;
307
+ let name;
308
+ let description;
309
+ let snippet;
310
+ let geom;
311
+ let visibility;
312
+ let style;
284
313
  for (node of kmlNode.childNodes)
285
314
  {
286
315
  switch (node.nodeName)
@@ -296,6 +325,9 @@ function parseKMLPlacemark(kmlNode, doc)
296
325
  case "snippet":
297
326
  snippet = node.textContent;
298
327
  break;
328
+ case "Snippet":
329
+ snippet = node.textContent;
330
+ break;
299
331
  case "styleUrl":
300
332
  if (node.textContent.startsWith("#") && doc)
301
333
  {
@@ -314,63 +346,9 @@ function parseKMLPlacemark(kmlNode, doc)
314
346
  visibility = (node.textContent == "1");
315
347
  break;
316
348
  case "Point":
317
- for (subNode of node.childNodes)
318
- {
319
- switch (subNode.nodeName)
320
- {
321
- case "#text":
322
- break;
323
- case "extrude":
324
- break;
325
- case "altitudeMode":
326
- break;
327
- case "tessellate":
328
- break;
329
- case "coordinates":
330
- coords = subNode.textContent.split(",");
331
- geom = new geomtry.Point(4326, text.arrayToNumbers(coords));
332
- break;
333
- default:
334
- console.log("Unknown node in kml Point: "+subNode.nodeName, subNode);
335
- break;
336
- }
337
- }
338
- break;
339
349
  case "LineString":
340
- for (subNode of node.childNodes)
341
- {
342
- switch (subNode.nodeName)
343
- {
344
- case "#text":
345
- break;
346
- case "extrude":
347
- break;
348
- case "altitudeMode":
349
- break;
350
- case "tessellate":
351
- break;
352
- case "coordinates":
353
- {
354
- var pts = subNode.textContent.split(/\s+/);
355
- var ls = [];
356
- var i;
357
- var arr;
358
- for (i in pts)
359
- {
360
- arr = pts[i].split(",");
361
- if (arr.length >= 2)
362
- {
363
- ls.push(text.arrayToNumbers(arr));
364
- }
365
- }
366
- geom = new geomtry.LineString(4326, ls);
367
- }
368
- break;
369
- default:
370
- console.log("Unknown node in kml LineString: "+subNode.nodeName, subNode);
371
- break;
372
- }
373
- }
350
+ case "MultiGeometry":
351
+ geom = parseKMLGeometry(node);
374
352
  break;
375
353
  case "ExtendedData":
376
354
  break;
@@ -381,7 +359,7 @@ function parseKMLPlacemark(kmlNode, doc)
381
359
  }
382
360
  if (geom)
383
361
  {
384
- var feature = new kml.Placemark(geom);
362
+ let feature = new kml.Placemark(geom);
385
363
  if (name)
386
364
  feature.setName(name);
387
365
  if (description)
@@ -397,6 +375,269 @@ function parseKMLPlacemark(kmlNode, doc)
397
375
  return null;
398
376
  }
399
377
 
378
+ function parseKMLGeometry(kmlNode)
379
+ {
380
+ let subNode;
381
+ let subNode2;
382
+ let geomList;
383
+ let geom;
384
+ let coords;
385
+ let i;
386
+ switch (kmlNode.nodeName)
387
+ {
388
+ case "Point":
389
+ for (subNode of kmlNode.childNodes)
390
+ {
391
+ switch (subNode.nodeName)
392
+ {
393
+ case "#text":
394
+ break;
395
+ case "extrude":
396
+ break;
397
+ case "altitudeMode":
398
+ break;
399
+ case "tessellate":
400
+ break;
401
+ case "coordinates":
402
+ coords = subNode.textContent.split(",");
403
+ return new geometry.Point(4326, text.arrayToNumbers(coords));
404
+ default:
405
+ console.log("Unknown node in kml Point: "+subNode.nodeName, subNode);
406
+ break;
407
+ }
408
+ }
409
+ break;
410
+ case "LineString":
411
+ for (subNode of kmlNode.childNodes)
412
+ {
413
+ switch (subNode.nodeName)
414
+ {
415
+ case "#text":
416
+ break;
417
+ case "extrude":
418
+ break;
419
+ case "altitudeMode":
420
+ break;
421
+ case "tessellate":
422
+ break;
423
+ case "coordinates":
424
+ {
425
+ let pts = subNode.textContent.split(/\s+/);
426
+ let ls = [];
427
+ let i;
428
+ let arr;
429
+ for (i in pts)
430
+ {
431
+ arr = pts[i].split(",");
432
+ if (arr.length >= 2)
433
+ {
434
+ ls.push(text.arrayToNumbers(arr));
435
+ }
436
+ }
437
+ return new geometry.LineString(4326, ls);
438
+ }
439
+ default:
440
+ console.log("Unknown node in kml LineString: "+subNode.nodeName, subNode);
441
+ break;
442
+ }
443
+ }
444
+ break;
445
+ case "LinearRing":
446
+ for (subNode of kmlNode.childNodes)
447
+ {
448
+ switch (subNode.nodeName)
449
+ {
450
+ case "#text":
451
+ break;
452
+ case "extrude":
453
+ break;
454
+ case "altitudeMode":
455
+ break;
456
+ case "tessellate":
457
+ break;
458
+ case "coordinates":
459
+ {
460
+ let pts = subNode.textContent.split(/\s+/);
461
+ let ls = [];
462
+ let i;
463
+ let arr;
464
+ for (i in pts)
465
+ {
466
+ arr = pts[i].split(",");
467
+ if (arr.length >= 2)
468
+ {
469
+ ls.push(text.arrayToNumbers(arr));
470
+ }
471
+ }
472
+ return new geometry.LinearRing(4326, ls);
473
+ }
474
+ default:
475
+ console.log("Unknown node in kml LinearRing: "+subNode.nodeName, subNode);
476
+ break;
477
+ }
478
+ }
479
+ break;
480
+ case "Polygon":
481
+ geomList = [];
482
+ for (subNode of kmlNode.childNodes)
483
+ {
484
+ switch (subNode.nodeName)
485
+ {
486
+ case "#text":
487
+ break;
488
+ case "extrude":
489
+ break;
490
+ case "altitudeMode":
491
+ break;
492
+ case "tessellate":
493
+ break;
494
+ case "outerBoundaryIs":
495
+ for (subNode2 of subNode.childNodes)
496
+ {
497
+ switch (subNode2.nodeName)
498
+ {
499
+ case "#text":
500
+ break;
501
+ case "LinearRing":
502
+ geom = parseKMLGeometry(subNode2);
503
+ if (geom)
504
+ geomList.push(geom);
505
+ break;
506
+ default:
507
+ console.log("Unknown node in kml Polygon.outerBoundaryIs", subNode2);
508
+ break;
509
+ }
510
+ }
511
+ break;
512
+ default:
513
+ console.log("Unknown node in kml Polygon: "+subNode.nodeName, subNode);
514
+ break;
515
+ }
516
+ }
517
+ if (geomList.length > 0)
518
+ {
519
+ geom = new geometry.Polygon(4326);
520
+ for (i in geomList)
521
+ {
522
+ geom.geometries.push(geomList[i]);
523
+ }
524
+ return geom;
525
+ }
526
+ break;
527
+ case "MultiGeometry":
528
+ geomList = [];
529
+ for (subNode of kmlNode.childNodes)
530
+ {
531
+ switch (subNode.nodeName)
532
+ {
533
+ case "#text":
534
+ break;
535
+ default:
536
+ geom = parseKMLGeometry(subNode);
537
+ if (geom)
538
+ {
539
+ geomList.push(geom);
540
+ }
541
+ else
542
+ {
543
+ console.log("Unknown node in kml MultiGeometry", subNode);
544
+ }
545
+ break;
546
+ }
547
+ }
548
+ if (geomList.length > 0)
549
+ {
550
+ if (geomList[0] instanceof geometry.Polygon && (geomList.length == 1 || geomList[1] instanceof geometry.Polygon))
551
+ {
552
+ geom = new geometry.MultiPolygon(4326);
553
+ for (i in geomList)
554
+ {
555
+ geom.geometries.push(geomList[i]);
556
+ }
557
+ return geom;
558
+ }
559
+ else
560
+ {
561
+ geom = new geometry.GeometryCollection(4326);
562
+ for (i in geomList)
563
+ {
564
+ geom.geometries.push(geomList[i]);
565
+ }
566
+ return geom;
567
+ }
568
+ }
569
+ break;
570
+ default:
571
+ console.log("Unknown node in kml geometry", kmlNode);
572
+ break;
573
+ }
574
+ return null;
575
+ }
576
+
577
+ function parseKMLLookAt(kmlNode)
578
+ {
579
+ let longitude;
580
+ let latitude;
581
+ let altitude;
582
+ let range;
583
+ let heading;
584
+ let tilt;
585
+ let altitudeMode;
586
+ let node;
587
+ for (node of kmlNode.childNodes)
588
+ {
589
+ switch (node.nodeName)
590
+ {
591
+ case "#text":
592
+ break;
593
+ case "#comment":
594
+ break;
595
+ case "longitude":
596
+ longitude = Number.parseFloat(node.textContent);
597
+ break;
598
+ case "latitude":
599
+ latitude = Number.parseFloat(node.textContent);
600
+ break;
601
+ case "altitude":
602
+ altitude = Number.parseFloat(node.textContent);
603
+ break;
604
+ case "altitudeMode":
605
+ altitudeMode = node.textContent;
606
+ break;
607
+ case "range":
608
+ range = Number.parseFloat(node.textContent);
609
+ break;
610
+ case "heading":
611
+ heading = Number.parseFloat(node.textContent);
612
+ break;
613
+ case "tilt":
614
+ tilt = Number.parseFloat(node.textContent);
615
+ break;
616
+ default:
617
+ console.log("Unknown node in kml LookAt", node);
618
+ break;
619
+ }
620
+ }
621
+ if (longitude != null && latitude != null && altitude != null && range != null)
622
+ {
623
+ let lookAt = new kml.LookAt(longitude, latitude, altitude, range);
624
+ if (altitudeMode)
625
+ {
626
+ lookAt.setAltitudeMode(altitudeMode);
627
+ }
628
+ if (heading != null)
629
+ lookAt.setHeading(heading);
630
+ if (tilt != null)
631
+ lookAt.setTilt(tilt);
632
+ return lookAt;
633
+ }
634
+ else
635
+ {
636
+ console.log("Some fields in LookAt are missing", kmlNode);
637
+ return null;
638
+ }
639
+ }
640
+
400
641
  function parseKMLNode(kmlNode, doc)
401
642
  {
402
643
  switch (kmlNode.nodeName)
@@ -415,9 +656,9 @@ function parseKMLNode(kmlNode, doc)
415
656
 
416
657
  export function parseXML(txt)
417
658
  {
418
- var parser = new DOMParser();
419
- var xmlDoc = parser.parseFromString(txt, "application/xml");
420
- var xmlRoot;
659
+ let parser = new DOMParser();
660
+ let xmlDoc = parser.parseFromString(txt, "application/xml");
661
+ let xmlRoot;
421
662
  for (xmlRoot of xmlDoc.childNodes)
422
663
  {
423
664
  switch (xmlRoot.nodeName)
@@ -427,7 +668,7 @@ export function parseXML(txt)
427
668
  default:
428
669
  if (xmlRoot.nodeName == "kml")
429
670
  {
430
- var kmlNode;
671
+ let kmlNode;
431
672
  for (kmlNode of xmlRoot.childNodes)
432
673
  {
433
674
  if (kmlNode.nodeName != "#text")
@@ -447,13 +688,19 @@ export function parseXML(txt)
447
688
 
448
689
  export async function parseFile(file)
449
690
  {
450
- if (file.type == "application/vnd.google-earth.kml+xml")
691
+ let t = file.type;
692
+ if (t == "")
693
+ {
694
+ t = web.mimeFromFileName(file.name);
695
+ }
696
+
697
+ if (t == "application/vnd.google-earth.kml+xml")
451
698
  {
452
699
  return parseXML(await file.text());
453
700
  }
454
701
  else
455
702
  {
456
- console.log("Unsupported file type", file.type);
703
+ console.log("Unsupported file type", t);
457
704
  return null;
458
705
  }
459
706
  }
package/text.d.ts CHANGED
@@ -1,3 +1,17 @@
1
+ export enum Base64Charset
2
+ {
3
+ Normal,
4
+ URL
5
+ }
6
+
7
+ export enum LineBreakType
8
+ {
9
+ None,
10
+ CR,
11
+ LF,
12
+ CRLF
13
+ };
14
+
1
15
  export function zeroPad(val: string | number, ndigits: number): string;
2
16
  export function isInteger(s: string): boolean;
3
17
  export function toJSText(s: string): string;
@@ -5,4 +19,116 @@ export function toXMLText(s: string): string;
5
19
  export function toAttrText(s: string): string;
6
20
  export function toHTMLText(s: string): string;
7
21
  export function bracketToHTML(s: string): string;
8
- export function arrayToNumbers(arr: string[]): number[];
22
+ export function arrayToNumbers(arr: string[]): number[];
23
+ export function toHex8(v: number): string;
24
+ export function toHex16(v: number): string;
25
+ export function toHex32(v: number): string;
26
+ export function getEncList(): TextBinEnc[];
27
+
28
+ export class TextBinEnc
29
+ {
30
+ constructor(name: string);
31
+ getName(): string;
32
+ getClassName(): string;
33
+ encodeBin(buff: ArrayBuffer): string;
34
+ decodeBin(str: string): Uint8Array;
35
+ }
36
+
37
+ export class UTF8TextBinEnc extends TextBinEnc
38
+ {
39
+ constructor();
40
+ encodeBin(buff: ArrayBuffer): string;
41
+ decodeBin(str: string): Uint8Array;
42
+ }
43
+
44
+ export class UTF8LCaseTextBinEnc extends TextBinEnc
45
+ {
46
+ constructor();
47
+ encodeBin(buff: ArrayBuffer): string;
48
+ decodeBin(str: string): Uint8Array;
49
+ }
50
+
51
+ export class UTF8UCaseTextBinEnc extends TextBinEnc
52
+ {
53
+ constructor();
54
+ encodeBin(buff: ArrayBuffer): string;
55
+ decodeBin(str: string): Uint8Array;
56
+ }
57
+
58
+ export class HexTextBinEnc extends TextBinEnc
59
+ {
60
+ constructor();
61
+ encodeBin(buff: ArrayBuffer): string;
62
+ decodeBin(str: string): Uint8Array;
63
+ }
64
+
65
+ export class Base64Enc extends TextBinEnc
66
+ {
67
+ static getEncArr(charset: Base64Charset): string;
68
+ constructor(charset?: Base64Charset, noPadding?: boolean);
69
+ encodeBin(buff: ArrayBuffer, lbt?: LineBreakType, charsPerLine?: number): string;
70
+ decodeBin(str: string): Uint8Array;
71
+ }
72
+
73
+ export class UTF16LETextBinEnc extends TextBinEnc
74
+ {
75
+ constructor();
76
+ encodeBin(buff: ArrayBuffer): string;
77
+ decodeBin(str: string): Uint8Array;
78
+ }
79
+
80
+ export class UTF16BETextBinEnc extends TextBinEnc
81
+ {
82
+ constructor();
83
+ encodeBin(buff: ArrayBuffer): string;
84
+ decodeBin(str: string): Uint8Array;
85
+ }
86
+
87
+ export class CPPByteArrBinEnc extends TextBinEnc
88
+ {
89
+ constructor();
90
+ encodeBin(buff: ArrayBuffer): string;
91
+ decodeBin(str: string): Uint8Array;
92
+ }
93
+
94
+ export class CPPTextBinEnc extends TextBinEnc
95
+ {
96
+ constructor();
97
+ encodeBin(buff: ArrayBuffer): string;
98
+ decodeBin(str: string): Uint8Array;
99
+ }
100
+
101
+ export class QuotedPrintableEnc extends TextBinEnc
102
+ {
103
+ constructor();
104
+ encodeBin(buff: ArrayBuffer): string;
105
+ decodeBin(str: string): Uint8Array;
106
+ }
107
+
108
+ export class ASN1OIDBinEnc extends TextBinEnc
109
+ {
110
+ constructor();
111
+ encodeBin(buff: ArrayBuffer): string;
112
+ decodeBin(str: string): Uint8Array;
113
+ }
114
+
115
+ export class URIEncoding extends TextBinEnc
116
+ {
117
+ constructor();
118
+ encodeBin(buff: ArrayBuffer): string;
119
+ decodeBin(str: string): Uint8Array;
120
+ }
121
+
122
+ export class FormEncoding extends TextBinEnc
123
+ {
124
+ constructor();
125
+ encodeBin(buff: ArrayBuffer): string;
126
+ decodeBin(str: string): Uint8Array;
127
+ }
128
+
129
+ export class ASCII85Enc extends TextBinEnc
130
+ {
131
+ constructor();
132
+ encodeBin(buff: ArrayBuffer): string;
133
+ decodeBin(str: string): Uint8Array;
134
+ }