Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make external interfaces compatible with xmldom #138

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ with Polygon geometries. They have two defined properties:

Both `gx:LatLonQuad` and `LatLonBox`-based ground overlays are supported.

## TypeScript

Install the `@types/geojson` package alongside this for its types
to work. This will also use types from `@xmldom/xmldom` as
input types if it's available.

## CLI

Use [@tmcw/togeojson-cli](https://github.com/tmcw/togeojson-cli) to use this
Expand Down
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
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "@tmcw/togeojson",
"version": "6.0.1",
"version": "6.0.2-0",
"description": "convert KML and GPX to GeoJSON",
"source": "lib/index.ts",
"umd:main": "dist/togeojson.umd.js",
Expand All @@ -26,16 +26,12 @@
"prepare": "rollup -c rollup.config.mjs",
"release": "standard-version"
},
"peerDependencies": {
"@types/geojson": "*",
"@xmldom/xmldom": "^0.9.5"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@placemarkio/check-geojson": "^0.1.12",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^12.1.2",
"@types/geojson": "^7946.0.16",
"@types/geojson": "*",
"@vitest/coverage-v8": "^3.0.4",
"@xmldom/xmldom": "^0.9.7",
"rollup": "^4.32.0",
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading