From bd1aec8b8da7ece66d102e4c11cd4b34144e8ccb Mon Sep 17 00:00:00 2001 From: maximo Date: Sat, 2 Sep 2023 17:29:27 +0700 Subject: [PATCH] Consider antimeridian when filtering by bounding box --- src/stores/cluster.ts | 5 ++++- src/stores/locations.ts | 15 +++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/stores/cluster.ts b/src/stores/cluster.ts index 423f6b65..cc9798a8 100644 --- a/src/stores/cluster.ts +++ b/src/stores/cluster.ts @@ -38,7 +38,10 @@ export const useCluster = defineStore('cluster', () => { const categoriesStr = toStr(selectedCategories.value) const currenciesStr = toStr(selectedCurrencies.value) for (const { boundingBox: { neLat: memNeLat, neLng: memNeLng, swLat: memSwLat, swLng: memSwLng }, clusters: memoizedCluster, categories, currencies } of existingData) { - const isWithinBoundingBox = neLat <= memNeLat && neLng <= memNeLng && swLat >= memSwLat && swLng >= memSwLng + const isWithinBoundingBox = neLng > swLng + ? neLat <= memNeLat && neLng <= memNeLng && swLat >= memSwLat && swLng >= memSwLng + : neLat <= memNeLat && (neLng <= memNeLng || neLng >= memSwLng) && swLat >= memSwLat // Consider anti-meridian + const hasSameCategories = toStr(categories) === categoriesStr const hasSameCurrencies = toStr(currencies) === currenciesStr if (isWithinBoundingBox && hasSameCategories && hasSameCurrencies) { diff --git a/src/stores/locations.ts b/src/stores/locations.ts index bd9dec07..7eaaeb1f 100644 --- a/src/stores/locations.ts +++ b/src/stores/locations.ts @@ -24,12 +24,7 @@ export const useLocations = defineStore('locations', () => { const locations = computed(() => { if (!currentBoundingBox.value) return [] - const { neLat, neLng, swLat, swLng } = currentBoundingBox.value - return [...locationsMap.values()].filter((location) => { - const { lat, lng } = location - const isWithinBoundingBox = lat <= neLat && lng <= neLng && lat >= swLat && lng >= swLng - return isWithinBoundingBox && includeLocation(location) - }) + return [...locationsMap.values()].filter(location => includeLocation(location, currentBoundingBox.value!)) }) async function getLocations(boundingBox: BoundingBox) { @@ -61,11 +56,15 @@ export const useLocations = defineStore('locations', () => { const { selectedCurrencies, selectedCategories } = storeToRefs(useFilters()) - function includeLocation({ category, accepts, sells }: Location) { + function includeLocation({ category, accepts, sells, lat, lng }: Location, { neLat, neLng, swLat, swLng }: BoundingBox) { + const isWithinBoundingBox = neLng > swLng + ? lat <= neLat && lng <= neLng && lat >= swLat && lng >= swLng + : lat <= neLat && (lng <= neLng || lng >= swLng) && lat >= swLat // Consider anti-meridian + const currencies = accepts.concat(sells) const isFilteredByCurrencies = selectedCurrencies.value.length === 0 || currencies.some(c => selectedCurrencies.value.includes(c)) const isFilteredByCategories = selectedCategories.value.length === 0 || selectedCategories.value.includes(category) - return isFilteredByCurrencies && isFilteredByCategories + return isWithinBoundingBox && isFilteredByCurrencies && isFilteredByCategories } const router = useRouter()