First fully working version

This commit is contained in:
Pieter Vander Vennet 2024-02-19 15:38:46 +01:00
parent 995f48b97a
commit 1fa66e50f8
8 changed files with 73 additions and 88 deletions

View file

@ -4,12 +4,13 @@ import { BBox } from "../../BBox"
import { FeatureSource } from "../FeatureSource"
import FeatureSourceMerger from "../Sources/FeatureSourceMerger"
/***
* A tiled source which dynamically loads the required tiles at a fixed zoom level.
* A single featureSource will be initialized for every tile in view; which will later be merged into this featureSource
*/
export default class DynamicTileSource<Src extends FeatureSource = FeatureSource> extends FeatureSourceMerger<Src> {
export default class DynamicTileSource<
Src extends FeatureSource = FeatureSource
> extends FeatureSourceMerger<Src> {
/**
*
* @param zoomlevel If {z} is specified in the source, the 'zoomlevel' will be used as zoomlevel to download from
@ -28,10 +29,12 @@ export default class DynamicTileSource<Src extends FeatureSource = FeatureSource
},
options?: {
isActive?: Store<boolean>
},
zDiff?: number
}
) {
super()
const loadedTiles = new Set<number>()
const zDiff = options?.zDiff ?? 0
const neededTiles: Store<number[]> = Stores.ListStabilized(
mapProperties.bounds
.mapD(
@ -43,32 +46,32 @@ export default class DynamicTileSource<Src extends FeatureSource = FeatureSource
if (mapProperties.zoom.data < minzoom) {
return undefined
}
const z = Math.floor(zoomlevel.data)
const z = Math.floor(zoomlevel.data) + zDiff
const tileRange = Tiles.TileRangeBetween(
z,
bounds.getNorth(),
bounds.getEast(),
bounds.getSouth(),
bounds.getWest(),
bounds.getWest()
)
if (tileRange.total > 500) {
console.warn(
"Got a really big tilerange, bounds and location might be out of sync",
"Got a really big tilerange, bounds and location might be out of sync"
)
return undefined
}
const needed = Tiles.MapRange(tileRange, (x, y) =>
Tiles.tile_index(z, x, y),
Tiles.tile_index(z, x, y)
).filter((i) => !loadedTiles.has(i))
if (needed.length === 0) {
return undefined
}
return needed
},
[options?.isActive, mapProperties.zoom],
[options?.isActive, mapProperties.zoom]
)
.stabilized(250),
.stabilized(250)
)
neededTiles.addCallbackAndRunD((neededIndexes) => {
@ -79,5 +82,3 @@ export default class DynamicTileSource<Src extends FeatureSource = FeatureSource
})
}
}

View file

@ -24,12 +24,13 @@ export class SummaryTileSource extends DynamicTileSource {
}
) {
const layersSummed = layers.join("+")
const zDiff = 2
super(
zoomRounded,
0, // minzoom
(tileIndex) => {
const [z, x, y] = Tiles.tile_from_index(tileIndex)
const coordinates = Tiles.centerPointOf(z, x, y)
let coordinates = Tiles.centerPointOf(z, x, y)
const count = UIEventSource.FromPromiseWithErr(
Utils.downloadJson(`${cacheserver}/${layersSummed}/${z}/${x}/${y}.json`)
@ -50,6 +51,21 @@ export class SummaryTileSource extends DynamicTileSource {
if (counts === undefined || counts["total"] === 0) {
return SummaryTileSource.empty
}
const lat = counts["lat"]
const lon = counts["lon"]
const total = Utils.numberWithMetrixPrefix(Number(counts["total"]))
const tileBbox = new BBox(Tiles.tile_bounds_lon_lat(z, x, y))
if (!tileBbox.contains([lon, lat])) {
console.error(
"Average coordinate is outside of bbox!?",
lon,
lat,
tileBbox,
counts
)
} else {
coordinates = [lon, lat]
}
return [
{
type: "Feature",
@ -57,6 +73,7 @@ export class SummaryTileSource extends DynamicTileSource {
id: "summary_" + tileIndex,
summary: "yes",
...counts,
total,
layers: layersSummed,
},
geometry: {
@ -69,7 +86,8 @@ export class SummaryTileSource extends DynamicTileSource {
return new StaticFeatureSource(
features.map(
(f) => {
if (z !== zoomRounded.data) {
console.log("z, zdiff, rounded:", z, zDiff, zoomRounded.data)
if (z - zDiff !== zoomRounded.data) {
return SummaryTileSource.empty
}
return f
@ -79,7 +97,7 @@ export class SummaryTileSource extends DynamicTileSource {
)
},
mapProperties,
options
{ ...options, zDiff }
)
}
}