osm2geojson-lite 1.2.0 → 2.0.1
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.
- package/README.md +5 -0
- package/dist/cli.mjs +36 -0
- package/dist/index.d.ts +203 -0
- package/dist/index.js +924 -0
- package/dist/index.mjs +924 -0
- package/package.json +43 -56
- package/dist/node/index.d.ts +0 -25
- package/dist/node/index.js +0 -196
- package/dist/node/late-binder.d.ts +0 -8
- package/dist/node/late-binder.js +0 -24
- package/dist/node/node.d.ts +0 -14
- package/dist/node/node.js +0 -31
- package/dist/node/osm-object.d.ts +0 -25
- package/dist/node/osm-object.js +0 -38
- package/dist/node/ref-elements.d.ts +0 -9
- package/dist/node/ref-elements.js +0 -19
- package/dist/node/relation.d.ts +0 -21
- package/dist/node/relation.js +0 -220
- package/dist/node/utils.d.ts +0 -22
- package/dist/node/utils.js +0 -97
- package/dist/node/way-collection.d.ts +0 -14
- package/dist/node/way-collection.js +0 -109
- package/dist/node/way.d.ts +0 -21
- package/dist/node/way.js +0 -87
- package/dist/node/xmlparser.d.ts +0 -32
- package/dist/node/xmlparser.js +0 -161
- package/dist/web/osm2geojson-lite.js +0 -1
- /package/dist/{node/polytags.json → polytags.json} +0 -0
package/README.md
CHANGED
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import osm2geojson from "./index.js";
|
|
5
|
+
//#region src/cli.ts
|
|
6
|
+
function printHelp() {
|
|
7
|
+
process.stdout.write([
|
|
8
|
+
"Convert OpenStreetMap XML or JSON data to GeoJSON.",
|
|
9
|
+
"",
|
|
10
|
+
"Usage:",
|
|
11
|
+
" npx osm2geojson INPUT_FILE OUTPUT_FILE",
|
|
12
|
+
" npx osm2geojson < INPUT_FILE > OUTPUT_FILE",
|
|
13
|
+
""
|
|
14
|
+
].join("\n"));
|
|
15
|
+
}
|
|
16
|
+
async function main(positionals) {
|
|
17
|
+
const [inputFile = "/dev/stdin", outputFile = "/dev/stdout"] = positionals;
|
|
18
|
+
const geojson = osm2geojson(await readFile(inputFile, "utf-8"), {
|
|
19
|
+
completeFeature: true,
|
|
20
|
+
renderTagged: true
|
|
21
|
+
});
|
|
22
|
+
await writeFile(outputFile, `${JSON.stringify(geojson)}\n`);
|
|
23
|
+
}
|
|
24
|
+
const { values, positionals } = parseArgs({
|
|
25
|
+
args: process.argv.slice(2),
|
|
26
|
+
options: { help: {
|
|
27
|
+
short: "h",
|
|
28
|
+
type: "boolean"
|
|
29
|
+
} }
|
|
30
|
+
});
|
|
31
|
+
if (values.help) printHelp();
|
|
32
|
+
else main(positionals).catch((err) => {
|
|
33
|
+
process.stderr.write(`${err.stack}\n`);
|
|
34
|
+
process.exit(1);
|
|
35
|
+
});
|
|
36
|
+
//#endregion
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
//#region node_modules/@types/geojson/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The value values for the "type" property of GeoJSON Objects.
|
|
4
|
+
* https://tools.ietf.org/html/rfc7946#section-1.4
|
|
5
|
+
*/
|
|
6
|
+
type GeoJsonTypes = GeoJSON["type"];
|
|
7
|
+
/**
|
|
8
|
+
* Bounding box
|
|
9
|
+
* https://tools.ietf.org/html/rfc7946#section-5
|
|
10
|
+
*/
|
|
11
|
+
type BBox = [number, number, number, number] | [number, number, number, number, number, number];
|
|
12
|
+
/**
|
|
13
|
+
* A Position is an array of coordinates.
|
|
14
|
+
* https://tools.ietf.org/html/rfc7946#section-3.1.1
|
|
15
|
+
* Array should contain between two and three elements.
|
|
16
|
+
* The previous GeoJSON specification allowed more elements (e.g., which could be used to represent M values),
|
|
17
|
+
* but the current specification only allows X, Y, and (optionally) Z to be defined.
|
|
18
|
+
*
|
|
19
|
+
* Note: the type will not be narrowed down to `[number, number] | [number, number, number]` due to
|
|
20
|
+
* marginal benefits and the large impact of breaking change.
|
|
21
|
+
*
|
|
22
|
+
* See previous discussions on the type narrowing:
|
|
23
|
+
* - {@link https://github.com/DefinitelyTyped/DefinitelyTyped/pull/21590|Nov 2017}
|
|
24
|
+
* - {@link https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/67773|Dec 2023}
|
|
25
|
+
* - {@link https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/71441| Dec 2024}
|
|
26
|
+
*
|
|
27
|
+
* One can use a
|
|
28
|
+
* {@link https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates|user-defined type guard that returns a type predicate}
|
|
29
|
+
* to determine if a position is a 2D or 3D position.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* import type { Position } from 'geojson';
|
|
33
|
+
*
|
|
34
|
+
* type StrictPosition = [x: number, y: number] | [x: number, y: number, z: number]
|
|
35
|
+
*
|
|
36
|
+
* function isStrictPosition(position: Position): position is StrictPosition {
|
|
37
|
+
* return position.length === 2 || position.length === 3
|
|
38
|
+
* };
|
|
39
|
+
*
|
|
40
|
+
* let position: Position = [-116.91, 45.54];
|
|
41
|
+
*
|
|
42
|
+
* let x: number;
|
|
43
|
+
* let y: number;
|
|
44
|
+
* let z: number | undefined;
|
|
45
|
+
*
|
|
46
|
+
* if (isStrictPosition(position)) {
|
|
47
|
+
* // `tsc` would throw an error if we tried to destructure a fourth parameter
|
|
48
|
+
* [x, y, z] = position;
|
|
49
|
+
* } else {
|
|
50
|
+
* throw new TypeError("Position is not a 2D or 3D point");
|
|
51
|
+
* }
|
|
52
|
+
*/
|
|
53
|
+
type Position = number[];
|
|
54
|
+
/**
|
|
55
|
+
* The base GeoJSON object.
|
|
56
|
+
* https://tools.ietf.org/html/rfc7946#section-3
|
|
57
|
+
* The GeoJSON specification also allows foreign members
|
|
58
|
+
* (https://tools.ietf.org/html/rfc7946#section-6.1)
|
|
59
|
+
* Developers should use "&" type in TypeScript or extend the interface
|
|
60
|
+
* to add these foreign members.
|
|
61
|
+
*/
|
|
62
|
+
interface GeoJsonObject {
|
|
63
|
+
// Don't include foreign members directly into this type def.
|
|
64
|
+
// in order to preserve type safety.
|
|
65
|
+
// [key: string]: any;
|
|
66
|
+
/**
|
|
67
|
+
* Specifies the type of GeoJSON object.
|
|
68
|
+
*/
|
|
69
|
+
type: GeoJsonTypes;
|
|
70
|
+
/**
|
|
71
|
+
* Bounding box of the coordinate range of the object's Geometries, Features, or Feature Collections.
|
|
72
|
+
* The value of the bbox member is an array of length 2*n where n is the number of dimensions
|
|
73
|
+
* represented in the contained geometries, with all axes of the most southwesterly point
|
|
74
|
+
* followed by all axes of the more northeasterly point.
|
|
75
|
+
* The axes order of a bbox follows the axes order of geometries.
|
|
76
|
+
* https://tools.ietf.org/html/rfc7946#section-5
|
|
77
|
+
*/
|
|
78
|
+
bbox?: BBox | undefined;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Union of GeoJSON objects.
|
|
82
|
+
*/
|
|
83
|
+
type GeoJSON<G extends Geometry | null = Geometry, P = GeoJsonProperties> = G | Feature<G, P> | FeatureCollection<G, P>;
|
|
84
|
+
/**
|
|
85
|
+
* Geometry object.
|
|
86
|
+
* https://tools.ietf.org/html/rfc7946#section-3
|
|
87
|
+
*/
|
|
88
|
+
type Geometry = Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon | GeometryCollection;
|
|
89
|
+
type GeometryObject = Geometry;
|
|
90
|
+
/**
|
|
91
|
+
* Point geometry object.
|
|
92
|
+
* https://tools.ietf.org/html/rfc7946#section-3.1.2
|
|
93
|
+
*/
|
|
94
|
+
interface Point extends GeoJsonObject {
|
|
95
|
+
type: "Point";
|
|
96
|
+
coordinates: Position;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* MultiPoint geometry object.
|
|
100
|
+
* https://tools.ietf.org/html/rfc7946#section-3.1.3
|
|
101
|
+
*/
|
|
102
|
+
interface MultiPoint extends GeoJsonObject {
|
|
103
|
+
type: "MultiPoint";
|
|
104
|
+
coordinates: Position[];
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* LineString geometry object.
|
|
108
|
+
* https://tools.ietf.org/html/rfc7946#section-3.1.4
|
|
109
|
+
*/
|
|
110
|
+
interface LineString extends GeoJsonObject {
|
|
111
|
+
type: "LineString";
|
|
112
|
+
coordinates: Position[];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* MultiLineString geometry object.
|
|
116
|
+
* https://tools.ietf.org/html/rfc7946#section-3.1.5
|
|
117
|
+
*/
|
|
118
|
+
interface MultiLineString extends GeoJsonObject {
|
|
119
|
+
type: "MultiLineString";
|
|
120
|
+
coordinates: Position[][];
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Polygon geometry object.
|
|
124
|
+
* https://tools.ietf.org/html/rfc7946#section-3.1.6
|
|
125
|
+
*/
|
|
126
|
+
interface Polygon extends GeoJsonObject {
|
|
127
|
+
type: "Polygon";
|
|
128
|
+
coordinates: Position[][];
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* MultiPolygon geometry object.
|
|
132
|
+
* https://tools.ietf.org/html/rfc7946#section-3.1.7
|
|
133
|
+
*/
|
|
134
|
+
interface MultiPolygon extends GeoJsonObject {
|
|
135
|
+
type: "MultiPolygon";
|
|
136
|
+
coordinates: Position[][][];
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Geometry Collection
|
|
140
|
+
* https://tools.ietf.org/html/rfc7946#section-3.1.8
|
|
141
|
+
*/
|
|
142
|
+
interface GeometryCollection<G extends Geometry = Geometry> extends GeoJsonObject {
|
|
143
|
+
type: "GeometryCollection";
|
|
144
|
+
geometries: G[];
|
|
145
|
+
}
|
|
146
|
+
type GeoJsonProperties = {
|
|
147
|
+
[name: string]: any;
|
|
148
|
+
} | null;
|
|
149
|
+
/**
|
|
150
|
+
* A feature object which contains a geometry and associated properties.
|
|
151
|
+
* https://tools.ietf.org/html/rfc7946#section-3.2
|
|
152
|
+
*/
|
|
153
|
+
interface Feature<G extends Geometry | null = Geometry, P = GeoJsonProperties> extends GeoJsonObject {
|
|
154
|
+
type: "Feature";
|
|
155
|
+
/**
|
|
156
|
+
* The feature's geometry
|
|
157
|
+
*/
|
|
158
|
+
geometry: G;
|
|
159
|
+
/**
|
|
160
|
+
* A value that uniquely identifies this feature in a
|
|
161
|
+
* https://tools.ietf.org/html/rfc7946#section-3.2.
|
|
162
|
+
*/
|
|
163
|
+
id?: string | number | undefined;
|
|
164
|
+
/**
|
|
165
|
+
* Properties associated with this feature.
|
|
166
|
+
*/
|
|
167
|
+
properties: P;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* A collection of feature objects.
|
|
171
|
+
* https://tools.ietf.org/html/rfc7946#section-3.3
|
|
172
|
+
*/
|
|
173
|
+
interface FeatureCollection<G extends Geometry | null = Geometry, P = GeoJsonProperties> extends GeoJsonObject {
|
|
174
|
+
type: "FeatureCollection";
|
|
175
|
+
features: Array<Feature<G, P>>;
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/index.d.ts
|
|
179
|
+
interface IOptions {
|
|
180
|
+
/**
|
|
181
|
+
* When it's set to `true`, the returned geojson will include all elements that meet the specified conditions in `FeatureCollection` format;
|
|
182
|
+
* otherwise, only the bare geometry of the first `relation` element will be returned.
|
|
183
|
+
* @default false
|
|
184
|
+
*/
|
|
185
|
+
completeFeature?: boolean;
|
|
186
|
+
/**
|
|
187
|
+
* When it's set to `true`, the returned geojson will include all elements with tags (i.e., tagged)
|
|
188
|
+
* until `suppressWay` changes its behavior a bit; otherwise only the unreferenced ones get returned.
|
|
189
|
+
* @default false
|
|
190
|
+
*/
|
|
191
|
+
renderTagged?: boolean;
|
|
192
|
+
/**
|
|
193
|
+
* When it's set to `true`, the returned `FeatureCollection` will exclude all referenced `way`s even though they are tagged;
|
|
194
|
+
* otherwise the features of those `way`s will be included in the resulted result as well.
|
|
195
|
+
* @default true
|
|
196
|
+
*/
|
|
197
|
+
excludeWay?: boolean;
|
|
198
|
+
}
|
|
199
|
+
declare function osm2geojson(osm: string | {
|
|
200
|
+
[k: string]: any;
|
|
201
|
+
}, opts?: IOptions): FeatureCollection<GeometryObject>;
|
|
202
|
+
//#endregion
|
|
203
|
+
export { osm2geojson as default };
|