@tmcw/togeojson 5.2.2 → 5.3.1-0

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.
@@ -529,6 +529,14 @@ function numericProperty(node, source, target) {
529
529
  function getColor(node, output) {
530
530
  return get(node, "color", (elem) => fixColor(nodeVal(elem), output));
531
531
  }
532
+ function extractIconHref(node) {
533
+ return get(node, "Icon", (icon, properties) => {
534
+ val1(icon, "href", (href) => {
535
+ properties.icon = href;
536
+ });
537
+ return properties;
538
+ });
539
+ }
532
540
  function extractIcon(node) {
533
541
  return get(node, "IconStyle", (iconStyle) => {
534
542
  return Object.assign(getColor(iconStyle, "icon"), numericProperty(iconStyle, "scale", "icon-scale"), numericProperty(iconStyle, "heading", "icon-heading"), get(iconStyle, "hotSpot", (hotspot) => {
@@ -542,12 +550,7 @@ function extractIcon(node) {
542
550
  "icon-offset-units": [xunits, yunits],
543
551
  };
544
552
  return {};
545
- }), get(iconStyle, "Icon", (icon, properties) => {
546
- val1(icon, "href", (href) => {
547
- properties.icon = href;
548
- });
549
- return properties;
550
- }));
553
+ }), extractIconHref(iconStyle));
551
554
  });
552
555
  }
553
556
  function extractLabel(node) {
@@ -575,6 +578,58 @@ function extractStyle(node) {
575
578
  return Object.assign({}, extractPoly(node), extractLine(node), extractLabel(node), extractIcon(node));
576
579
  }
577
580
 
581
+ function extractExtendedData(node) {
582
+ return get(node, "ExtendedData", (extendedData, properties) => {
583
+ for (const data of $(extendedData, "Data")) {
584
+ properties[data.getAttribute("name") || ""] = nodeVal(get1(data, "value"));
585
+ }
586
+ for (const simpleData of $(extendedData, "SimpleData")) {
587
+ properties[simpleData.getAttribute("name") || ""] = nodeVal(simpleData);
588
+ }
589
+ return properties;
590
+ });
591
+ }
592
+ function getMaybeHTMLDescription(node) {
593
+ const descriptionNode = get1(node, "description");
594
+ for (const c of Array.from(descriptionNode?.childNodes || [])) {
595
+ if (c.nodeType === 4) {
596
+ return {
597
+ description: {
598
+ "@type": "html",
599
+ value: nodeVal(c),
600
+ },
601
+ };
602
+ }
603
+ }
604
+ return {};
605
+ }
606
+ function extractTimeSpan(node) {
607
+ return get(node, "TimeSpan", (timeSpan) => {
608
+ return {
609
+ timespan: {
610
+ begin: nodeVal(get1(timeSpan, "begin")),
611
+ end: nodeVal(get1(timeSpan, "end")),
612
+ },
613
+ };
614
+ });
615
+ }
616
+ function extractTimeStamp(node) {
617
+ return get(node, "TimeStamp", (timeStamp) => {
618
+ return { timestamp: nodeVal(get1(timeStamp, "when")) };
619
+ });
620
+ }
621
+ function extractCascadedStyle(node, styleMap) {
622
+ return val1(node, "styleUrl", (styleUrl) => {
623
+ styleUrl = normalizeId(styleUrl);
624
+ if (styleMap[styleUrl]) {
625
+ return Object.assign({ styleUrl }, styleMap[styleUrl]);
626
+ }
627
+ // For backward-compatibility. Should we still include
628
+ // styleUrl even if it's not resolved?
629
+ return { styleUrl };
630
+ });
631
+ }
632
+
578
633
  const removeSpace = /\s*/g;
579
634
  const trimSpace = /^\s*|\s*$/g;
580
635
  const splitSpace = /\s+/;
@@ -642,30 +697,26 @@ function fixRing(ring) {
642
697
  }
643
698
  return ring;
644
699
  }
645
- const GEO_TYPES = [
646
- "Polygon",
647
- "LineString",
648
- "Point",
649
- "Track",
650
- "gx:Track",
651
- ];
652
700
  function getCoordinates(node) {
653
701
  return nodeVal(get1(node, "coordinates"));
654
702
  }
