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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

92 lines
2.9 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"
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-10-19 14:44:55 +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-10-19 14:44:55 +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()
const features = MvtToGeojson.fromBuffer(buffer, this.x, this.y, this.z)
for (const feature of features) {
const properties = feature.properties
2024-10-19 14:44:55 +02:00
if (!properties["osm_type"]) {
continue
}
let type: string = "node"
switch (properties["osm_type"]) {
case "N":
type = "node"
break
case "W":
type = "way"
break
case "R":
type = "relation"
break
}
properties["id"] = type + "/" + properties["osm_id"]
delete properties["osm_id"]
delete properties["osm_type"]
}
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)
}
}
}