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