2022-06-05 02:24:14 +02:00
|
|
|
import { Store } from "../../Logic/UIEventSource"
|
2022-01-21 01:57:16 +01:00
|
|
|
import { GeoOperations } from "../../Logic/GeoOperations"
|
2023-01-12 01:16:22 +01:00
|
|
|
import { Feature, Point } from "geojson"
|
2022-01-21 01:57:16 +01:00
|
|
|
|
|
|
|
export class ImportUtils {
|
2022-07-08 03:14:55 +02:00
|
|
|
public static partitionFeaturesIfNearby(
|
2023-01-12 01:16:22 +01:00
|
|
|
toPartitionFeatureCollection: { features: Feature[] },
|
2022-07-08 03:14:55 +02:00
|
|
|
compareWith: Store<{ features: Feature[] }>,
|
|
|
|
cutoffDistanceInMeters: Store<number>
|
|
|
|
): Store<{ hasNearby: Feature[]; noNearby: Feature[] }> {
|
2022-01-21 01:57:16 +01:00
|
|
|
return compareWith.map(
|
|
|
|
(osmData) => {
|
|
|
|
if (osmData?.features === undefined) {
|
|
|
|
return undefined
|
|
|
|
}
|
2022-01-26 21:40:38 +01:00
|
|
|
if (osmData.features.length === 0) {
|
2022-01-22 02:56:35 +01:00
|
|
|
return { noNearby: toPartitionFeatureCollection.features, hasNearby: [] }
|
|
|
|
}
|
2022-01-21 01:57:16 +01:00
|
|
|
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(
|
2023-01-12 01:16:22 +01:00
|
|
|
<any>(<Point>toImportElement.geometry).coordinates,
|
2022-07-08 03:14:55 +02:00
|
|
|
GeoOperations.centerpointCoordinates(f)
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
|
|
|
)
|
2022-01-21 01:57:16 +01:00
|
|
|
if (hasNearbyFeature) {
|
|
|
|
hasNearby.push(toImportElement)
|
|
|
|
} else {
|
|
|
|
noNearby.push(toImportElement)
|
2022-09-08 21:40:48 +02:00
|
|
|
}
|
2022-01-21 01:57:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return { hasNearby, noNearby }
|
|
|
|
},
|
|
|
|
[cutoffDistanceInMeters]
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-01-21 01:57:16 +01:00
|
|
|
}
|
|
|
|
}
|