-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
geojson-layer-props.ts
145 lines (127 loc) · 4.59 KB
/
geojson-layer-props.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {BinaryAttribute, LayerData, LayerProps} from '@deck.gl/core';
import {PolygonLayerProps, ScatterplotLayerProps} from '..';
import {calculatePickingColors} from './geojson-binary';
import {BinaryFeatures} from '@loaders.gl/schema';
import {SeparatedGeometries} from './geojson';
// TODO: PathLayer is not yet typed
type PathLayerProps = LayerProps & Record<string, any>;
export type SubLayersProps = {
points: Partial<ScatterplotLayerProps>;
lines: Partial<PathLayerProps>;
polygons: Partial<PolygonLayerProps>;
polygonsOutline: Partial<PathLayerProps>;
};
type ExtendedBinaryFeatures = {
[P in keyof BinaryFeatures]: BinaryFeatures[P] & {attributes?: Record<string, BinaryAttribute>};
};
function createEmptyLayerProps(): SubLayersProps {
return {
points: {},
lines: {},
polygons: {},
polygonsOutline: {}
};
}
function getCoordinates(f) {
return f.geometry.coordinates;
}
export function createLayerPropsFromFeatures(
features: SeparatedGeometries,
featuresDiff
): SubLayersProps {
const layerProps = createEmptyLayerProps();
const {pointFeatures, lineFeatures, polygonFeatures, polygonOutlineFeatures} = features;
layerProps.points.data = pointFeatures;
layerProps.points._dataDiff = featuresDiff.pointFeatures && (() => featuresDiff.pointFeatures);
layerProps.points.getPosition = getCoordinates;
layerProps.lines.data = lineFeatures;
layerProps.lines._dataDiff = featuresDiff.lineFeatures && (() => featuresDiff.lineFeatures);
layerProps.lines.getPath = getCoordinates;
layerProps.polygons.data = polygonFeatures;
layerProps.polygons._dataDiff =
featuresDiff.polygonFeatures && (() => featuresDiff.polygonFeatures);
layerProps.polygons.getPolygon = getCoordinates;
layerProps.polygonsOutline.data = polygonOutlineFeatures;
layerProps.polygonsOutline._dataDiff =
featuresDiff.polygonOutlineFeatures && (() => featuresDiff.polygonOutlineFeatures);
layerProps.polygonsOutline.getPath = getCoordinates;
return layerProps;
}
export function createLayerPropsFromBinary(
geojsonBinary: Required<ExtendedBinaryFeatures>,
encodePickingColor
): SubLayersProps {
// The binary data format is documented here
// https://github.com/visgl/loaders.gl/blob/master/modules/gis/docs/api-reference/geojson-to-binary.md
// It is the default output from the `MVTLoader` and can also be obtained
// from GeoJSON by using the `geojsonToBinary` utility function.
const layerProps = createEmptyLayerProps();
const {points, lines, polygons} = geojsonBinary;
const customPickingColors = calculatePickingColors(geojsonBinary, encodePickingColor);
layerProps.points.data = {
length: points.positions.value.length / points.positions.size,
attributes: {
...points.attributes,
getPosition: points.positions,
instancePickingColors: {
size: 3,
value: customPickingColors.points!
}
},
properties: points.properties,
numericProps: points.numericProps,
featureIds: points.featureIds
} as LayerData<any>;
layerProps.lines.data = {
length: lines.pathIndices.value.length - 1,
startIndices: lines.pathIndices.value,
attributes: {
...lines.attributes,
getPath: lines.positions,
instancePickingColors: {
size: 3,
value: customPickingColors.lines!
}
},
properties: lines.properties,
numericProps: lines.numericProps,
featureIds: lines.featureIds
} as LayerData<any>;
layerProps.lines._pathType = 'open';
layerProps.polygons.data = {
length: polygons.polygonIndices.value.length - 1,
startIndices: polygons.polygonIndices.value,
attributes: {
...polygons.attributes,
getPolygon: polygons.positions,
pickingColors: {
size: 3,
value: customPickingColors.polygons!
}
},
properties: polygons.properties,
numericProps: polygons.numericProps,
featureIds: polygons.featureIds
} as LayerData<any>;
layerProps.polygons._normalize = false;
if (polygons.triangles) {
(layerProps.polygons.data as any).attributes.indices = polygons.triangles.value;
}
layerProps.polygonsOutline.data = {
length: polygons.primitivePolygonIndices.value.length - 1,
startIndices: polygons.primitivePolygonIndices.value,
attributes: {
...polygons.attributes,
getPath: polygons.positions,
instancePickingColors: {
size: 3,
value: customPickingColors.polygons!
}
},
properties: polygons.properties,
numericProps: polygons.numericProps,
featureIds: polygons.featureIds
} as LayerData<any>;
layerProps.polygonsOutline._pathType = 'open';
return layerProps;
}