-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
layer-utils.ts
131 lines (115 loc) · 3.73 KB
/
layer-utils.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
import * as arrow from 'apache-arrow';
import {Feature, BBox} from 'geojson';
import {Field, FieldPair} from '@kepler.gl/types';
import {DataContainerInterface} from '@kepler.gl/utils';
import {
getBinaryGeometriesFromArrow,
parseGeometryFromArrow,
BinaryGeometriesFromArrowOptions
} from '@loaders.gl/arrow';
import {DeckGlGeoTypes, GeojsonDataMaps} from './geojson-layer/geojson-utils';
export function assignPointPairToLayerColumn(pair: FieldPair, hasAlt: boolean) {
const {lat, lng, alt} = pair.pair;
if (!hasAlt) {
return {lat, lng};
}
const defaultAltColumn = {value: null, fieldIdx: -1, optional: true};
return {
lat,
lng,
altitude: alt ? {...defaultAltColumn, ...alt} : defaultAltColumn
};
}
export type GeojsonLayerMetaProps = {
dataToFeature: GeojsonDataMaps;
featureTypes: DeckGlGeoTypes;
bounds: BBox | null;
fixedRadius: boolean;
centroids?: Array<number[] | null>;
};
export function getGeojsonLayerMetaFromArrow({
dataContainer,
getGeoColumn,
getGeoField,
chunkIndex
}: {
dataContainer: DataContainerInterface;
getGeoColumn: (dataContainer: DataContainerInterface) => unknown;
getGeoField: (dataContainer: DataContainerInterface) => Field | null;
chunkIndex?: number;
}): GeojsonLayerMetaProps {
const geoColumn = getGeoColumn(dataContainer) as arrow.Vector;
const arrowField = getGeoField(dataContainer);
const encoding = arrowField?.metadata?.get('ARROW:extension:name');
const options: BinaryGeometriesFromArrowOptions = {
...(chunkIndex !== undefined && chunkIndex >= 0
? {
chunkIndex,
chunkOffset: geoColumn.data[0].length * chunkIndex
}
: {}),
triangulate: true,
calculateMeanCenters: true
};
// create binary data from arrow data for GeoJsonLayer
const {binaryGeometries, featureTypes, bounds, meanCenters} = getBinaryGeometriesFromArrow(
geoColumn,
encoding,
options
);
// since there is no feature.properties.radius, we set fixedRadius to false
const fixedRadius = false;
return {
dataToFeature: binaryGeometries,
featureTypes,
bounds,
fixedRadius,
centroids: meanCenters
};
}
export function isLayerHoveredFromArrow(objectInfo, layerId: string): boolean {
// there could be multiple deck.gl layers created from multiple chunks in arrow table
// the objectInfo.layer id should be `${this.id}-${i}`
if (objectInfo?.picked) {
const deckLayerId = objectInfo?.layer?.props?.id;
return deckLayerId.startsWith(layerId);
}
return false;
}
export function getHoveredObjectFromArrow(
objectInfo,
dataContainer,
layerId,
columnAccessor,
fieldAccessor
): Feature | null {
// hover object returns the index of the object in the data array
// NOTE: this could be done in Deck.gl getPickingInfo(params) and binaryToGeojson()
if (isLayerHoveredFromArrow(objectInfo, layerId) && objectInfo.index >= 0 && dataContainer) {
const col = columnAccessor(dataContainer);
const rawGeometry = col?.get(objectInfo.index);
const field = fieldAccessor(dataContainer);
const encoding = field?.metadata?.get('ARROW:extension:name');
const hoveredFeature = parseGeometryFromArrow(rawGeometry, encoding);
const properties = dataContainer.rowAsArray(objectInfo.index).reduce((prev, cur, i) => {
const fieldName = dataContainer?.getField?.(i).name;
if (fieldName !== field.name) {
prev[fieldName] = cur;
}
return prev;
}, {});
return hoveredFeature
? {
type: 'Feature',
geometry: hoveredFeature,
properties: {
...properties,
index: objectInfo.index
}
}
: null;
}
return null;
}