-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
geojson-binary.ts
84 lines (71 loc) · 2.43 KB
/
geojson-binary.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
73
74
75
76
77
78
79
80
81
82
83
84
// This module implement some utility functions to work with
// the geojson-binary format defined at loaders.gl:
// https://github.com/visgl/loaders.gl/blob/master/modules/gis/docs/api-reference/geojson-to-binary.md
import {BinaryAttribute} from '@deck.gl/core';
import {
BinaryFeatureCollection,
BinaryLineFeature,
BinaryPointFeature,
BinaryPolygonFeature,
Feature
} from '@loaders.gl/schema';
export type BinaryFeatureTypes = BinaryPointFeature | BinaryLineFeature | BinaryPolygonFeature;
export type ExtendedBinaryFeatureCollection = {
[P in keyof Omit<BinaryFeatureCollection, 'shape'>]: BinaryFeatureCollection[P] & {
attributes?: Record<string, BinaryAttribute>;
};
};
type FeaureOnlyProperties = Pick<Feature, 'properties'>;
/**
* Return the feature for an accesor
*/
export function binaryToFeatureForAccesor(
data: BinaryFeatureTypes,
index: number
): FeaureOnlyProperties | null {
if (!data) {
return null;
}
const featureIndex = 'startIndices' in data ? (data as any).startIndices[index] : index;
const geometryIndex = data.featureIds.value[featureIndex];
if (featureIndex !== -1) {
return getPropertiesForIndex(data, geometryIndex, featureIndex);
}
return null;
}
function getPropertiesForIndex(
data: BinaryFeatureTypes,
propertiesIndex: number,
numericPropsIndex: number
): FeaureOnlyProperties {
const feature = {
properties: {...data.properties[propertiesIndex]}
};
for (const prop in data.numericProps) {
feature.properties[prop] = data.numericProps[prop].value[numericPropsIndex];
}
return feature;
}
// Custom picking color to keep binary indexes
export function calculatePickingColors(
geojsonBinary: Required<ExtendedBinaryFeatureCollection>,
encodePickingColor: (id: number, result: number[]) => void
): Record<string, Uint8ClampedArray | null> {
const pickingColors: Record<string, Uint8ClampedArray | null> = {
points: null,
lines: null,
polygons: null
};
for (const key in pickingColors) {
const featureIds = geojsonBinary[key].globalFeatureIds.value;
pickingColors[key] = new Uint8ClampedArray(featureIds.length * 3);
const pickingColor = [];
for (let i = 0; i < featureIds.length; i++) {
encodePickingColor(featureIds[i], pickingColor);
pickingColors[key][i * 3 + 0] = pickingColor[0];
pickingColors[key][i * 3 + 1] = pickingColor[1];
pickingColors[key][i * 3 + 2] = pickingColor[2];
}
}
return pickingColors;
}