@trackunit/shared-utils 0.0.82 → 0.0.84
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/index.cjs.js +42 -7
- package/index.esm.js +41 -8
- package/package.json +1 -1
- package/src/GeoJson/GeoJsonUtils.d.ts +12 -1
package/index.cjs.js
CHANGED
|
@@ -589,6 +589,31 @@ const geoJsonRectangularBoxPolygonSchema = zod.z
|
|
|
589
589
|
});
|
|
590
590
|
|
|
591
591
|
const EARTH_RADIUS = 6378137; // Earth’s mean radius in meters
|
|
592
|
+
const isDoubleNestedCoords = (coords) => Array.isArray(coords) &&
|
|
593
|
+
Array.isArray(coords[0]) &&
|
|
594
|
+
Array.isArray(coords[0][0]) &&
|
|
595
|
+
typeof coords[0][0][0] === "number";
|
|
596
|
+
const isSingleCoords = (coords) => typeof coords[0] === "number";
|
|
597
|
+
/**
|
|
598
|
+
* @description Returns coordinates in consistent format
|
|
599
|
+
* @param inconsistentCoordinates Single point, array of points or nested array of points
|
|
600
|
+
* @returns GeoPoint[]
|
|
601
|
+
*/
|
|
602
|
+
const coordinatesToStandardFormat = (inconsistentCoordinates) => {
|
|
603
|
+
if (!inconsistentCoordinates) {
|
|
604
|
+
return [];
|
|
605
|
+
}
|
|
606
|
+
if (isSingleCoords(inconsistentCoordinates)) {
|
|
607
|
+
return [inconsistentCoordinates];
|
|
608
|
+
}
|
|
609
|
+
if (isDoubleNestedCoords(inconsistentCoordinates)) {
|
|
610
|
+
return inconsistentCoordinates[0] || [];
|
|
611
|
+
}
|
|
612
|
+
if (inconsistentCoordinates[0] && typeof inconsistentCoordinates[0][0] === "number") {
|
|
613
|
+
return inconsistentCoordinates;
|
|
614
|
+
}
|
|
615
|
+
return [];
|
|
616
|
+
};
|
|
592
617
|
/**
|
|
593
618
|
* @description Extracts a point coordinate from a GeoJSON object.
|
|
594
619
|
* @param geoObject A GeoJSON object.
|
|
@@ -605,7 +630,13 @@ const getPointCoordinateFromGeoJsonObject = (geoObject) => {
|
|
|
605
630
|
Array.isArray(geoObject.coordinates) &&
|
|
606
631
|
typeof geoObject.coordinates[0] === "number" &&
|
|
607
632
|
typeof geoObject.coordinates[1] === "number") {
|
|
608
|
-
|
|
633
|
+
const [point] = coordinatesToStandardFormat(geoObject.coordinates);
|
|
634
|
+
if (point) {
|
|
635
|
+
return { latitude: point[1], longitude: point[0] };
|
|
636
|
+
}
|
|
637
|
+
else {
|
|
638
|
+
throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
|
|
639
|
+
}
|
|
609
640
|
}
|
|
610
641
|
else {
|
|
611
642
|
throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
|
|
@@ -624,12 +655,8 @@ const getMultipleCoordinatesFromGeoJsonObject = (geoObject) => {
|
|
|
624
655
|
else if ("geometry" in geoObject) {
|
|
625
656
|
return getMultipleCoordinatesFromGeoJsonObject(geoObject.geometry);
|
|
626
657
|
}
|
|
627
|
-
else if ("coordinates" in geoObject
|
|
628
|
-
|
|
629
|
-
Array.isArray(geoObject.coordinates[0])) {
|
|
630
|
-
// @ts-ignore - suppressImplicitAnyIndexErrors
|
|
631
|
-
// We would not be here if element was not an array.
|
|
632
|
-
return geoObject.coordinates.map(element => ({ longitude: element[0], latitude: element[1] }));
|
|
658
|
+
else if ("coordinates" in geoObject) {
|
|
659
|
+
return coordinatesToStandardFormat(geoObject.coordinates).map(([longitude, latitude]) => ({ longitude, latitude }));
|
|
633
660
|
}
|
|
634
661
|
else {
|
|
635
662
|
throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
|
|
@@ -716,6 +743,12 @@ const getGeoJsonPolygonFromBoundingBox = (boundingBox) => {
|
|
|
716
743
|
],
|
|
717
744
|
};
|
|
718
745
|
};
|
|
746
|
+
/**
|
|
747
|
+
* @description Creates TU point coordinate from a GeoJSON Point.
|
|
748
|
+
*/
|
|
749
|
+
const getPointCoordinateFromGeoJsonPoint = (point) => {
|
|
750
|
+
return { latitude: point.coordinates[1], longitude: point.coordinates[0] };
|
|
751
|
+
};
|
|
719
752
|
|
|
720
753
|
/**
|
|
721
754
|
* Group an array of items by a key.
|
|
@@ -1374,6 +1407,7 @@ exports.capitalize = capitalize;
|
|
|
1374
1407
|
exports.convertBlobToBase64 = convertBlobToBase64;
|
|
1375
1408
|
exports.convertMetersToYards = convertMetersToYards;
|
|
1376
1409
|
exports.convertYardsToMeters = convertYardsToMeters;
|
|
1410
|
+
exports.coordinatesToStandardFormat = coordinatesToStandardFormat;
|
|
1377
1411
|
exports.dateCompare = dateCompare;
|
|
1378
1412
|
exports.deleteUndefinedKeys = deleteUndefinedKeys;
|
|
1379
1413
|
exports.difference = difference;
|
|
@@ -1406,6 +1440,7 @@ exports.getGeoJsonPolygonFromBoundingBox = getGeoJsonPolygonFromBoundingBox;
|
|
|
1406
1440
|
exports.getISOStringFromDate = getISOStringFromDate;
|
|
1407
1441
|
exports.getMultipleCoordinatesFromGeoJsonObject = getMultipleCoordinatesFromGeoJsonObject;
|
|
1408
1442
|
exports.getPointCoordinateFromGeoJsonObject = getPointCoordinateFromGeoJsonObject;
|
|
1443
|
+
exports.getPointCoordinateFromGeoJsonPoint = getPointCoordinateFromGeoJsonPoint;
|
|
1409
1444
|
exports.getPolygonFromBbox = getPolygonFromBbox;
|
|
1410
1445
|
exports.getPolygonFromPointAndRadius = getPolygonFromPointAndRadius;
|
|
1411
1446
|
exports.getResizedDimensions = getResizedDimensions;
|
package/index.esm.js
CHANGED
|
@@ -587,6 +587,31 @@ const geoJsonRectangularBoxPolygonSchema = z
|
|
|
587
587
|
});
|
|
588
588
|
|
|
589
589
|
const EARTH_RADIUS = 6378137; // Earth’s mean radius in meters
|
|
590
|
+
const isDoubleNestedCoords = (coords) => Array.isArray(coords) &&
|
|
591
|
+
Array.isArray(coords[0]) &&
|
|
592
|
+
Array.isArray(coords[0][0]) &&
|
|
593
|
+
typeof coords[0][0][0] === "number";
|
|
594
|
+
const isSingleCoords = (coords) => typeof coords[0] === "number";
|
|
595
|
+
/**
|
|
596
|
+
* @description Returns coordinates in consistent format
|
|
597
|
+
* @param inconsistentCoordinates Single point, array of points or nested array of points
|
|
598
|
+
* @returns GeoPoint[]
|
|
599
|
+
*/
|
|
600
|
+
const coordinatesToStandardFormat = (inconsistentCoordinates) => {
|
|
601
|
+
if (!inconsistentCoordinates) {
|
|
602
|
+
return [];
|
|
603
|
+
}
|
|
604
|
+
if (isSingleCoords(inconsistentCoordinates)) {
|
|
605
|
+
return [inconsistentCoordinates];
|
|
606
|
+
}
|
|
607
|
+
if (isDoubleNestedCoords(inconsistentCoordinates)) {
|
|
608
|
+
return inconsistentCoordinates[0] || [];
|
|
609
|
+
}
|
|
610
|
+
if (inconsistentCoordinates[0] && typeof inconsistentCoordinates[0][0] === "number") {
|
|
611
|
+
return inconsistentCoordinates;
|
|
612
|
+
}
|
|
613
|
+
return [];
|
|
614
|
+
};
|
|
590
615
|
/**
|
|
591
616
|
* @description Extracts a point coordinate from a GeoJSON object.
|
|
592
617
|
* @param geoObject A GeoJSON object.
|
|
@@ -603,7 +628,13 @@ const getPointCoordinateFromGeoJsonObject = (geoObject) => {
|
|
|
603
628
|
Array.isArray(geoObject.coordinates) &&
|
|
604
629
|
typeof geoObject.coordinates[0] === "number" &&
|
|
605
630
|
typeof geoObject.coordinates[1] === "number") {
|
|
606
|
-
|
|
631
|
+
const [point] = coordinatesToStandardFormat(geoObject.coordinates);
|
|
632
|
+
if (point) {
|
|
633
|
+
return { latitude: point[1], longitude: point[0] };
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
|
|
637
|
+
}
|
|
607
638
|
}
|
|
608
639
|
else {
|
|
609
640
|
throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
|
|
@@ -622,12 +653,8 @@ const getMultipleCoordinatesFromGeoJsonObject = (geoObject) => {
|
|
|
622
653
|
else if ("geometry" in geoObject) {
|
|
623
654
|
return getMultipleCoordinatesFromGeoJsonObject(geoObject.geometry);
|
|
624
655
|
}
|
|
625
|
-
else if ("coordinates" in geoObject
|
|
626
|
-
|
|
627
|
-
Array.isArray(geoObject.coordinates[0])) {
|
|
628
|
-
// @ts-ignore - suppressImplicitAnyIndexErrors
|
|
629
|
-
// We would not be here if element was not an array.
|
|
630
|
-
return geoObject.coordinates.map(element => ({ longitude: element[0], latitude: element[1] }));
|
|
656
|
+
else if ("coordinates" in geoObject) {
|
|
657
|
+
return coordinatesToStandardFormat(geoObject.coordinates).map(([longitude, latitude]) => ({ longitude, latitude }));
|
|
631
658
|
}
|
|
632
659
|
else {
|
|
633
660
|
throw new Error(`Unable to extract point coordinate from ${JSON.stringify(geoObject)}`);
|
|
@@ -714,6 +741,12 @@ const getGeoJsonPolygonFromBoundingBox = (boundingBox) => {
|
|
|
714
741
|
],
|
|
715
742
|
};
|
|
716
743
|
};
|
|
744
|
+
/**
|
|
745
|
+
* @description Creates TU point coordinate from a GeoJSON Point.
|
|
746
|
+
*/
|
|
747
|
+
const getPointCoordinateFromGeoJsonPoint = (point) => {
|
|
748
|
+
return { latitude: point.coordinates[1], longitude: point.coordinates[0] };
|
|
749
|
+
};
|
|
717
750
|
|
|
718
751
|
/**
|
|
719
752
|
* Group an array of items by a key.
|
|
@@ -1361,4 +1394,4 @@ const uuidv4 = () => {
|
|
|
1361
1394
|
*/
|
|
1362
1395
|
const uuidv5 = (name, namespace) => v5(name, namespace);
|
|
1363
1396
|
|
|
1364
|
-
export { DateTimeFormat, EARTH_RADIUS, HoursAndMinutesFormat, align, alphabeticallySort, arrayLengthCompare, arrayNotEmpty, booleanCompare, capitalize, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, dateCompare, deleteUndefinedKeys, difference, doNothing, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, geoJsonBboxSchema, geoJsonGeometrySchema, geoJsonLineStringSchema, geoJsonLinearRingSchema, geoJsonMultiLineStringSchema, geoJsonMultiPointSchema, geoJsonMultiPolygonSchema, geoJsonPointSchema, geoJsonPolygonNoHolesSchema, geoJsonPolygonSchema, geoJsonPositionSchema, geoJsonRectangularBoxPolygonSchema, getBoundingBoxFromGeoJsonPolygon, getDifferenceBetweenDates, getEndOfDay, getFirstLevelObjectPropertyDifferences, getGeoJsonPolygonFromBoundingBox, getISOStringFromDate, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getPolygonFromBbox, getPolygonFromPointAndRadius, getResizedDimensions, getStartOfDay, groupBy, groupTinyDataToOthers, hourIntervals, intersection, isArrayEqual, isSorted, isUUID, isValidImage, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, objectEntries, objectFromEntries, objectKeys, objectValues, pick, removeLeftPadding, resizeBlob, resizeImage, size, stringCompare, stringCompareFromKey, stringNaturalCompare, stripHiddenCharacters, titleCase, toID, toIDs, toUUID, trimIds, trimPath, truthy, unionArraysByKey, uuidv3, uuidv4, uuidv5 };
|
|
1397
|
+
export { DateTimeFormat, EARTH_RADIUS, HoursAndMinutesFormat, align, alphabeticallySort, arrayLengthCompare, arrayNotEmpty, booleanCompare, capitalize, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, coordinatesToStandardFormat, dateCompare, deleteUndefinedKeys, difference, doNothing, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, geoJsonBboxSchema, geoJsonGeometrySchema, geoJsonLineStringSchema, geoJsonLinearRingSchema, geoJsonMultiLineStringSchema, geoJsonMultiPointSchema, geoJsonMultiPolygonSchema, geoJsonPointSchema, geoJsonPolygonNoHolesSchema, geoJsonPolygonSchema, geoJsonPositionSchema, geoJsonRectangularBoxPolygonSchema, getBoundingBoxFromGeoJsonPolygon, getDifferenceBetweenDates, getEndOfDay, getFirstLevelObjectPropertyDifferences, getGeoJsonPolygonFromBoundingBox, getISOStringFromDate, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getPointCoordinateFromGeoJsonPoint, getPolygonFromBbox, getPolygonFromPointAndRadius, getResizedDimensions, getStartOfDay, groupBy, groupTinyDataToOthers, hourIntervals, intersection, isArrayEqual, isSorted, isUUID, isValidImage, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, objectEntries, objectFromEntries, objectKeys, objectValues, pick, removeLeftPadding, resizeBlob, resizeImage, size, stringCompare, stringCompareFromKey, stringNaturalCompare, stripHiddenCharacters, titleCase, toID, toIDs, toUUID, trimIds, trimPath, truthy, unionArraysByKey, uuidv3, uuidv4, uuidv5 };
|
package/package.json
CHANGED
|
@@ -8,14 +8,21 @@ interface BoundingBox {
|
|
|
8
8
|
nw: PointCoordinate;
|
|
9
9
|
se: PointCoordinate;
|
|
10
10
|
}
|
|
11
|
+
export type GeoPoint = [number, number];
|
|
11
12
|
interface GeoJSONGeometry {
|
|
12
13
|
type?: unknown;
|
|
13
|
-
coordinates?:
|
|
14
|
+
coordinates?: GeoPoint | GeoPoint[] | GeoPoint[][] | null;
|
|
14
15
|
}
|
|
15
16
|
interface GeoJsonFeature {
|
|
16
17
|
type?: unknown;
|
|
17
18
|
geometry?: GeoJSONGeometry | null;
|
|
18
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* @description Returns coordinates in consistent format
|
|
22
|
+
* @param inconsistentCoordinates Single point, array of points or nested array of points
|
|
23
|
+
* @returns GeoPoint[]
|
|
24
|
+
*/
|
|
25
|
+
export declare const coordinatesToStandardFormat: (inconsistentCoordinates: GeoPoint | GeoPoint[] | GeoPoint[][] | null | undefined) => GeoPoint[];
|
|
19
26
|
/**
|
|
20
27
|
* @description Extracts a point coordinate from a GeoJSON object.
|
|
21
28
|
* @param geoObject A GeoJSON object.
|
|
@@ -45,4 +52,8 @@ export declare const getBoundingBoxFromGeoJsonPolygon: (polygon: GeoJsonPolygon)
|
|
|
45
52
|
* @description Creates a GeoJSON Polygon from a TU bounding box.
|
|
46
53
|
*/
|
|
47
54
|
export declare const getGeoJsonPolygonFromBoundingBox: (boundingBox: BoundingBox) => GeoJsonPolygon;
|
|
55
|
+
/**
|
|
56
|
+
* @description Creates TU point coordinate from a GeoJSON Point.
|
|
57
|
+
*/
|
|
58
|
+
export declare const getPointCoordinateFromGeoJsonPoint: (point: GeoJsonPoint) => PointCoordinate;
|
|
48
59
|
export {};
|