655
703
  function getGeometry(node) {
656
- const geometries = [];
657
- const coordTimes = [];
658
- for (const t of ["MultiGeometry", "MultiTrack", "gx:MultiTrack"]) {
659
- const elem = get1(node, t);
660
- if (elem) {
661
- return getGeometry(elem);
662
- }
663
- }
664
- for (const geoType of GEO_TYPES) {
665
- for (const geomNode of $(node, geoType)) {
666
- switch (geoType) {
704
+ let geometries = [];
705
+ let coordTimes = [];
706
+ for (let i = 0; i < node.childNodes.length; i++) {
707
+ const child = node.childNodes.item(i);
708
+ if (isElement(child)) {
709
+ switch (child.tagName) {
710
+ case "MultiGeometry":
711
+ case "MultiTrack":
712
+ case "gx:MultiTrack": {
713
+ const childGeometries = getGeometry(child);
714
+ geometries = geometries.concat(childGeometries.geometries);
715
+ coordTimes = coordTimes.concat(childGeometries.coordTimes);
716
+ break;
717
+ }
667
718
  case "Point": {
668
- const coordinates = coord1(getCoordinates(geomNode));
719
+ const coordinates = coord1(getCoordinates(child));
669
720
  if (coordinates.length >= 2) {
670
721
  geometries.push({
671
722
  type: "Point",
@@ -675,7 +726,7 @@ function getGeometry(node) {
675
726
  break;
676
727
  }
677
728
  case "LineString": {
678
- const coordinates = coord(getCoordinates(geomNode));
729
+ const coordinates = coord(getCoordinates(child));
679
730
  if (coordinates.length >= 2) {
680
731
  geometries.push({
681
732
  type: "LineString",
@@ -686,7 +737,7 @@ function getGeometry(node) {
686
737
  }
687
738
  case "Polygon": {
688
739
  const coords = [];
689
- for (const linearRing of $(geomNode, "LinearRing")) {
740
+ for (const linearRing of $(child, "LinearRing")) {
690
741
  const ring = fixRing(coord(getCoordinates(linearRing)));
691
742
  if (ring.length >= 4) {
692
743
  coords.push(ring);
@@ -702,7 +753,7 @@ function getGeometry(node) {
702
753
  }
703
754
  case "Track":
704
755
  case "gx:Track": {
705
- const gx = gxCoords(geomNode);
756
+ const gx = gxCoords(child);
706
757
  if (!gx)
707
758
  break;
708
759
  const { times, geometry } = gx;
@@ -720,17 +771,6 @@ function getGeometry(node) {
720
771
  };
721
772
  }
722
773
 
723
- function extractExtendedData(node) {
724
- return get(node, "ExtendedData", (extendedData, properties) => {
725
- for (const data of $(extendedData, "Data")) {
726
- properties[data.getAttribute("name") || ""] = nodeVal(get1(data, "value"));
727
- }
728
- for (const simpleData of $(extendedData, "SimpleData")) {
729
- properties[simpleData.getAttribute("name") || ""] = nodeVal(simpleData);
730
- }
731
- return properties;
732
- });
733
- }
734
774
  function geometryListToGeometry(geometries) {
735
775
  return geometries.length === 0
736
776
  ? null
@@ -741,46 +781,6 @@ function geometryListToGeometry(geometries) {
741
781
  geometries,
742
782
  };
743
783
  }
744
- function extractTimeSpan(node) {
745
- return get(node, "TimeSpan", (timeSpan) => {
746
- return {
747
- timespan: {
748
- begin: nodeVal(get1(timeSpan, "begin")),
749
- end: nodeVal(get1(timeSpan, "end")),
750
- },
751
- };
752
- });
753
- }
754
- function extractTimeStamp(node) {
755
- return get(node, "TimeStamp", (timeStamp) => {
756
- return { timestamp: nodeVal(get1(timeStamp, "when")) };
757
- });
758
- }
759
- function extractCascadedStyle(node, styleMap) {
760
- return val1(node, "styleUrl", (styleUrl) => {
761
- styleUrl = normalizeId(styleUrl);
762
- if (styleMap[styleUrl]) {
763
- return Object.assign({ styleUrl }, styleMap[styleUrl]);
764
- }
765
- // For backward-compatibility. Should we still include
766
- // styleUrl even if it's not resolved?
767
- return { styleUrl };
768
- });
769
- }
770
- function getMaybeHTMLDescription(node) {
771
- const descriptionNode = get1(node, "description");
772
- for (const c of Array.from(descriptionNode?.childNodes || [])) {
773
- if (c.nodeType === 4) {
774
- return {
775
- description: {
776
- "@type": "html",
777
- value: nodeVal(c),
778
- },
779
- };
780
- }
781
- }
782
- return {};
783
- }
784
784
  function getPlacemark(node, styleMap) {
785
785
  const { coordTimes, geometries } = getGeometry(node);
786
786
  const feature = {
@@ -810,6 +810,94 @@ function getPlacemark(node, styleMap) {
810
810
  return feature;
811
811
  }
812
812
 
813
+ function getGroundOverlayBox(node) {
814
+ const latLonQuad = get1(node, "gx:LatLonQuad");
815
+ if (latLonQuad) {
816
+ const ring = fixRing(coord(getCoordinates(node)));
817
+ return {
818
+ type: "Polygon",
819
+ coordinates: [ring],
820
+ };
821
+ }
822
+ return getLatLonBox(node);
823
+ }
824
+ const DEGREES_TO_RADIANS = Math.PI / 180;
825
+ function rotateBox(bbox, coordinates, rotation) {
826
+ const center = [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2];
827
+ return [
828
+ coordinates[0].map((coordinate) => {
829
+ const dy = coordinate[1] - center[1];
830
+ const dx = coordinate[0] - center[0];
831
+ const distance = Math.sqrt(Math.pow(dy, 2) + Math.pow(dx, 2));
832
+ const angle = Math.atan2(dy, dx) - rotation * DEGREES_TO_RADIANS;
833
+ return [
834
+ center[0] + Math.cos(angle) * distance,
835
+ center[1] + Math.sin(angle) * distance,
836
+ ];
837
+ }),
838
+ ];
839
+ }
840
+ function getLatLonBox(node) {
841
+ const latLonBox = get1(node, "LatLonBox");
842
+ if (latLonBox) {
843
+ const north = num1(latLonBox, "north");
844
+ const west = num1(latLonBox, "west");
845
+ const east = num1(latLonBox, "east");
846
+ const south = num1(latLonBox, "south");
847
+ const rotation = num1(latLonBox, "rotation");
848
+ if (typeof north === "number" &&
849
+ typeof south === "number" &&
850
+ typeof west === "number" &&
851
+ typeof east === "number") {
852
+ const bbox = [west, south, east, north];
853
+ let coordinates = [
854
+ [
855
+ [west, south],
856
+ [west, north],
857
+ [east, north],
858
+ [west, north],
859
+ [west, south], // bottom left (again)
860
+ ],
861
+ ];
862
+ if (typeof rotation === "number") {
863
+ coordinates = rotateBox(bbox, coordinates, rotation);
864
+ }
865
+ return {
866
+ type: "Polygon",
867
+ coordinates,
868
+ };
869
+ }
870
+ }
871
+ return null;
872
+ }
873
+ function getGroundOverlay(node, styleMap) {
874
+ const geometry = getGroundOverlayBox(node);
875
+ const feature = {
876
+ type: "Feature",
877
+ geometry,
878
+ properties: Object.assign(
879
+ /**
880
+ * Related to
881
+ * https://gist.github.com/tmcw/037a1cb6660d74a392e9da7446540f46
882
+ */
883
+ { "@geometry-type": "groundoverlay" }, getMulti(node, [
884
+ "name",
885
+ "address",
886
+ "visibility",
887
+ "open",
888
+ "phoneNumber",
889
+ "description",
890
+ ]), getMaybeHTMLDescription(node), extractCascadedStyle(node, styleMap), extractStyle(node), extractIconHref(node), extractExtendedData(node), extractTimeSpan(node), extractTimeStamp(node)),
891
+ };
892
+ if (feature.properties?.visibility !== undefined) {
893
+ feature.properties.visibility = feature.properties.visibility !== "0";
894
+ }
895
+ const id = node.getAttribute("id");
896
+ if (id !== null && id !== "")
897
+ feature.id = id;
898
+ return feature;
899
+ }
900
+
813
901
  function getStyleId(style) {
814
902
  let id = style.getAttribute("id");
815
903
  const parentNode = style.parentNode;
@@ -880,6 +968,23 @@ function getFolder(node) {
880
968
  * ]
881
969
  * }
882
970
  * ```
971
+ *
972
+ * ### GroundOverlay
973
+ *
974
+ * GroundOverlay elements are converted into
975
+ * `Feature` objects with `Polygon` geometries,
976
+ * a property like:
977
+ *
978
+ * ```json
979
+ * {
980
+ * "@geometry-type": "groundoverlay"
981
+ * }
982
+ * ```
983
+ *
984
+ * And the ground overlay's image URL in the `href`
985
+ * property. Ground overlays will need to be displayed
986
+ * with a separate method to other features, depending
987
+ * on which map framework you're using.
883
988
  */
884
989
  function kmlWithFolders(node) {
885
990
  const styleMap = buildStyleMap(node);
@@ -887,6 +992,13 @@ function kmlWithFolders(node) {
887
992
  function traverse(node, pointer) {
888
993
  if (isElement(node)) {
889
994
  switch (node.tagName) {
995
+ case "GroundOverlay": {
996
+ const placemark = getGroundOverlay(node, styleMap);
997
+ if (placemark) {
998
+ pointer.children.push(placemark);
999
+ }
1000
+ break;
1001
+ }
890
1002
  case "Placemark": {
891
1003
  const placemark = getPlacemark(node, styleMap);
892
1004
  if (placemark) {
@@ -923,6 +1035,11 @@ function* kmlGen(node) {
923
1035
  if (feature)
924
1036
  yield feature;
925
1037
  }
1038
+ for (const groundOverlay of $(node, "GroundOverlay")) {
1039
+ const feature = getGroundOverlay(groundOverlay, styleMap);
1040
+ if (feature)
1041
+ yield feature;
1042
+ }
926
1043
  }
927
1044
  /**
928
1045
  * Convert a KML document to GeoJSON. The first argument, `doc`, must be a KML