Skip to content

Commit

Permalink
[ts] modules/layers leftovers
Browse files Browse the repository at this point in the history
  • Loading branch information
zbigg committed Jun 9, 2022
1 parent e7f1303 commit 8ad29ae
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@
// 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 {
BinaryFeatures,
BinaryLineFeatures,
BinaryPointFeatures,
BinaryPolygonFeatures,
Feature
} from '@loaders.gl/schema';

export type BinaryFeatureTypes = BinaryPointFeatures | BinaryLineFeatures | BinaryPolygonFeatures;

type FeaureOnlyProperties = Pick<Feature, 'properties'>;

/**
* Return the feature for an accesor
*
* @param {Object} data - The data in binary format
* @param {Number} index - The requested index
*/
export function binaryToFeatureForAccesor(data, index) {
export function binaryToFeatureForAccesor(
data: BinaryFeatureTypes,
index: number
): FeaureOnlyProperties | null {
if (!data) {
return null;
}

const featureIndex = 'startIndices' in data ? data.startIndices[index] : index;
const featureIndex = 'startIndices' in data ? (data as any).startIndices[index] : index;
const geometryIndex = data.featureIds.value[featureIndex];

if (featureIndex !== -1) {
Expand All @@ -23,7 +35,11 @@ export function binaryToFeatureForAccesor(data, index) {
return null;
}

function getPropertiesForIndex(data, propertiesIndex, numericPropsIndex) {
function getPropertiesForIndex(
data: BinaryFeatureTypes,
propertiesIndex: number,
numericPropsIndex: number
): FeaureOnlyProperties {
const feature = {
properties: {...data.properties[propertiesIndex]}
};
Expand All @@ -36,8 +52,11 @@ function getPropertiesForIndex(data, propertiesIndex, numericPropsIndex) {
}

// Custom picking color to keep binary indexes
export function calculatePickingColors(geojsonBinary, encodePickingColor) {
const pickingColors = {
export function calculatePickingColors(
geojsonBinary: BinaryFeatures,
encodePickingColor: (id: number, result: number[]) => void
): Record<string, Uint8ClampedArray | null> {
const pickingColors: Record<string, Uint8ClampedArray | null> = {
points: null,
lines: null,
polygons: null
Expand All @@ -48,9 +67,9 @@ export function calculatePickingColors(geojsonBinary, encodePickingColor) {
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];
pickingColors[key]![i * 3 + 0] = pickingColor[0];
pickingColors[key]![i * 3 + 1] = pickingColor[1];
pickingColors[key]![i * 3 + 2] = pickingColor[2];
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/layers/src/geojson-layer/geojson-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import type {BinaryFeatures} from '@loaders.gl/schema';
import type {Feature} from 'geojson';

import {replaceInRange} from '../utils';
import {binaryToFeatureForAccesor} from './geojson-binary';
import {BinaryFeatureTypes, binaryToFeatureForAccesor} from './geojson-binary';
import {
POINT_LAYER,
LINE_LAYER,
Expand Down Expand Up @@ -565,7 +565,7 @@ export default class GeoJsonLayer<

return (object, info) => {
const {data, index} = info;
const feature = binaryToFeatureForAccesor(data, index);
const feature = binaryToFeatureForAccesor(data as unknown as BinaryFeatureTypes, index);
// @ts-ignore (TS2349) accessor is always function
return accessor(feature, info);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {CompositeLayer, Layer, LayerProps} from '@deck.gl/core';

import IconLayer from '../icon-layer/icon-layer';
import ScatterplotLayer from '../scatterplot-layer/scatterplot-layer';
import TextLayer from '../text-layer/text-layer';
Expand Down Expand Up @@ -115,15 +117,24 @@ export const POLYGON_LAYER = {
}
};

export function getDefaultProps({type, props}) {
export function getDefaultProps({
type,
props
}: {
type: typeof Layer;
props: Record<string, string>;
}): Record<string, any> {
const result = {};
for (const key in props) {
result[key] = type.defaultProps[props[key]];
}
return result;
}

export function forwardProps(layer, mapping) {
export function forwardProps(
layer: any,
mapping: Record<string, string>
): Record<string, any>{
const {transitions, updateTriggers} = layer.props;
const result = {
updateTriggers: {},
Expand All @@ -137,7 +148,7 @@ export function forwardProps(layer, mapping) {
let value = layer.props[sourceKey];
if (sourceKey.startsWith('get')) {
// isAccessor
value = layer.getSubLayerAccessor(value);
value = (layer as any).getSubLayerAccessor(value);
result.updateTriggers[targetKey] = updateTriggers[sourceKey];
if (transitions) {
result.transitions[targetKey] = transitions[sourceKey];
Expand Down
16 changes: 13 additions & 3 deletions modules/layers/src/utils.js → modules/layers/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
// Replaces the specified range with a new subarray
// Mutates the data array
// Returns {startRow, endRow} of the inserted items
export function replaceInRange({data, getIndex, dataRange, replace}) {
export function replaceInRange({
data,
getIndex,
dataRange,
replace
}: {
data: any[];
getIndex: (d: any) => number;
dataRange: {startRow?: number; endRow?: number};
replace: any[];
}): {startRow: Number; endRow: number} {
const {startRow = 0, endRow = Infinity} = dataRange;
const count = data.length;
let replaceStart = count;
Expand All @@ -20,12 +30,12 @@ export function replaceInRange({data, getIndex, dataRange, replace}) {
let index = replaceStart;
const dataLengthChanged = replaceEnd - replaceStart !== replace.length;
// Save the items after replaceEnd before we overwrite data
const endChunk = dataLengthChanged && data.slice(replaceEnd);
const endChunk = dataLengthChanged ? data.slice(replaceEnd) : undefined;
// Insert new items
for (let i = 0; i < replace.length; i++) {
data[index++] = replace[i];
}
if (dataLengthChanged) {
if (endChunk) {
// Append items after replaceEnd
for (let i = 0; i < endChunk.length; i++) {
data[index++] = endChunk[i];
Expand Down

0 comments on commit 8ad29ae

Please sign in to comment.