MapComplete/src/Logic/FeatureSource/Sources/MvtSource.ts

75 lines
2.2 KiB
TypeScript
Raw Normal View History

import { Feature as GeojsonFeature, Geometry } from "geojson"
2024-02-20 02:01:08 +01:00
import { Store, UIEventSource } from "../../UIEventSource"
import { FeatureSourceForTile, UpdatableFeatureSource } from "../FeatureSource"
2024-09-29 01:04:31 +02:00
import { MvtToGeojson } from "mvt-to-geojson"
2024-02-20 02:01:08 +01:00
export default class MvtSource implements FeatureSourceForTile, UpdatableFeatureSource {
2024-02-20 02:01:08 +01:00
public readonly features: Store<GeojsonFeature<Geometry, { [name: string]: any }>[]>
public readonly x: number
public readonly y: number
public readonly z: number
private readonly _url: string
private readonly _layerName: string
2024-02-20 02:01:08 +01:00
private readonly _features: UIEventSource<
GeojsonFeature<
Geometry,
{
[name: string]: any
}
>[]
> = new UIEventSource<GeojsonFeature<Geometry, { [p: string]: any }>[]>([])
private currentlyRunning: Promise<any>
2024-02-20 02:01:08 +01:00
constructor(
url: string,
x: number,
y: number,
z: number,
layerName?: string,
2024-09-29 01:04:31 +02:00
isActive?: Store<boolean>,
2024-02-20 02:01:08 +01:00
) {
this._url = url
this._layerName = layerName
this.x = x
this.y = y
this.z = z
this.updateAsync()
2024-02-20 02:01:08 +01:00
this.features = this._features.map(
(fs) => {
if (fs === undefined || isActive?.data === false) {
return []
}
return fs
},
2024-09-29 01:04:31 +02:00
[isActive],
2024-02-20 02:01:08 +01:00
)
}
async updateAsync() {
if (!this.currentlyRunning) {
this.currentlyRunning = this.download()
}
await this.currentlyRunning
}
private async download(): Promise<void> {
try {
const result = await fetch(this._url)
if (result.status !== 200) {
console.error("Could not download tile " + this._url)
return
}
const buffer = await result.arrayBuffer()
2024-09-29 01:04:31 +02:00
const features = await MvtToGeojson.fromBuffer(buffer, this.x, this.y, this.z)
this._features.setData(features)
} catch (e) {
2024-08-09 16:55:08 +02:00
console.error("Could not download MVT " + this._url + " tile due to", e)
}
}
}