@trackunit/geo-json-utils 1.12.27 → 1.12.29
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 +20 -5
- package/index.cjs.js +83 -0
- package/index.esm.js +79 -1
- package/package.json +1 -1
- package/src/GeoJsonRuntimeValidation.d.ts +38 -0
- package/src/index.d.ts +1 -0
package/README.md
CHANGED
|
@@ -1,17 +1,32 @@
|
|
|
1
1
|
# Trackunit geo-json-utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@trackunit/geo-json-utils` is Trackunit's shared GeoJSON utility package.
|
|
4
|
+
It contains runtime validation schemas, geometry helpers, and conversion utilities used by map adapters and other frontend libraries.
|
|
4
5
|
|
|
5
|
-
This library is
|
|
6
|
-
|
|
7
|
-
To browse all avaliable components visit the [Design System](https://design.iris.trackunit.com/).
|
|
6
|
+
This library is published for use in the Trackunit [Iris App SDK](https://www.npmjs.com/package/@trackunit/iris-app) and other npm consumers.
|
|
8
7
|
|
|
9
8
|
For more info and a full guide on Iris App SDK Development, please visit our [Developer Hub](https://developers.trackunit.com/).
|
|
10
9
|
|
|
10
|
+
## Exports
|
|
11
|
+
|
|
12
|
+
The package entry (`src/index.ts`) exports these module surfaces:
|
|
13
|
+
|
|
14
|
+
- **Runtime validation** (`GeoJsonRuntimeValidation`) — These helpers validate unknown input against GeoJSON schemas and log warnings for invalid values so callers can fail safely without throwing.
|
|
15
|
+
- **Geometry helpers** (`GeoJsonGeometryUtils`)
|
|
16
|
+
- **General GeoJSON utilities** (`GeoJsonUtils`)
|
|
17
|
+
- **Zod schemas** — GeoJSON (`GeoJsonSchemas`) and Trackunit-oriented variants (`TuGeoJsonSchemas`)
|
|
18
|
+
- **Meridian / longitude helpers** (`MeridianUtils`)
|
|
19
|
+
- **Bridge utilities** — Trackunit GeoJSON bridging (`TuGeoJsonBridgeUtils`)
|
|
20
|
+
- **Conversions** — Trackunit-oriented conversions (`TuGeoJsonConversions`)
|
|
21
|
+
|
|
11
22
|
## Development
|
|
12
23
|
|
|
13
24
|
At this point this library is only developed by Trackunit Employees.
|
|
14
|
-
|
|
25
|
+
Run library targets through Nx:
|
|
26
|
+
|
|
27
|
+
- `yarn nx run geo-json-utils:tsc-validate`
|
|
28
|
+
- `yarn nx run geo-json-utils:test`
|
|
29
|
+
- `yarn nx run geo-json-utils:build`
|
|
15
30
|
|
|
16
31
|
## Trackunit
|
|
17
32
|
|
package/index.cjs.js
CHANGED
|
@@ -455,6 +455,84 @@ const geoJsonFeatureCollectionSchema = zod.z.object({
|
|
|
455
455
|
bbox: geoJsonBboxSchema.optional(),
|
|
456
456
|
});
|
|
457
457
|
|
|
458
|
+
/**
|
|
459
|
+
* Empty FeatureCollection fallback used when runtime validation fails.
|
|
460
|
+
*/
|
|
461
|
+
const EMPTY_FEATURE_COLLECTION = {
|
|
462
|
+
type: "FeatureCollection",
|
|
463
|
+
features: [],
|
|
464
|
+
};
|
|
465
|
+
/**
|
|
466
|
+
* Validates a GeoJSON position value.
|
|
467
|
+
*
|
|
468
|
+
* @param value Unknown runtime value to validate.
|
|
469
|
+
* @param context Optional prefix used in warn messages.
|
|
470
|
+
* @returns {GeoJsonPosition | null} Parsed position when valid, otherwise null.
|
|
471
|
+
*/
|
|
472
|
+
const validatePosition = (value, context) => {
|
|
473
|
+
const result = geoJsonPositionSchema.safeParse(value);
|
|
474
|
+
if (result.success) {
|
|
475
|
+
return result.data;
|
|
476
|
+
}
|
|
477
|
+
const message = context ? `${context} ${result.error.message}` : result.error.message;
|
|
478
|
+
// eslint-disable-next-line no-console -- Intentional: warn developers when invalid GeoJSON position is passed; fallback to null
|
|
479
|
+
console.warn(message);
|
|
480
|
+
return null;
|
|
481
|
+
};
|
|
482
|
+
/**
|
|
483
|
+
* Validates a GeoJSON bounding box value.
|
|
484
|
+
*
|
|
485
|
+
* @param value Unknown runtime value to validate.
|
|
486
|
+
* @param context Optional prefix used in warn messages.
|
|
487
|
+
* @returns {GeoJsonBbox | null} Parsed bbox when valid, otherwise null.
|
|
488
|
+
*/
|
|
489
|
+
const validateBbox = (value, context) => {
|
|
490
|
+
const result = geoJsonBboxSchema.safeParse(value);
|
|
491
|
+
if (result.success) {
|
|
492
|
+
return result.data;
|
|
493
|
+
}
|
|
494
|
+
const message = context ? `${context} ${result.error.message}` : result.error.message;
|
|
495
|
+
// eslint-disable-next-line no-console -- Intentional: warn developers when invalid GeoJSON bbox is passed; fallback to null
|
|
496
|
+
console.warn(message);
|
|
497
|
+
return null;
|
|
498
|
+
};
|
|
499
|
+
/**
|
|
500
|
+
* Validates a GeoJSON bounding box and falls back to a provided default.
|
|
501
|
+
*
|
|
502
|
+
* @param value Unknown runtime value to validate.
|
|
503
|
+
* @param fallback Bbox returned when validation fails.
|
|
504
|
+
* @param context Optional prefix used in warn messages.
|
|
505
|
+
* @returns {GeoJsonBbox} Parsed bbox when valid, otherwise a copied fallback bbox.
|
|
506
|
+
*/
|
|
507
|
+
const validateBboxWithFallback = (value, fallback, context) => {
|
|
508
|
+
const result = geoJsonBboxSchema.safeParse(value);
|
|
509
|
+
if (result.success) {
|
|
510
|
+
return result.data;
|
|
511
|
+
}
|
|
512
|
+
const message = context ? `${context} ${result.error.message}` : result.error.message;
|
|
513
|
+
// eslint-disable-next-line no-console -- Intentional: warn developers when invalid GeoJSON bbox is passed; fallback to provided default
|
|
514
|
+
console.warn(message);
|
|
515
|
+
const copiedFallback = [fallback[0], fallback[1], fallback[2], fallback[3]];
|
|
516
|
+
return copiedFallback;
|
|
517
|
+
};
|
|
518
|
+
/**
|
|
519
|
+
* Validates a GeoJSON FeatureCollection value.
|
|
520
|
+
*
|
|
521
|
+
* @param value Unknown runtime value to validate.
|
|
522
|
+
* @param context Optional prefix used in warn messages.
|
|
523
|
+
* @returns {GeoJsonFeatureCollection | null} Parsed feature collection when valid, otherwise null.
|
|
524
|
+
*/
|
|
525
|
+
const validateFeatureCollection = (value, context) => {
|
|
526
|
+
const result = geoJsonFeatureCollectionSchema.safeParse(value);
|
|
527
|
+
if (result.success) {
|
|
528
|
+
return result.data;
|
|
529
|
+
}
|
|
530
|
+
const message = context ? `${context} ${result.error.message}` : result.error.message;
|
|
531
|
+
// eslint-disable-next-line no-console -- Intentional: warn developers when invalid GeoJSON FeatureCollection is passed; fallback to null
|
|
532
|
+
console.warn(message);
|
|
533
|
+
return null;
|
|
534
|
+
};
|
|
535
|
+
|
|
458
536
|
/**
|
|
459
537
|
* Utilities for handling coordinates and bounding boxes that cross the 180/-180 meridian.
|
|
460
538
|
*/
|
|
@@ -1442,6 +1520,7 @@ const tuGeoJsonRectangularBoxPolygonSchema = zod.z
|
|
|
1442
1520
|
});
|
|
1443
1521
|
|
|
1444
1522
|
exports.EARTH_RADIUS = EARTH_RADIUS;
|
|
1523
|
+
exports.EMPTY_FEATURE_COLLECTION = EMPTY_FEATURE_COLLECTION;
|
|
1445
1524
|
exports.boundingBoxCrossesMeridian = boundingBoxCrossesMeridian;
|
|
1446
1525
|
exports.checkCrossesMeridian = checkCrossesMeridian;
|
|
1447
1526
|
exports.computeGeometryCentroid = computeGeometryCentroid;
|
|
@@ -1489,3 +1568,7 @@ exports.toPosition2d = toPosition2d;
|
|
|
1489
1568
|
exports.tuGeoJsonPointRadiusSchema = tuGeoJsonPointRadiusSchema;
|
|
1490
1569
|
exports.tuGeoJsonPolygonNoHolesSchema = tuGeoJsonPolygonNoHolesSchema;
|
|
1491
1570
|
exports.tuGeoJsonRectangularBoxPolygonSchema = tuGeoJsonRectangularBoxPolygonSchema;
|
|
1571
|
+
exports.validateBbox = validateBbox;
|
|
1572
|
+
exports.validateBboxWithFallback = validateBboxWithFallback;
|
|
1573
|
+
exports.validateFeatureCollection = validateFeatureCollection;
|
|
1574
|
+
exports.validatePosition = validatePosition;
|
package/index.esm.js
CHANGED
|
@@ -453,6 +453,84 @@ const geoJsonFeatureCollectionSchema = z.object({
|
|
|
453
453
|
bbox: geoJsonBboxSchema.optional(),
|
|
454
454
|
});
|
|
455
455
|
|
|
456
|
+
/**
|
|
457
|
+
* Empty FeatureCollection fallback used when runtime validation fails.
|
|
458
|
+
*/
|
|
459
|
+
const EMPTY_FEATURE_COLLECTION = {
|
|
460
|
+
type: "FeatureCollection",
|
|
461
|
+
features: [],
|
|
462
|
+
};
|
|
463
|
+
/**
|
|
464
|
+
* Validates a GeoJSON position value.
|
|
465
|
+
*
|
|
466
|
+
* @param value Unknown runtime value to validate.
|
|
467
|
+
* @param context Optional prefix used in warn messages.
|
|
468
|
+
* @returns {GeoJsonPosition | null} Parsed position when valid, otherwise null.
|
|
469
|
+
*/
|
|
470
|
+
const validatePosition = (value, context) => {
|
|
471
|
+
const result = geoJsonPositionSchema.safeParse(value);
|
|
472
|
+
if (result.success) {
|
|
473
|
+
return result.data;
|
|
474
|
+
}
|
|
475
|
+
const message = context ? `${context} ${result.error.message}` : result.error.message;
|
|
476
|
+
// eslint-disable-next-line no-console -- Intentional: warn developers when invalid GeoJSON position is passed; fallback to null
|
|
477
|
+
console.warn(message);
|
|
478
|
+
return null;
|
|
479
|
+
};
|
|
480
|
+
/**
|
|
481
|
+
* Validates a GeoJSON bounding box value.
|
|
482
|
+
*
|
|
483
|
+
* @param value Unknown runtime value to validate.
|
|
484
|
+
* @param context Optional prefix used in warn messages.
|
|
485
|
+
* @returns {GeoJsonBbox | null} Parsed bbox when valid, otherwise null.
|
|
486
|
+
*/
|
|
487
|
+
const validateBbox = (value, context) => {
|
|
488
|
+
const result = geoJsonBboxSchema.safeParse(value);
|
|
489
|
+
if (result.success) {
|
|
490
|
+
return result.data;
|
|
491
|
+
}
|
|
492
|
+
const message = context ? `${context} ${result.error.message}` : result.error.message;
|
|
493
|
+
// eslint-disable-next-line no-console -- Intentional: warn developers when invalid GeoJSON bbox is passed; fallback to null
|
|
494
|
+
console.warn(message);
|
|
495
|
+
return null;
|
|
496
|
+
};
|
|
497
|
+
/**
|
|
498
|
+
* Validates a GeoJSON bounding box and falls back to a provided default.
|
|
499
|
+
*
|
|
500
|
+
* @param value Unknown runtime value to validate.
|
|
501
|
+
* @param fallback Bbox returned when validation fails.
|
|
502
|
+
* @param context Optional prefix used in warn messages.
|
|
503
|
+
* @returns {GeoJsonBbox} Parsed bbox when valid, otherwise a copied fallback bbox.
|
|
504
|
+
*/
|
|
505
|
+
const validateBboxWithFallback = (value, fallback, context) => {
|
|
506
|
+
const result = geoJsonBboxSchema.safeParse(value);
|
|
507
|
+
if (result.success) {
|
|
508
|
+
return result.data;
|
|
509
|
+
}
|
|
510
|
+
const message = context ? `${context} ${result.error.message}` : result.error.message;
|
|
511
|
+
// eslint-disable-next-line no-console -- Intentional: warn developers when invalid GeoJSON bbox is passed; fallback to provided default
|
|
512
|
+
console.warn(message);
|
|
513
|
+
const copiedFallback = [fallback[0], fallback[1], fallback[2], fallback[3]];
|
|
514
|
+
return copiedFallback;
|
|
515
|
+
};
|
|
516
|
+
/**
|
|
517
|
+
* Validates a GeoJSON FeatureCollection value.
|
|
518
|
+
*
|
|
519
|
+
* @param value Unknown runtime value to validate.
|
|
520
|
+
* @param context Optional prefix used in warn messages.
|
|
521
|
+
* @returns {GeoJsonFeatureCollection | null} Parsed feature collection when valid, otherwise null.
|
|
522
|
+
*/
|
|
523
|
+
const validateFeatureCollection = (value, context) => {
|
|
524
|
+
const result = geoJsonFeatureCollectionSchema.safeParse(value);
|
|
525
|
+
if (result.success) {
|
|
526
|
+
return result.data;
|
|
527
|
+
}
|
|
528
|
+
const message = context ? `${context} ${result.error.message}` : result.error.message;
|
|
529
|
+
// eslint-disable-next-line no-console -- Intentional: warn developers when invalid GeoJSON FeatureCollection is passed; fallback to null
|
|
530
|
+
console.warn(message);
|
|
531
|
+
return null;
|
|
532
|
+
};
|
|
533
|
+
|
|
456
534
|
/**
|
|
457
535
|
* Utilities for handling coordinates and bounding boxes that cross the 180/-180 meridian.
|
|
458
536
|
*/
|
|
@@ -1439,4 +1517,4 @@ const tuGeoJsonRectangularBoxPolygonSchema = z
|
|
|
1439
1517
|
}
|
|
1440
1518
|
});
|
|
1441
1519
|
|
|
1442
|
-
export { EARTH_RADIUS, boundingBoxCrossesMeridian, checkCrossesMeridian, computeGeometryCentroid, coordinatesToStandardFormat, denormalizeLongitude, extractEdges, extractFirstPointCoordinate, extractPositionsFromGeometry, geoJsonBboxSchema, geoJsonFeatureCollectionSchema, geoJsonFeatureSchema, geoJsonGeometryCollectionSchema, geoJsonGeometrySchema, geoJsonLineStringSchema, geoJsonLinearRingSchema, geoJsonMultiLineStringSchema, geoJsonMultiPointSchema, geoJsonMultiPolygonSchema, geoJsonPointSchema, geoJsonPolygonSchema, geoJsonPosition2dSchema, geoJsonPositionSchema, getBboxFromGeoJsonPolygon, getBoundingBoxFromGeoJsonBbox, getBoundingBoxFromGeoJsonPolygon, getExtremeGeoJsonPointFromPolygon, getGeoJsonPolygonFromBoundingBox, getGeoJsonPolygonIntersection, getMinMaxLongitudes, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getPointCoordinateFromGeoJsonPoint, getPolygonFromBbox, getPolygonFromPointAndRadius, isBboxInsideFeatureCollection, isFullyContainedInGeoJsonPolygon, isGeoJsonPointInPolygon, isGeoJsonPositionInLinearRing, isPositionInsideRing, normalizeLongitudes, splitPolygonAtAntimeridian, splitPolygonWithHolesAtAntimeridian, toFeatureCollection, toPosition2d, tuGeoJsonPointRadiusSchema, tuGeoJsonPolygonNoHolesSchema, tuGeoJsonRectangularBoxPolygonSchema };
|
|
1520
|
+
export { EARTH_RADIUS, EMPTY_FEATURE_COLLECTION, boundingBoxCrossesMeridian, checkCrossesMeridian, computeGeometryCentroid, coordinatesToStandardFormat, denormalizeLongitude, extractEdges, extractFirstPointCoordinate, extractPositionsFromGeometry, geoJsonBboxSchema, geoJsonFeatureCollectionSchema, geoJsonFeatureSchema, geoJsonGeometryCollectionSchema, geoJsonGeometrySchema, geoJsonLineStringSchema, geoJsonLinearRingSchema, geoJsonMultiLineStringSchema, geoJsonMultiPointSchema, geoJsonMultiPolygonSchema, geoJsonPointSchema, geoJsonPolygonSchema, geoJsonPosition2dSchema, geoJsonPositionSchema, getBboxFromGeoJsonPolygon, getBoundingBoxFromGeoJsonBbox, getBoundingBoxFromGeoJsonPolygon, getExtremeGeoJsonPointFromPolygon, getGeoJsonPolygonFromBoundingBox, getGeoJsonPolygonIntersection, getMinMaxLongitudes, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getPointCoordinateFromGeoJsonPoint, getPolygonFromBbox, getPolygonFromPointAndRadius, isBboxInsideFeatureCollection, isFullyContainedInGeoJsonPolygon, isGeoJsonPointInPolygon, isGeoJsonPositionInLinearRing, isPositionInsideRing, normalizeLongitudes, splitPolygonAtAntimeridian, splitPolygonWithHolesAtAntimeridian, toFeatureCollection, toPosition2d, tuGeoJsonPointRadiusSchema, tuGeoJsonPolygonNoHolesSchema, tuGeoJsonRectangularBoxPolygonSchema, validateBbox, validateBboxWithFallback, validateFeatureCollection, validatePosition };
|
package/package.json
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type GeoJsonBbox, type GeoJsonFeatureCollection, type GeoJsonPosition } from "./GeoJsonSchemas";
|
|
2
|
+
/**
|
|
3
|
+
* Empty FeatureCollection fallback used when runtime validation fails.
|
|
4
|
+
*/
|
|
5
|
+
export declare const EMPTY_FEATURE_COLLECTION: GeoJsonFeatureCollection;
|
|
6
|
+
/**
|
|
7
|
+
* Validates a GeoJSON position value.
|
|
8
|
+
*
|
|
9
|
+
* @param value Unknown runtime value to validate.
|
|
10
|
+
* @param context Optional prefix used in warn messages.
|
|
11
|
+
* @returns {GeoJsonPosition | null} Parsed position when valid, otherwise null.
|
|
12
|
+
*/
|
|
13
|
+
export declare const validatePosition: (value: unknown, context?: string) => GeoJsonPosition | null;
|
|
14
|
+
/**
|
|
15
|
+
* Validates a GeoJSON bounding box value.
|
|
16
|
+
*
|
|
17
|
+
* @param value Unknown runtime value to validate.
|
|
18
|
+
* @param context Optional prefix used in warn messages.
|
|
19
|
+
* @returns {GeoJsonBbox | null} Parsed bbox when valid, otherwise null.
|
|
20
|
+
*/
|
|
21
|
+
export declare const validateBbox: (value: unknown, context?: string) => GeoJsonBbox | null;
|
|
22
|
+
/**
|
|
23
|
+
* Validates a GeoJSON bounding box and falls back to a provided default.
|
|
24
|
+
*
|
|
25
|
+
* @param value Unknown runtime value to validate.
|
|
26
|
+
* @param fallback Bbox returned when validation fails.
|
|
27
|
+
* @param context Optional prefix used in warn messages.
|
|
28
|
+
* @returns {GeoJsonBbox} Parsed bbox when valid, otherwise a copied fallback bbox.
|
|
29
|
+
*/
|
|
30
|
+
export declare const validateBboxWithFallback: (value: unknown, fallback: GeoJsonBbox, context?: string) => GeoJsonBbox;
|
|
31
|
+
/**
|
|
32
|
+
* Validates a GeoJSON FeatureCollection value.
|
|
33
|
+
*
|
|
34
|
+
* @param value Unknown runtime value to validate.
|
|
35
|
+
* @param context Optional prefix used in warn messages.
|
|
36
|
+
* @returns {GeoJsonFeatureCollection | null} Parsed feature collection when valid, otherwise null.
|
|
37
|
+
*/
|
|
38
|
+
export declare const validateFeatureCollection: (value: unknown, context?: string) => GeoJsonFeatureCollection | null;
|