-
-
Notifications
You must be signed in to change notification settings - Fork 643
/
vectortile_to_geojson.ts
72 lines (62 loc) · 2.04 KB
/
vectortile_to_geojson.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import type {VectorTileFeature} from '@mapbox/vector-tile';
import type {LayerSpecification} from '@maplibre/maplibre-gl-style-spec';
/**
* A helper for type to omit a property from a type
*/
type DistributiveKeys<T> = T extends T ? keyof T : never;
/**
* A helper for type to omit a property from a type
*/
type DistributiveOmit<T, K extends DistributiveKeys<T>> = T extends unknown
? Omit<T, K>
: never;
/**
* An extended geojson feature used by the events to return data to the listener
*/
export type MapGeoJSONFeature = GeoJSONFeature & {
layer: DistributiveOmit<LayerSpecification, 'source'> & {source: string};
source: string;
sourceLayer?: string;
state: { [key: string]: any };
}
/**
* A geojson feature
*/
export class GeoJSONFeature {
type: 'Feature';
_geometry: GeoJSON.Geometry;
properties: { [name: string]: any };
id: number | string | undefined;
_vectorTileFeature: VectorTileFeature;
constructor(vectorTileFeature: VectorTileFeature, z: number, x: number, y: number, id: string | number | undefined) {
this.type = 'Feature';
this._vectorTileFeature = vectorTileFeature;
(vectorTileFeature as any)._z = z;
(vectorTileFeature as any)._x = x;
(vectorTileFeature as any)._y = y;
this.properties = vectorTileFeature.properties;
this.id = id;
}
get geometry(): GeoJSON.Geometry {
if (this._geometry === undefined) {
this._geometry = this._vectorTileFeature.toGeoJSON(
(this._vectorTileFeature as any)._x,
(this._vectorTileFeature as any)._y,
(this._vectorTileFeature as any)._z).geometry;
}
return this._geometry;
}
set geometry(g: GeoJSON.Geometry) {
this._geometry = g;
}
toJSON() {
const json: any = {
geometry: this.geometry
};
for (const i in this) {
if (i === '_geometry' || i === '_vectorTileFeature') continue;
json[i] = (this)[i];
}
return json;
}
}