@deck.gl/carto
Version:
CARTO official integration with Deck.gl. Build geospatial applications using CARTO and Deck.gl.
110 lines • 3.74 kB
JavaScript
// deck.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import { log } from '@deck.gl/core';
export function assert(condition, message) {
log.assert(condition, message);
}
// Returns a Proxy object that allows accessing binary data
// as if it were JSON properties
export function createBinaryProxy(data, index) {
const { properties, numericProps } = data;
return new Proxy(properties[index] || {}, {
get(target, property) {
if (property in numericProps) {
return numericProps[property].value[index];
}
return target[property];
},
has(target, property) {
return property in numericProps || property in target;
},
ownKeys(target) {
return [...Object.keys(numericProps), ...Reflect.ownKeys(target)];
},
getOwnPropertyDescriptor(target, prop) {
return { enumerable: true, configurable: true };
}
});
}
export function getWorkerUrl(id, version) {
// For local testing `yarn build-workers` and then host `modules/carto/dist/`
// return `http://localhost:8081/dist/${id}-worker.js`;
return `https://unpkg.com/@deck.gl/carto@${version}/dist/${id}-worker.js`;
}
export function scaleIdentity() {
let unknown;
function scale(x) {
return x === null ? unknown : x;
}
scale.invert = scale;
scale.domain = scale.range = d => d;
scale.unknown = u => {
if (u) {
unknown = u;
}
return unknown;
};
scale.copy = () => {
const scaleCopy = scaleIdentity();
scaleCopy.unknown(unknown);
return scaleCopy;
};
return scale;
}
export const isObject = x => x !== null && typeof x === 'object';
export const isPureObject = x => isObject(x) && x.constructor === {}.constructor;
// Helpers for binary data
const EMPTY_UINT16ARRAY = new Uint16Array();
const EMPTY_BINARY_PROPS = {
positions: { value: new Float32Array(), size: 2 },
properties: [],
numericProps: {},
featureIds: { value: EMPTY_UINT16ARRAY, size: 1 },
globalFeatureIds: { value: EMPTY_UINT16ARRAY, size: 1 }
};
export function createEmptyBinary() {
return {
shape: 'binary-feature-collection',
points: {
type: 'Point',
...EMPTY_BINARY_PROPS
},
lines: {
type: 'LineString',
pathIndices: { value: EMPTY_UINT16ARRAY, size: 1 },
...EMPTY_BINARY_PROPS
},
polygons: {
type: 'Polygon',
polygonIndices: { value: EMPTY_UINT16ARRAY, size: 1 },
primitivePolygonIndices: { value: EMPTY_UINT16ARRAY, size: 1 },
...EMPTY_BINARY_PROPS
}
};
}
export function createBinaryPointFeature(positions, featureIds, globalFeatureIds, numericProps, properties, size = 2) {
return {
type: 'Point',
positions: { value: new Float32Array(positions), size },
featureIds: { value: new Uint16Array(featureIds), size: 1 },
globalFeatureIds: { value: new Uint32Array(globalFeatureIds), size: 1 },
numericProps,
properties
};
}
export function initializeNumericProps(numPoints, sourceProps) {
const numericProps = {};
if (sourceProps) {
Object.keys(sourceProps).forEach(prop => {
numericProps[prop] = { value: new Float32Array(numPoints), size: 1 };
});
}
return numericProps;
}
export function copyNumericProps(sourceProps, targetProps, sourceIndex, targetIndex) {
Object.keys(sourceProps).forEach(prop => {
targetProps[prop].value[targetIndex] = sourceProps[prop].value[sourceIndex];
});
}
//# sourceMappingURL=utils.js.map