Skip to content

Commit

Permalink
updated radius arg for cluster alg
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Sep 11, 2023
1 parent b22a9d3 commit 3fa0540
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion shared/compute-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Location } from '../types/location.ts'
import { toPoint } from './index.ts'

export const CLUSTERS_MAX_ZOOM = 17
export const algorithm = new Supercluster({ radius: 88, maxZoom: CLUSTERS_MAX_ZOOM })
export const algorithm = (radius: number) => new Supercluster({ radius, maxZoom: CLUSTERS_MAX_ZOOM })

export function computeCluster(algorithm: Supercluster, locations: Location[], { zoom, boundingBox: bbox }: ClusterArea): ComputedClusterSet {
const singles: Location[] = []
Expand Down
2 changes: 1 addition & 1 deletion src/stores/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const useCluster = defineStore('cluster', () => {

async function getClusterFromClient(): Promise<ComputedClusterSet> {
const locations = await getLocations(boundingBox.value!)
return computeCluster(algorithm, locations, { boundingBox: boundingBox.value!, zoom: zoom.value })
return computeCluster(algorithm(80), locations, { boundingBox: boundingBox.value!, zoom: zoom.value })
}

async function getClusterFromDatabase(): Promise<ComputedClusterSet> {
Expand Down
2 changes: 1 addition & 1 deletion supabase/deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"lock": false,
"tasks": {
"run": "deno run --allow-env --allow-read --allow-net ./functions/generate-locations-clusters-set/index.ts",
"deploy": "npx supabase functions deploy generate-locations-clusters-set --import-map supabase/import_map.json --no-verify-jwt",
"deploy": "npx supabase functions deploy generate-locations-clusters-set --import-map supabase/import_map.json",
"og:dev": "npx supabase functions serve og-image --no-verify-jwt --env-file .env",
"og:deploy": "npx supabase functions deploy og-image --import-map supabase/import_map.json"
}
Expand Down
5 changes: 4 additions & 1 deletion supabase/functions/generate-locations-clusters-set/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ const locations = await getLocations(dbArgs, bbox)
const minZoom = Number(Deno.env.get('MIN_ZOOM'))
const maxZoom = Number(Deno.env.get('MAX_ZOOM'))

const radius: Record<number /* minZoom, maxZoom */, number /* radius in minZoom is 120, maxZoom is 150 */>
= Array.from({ length: maxZoom - minZoom + 1 }, (_, i) => 120 + i * 30 / (maxZoom - minZoom)).reduce((acc, radius, i) => ({ ...acc, [minZoom + i]: radius }), {})

const promises: Promise<unknown>[] = []
for (let zoom = minZoom; zoom <= maxZoom; zoom++) {
const res = computeCluster(algorithm, locations, { zoom, boundingBox: bbox })
const res = computeCluster(algorithm(radius[zoom]), locations, { zoom, boundingBox: bbox })
const singles: InsertLocationsClustersSetParamsItem[] = (res.singles as Location[]).map(({ lng, lat, uuid }) => ({ lat, lng, count: 1, locationUuid: uuid }))
promises.push(insertLocationsClusterSet(dbArgs, {
zoom_level: zoom,
Expand Down

0 comments on commit 3fa0540

Please sign in to comment.