@osmix/geojson 0.0.13 → 0.0.16

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/src/types.ts CHANGED
@@ -3,17 +3,17 @@
3
3
  * @module
4
4
  */
5
5
 
6
- import type { OsmEntityType, OsmInfoParsed, OsmTags } from "@osmix/shared/types"
6
+ import type { OsmEntityType, OsmInfoParsed, OsmTags } from "@osmix/types";
7
7
  import type {
8
- Feature,
9
- GeometryCollection,
10
- LineString,
11
- MultiLineString,
12
- MultiPoint,
13
- MultiPolygon,
14
- Point,
15
- Polygon,
16
- } from "geojson"
8
+ Feature,
9
+ GeometryCollection,
10
+ LineString,
11
+ MultiLineString,
12
+ MultiPoint,
13
+ MultiPolygon,
14
+ Point,
15
+ Polygon,
16
+ } from "geojson";
17
17
 
18
18
  /**
19
19
  * GeoJSON Feature properties for OSM entities.
@@ -22,15 +22,15 @@ import type {
22
22
  * All OSM tags are spread into the properties object.
23
23
  */
24
24
  export type OsmGeoJSONProperties = {
25
- /** OSM entity ID. */
26
- id: number
27
- /** Entity type: "node", "way", or "relation". */
28
- type: OsmEntityType
29
- /** OSM tags (key-value pairs). */
30
- tags?: OsmTags
31
- /** Entity metadata (version, timestamp, user, etc.). */
32
- info?: OsmInfoParsed
33
- }
25
+ /** OSM entity ID. */
26
+ id: number;
27
+ /** Entity type: "node", "way", or "relation". */
28
+ type: OsmEntityType;
29
+ /** OSM tags (key-value pairs). */
30
+ tags?: OsmTags;
31
+ /** Entity metadata (version, timestamp, user, etc.). */
32
+ info?: OsmInfoParsed;
33
+ };
34
34
 
35
35
  /**
36
36
  * GeoJSON Feature with OSM-specific properties.
@@ -41,15 +41,15 @@ export type OsmGeoJSONProperties = {
41
41
  * - Relations → MultiPolygon, MultiLineString, MultiPoint, or GeometryCollection
42
42
  */
43
43
  export type OsmGeoJSONFeature<
44
- T extends
45
- | Point
46
- | LineString
47
- | Polygon
48
- | MultiPolygon
49
- | MultiLineString
50
- | MultiPoint
51
- | GeometryCollection,
52
- > = Feature<T, OsmGeoJSONProperties>
44
+ T extends
45
+ | Point
46
+ | LineString
47
+ | Polygon
48
+ | MultiPolygon
49
+ | MultiLineString
50
+ | MultiPoint
51
+ | GeometryCollection,
52
+ > = Feature<T, OsmGeoJSONProperties>;
53
53
 
54
54
  /**
55
55
  * GeoJSON FeatureCollection types that can be imported into an Osm index.
@@ -58,8 +58,8 @@ export type OsmGeoJSONFeature<
58
58
  * Other geometry types are not supported for import.
59
59
  */
60
60
  export type ImportableGeoJSON = GeoJSON.FeatureCollection<
61
- GeoJSON.Point | GeoJSON.LineString | GeoJSON.Polygon | GeoJSON.MultiPolygon
62
- >
61
+ GeoJSON.Point | GeoJSON.LineString | GeoJSON.Polygon | GeoJSON.MultiPolygon
62
+ >;
63
63
 
64
64
  /**
65
65
  * Input types accepted by `fromGeoJSON`.
@@ -70,8 +70,4 @@ export type ImportableGeoJSON = GeoJSON.FeatureCollection<
70
70
  * - `string` - JSON string
71
71
  * - `ImportableGeoJSON` - Already-parsed FeatureCollection
72
72
  */
73
- export type ReadOsmDataTypes =
74
- | ArrayBufferLike
75
- | ReadableStream
76
- | string
77
- | ImportableGeoJSON
73
+ export type ReadOsmDataTypes = ArrayBufferLike | ReadableStream | string | ImportableGeoJSON;
package/src/utils.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * @module
4
4
  */
5
5
 
6
- import type { ImportableGeoJSON, ReadOsmDataTypes } from "./types.ts"
6
+ import type { ImportableGeoJSON, ReadOsmDataTypes } from "./types.ts";
7
7
 
