mapcomplete/UI/ImportFlow/ImportUtils.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
1.7 KiB
TypeScript
Raw Normal View History

import { Store } from "../../Logic/UIEventSource"
import { GeoOperations } from "../../Logic/GeoOperations"
2022-07-08 03:14:55 +02:00
import { Feature, Geometry } from "@turf/turf"
export class ImportUtils {
2022-07-08 03:14:55 +02:00
public static partitionFeaturesIfNearby(
toPartitionFeatureCollection: { features: Feature<Geometry>[] },
compareWith: Store<{ features: Feature[] }>,
cutoffDistanceInMeters: Store<number>
): Store<{ hasNearby: Feature[]; noNearby: Feature[] }> {
return compareWith.map(
(osmData) => {
if (osmData?.features === undefined) {
return undefined
}
2022-01-26 21:40:38 +01:00
if (osmData.features.length === 0) {
return { noNearby: toPartitionFeatureCollection.features, hasNearby: [] }
}
const maxDist = cutoffDistanceInMeters.data
const hasNearby = []
const noNearby = []
for (const toImportElement of toPartitionFeatureCollection.features) {
const hasNearbyFeature = osmData.features.some(
(f) =>
2022-07-08 03:14:55 +02:00
maxDist >=
GeoOperations.distanceBetween(
<any>toImportElement.geometry.coordinates,
GeoOperations.centerpointCoordinates(f)
2022-09-08 21:40:48 +02:00
)
)
if (hasNearbyFeature) {
hasNearby.push(toImportElement)
} else {
noNearby.push(toImportElement)
2022-09-08 21:40:48 +02:00
}
}
return { hasNearby, noNearby }
},
[cutoffDistanceInMeters]
2022-09-08 21:40:48 +02:00
)
}
}