@nexgen-os/geojson 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/index.d.ts +45 -0
- package/dist/index.js +87 -0
- package/package.json +32 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nexgen OS contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@nexgen-os/geojson` — GeoJSON ⇄ plain features
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[GeoJSON](https://www.rfc-editor.org/rfc/rfc7946) is the format every web
|
|
4
|
+
map, every GIS and every open-data portal reads, and the one interchange
|
|
5
|
+
format of places in Nexgen OS
|
|
6
|
+
([0025](../../../docs/decisions/0025-a-place-has-one-shape.md)). This
|
|
7
|
+
library declares the shapes of RFC 7946, writes features as one
|
|
8
|
+
FeatureCollection with its `bbox` and the foreign members a writer adds, and
|
|
9
|
+
reads a document — a FeatureCollection, a Feature or a bare geometry — back
|
|
10
|
+
into features. MIT; no dependency, no database, no network.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { readGeoJson, writeGeoJson } from '@nexgen-os/geojson';
|
|
14
|
+
|
|
15
|
+
const fc = writeGeoJson(features, { source: { code: 'urbis' } }); // type, bbox, features, then the members
|
|
16
|
+
const { features, members, warnings } = readGeoJson(text);
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
`readGeoJson()` reports a member that is not a feature (`not_a_feature`)
|
|
20
|
+
and a geometry of no RFC 7946 type (`geometry_unknown`) in `warnings` and
|
|
21
|
+
keeps the rest; it refuses by name what is not GeoJSON (`not_geojson`). What
|
|
22
|
+
it never does: transform or round coordinates, rewind a polygon, open a
|
|
23
|
+
connection.
|
|
24
|
+
|
|
25
|
+
## The export of a participant or a project
|
|
26
|
+
|
|
27
|
+
`exportGeoJson()` of `@nexgen-os/core` writes a participant, or every
|
|
28
|
+
participant taking part in a project, as one FeatureCollection through `writeGeoJson()`: the
|
|
29
|
+
places and the figures computed on them travel together, so that a study
|
|
30
|
+
can be shown to the people it concerns.
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { exportGeoJson } from '@nexgen-os/core';
|
|
34
|
+
|
|
35
|
+
const fc = await exportGeoJson(db, { participantId }); // everything the user may read
|
|
36
|
+
const open = await exportGeoJson(db, { projectId }, { open: true }); // what may be published
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### What goes out
|
|
40
|
+
|
|
41
|
+
| Nexgen OS | Feature | Properties |
|
|
42
|
+
|---|---|---|
|
|
43
|
+
| the project (`projectId`) | its perimeter, first | `kind: "project"`, `project_kind`, `country`, `status`, `start_date`, `end_date` |
|
|
44
|
+
| each participant with a place | its point or outline | `project_id`, `role`, `segment`, `status`, address fields with the `municipality` code, `time_zone`, `location_precision`, `location_source_code` |
|
|
45
|
+
| its ground, with the `building` module | its parcel, or `null` | `kind: "site"`, `participant_id`, `use`, `area_m2`, `global_id`, `source_code` |
|
|
46
|
+
| each building, with the `building` module | its footprint | `participant_id`, `site_id`, `use`, `elevation_m`, `height_m`, `year_built`, `floors_above_ground`, `floors_below_ground`, `floor_area_m2`, `energy_label`, `status`, `global_id`, `source_code` |
|
|
47
|
+
| each surface, with the `building` module | its face, or `null` | `surface_kind`, `participant_id`, `site_id`, `building_id`, `azimuth_deg`, `tilt_deg`, `area_m2`, `material`, `u_value`, `is_external`, `global_id`, `source_code` |
|
|
48
|
+
| each device | `null`: a device is not placed | `device_type`, `participant_id`, `manufacturer`, `model`, `rated_power_kw`, `unit_count`, `status`, `installation_date`, `source_code` |
|
|
49
|
+
|
|
50
|
+
Every feature also carries its `attributes` (read by `attributes_of()` at
|
|
51
|
+
the moment asked) and its `identifiers` — an object of the core only has
|
|
52
|
+
them — and the `calculations` every installed module computed on it (each
|
|
53
|
+
module's table of them, `solar.estimates`, `community.settlements`…):
|
|
54
|
+
module, table, provenance, last update and results, exactly as the module
|
|
55
|
+
keeps them. The export computes nothing.
|
|
56
|
+
|
|
57
|
+
- Coordinates: WGS 84 longitude, latitude and height in metres, seven
|
|
58
|
+
decimals (about a centimetre); outer rings counter-clockwise, holes
|
|
59
|
+
clockwise (RFC 7946 §3.1.6); a `bbox` around every position.
|
|
60
|
+
- Feature `id`: the object's identifier in Nexgen OS.
|
|
61
|
+
- A foreign member, `nexgen_os`, says what the document is: the schema
|
|
62
|
+
version, the moment, whether it is open, every source drawn
|
|
63
|
+
on with its licence and attribution, and how many objects were withheld.
|
|
64
|
+
- The same database gives the same document: no export time inside it.
|
|
65
|
+
|
|
66
|
+
### The open export
|
|
67
|
+
|
|
68
|
+
`open: true` is the document meant for the public — a citizens' portal, a
|
|
69
|
+
consultation, an open-data catalogue:
|
|
70
|
+
|
|
71
|
+
- the street lines of an address go (`address_line1`, `address_line2`); the postal
|
|
72
|
+
code, the city and the municipality stay;
|
|
73
|
+
- serial numbers and the `metadata` of a described object go;
|
|
74
|
+
- an object described by a source whose licence forbids passing it on
|
|
75
|
+
(`sources.redistributable = false`) is withheld, with everything on it;
|
|
76
|
+
a participant placed by such a source keeps its address and loses its shape;
|
|
77
|
+
- attributes and identifiers from such a source go;
|
|
78
|
+
- the calculations stay: a study shown to the public is its figures beside its
|
|
79
|
+
places.
|
|
80
|
+
|
|
81
|
+
Whoever publishes still reads the document first: names of participants and
|
|
82
|
+
buildings are written by people and may name people.
|
|
83
|
+
|
|
84
|
+
### What it refuses
|
|
85
|
+
|
|
86
|
+
Every refusal starts with its name: `target_required` (a `participantId` or a
|
|
87
|
+
`projectId`, not both), `participant_not_found`, `project_not_found` (absent, or
|
|
88
|
+
not readable under row level security).
|
|
89
|
+
|
|
90
|
+
Not yet: importing GeoJSON places into participants, meters, series.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export type Position = number[];
|
|
2
|
+
export interface Geometry {
|
|
3
|
+
type: 'Point' | 'MultiPoint' | 'LineString' | 'MultiLineString' | 'Polygon' | 'MultiPolygon' | 'GeometryCollection';
|
|
4
|
+
coordinates?: unknown;
|
|
5
|
+
geometries?: Geometry[];
|
|
6
|
+
}
|
|
7
|
+
export interface Feature<P = Record<string, unknown>> {
|
|
8
|
+
type: 'Feature';
|
|
9
|
+
id?: string | number;
|
|
10
|
+
geometry: Geometry | null;
|
|
11
|
+
properties: P;
|
|
12
|
+
}
|
|
13
|
+
export interface FeatureCollection<P = Record<string, unknown>> {
|
|
14
|
+
type: 'FeatureCollection';
|
|
15
|
+
bbox?: number[];
|
|
16
|
+
features: Feature<P>[];
|
|
17
|
+
/** Foreign members (RFC 7946 §6.1): what a writer says of the document. */
|
|
18
|
+
[member: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Features as one FeatureCollection: a `bbox` around every position when
|
|
22
|
+
* there is one, then the features in the order given, then the foreign
|
|
23
|
+
* members. Coordinates are written as given: WGS 84 longitude, latitude
|
|
24
|
+
* and height, outer rings counter-clockwise, are the writer's to give.
|
|
25
|
+
*/
|
|
26
|
+
export declare function writeGeoJson<P, M extends Record<string, unknown> = Record<string, never>>(features: Feature<P>[], members?: M): FeatureCollection<P> & M;
|
|
27
|
+
export interface GeoJsonWarning {
|
|
28
|
+
code: 'not_a_feature' | 'geometry_unknown';
|
|
29
|
+
/** The position of the feature in the document. */
|
|
30
|
+
index: number;
|
|
31
|
+
message: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A GeoJSON document as features: a FeatureCollection gives its features,
|
|
35
|
+
* a Feature itself, a bare geometry a feature without properties. A member
|
|
36
|
+
* that is not a feature, or a geometry of no RFC 7946 type, is left out and
|
|
37
|
+
* reported. Refuses `not_geojson` for anything else.
|
|
38
|
+
*/
|
|
39
|
+
export declare function readGeoJson(input: string | unknown): {
|
|
40
|
+
features: Feature[];
|
|
41
|
+
members: Record<string, unknown>;
|
|
42
|
+
warnings: GeoJsonWarning[];
|
|
43
|
+
};
|
|
44
|
+
/** West, south, east, north of every position of the features (RFC 7946 §5), or null when none has a shape. */
|
|
45
|
+
export declare function bbox(features: Feature<unknown>[]): number[] | null;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// GeoJSON (RFC 7946) ⇄ plain features.
|
|
2
|
+
//
|
|
3
|
+
// GeoJSON is what every web map, every GIS and every open-data portal
|
|
4
|
+
// reads, and the one interchange format of places: a place travels as a
|
|
5
|
+
// Feature whose properties carry its address, its precision and its source.
|
|
6
|
+
// This library declares the shapes of RFC 7946, writes features as one
|
|
7
|
+
// FeatureCollection with its bounding box and the foreign members a writer
|
|
8
|
+
// adds, and reads a document — a FeatureCollection, a Feature or a bare
|
|
9
|
+
// geometry — back into features, reporting what it cannot read rather than
|
|
10
|
+
// correcting it. It imports nothing, opens no connection and calls no
|
|
11
|
+
// network.
|
|
12
|
+
//
|
|
13
|
+
// SPDX-License-Identifier: MIT
|
|
14
|
+
const GEOMETRY_TYPES = new Set(['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon', 'GeometryCollection']);
|
|
15
|
+
/**
|
|
16
|
+
* Features as one FeatureCollection: a `bbox` around every position when
|
|
17
|
+
* there is one, then the features in the order given, then the foreign
|
|
18
|
+
* members. Coordinates are written as given: WGS 84 longitude, latitude
|
|
19
|
+
* and height, outer rings counter-clockwise, are the writer's to give.
|
|
20
|
+
*/
|
|
21
|
+
export function writeGeoJson(features, members) {
|
|
22
|
+
const box = bbox(features);
|
|
23
|
+
return { type: 'FeatureCollection', ...(box ? { bbox: box } : {}), features, ...(members ?? {}) };
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A GeoJSON document as features: a FeatureCollection gives its features,
|
|
27
|
+
* a Feature itself, a bare geometry a feature without properties. A member
|
|
28
|
+
* that is not a feature, or a geometry of no RFC 7946 type, is left out and
|
|
29
|
+
* reported. Refuses `not_geojson` for anything else.
|
|
30
|
+
*/
|
|
31
|
+
export function readGeoJson(input) {
|
|
32
|
+
const doc = (typeof input === 'string' ? JSON.parse(input) : input);
|
|
33
|
+
const warnings = [];
|
|
34
|
+
if (!doc || typeof doc !== 'object')
|
|
35
|
+
throw new Error('not_geojson: the document is not a GeoJSON object');
|
|
36
|
+
if (doc.type === 'FeatureCollection' && Array.isArray(doc.features)) {
|
|
37
|
+
const { type: _t, features, bbox: _b, ...members } = doc;
|
|
38
|
+
const out = [];
|
|
39
|
+
features.forEach((f, index) => {
|
|
40
|
+
const feature = f;
|
|
41
|
+
if (!feature || feature.type !== 'Feature') {
|
|
42
|
+
warnings.push({ code: 'not_a_feature', index, message: `member ${index} of the collection is not a Feature` });
|
|
43
|
+
}
|
|
44
|
+
else if (feature.geometry && !GEOMETRY_TYPES.has(feature.geometry.type)) {
|
|
45
|
+
warnings.push({ code: 'geometry_unknown', index, message: `feature ${index} has a geometry of type ${String(feature.geometry.type)}` });
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
out.push({ ...feature, geometry: feature.geometry ?? null, properties: feature.properties ?? {} });
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return { features: out, members, warnings };
|
|
52
|
+
}
|
|
53
|
+
if (doc.type === 'Feature') {
|
|
54
|
+
const feature = doc;
|
|
55
|
+
return { features: [{ ...feature, geometry: feature.geometry ?? null, properties: feature.properties ?? {} }], members: {}, warnings };
|
|
56
|
+
}
|
|
57
|
+
if (typeof doc.type === 'string' && GEOMETRY_TYPES.has(doc.type)) {
|
|
58
|
+
return { features: [{ type: 'Feature', geometry: doc, properties: {} }], members: {}, warnings };
|
|
59
|
+
}
|
|
60
|
+
throw new Error('not_geojson: neither a FeatureCollection, a Feature nor a geometry');
|
|
61
|
+
}
|
|
62
|
+
/** West, south, east, north of every position of the features (RFC 7946 §5), or null when none has a shape. */
|
|
63
|
+
export function bbox(features) {
|
|
64
|
+
let box = null;
|
|
65
|
+
const visit = (c) => {
|
|
66
|
+
if (!Array.isArray(c))
|
|
67
|
+
return;
|
|
68
|
+
if (typeof c[0] === 'number') {
|
|
69
|
+
const [x, y] = c;
|
|
70
|
+
box = box ? [Math.min(box[0], x), Math.min(box[1], y), Math.max(box[2], x), Math.max(box[3], y)] : [x, y, x, y];
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
for (const inner of c)
|
|
74
|
+
visit(inner);
|
|
75
|
+
};
|
|
76
|
+
const walk = (g) => {
|
|
77
|
+
if (!g)
|
|
78
|
+
return;
|
|
79
|
+
if (g.type === 'GeometryCollection')
|
|
80
|
+
(g.geometries ?? []).forEach(walk);
|
|
81
|
+
else
|
|
82
|
+
visit(g.coordinates);
|
|
83
|
+
};
|
|
84
|
+
for (const f of features)
|
|
85
|
+
walk(f.geometry);
|
|
86
|
+
return box;
|
|
87
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexgen-os/geojson",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "GeoJSON (RFC 7946) features written as a FeatureCollection with its bounding box, and read back with what could not be read reported. No database, no dependency.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/nexgen-os/nexgen-os.git",
|
|
9
|
+
"directory": "packages/formats/geojson"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=20"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
],
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc -p tsconfig.build.json"
|
|
33
|
+
}
|
|
34
|
+
}
|