8
8
  /**
9
9
  * Parse GeoJSON from various input formats.
@@ -28,31 +28,29 @@ import type { ImportableGeoJSON, ReadOsmDataTypes } from "./types.ts"
28
28
  * const geojson = await readDataAsGeoJSON(response.body!)
29
29
  * ```
30
30
  */
31
- export async function readDataAsGeoJSON(
32
- data: ReadOsmDataTypes,
33
- ): Promise<ImportableGeoJSON> {
34
- if (data == null) throw new Error("Data is null")
35
- if (typeof data === "string") return JSON.parse(data) as ImportableGeoJSON
31
+ export async function readDataAsGeoJSON(data: ReadOsmDataTypes): Promise<ImportableGeoJSON> {
32
+ if (data == null) throw new Error("Data is null");
33
+ if (typeof data === "string") return JSON.parse(data) as ImportableGeoJSON;
36
34
 
37
- if (data instanceof ReadableStream) {
38
- const reader = data.pipeThrough(new TextDecoderStream()).getReader()
39
- let result = ""
40
- while (true) {
41
- const { done, value } = await reader.read()
42
- if (done) break
43
- if (value !== undefined) result += value
44
- }
45
- return JSON.parse(result) as ImportableGeoJSON
46
- }
47
- if (data instanceof ArrayBuffer || data instanceof SharedArrayBuffer) {
48
- const decoder = new TextDecoder()
49
- const text = decoder.decode(new Uint8Array(data))
50
- return JSON.parse(text) as ImportableGeoJSON
51
- }
52
- if (typeof data === "object" && "type" in data && "features" in data) {
53
- return data as ImportableGeoJSON
54
- }
55
- throw new Error(
56
- "Invalid data type. Accepts string, ReadableStream, ArrayBufferLike, or GeoJSON FeatureCollection.",
57
- )
35
+ if (data instanceof ReadableStream) {
36
+ const reader = data.pipeThrough(new TextDecoderStream()).getReader();
37
+ let result = "";
38
+ while (true) {
39
+ const { done, value } = await reader.read();
40
+ if (done) break;
41
+ if (value !== undefined) result += value;
42
+ }
43
+ return JSON.parse(result) as ImportableGeoJSON;
44
+ }
45
+ if (data instanceof ArrayBuffer || data instanceof SharedArrayBuffer) {
46
+ const decoder = new TextDecoder();
47
+ const text = decoder.decode(new Uint8Array(data));
48
+ return JSON.parse(text) as ImportableGeoJSON;
49
+ }
50
+ if (typeof data === "object" && "type" in data && "features" in data) {
51
+ return data as ImportableGeoJSON;
52
+ }
53
+ throw new Error(
54
+ "Invalid data type. Accepts string, ReadableStream, ArrayBufferLike, or GeoJSON FeatureCollection.",
55
+ );
58
56
  }
@@ -1,65 +1,62 @@
1
- import { describe, expect, it } from "bun:test"
2
- import type { OsmNode, OsmRelation, OsmWay } from "@osmix/shared/types"
3
- import {
4
- nodeToFeature,
5
- relationToFeature,
6
- wayToFeature,
7
- } from "../src/entity-to-feature"
1
+ import type { OsmNode, OsmRelation, OsmWay } from "@osmix/types";
2
+ import { describe, expect, it } from "vitest";
3
+
4
+ import { nodeToFeature, relationToFeature, wayToFeature } from "../src/entity-to-feature";
8
5
 
9
6
  describe("geojson helpers", () => {
10
- const node0: OsmNode = { id: 1, lat: 0, lon: 0, tags: { amenity: "cafe" } }
11
- const node1: OsmNode = { id: 2, lat: 1, lon: 1 }
12
- const nodes: OsmNode[] = [node0, node1]
13
- const nodeMap = new Map(nodes.map((n) => [n.id, n]))
14
- const refToPosition = (id: number) => {
15
- const node = nodeMap.get(id)
16
- if (!node) throw new Error(`Node ${id} not found`)
17
- return [node.lon, node.lat] as [number, number]
18
- }
7
+ const node0: OsmNode = { id: 1, lat: 0, lon: 0, tags: { amenity: "cafe" } };
8
+ const node1: OsmNode = { id: 2, lat: 1, lon: 1 };
9
+ const nodes: OsmNode[] = [node0, node1];
10
+ const nodeMap = new Map(nodes.map((n) => [n.id, n]));
11
+ const refToPosition = (id: number) => {
12
+ const node = nodeMap.get(id);
13
+ if (!node) throw new Error(`Node ${id} not found`);
14
+ return [node.lon, node.lat] as [number, number];
15
+ };
19
16
 
20
- it("converts node to GeoJSON Point", () => {
21
- const feature = nodeToFeature(node0)
22
- expect(feature.type).toBe("Feature")
23
- expect(feature.geometry.type).toBe("Point")
24
- expect(feature.geometry.coordinates).toEqual([node0.lon, node0.lat])
25
- expect(feature.properties).toEqual({
26
- id: node0.id,
27
- type: "node",
28
- ...node0.tags,
29
- ...node0.info,
30
- })
31
- })
17
+ it("converts node to GeoJSON Point", () => {
18
+ const feature = nodeToFeature(node0);
19
+ expect(feature.type).toBe("Feature");
20
+ expect(feature.geometry.type).toBe("Point");
21
+ expect(feature.geometry.coordinates).toEqual([node0.lon, node0.lat]);
22
+ expect(feature.properties).toEqual({
23
+ id: node0.id,
24
+ type: "node",
25
+ ...node0.tags,
26
+ ...node0.info,
27
+ });
28
+ });
32
29
 
33
- it("creates polygon for closed ways", () => {
34
- const way: OsmWay = {
35
- id: 10,
36
- refs: [1, 2, 1],
37
- tags: { area: "yes" },
38
- }
39
- const feature = wayToFeature(way, refToPosition)
40
- expect(feature.geometry.type).toBe("Polygon")
41
- })
30
+ it("creates polygon for closed ways", () => {
31
+ const way: OsmWay = {
32
+ id: 10,
33
+ refs: [1, 2, 1],
34
+ tags: { area: "yes" },
35
+ };
36
+ const feature = wayToFeature(way, refToPosition);
37
+ expect(feature.geometry.type).toBe("Polygon");
38
+ });
42
39
 
43
- it("builds relation geometry collection", () => {
44
- // Create a way that forms a closed ring
45
- const way: OsmWay = {
46
- id: 10,
47
- refs: [1, 2, 1], // Closed way
48
- tags: { area: "yes" },
49
- }
50
- const wayMap = new Map([[way.id, way]])
51
- const getWay = (wayId: number) => wayMap.get(wayId) ?? null
40
+ it("builds relation geometry collection", () => {
41
+ // Create a way that forms a closed ring
42
+ const way: OsmWay = {
43
+ id: 10,
44
+ refs: [1, 2, 1], // Closed way
45
+ tags: { area: "yes" },
46
+ };
47
+ const wayMap = new Map([[way.id, way]]);
48
+ const getWay = (wayId: number) => wayMap.get(wayId) ?? null;
52
49
 
53
- const relation: OsmRelation = {
54
- id: 20,
55
- members: [{ type: "way", ref: 10, role: "outer" }],
56
- tags: { type: "multipolygon" },
57
- }
58
- const feature = relationToFeature(relation, refToPosition, getWay)
59
- expect(feature.geometry.type).toBe("Polygon")
60
- if (feature.geometry.type === "Polygon") {
61
- expect(feature.geometry.coordinates?.[0]).toBeDefined()
62
- expect(feature.geometry.coordinates[0]).toHaveLength(3) // Should have 3 coordinates (closed ring)
63
- }
64
- })
65
- })
50
+ const relation: OsmRelation = {
51
+ id: 20,
52
+ members: [{ type: "way", ref: 10, role: "outer" }],
53
+ tags: { type: "multipolygon" },
54
+ };
55
+ const feature = relationToFeature(relation, refToPosition, getWay);
56
+ expect(feature.geometry.type).toBe("Polygon");
57
+ if (feature.geometry.type === "Polygon") {
58
+ expect(feature.geometry.coordinates?.[0]).toBeDefined();
59
+ expect(feature.geometry.coordinates[0]).toHaveLength(3); // Should have 3 coordinates (closed ring)
60
+ }
61
+ });
62
+ });