Skip to content

Commit 3d58707

Browse files
authored
deps: Make external interfaces compatible with xmldom (#138)
* Make external interfaces compatible with xmldom * Remove peer dependencies * Add ts note * 6.0.2-0 * Sort imports * Update readme
1 parent 2f31a74 commit 3d58707

File tree

6 files changed

+44
-28
lines changed

6 files changed

+44
-28
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ with Polygon geometries. They have two defined properties:
5858

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

61+
## TypeScript
62+
63+
If you're using this with TypeScript, you'll want to also install:
64+
65+
- `@types/geojson`
66+
- `@xmldom/xmldom`
67+
68+
These will give you accurate types for both input and output
69+
types. Due to TypeScript limitations, it's currently necessary
70+
to install `@xmldom/xmldom` for accurate input types even
71+
if you aren't using that module for parsing XML.
72+
If you have ideas for how to improve this, please
73+
[comment on this PR!](https://github.com/placemark/togeojson/pull/138).
74+
6175
## CLI
6276

6377
Use [@tmcw/togeojson-cli](https://github.com/tmcw/togeojson-cli) to use this

lib/gpx.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Document as XDocument } from "@xmldom/xmldom";
12
import type {
23
Feature,
34
FeatureCollection,
@@ -164,12 +165,13 @@ function getPoint(ns: NS, node: Element): Feature<Point> | null {
164165
* a [Generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators)
165166
* that yields output feature by feature.
166167
*/
167-
export function* gpxGen(node: Document): Generator<Feature> {
168+
export function* gpxGen(node: Document | XDocument): Generator<Feature> {
169+
const n = node as Document;
168170
const GPXX = "gpxx";
169171
const GPXX_URI = "http://www.garmin.com/xmlschemas/GpxExtensions/v3";
170172
// Namespaces
171173
const ns: NS = [[GPXX, GPXX_URI]];
172-
const attrs = node.getElementsByTagName("gpx")[0]?.attributes;
174+
const attrs = n.getElementsByTagName("gpx")[0]?.attributes;
173175
if (attrs) {
174176
for (const attr of Array.from(attrs)) {
175177
if (attr.name?.startsWith("xmlns:") && attr.value !== GPXX_URI) {
@@ -178,17 +180,17 @@ export function* gpxGen(node: Document): Generator<Feature> {
178180
}
179181
}
180182

181-
for (const track of $(node, "trk")) {
183+
for (const track of $(n, "trk")) {
182184
const feature = getTrack(ns, track);
183185
if (feature) yield feature;
184186
}
185187

186-
for (const route of $(node, "rte")) {
188+
for (const route of $(n, "rte")) {
187189
const feature = getRoute(ns, route);
188190
if (feature) yield feature;
189191
}
190192

191-
for (const waypoint of $(node, "wpt")) {
193+
for (const waypoint of $(n, "wpt")) {
192194
const point = getPoint(ns, waypoint);
193195
if (point) yield point;
194196
}
@@ -205,7 +207,7 @@ export function* gpxGen(node: Document): Generator<Feature> {
205207
* addition of a `_gpxType` property on each `LineString` feature that indicates whether
206208
* the feature was encoded as a route (`rte`) or track (`trk`) in the GPX document.
207209
*/
208-
export function gpx(node: Document): FeatureCollection {
210+
export function gpx(node: Document | XDocument): FeatureCollection {
209211
return {
210212
type: "FeatureCollection",
211213
features: Array.from(gpxGen(node)),

lib/kml.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Document as XDocument } from "@xmldom/xmldom";
12
import type { FeatureCollection, Geometry } from "geojson";
23
import { extractStyle } from "./kml/extractStyle";
34
import { getGroundOverlay } from "./kml/ground_overlay";
@@ -174,13 +175,14 @@ function getFolder(node: Element): Folder {
174175
* on which map framework you're using.
175176
*/
176177
export function kmlWithFolders(
177-
node: Document,
178+
node: Document | XDocument,
178179
options: KMLOptions = {
179180
skipNullGeometry: false,
180181
}
181182
): Root {
182-
const styleMap = buildStyleMap(node);
183-
const schema = buildSchema(node);
183+
const n = node as Document;
184+
const styleMap = buildStyleMap(n);
185+
const schema = buildSchema(n);
184186

185187
// atomic geospatial types supported by KML - MultiGeometry is
186188
// handled separately
@@ -227,7 +229,7 @@ export function kmlWithFolders(
227229
}
228230
}
229231

230-
traverse(node, tree, options);
232+
traverse(n, tree, options);
231233

232234
return tree;
233235
}
@@ -238,18 +240,19 @@ export function kmlWithFolders(
238240
* that yields output feature by feature.
239241
*/
240242
export function* kmlGen(
241-
node: Document,
243+
node: Document | XDocument,
242244
options: KMLOptions = {
243245
skipNullGeometry: false,
244246
}
245247
): Generator<F> {
246-
const styleMap = buildStyleMap(node);
247-
const schema = buildSchema(node);
248-
for (const placemark of $(node, "Placemark")) {
248+
const n = node as Document;
249+
const styleMap = buildStyleMap(n);
250+
const schema = buildSchema(n);
251+
for (const placemark of $(n, "Placemark")) {
249252
const feature = getPlacemark(placemark, styleMap, schema, options);
250253
if (feature) yield feature;
251254
}
252-
for (const groundOverlay of $(node, "GroundOverlay")) {
255+
for (const groundOverlay of $(n, "GroundOverlay")) {
253256
const feature = getGroundOverlay(groundOverlay, styleMap, schema, options);
254257
if (feature) yield feature;
255258
}
@@ -266,13 +269,13 @@ export function* kmlGen(
266269
* or use it directly in libraries.
267270
*/
268271
export function kml(
269-
node: Document,
272+
node: Document | XDocument,
270273
options: KMLOptions = {
271274
skipNullGeometry: false,
272275
}
273276
): FeatureCollection<Geometry | null> {
274277
return {
275278
type: "FeatureCollection",
276-
features: Array.from(kmlGen(node, options)),
279+
features: Array.from(kmlGen(node as Document, options)),
277280
};
278281
}

lib/tcx.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Document as XDocument } from "@xmldom/xmldom";
12
import type { Feature, FeatureCollection, Position } from "geojson";
23
import { $, type P, get, get1, nodeVal, num1 } from "./shared";
34

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

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

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://json.schemastore.org/package.json",
33
"name": "@tmcw/togeojson",
4-
"version": "6.0.1",
4+
"version": "6.0.2-0",
55
"description": "convert KML and GPX to GeoJSON",
66
"source": "lib/index.ts",
77
"umd:main": "dist/togeojson.umd.js",
@@ -26,16 +26,12 @@
2626
"prepare": "rollup -c rollup.config.mjs",
2727
"release": "standard-version"
2828
},
29-
"peerDependencies": {
30-
"@types/geojson": "*",
31-
"@xmldom/xmldom": "^0.9.5"
32-
},
3329
"devDependencies": {
3430
"@biomejs/biome": "^1.9.4",
3531
"@placemarkio/check-geojson": "^0.1.12",
3632
"@rollup/plugin-terser": "^0.4.4",
3733
"@rollup/plugin-typescript": "^12.1.2",
38-
"@types/geojson": "^7946.0.16",
34+
"@types/geojson": "*",
3935
"@vitest/coverage-v8": "^3.0.4",
4036
"@xmldom/xmldom": "^0.9.7",
4137
"rollup": "^4.32.0",

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)