MapComplete/src/Logic/FeatureSource/TiledFeatureSource/SummaryTileSource.ts

102 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-02-15 17:39:59 +01:00
import DynamicTileSource from "./DynamicTileSource"
import { Store, UIEventSource } from "../../UIEventSource"
import { BBox } from "../../BBox"
import StaticFeatureSource from "../Sources/StaticFeatureSource"
import { Feature, Point } from "geojson"
import { Utils } from "../../../Utils"
import { Tiles } from "../../../Models/TileRange"
/**
* Provides features summarizing the total amount of features at a given location
*/
export class SummaryTileSource extends DynamicTileSource {
private static readonly empty = []
constructor(
cacheserver: string,
layers: string[],
zoomRounded: Store<number>,
mapProperties: {
bounds: Store<BBox>
zoom: Store<number>
},
options?: {
isActive?: Store<boolean>
}
) {
const layersSummed = layers.join("+")
2024-02-19 15:38:46 +01:00
const zDiff = 2
2024-02-15 17:39:59 +01:00
super(
zoomRounded,
0, // minzoom
(tileIndex) => {
const [z, x, y] = Tiles.tile_from_index(tileIndex)
2024-02-19 15:38:46 +01:00
let coordinates = Tiles.centerPointOf(z, x, y)
2024-02-20 02:56:23 +01:00
const url = `${cacheserver}/${layersSummed}/${z}/${x}/${y}.json`
const count = UIEventSource.FromPromiseWithErr(Utils.downloadJson(url))
2024-02-15 17:39:59 +01:00
const features: Store<Feature<Point>[]> = count.mapD((count) => {
if (count["error"] !== undefined) {
console.error(
"Could not download count for tile",
z,
x,
y,
"due to",
count["error"]
)
return SummaryTileSource.empty
}
const counts = count["success"]
if (counts === undefined || counts["total"] === 0) {
return SummaryTileSource.empty
}
2024-02-19 15:38:46 +01:00
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,
2024-02-20 02:56:23 +01:00
counts,
url
2024-02-19 15:38:46 +01:00
)
} else {
coordinates = [lon, lat]
}
2024-02-15 17:39:59 +01:00
return [
{
type: "Feature",
properties: {
id: "summary_" + tileIndex,
summary: "yes",
...counts,
2024-02-19 15:38:46 +01:00
total,
2024-02-15 17:39:59 +01:00
layers: layersSummed,
},
geometry: {
type: "Point",
coordinates,
},
},
]
})
return new StaticFeatureSource(
features.map(
(f) => {
2024-02-19 15:38:46 +01:00
if (z - zDiff !== zoomRounded.data) {
2024-02-15 17:39:59 +01:00
return SummaryTileSource.empty
}
return f
},
[zoomRounded]
)
)
},
mapProperties,
2024-02-19 15:38:46 +01:00
{ ...options, zDiff }
2024-02-15 17:39:59 +01:00
)
}
}