@trackunit/geo-json-utils 1.0.1 → 1.0.3
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 +21 -10
- package/index.esm.js +21 -11
- package/package.json +1 -1
- package/src/GeoJsonUtils.d.ts +6 -0
package/index.cjs.js
CHANGED
|
@@ -363,32 +363,30 @@ const getPointCoordinateFromGeoJsonPoint = (point) => {
|
|
|
363
363
|
* @returns {GeoJsonPoint} The extreme point in the given direction
|
|
364
364
|
*/
|
|
365
365
|
const getExtremeGeoJsonPointFromPolygon = ({ polygon, direction, }) => {
|
|
366
|
-
var _a, _b, _c;
|
|
367
366
|
const polygonParsed = geoJsonPolygonSchema.safeParse(polygon);
|
|
368
367
|
if (!polygonParsed.success) {
|
|
369
368
|
return null;
|
|
370
369
|
}
|
|
371
|
-
const firstPoint =
|
|
370
|
+
const firstPoint = polygonParsed.data.coordinates[0]?.[0];
|
|
372
371
|
if (!firstPoint) {
|
|
373
372
|
// Should never happen since the schema checks for it
|
|
374
373
|
return null;
|
|
375
374
|
}
|
|
376
|
-
const extremePosition =
|
|
377
|
-
var _a, _b, _c, _d;
|
|
375
|
+
const extremePosition = polygonParsed.data.coordinates[0]?.reduce((extremePoint, currentPoint) => {
|
|
378
376
|
switch (direction) {
|
|
379
377
|
case "top":
|
|
380
|
-
return currentPoint[1] > (
|
|
378
|
+
return currentPoint[1] > (extremePoint?.[1] ?? -Infinity) ? currentPoint : (extremePoint ?? currentPoint);
|
|
381
379
|
case "right":
|
|
382
|
-
return currentPoint[0] > (
|
|
380
|
+
return currentPoint[0] > (extremePoint?.[0] ?? -Infinity) ? currentPoint : (extremePoint ?? currentPoint);
|
|
383
381
|
case "bottom":
|
|
384
|
-
return currentPoint[1] < (
|
|
382
|
+
return currentPoint[1] < (extremePoint?.[1] ?? Infinity) ? currentPoint : (extremePoint ?? currentPoint);
|
|
385
383
|
case "left":
|
|
386
|
-
return currentPoint[0] < (
|
|
384
|
+
return currentPoint[0] < (extremePoint?.[0] ?? Infinity) ? currentPoint : (extremePoint ?? currentPoint);
|
|
387
385
|
default: {
|
|
388
386
|
throw new Error(`${direction} is not known`);
|
|
389
387
|
}
|
|
390
388
|
}
|
|
391
|
-
},
|
|
389
|
+
}, polygonParsed.data.coordinates[0]?.[0]);
|
|
392
390
|
return extremePosition
|
|
393
391
|
? {
|
|
394
392
|
type: "Point",
|
|
@@ -470,6 +468,18 @@ const getGeoJsonPolygonIntersection = (polygon1, polygon2) => {
|
|
|
470
468
|
coordinates: intersectionResult,
|
|
471
469
|
};
|
|
472
470
|
};
|
|
471
|
+
/**
|
|
472
|
+
* @description Creates a TU bounding box from a GeoJSON Bbox.
|
|
473
|
+
* @param bbox The GeoJSON Bbox to create a TU bounding box from
|
|
474
|
+
* @returns {TuBoundingBox} The TU bounding box created from the GeoJSON Bbox
|
|
475
|
+
*/
|
|
476
|
+
const getBoundingBoxFromGeoJsonBbox = (bbox) => {
|
|
477
|
+
const [minLon, minLat, maxLon, maxLat] = bbox;
|
|
478
|
+
return {
|
|
479
|
+
nw: { longitude: minLon, latitude: maxLat },
|
|
480
|
+
se: { longitude: maxLon, latitude: minLat },
|
|
481
|
+
};
|
|
482
|
+
};
|
|
473
483
|
|
|
474
484
|
//! These tools are used to bridge the gap with out poorly typed graphql types
|
|
475
485
|
// Should be ideally be avoided but are needed until we fix the graphql types
|
|
@@ -583,7 +593,7 @@ const tuGeoJsonRectangularBoxPolygonSchema = zod.z
|
|
|
583
593
|
.superRefine((data, ctx) => {
|
|
584
594
|
const coordinates = data.coordinates[0];
|
|
585
595
|
// Validate polygon has exactly 5 points
|
|
586
|
-
if (
|
|
596
|
+
if (coordinates?.length !== 5) {
|
|
587
597
|
ctx.addIssue({
|
|
588
598
|
code: zod.z.ZodIssueCode.custom,
|
|
589
599
|
message: "Polygon must have exactly 5 coordinates to form a closed box.",
|
|
@@ -630,6 +640,7 @@ exports.geoJsonPointSchema = geoJsonPointSchema;
|
|
|
630
640
|
exports.geoJsonPolygonSchema = geoJsonPolygonSchema;
|
|
631
641
|
exports.geoJsonPositionSchema = geoJsonPositionSchema;
|
|
632
642
|
exports.getBboxFromGeoJsonPolygon = getBboxFromGeoJsonPolygon;
|
|
643
|
+
exports.getBoundingBoxFromGeoJsonBbox = getBoundingBoxFromGeoJsonBbox;
|
|
633
644
|
exports.getBoundingBoxFromGeoJsonPolygon = getBoundingBoxFromGeoJsonPolygon;
|
|
634
645
|
exports.getExtremeGeoJsonPointFromPolygon = getExtremeGeoJsonPointFromPolygon;
|
|
635
646
|
exports.getGeoJsonPolygonFromBoundingBox = getGeoJsonPolygonFromBoundingBox;
|
package/index.esm.js
CHANGED
|
@@ -361,32 +361,30 @@ const getPointCoordinateFromGeoJsonPoint = (point) => {
|
|
|
361
361
|
* @returns {GeoJsonPoint} The extreme point in the given direction
|
|
362
362
|
*/
|
|
363
363
|
const getExtremeGeoJsonPointFromPolygon = ({ polygon, direction, }) => {
|
|
364
|
-
var _a, _b, _c;
|
|
365
364
|
const polygonParsed = geoJsonPolygonSchema.safeParse(polygon);
|
|
366
365
|
if (!polygonParsed.success) {
|
|
367
366
|
return null;
|
|
368
367
|
}
|
|
369
|
-
const firstPoint =
|
|
368
|
+
const firstPoint = polygonParsed.data.coordinates[0]?.[0];
|
|
370
369
|
if (!firstPoint) {
|
|
371
370
|
// Should never happen since the schema checks for it
|
|
372
371
|
return null;
|
|
373
372
|
}
|
|
374
|
-
const extremePosition =
|
|
375
|
-
var _a, _b, _c, _d;
|
|
373
|
+
const extremePosition = polygonParsed.data.coordinates[0]?.reduce((extremePoint, currentPoint) => {
|
|
376
374
|
switch (direction) {
|
|
377
375
|
case "top":
|
|
378
|
-
return currentPoint[1] > (
|
|
376
|
+
return currentPoint[1] > (extremePoint?.[1] ?? -Infinity) ? currentPoint : (extremePoint ?? currentPoint);
|
|
379
377
|
case "right":
|
|
380
|
-
return currentPoint[0] > (
|
|
378
|
+
return currentPoint[0] > (extremePoint?.[0] ?? -Infinity) ? currentPoint : (extremePoint ?? currentPoint);
|
|
381
379
|
case "bottom":
|
|
382
|
-
return currentPoint[1] < (
|
|
380
|
+
return currentPoint[1] < (extremePoint?.[1] ?? Infinity) ? currentPoint : (extremePoint ?? currentPoint);
|
|
383
381
|
case "left":
|
|
384
|
-
return currentPoint[0] < (
|
|
382
|
+
return currentPoint[0] < (extremePoint?.[0] ?? Infinity) ? currentPoint : (extremePoint ?? currentPoint);
|
|
385
383
|
default: {
|
|
386
384
|
throw new Error(`${direction} is not known`);
|
|
387
385
|
}
|
|
388
386
|
}
|
|
389
|
-
},
|
|
387
|
+
}, polygonParsed.data.coordinates[0]?.[0]);
|
|
390
388
|
return extremePosition
|
|
391
389
|
? {
|
|
392
390
|
type: "Point",
|
|
@@ -468,6 +466,18 @@ const getGeoJsonPolygonIntersection = (polygon1, polygon2) => {
|
|
|
468
466
|
coordinates: intersectionResult,
|
|
469
467
|
};
|
|
470
468
|
};
|
|
469
|
+
/**
|
|
470
|
+
* @description Creates a TU bounding box from a GeoJSON Bbox.
|
|
471
|
+
* @param bbox The GeoJSON Bbox to create a TU bounding box from
|
|
472
|
+
* @returns {TuBoundingBox} The TU bounding box created from the GeoJSON Bbox
|
|
473
|
+
*/
|
|
474
|
+
const getBoundingBoxFromGeoJsonBbox = (bbox) => {
|
|
475
|
+
const [minLon, minLat, maxLon, maxLat] = bbox;
|
|
476
|
+
return {
|
|
477
|
+
nw: { longitude: minLon, latitude: maxLat },
|
|
478
|
+
se: { longitude: maxLon, latitude: minLat },
|
|
479
|
+
};
|
|
480
|
+
};
|
|
471
481
|
|
|
472
482
|
//! These tools are used to bridge the gap with out poorly typed graphql types
|
|
473
483
|
// Should be ideally be avoided but are needed until we fix the graphql types
|
|
@@ -581,7 +591,7 @@ const tuGeoJsonRectangularBoxPolygonSchema = z
|
|
|
581
591
|
.superRefine((data, ctx) => {
|
|
582
592
|
const coordinates = data.coordinates[0];
|
|
583
593
|
// Validate polygon has exactly 5 points
|
|
584
|
-
if (
|
|
594
|
+
if (coordinates?.length !== 5) {
|
|
585
595
|
ctx.addIssue({
|
|
586
596
|
code: z.ZodIssueCode.custom,
|
|
587
597
|
message: "Polygon must have exactly 5 coordinates to form a closed box.",
|
|
@@ -612,4 +622,4 @@ const tuGeoJsonRectangularBoxPolygonSchema = z
|
|
|
612
622
|
}
|
|
613
623
|
});
|
|
614
624
|
|
|
615
|
-
export { EARTH_RADIUS, boundingBoxCrossesMeridian, checkCrossesMeridian, coordinatesToStandardFormat, denormalizeLongitude, geoJsonBboxSchema, geoJsonGeometrySchema, geoJsonLineStringSchema, geoJsonLinearRingSchema, geoJsonMultiLineStringSchema, geoJsonMultiPointSchema, geoJsonMultiPolygonSchema, geoJsonPointSchema, geoJsonPolygonSchema, geoJsonPositionSchema, getBboxFromGeoJsonPolygon, getBoundingBoxFromGeoJsonPolygon, getExtremeGeoJsonPointFromPolygon, getGeoJsonPolygonFromBoundingBox, getGeoJsonPolygonIntersection, getMinMaxLongitudes, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getPointCoordinateFromGeoJsonPoint, getPolygonFromBbox, getPolygonFromPointAndRadius, isFullyContainedInGeoJsonPolygon, isGeoJsonPointInPolygon, isGeoJsonPositionInLinearRing, normalizeLongitudes, tuGeoJsonPointRadiusSchema, tuGeoJsonPolygonNoHolesSchema, tuGeoJsonRectangularBoxPolygonSchema };
|
|
625
|
+
export { EARTH_RADIUS, boundingBoxCrossesMeridian, checkCrossesMeridian, coordinatesToStandardFormat, denormalizeLongitude, geoJsonBboxSchema, geoJsonGeometrySchema, geoJsonLineStringSchema, geoJsonLinearRingSchema, geoJsonMultiLineStringSchema, geoJsonMultiPointSchema, geoJsonMultiPolygonSchema, geoJsonPointSchema, geoJsonPolygonSchema, geoJsonPositionSchema, getBboxFromGeoJsonPolygon, getBoundingBoxFromGeoJsonBbox, getBoundingBoxFromGeoJsonPolygon, getExtremeGeoJsonPointFromPolygon, getGeoJsonPolygonFromBoundingBox, getGeoJsonPolygonIntersection, getMinMaxLongitudes, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getPointCoordinateFromGeoJsonPoint, getPolygonFromBbox, getPolygonFromPointAndRadius, isFullyContainedInGeoJsonPolygon, isGeoJsonPointInPolygon, isGeoJsonPositionInLinearRing, normalizeLongitudes, tuGeoJsonPointRadiusSchema, tuGeoJsonPolygonNoHolesSchema, tuGeoJsonRectangularBoxPolygonSchema };
|
package/package.json
CHANGED
package/src/GeoJsonUtils.d.ts
CHANGED
|
@@ -81,4 +81,10 @@ export declare const isFullyContainedInGeoJsonPolygon: (polygon1: GeoJsonPolygon
|
|
|
81
81
|
* @returns {(GeoJsonMultiPolygon | GeoJsonPolygon)} The intersection as either a Polygon (if one contains the other) or MultiPolygon
|
|
82
82
|
*/
|
|
83
83
|
export declare const getGeoJsonPolygonIntersection: (polygon1: GeoJsonPolygon | GeoJsonMultiPolygon, polygon2: GeoJsonPolygon | GeoJsonMultiPolygon) => GeoJsonMultiPolygon | GeoJsonPolygon | null;
|
|
84
|
+
/**
|
|
85
|
+
* @description Creates a TU bounding box from a GeoJSON Bbox.
|
|
86
|
+
* @param bbox The GeoJSON Bbox to create a TU bounding box from
|
|
87
|
+
* @returns {TuBoundingBox} The TU bounding box created from the GeoJSON Bbox
|
|
88
|
+
*/
|
|
89
|
+
export declare const getBoundingBoxFromGeoJsonBbox: (bbox: GeoJsonBbox) => TuBoundingBox;
|
|
84
90
|
export {};
|