Skip to content

Commit

Permalink
Make external interfaces compatible with xmldom
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Feb 15, 2025
1 parent 2f31a74 commit 6b8c4e8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
14 changes: 8 additions & 6 deletions lib/gpx.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Document as XDocument } from "@xmldom/xmldom";
import type {
Feature,
FeatureCollection,
Expand Down Expand Up @@ -164,12 +165,13 @@ function getPoint(ns: NS, node: Element): Feature<Point> | null {
* a [Generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators)
* that yields output feature by feature.
*/
export function* gpxGen(node: Document): Generator<Feature> {
export function* gpxGen(node: Document | XDocument): Generator<Feature> {
const n = node as Document;
const GPXX = "gpxx";
const GPXX_URI = "http://www.garmin.com/xmlschemas/GpxExtensions/v3";
// Namespaces
const ns: NS = [[GPXX, GPXX_URI]];
const attrs = node.getElementsByTagName("gpx")[0]?.attributes;
const attrs = n.getElementsByTagName("gpx")[0]?.attributes;
if (attrs) {
for (const attr of Array.from(attrs)) {
if (attr.name?.startsWith("xmlns:") && attr.value !== GPXX_URI) {
Expand All @@ -178,17 +180,17 @@ export function* gpxGen(node: Document): Generator<Feature> {
}
}

for (const track of $(node, "trk")) {
for (const track of $(n, "trk")) {
const feature = getTrack(ns, track);
if (feature) yield feature;
}

for (const route of $(node, "rte")) {
for (const route of $(n, "rte")) {
const feature = getRoute(ns, route);
if (feature) yield feature;
}

for (const waypoint of $(node, "wpt")) {
for (const waypoint of $(n, "wpt")) {
const point = getPoint(ns, waypoint);
if (point) yield point;
}
Expand All @@ -205,7 +207,7 @@ export function* gpxGen(node: Document): Generator<Feature> {
* addition of a `_gpxType` property on each `LineString` feature that indicates whether
* the feature was encoded as a route (`rte`) or track (`trk`) in the GPX document.
*/
export function gpx(node: Document): FeatureCollection {
export function gpx(node: Document | XDocument): FeatureCollection {
return {
type: "FeatureCollection",
features: Array.from(gpxGen(node)),
Expand Down
25 changes: 14 additions & 11 deletions lib/kml.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FeatureCollection, Geometry } from "geojson";
import type { Document as XDocument } from "@xmldom/xmldom";
import { extractStyle } from "./kml/extractStyle";
import { getGroundOverlay } from "./kml/ground_overlay";
import { getPlacemark } from "./kml/placemark";
Expand Down Expand Up @@ -174,13 +175,14 @@ function getFolder(node: Element): Folder {
* on which map framework you're using.
*/
export function kmlWithFolders(
node: Document,
node: Document | XDocument,
options: KMLOptions = {
skipNullGeometry: false,
}
): Root {
const styleMap = buildStyleMap(node);
const schema = buildSchema(node);
const n = node as Document;
const styleMap = buildStyleMap(n);
const schema = buildSchema(n);

// atomic geospatial types supported by KML - MultiGeometry is
// handled separately
Expand Down Expand Up @@ -227,7 +229,7 @@ export function kmlWithFolders(
}
}

traverse(node, tree, options);
traverse(n, tree, options);

return tree;
}
Expand All @@ -238,18 +240,19 @@ export function kmlWithFolders(
* that yields output feature by feature.
*/
export function* kmlGen(
node: Document,
node: Document | XDocument,
options: KMLOptions = {
skipNullGeometry: false,
}
): Generator<F> {
const styleMap = buildStyleMap(node);
const schema = buildSchema(node);
for (const placemark of $(node, "Placemark")) {
const n = node as Document;
const styleMap = buildStyleMap(n);
const schema = buildSchema(n);
for (const placemark of $(n, "Placemark")) {
const feature = getPlacemark(placemark, styleMap, schema, options);
if (feature) yield feature;
}
for (const groundOverlay of $(node, "GroundOverlay")) {
for (const groundOverlay of $(n, "GroundOverlay")) {
const feature = getGroundOverlay(groundOverlay, styleMap, schema, options);
if (feature) yield feature;
}
Expand All @@ -266,13 +269,13 @@ export function* kmlGen(
* or use it directly in libraries.
*/
export function kml(
node: Document,
node: Document | XDocument,
options: KMLOptions = {
skipNullGeometry: false,
}
): FeatureCollection<Geometry | null> {
return {
type: "FeatureCollection",
features: Array.from(kmlGen(node, options)),
features: Array.from(kmlGen(node as Document, options)),
};
}
9 changes: 5 additions & 4 deletions lib/tcx.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Document as XDocument } from "@xmldom/xmldom";
import type { Feature, FeatureCollection, Position } from "geojson";
import { $, type P, get, get1, nodeVal, num1 } from "./shared";

Expand Down Expand Up @@ -181,13 +182,13 @@ function getLap(node: Element): Feature | null {
* first argument, `doc`, must be a TCX
* document as an XML DOM - not as a string.
*/
export function* tcxGen(node: Document): Generator<Feature> {
for (const lap of $(node, "Lap")) {
export function* tcxGen(node: Document | XDocument): Generator<Feature> {
for (const lap of $(node as Document, "Lap")) {
const feature = getLap(lap);
if (feature) yield feature;
}

for (const course of $(node, "Courses")) {
for (const course of $(node as Document, "Courses")) {
const feature = getLap(course);
if (feature) yield feature;
}
Expand All @@ -197,7 +198,7 @@ export function* tcxGen(node: Document): Generator<Feature> {
* Convert a TCX document to GeoJSON. The first argument, `doc`, must be a TCX
* document as an XML DOM - not as a string.
*/
export function tcx(node: Document): FeatureCollection {
export function tcx(node: Document | XDocument): FeatureCollection {
return {
type: "FeatureCollection",
features: Array.from(tcxGen(node)),
Expand Down

0 comments on commit 6b8c4e8

Please sign in to comment.