Skip to content

Commit

Permalink
Fixed turf errors when checking within box
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Sep 8, 2023
1 parent 1700fa3 commit ff7340e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions shared/geo-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import bboxPolygon from '@turf/bbox-polygon'
import booleanWithin from '@turf/boolean-within'
import type { MultiPolygon } from '@turf/helpers'
import { featureCollection, multiPolygon, point } from '@turf/helpers'
import pointsWithinPolygon from '@turf/points-within-polygon'
import union from '@turf/union'
import booleanWithin from '@turf/boolean-within'
import type { BoundingBox, Point } from '../types/index.ts'

/**
Expand Down Expand Up @@ -41,9 +41,13 @@ function toPolygon(bbox: BoundingBox) {
export const toMultiPolygon = (bbox: BoundingBox) => multiPolygon(toPolygon(bbox).map((p => p.geometry.coordinates)))

/**
* Checks if a bounding box is within a multipolygon
* Checks if a bounding box is within a multipolygon.
* Since bounding boxes can cross the antimeridian, we need to check if any of the polygons created by toPolygon
* is within the multipolygon
*/
export const bBoxIsWithinArea = (bbox: BoundingBox, multiPoly?: MultiPolygon) => !multiPoly ? false : booleanWithin(toMultiPolygon(bbox), multiPoly)
export function bBoxIsWithinArea(bbox: BoundingBox, multiPoly?: MultiPolygon) {
return !multiPoly ? false : toPolygon(bbox).some(p => booleanWithin(p, multiPoly))
}

/**
* Adds a polygon (from a bounding box) to a multipolygon
Expand Down

0 comments on commit ff7340e

Please sign in to comment.