Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@loaders.gl/gis

Package Overview
Dependencies
Maintainers
9
Versions
259
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@loaders.gl/gis - npm Package Compare versions

Comparing version 4.2.0-alpha.4 to 4.2.0-alpha.5

dist/index.cjs.map

24

dist/index.d.ts

@@ -1,13 +0,13 @@

export { GEOPARQUET_METADATA_JSON_SCHEMA } from './lib/geo/geoparquet-metadata-schema';
export type { GeoMetadata } from './lib/geo/geoparquet-metadata';
export { getGeoMetadata, setGeoMetadata, unpackGeoMetadata } from './lib/geo/geoparquet-metadata';
export { unpackJSONStringMetadata } from './lib/geo/geoparquet-metadata';
export type { GeoArrowEncoding, GeoArrowMetadata } from './lib/geo/geoarrow-metadata';
export { getGeometryColumnsFromSchema } from './lib/geo/geoarrow-metadata';
export { convertWKBTableToGeoJSON } from './lib/tables/convert-table-to-geojson';
export { flatGeojsonToBinary } from './lib/binary-features/flat-geojson-to-binary';
export { geojsonToBinary } from './lib/binary-features/geojson-to-binary';
export { geojsonToFlatGeojson } from './lib/binary-features/geojson-to-flat-geojson';
export { binaryToGeojson, binaryToGeometry } from './lib/binary-features/binary-to-geojson';
export { transformBinaryCoords, transformGeoJsonCoords } from './lib/binary-features/transform';
export { GEOPARQUET_METADATA_JSON_SCHEMA } from "./lib/geo/geoparquet-metadata-schema.js";
export type { GeoMetadata } from "./lib/geo/geoparquet-metadata.js";
export { getGeoMetadata, setGeoMetadata, unpackGeoMetadata } from "./lib/geo/geoparquet-metadata.js";
export { unpackJSONStringMetadata } from "./lib/geo/geoparquet-metadata.js";
export type { GeoArrowEncoding, GeoArrowMetadata } from "./lib/geo/geoarrow-metadata.js";
export { getGeometryColumnsFromSchema } from "./lib/geo/geoarrow-metadata.js";
export { convertWKBTableToGeoJSON } from "./lib/tables/convert-table-to-geojson.js";
export { flatGeojsonToBinary } from "./lib/binary-features/flat-geojson-to-binary.js";
export { geojsonToBinary } from "./lib/binary-features/geojson-to-binary.js";
export { geojsonToFlatGeojson } from "./lib/binary-features/geojson-to-flat-geojson.js";
export { binaryToGeojson, binaryToGeometry } from "./lib/binary-features/binary-to-geojson.js";
export { transformBinaryCoords, transformGeoJsonCoords } from "./lib/binary-features/transform.js";
//# sourceMappingURL=index.d.ts.map

@@ -0,1 +1,5 @@

// Types from `@loaders.gl/schema`
// Geo Metadata
// import {default as GEOPARQUET_METADATA_SCHEMA} from './lib/geo/geoparquet-metadata-schema.json';
// export {GEOPARQUET_METADATA_SCHEMA};
export { GEOPARQUET_METADATA_JSON_SCHEMA } from "./lib/geo/geoparquet-metadata-schema.js";

@@ -5,3 +9,5 @@ export { getGeoMetadata, setGeoMetadata, unpackGeoMetadata } from "./lib/geo/geoparquet-metadata.js";

export { getGeometryColumnsFromSchema } from "./lib/geo/geoarrow-metadata.js";
// Table conversion
export { convertWKBTableToGeoJSON } from "./lib/tables/convert-table-to-geojson.js";
// Binary Geometries
export { flatGeojsonToBinary } from "./lib/binary-features/flat-geojson-to-binary.js";

@@ -12,2 +18,1 @@ export { geojsonToBinary } from "./lib/binary-features/geojson-to-binary.js";

export { transformBinaryCoords, transformGeoJsonCoords } from "./lib/binary-features/transform.js";
//# sourceMappingURL=index.js.map

@@ -0,198 +1,202 @@

// loaders.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
/**
* Convert binary geometry representation to GeoJSON
* @param data geometry data in binary representation
* @param options
* @param options.type Input data type: Point, LineString, or Polygon
* @param options.featureId Global feature id. If specified, only a single feature is extracted
* @return GeoJSON objects
*/
export function binaryToGeojson(data, options) {
const globalFeatureId = options === null || options === void 0 ? void 0 : options.globalFeatureId;
if (globalFeatureId !== undefined) {
return getSingleFeature(data, globalFeatureId);
}
return parseFeatures(data, options === null || options === void 0 ? void 0 : options.type);
const globalFeatureId = options?.globalFeatureId;
if (globalFeatureId !== undefined) {
return getSingleFeature(data, globalFeatureId);
}
return parseFeatures(data, options?.type);
}
/**
* Return a single feature from a binary geometry representation as GeoJSON
* @param data geometry data in binary representation
* @return GeoJSON feature
*/
function getSingleFeature(data, globalFeatureId) {
const dataArray = normalizeInput(data);
for (const data of dataArray) {
let lastIndex = 0;
let lastValue = data.featureIds.value[0];
for (let i = 0; i < data.featureIds.value.length; i++) {
const currValue = data.featureIds.value[i];
if (currValue === lastValue) {
continue;
}
if (globalFeatureId === data.globalFeatureIds.value[lastIndex]) {
return parseFeature(data, lastIndex, i);
}
lastIndex = i;
lastValue = currValue;
const dataArray = normalizeInput(data);
for (const data of dataArray) {
let lastIndex = 0;
let lastValue = data.featureIds.value[0];
// Scan through data until we find matching feature
for (let i = 0; i < data.featureIds.value.length; i++) {
const currValue = data.featureIds.value[i];
if (currValue === lastValue) {
// eslint-disable-next-line no-continue
continue;
}
if (globalFeatureId === data.globalFeatureIds.value[lastIndex]) {
return parseFeature(data, lastIndex, i);
}
lastIndex = i;
lastValue = currValue;
}
if (globalFeatureId === data.globalFeatureIds.value[lastIndex]) {
return parseFeature(data, lastIndex, data.featureIds.value.length);
}
}
if (globalFeatureId === data.globalFeatureIds.value[lastIndex]) {
return parseFeature(data, lastIndex, data.featureIds.value.length);
}
}
throw new Error(`featureId:${globalFeatureId} not found`);
throw new Error(`featureId:${globalFeatureId} not found`);
}
function parseFeatures(data, type) {
const dataArray = normalizeInput(data, type);
return parseFeatureCollection(dataArray);
const dataArray = normalizeInput(data, type);
return parseFeatureCollection(dataArray);
}
/** Parse input binary data and return a valid GeoJSON geometry object */
export function binaryToGeometry(data, startIndex, endIndex) {
switch (data.type) {
case 'Point':
return pointToGeoJson(data, startIndex, endIndex);
case 'LineString':
return lineStringToGeoJson(data, startIndex, endIndex);
case 'Polygon':
return polygonToGeoJson(data, startIndex, endIndex);
default:
const unexpectedInput = data;
throw new Error(`Unsupported geometry type: ${unexpectedInput === null || unexpectedInput === void 0 ? void 0 : unexpectedInput.type}`);
}
switch (data.type) {
case 'Point':
return pointToGeoJson(data, startIndex, endIndex);
case 'LineString':
return lineStringToGeoJson(data, startIndex, endIndex);
case 'Polygon':
return polygonToGeoJson(data, startIndex, endIndex);
default:
const unexpectedInput = data;
throw new Error(`Unsupported geometry type: ${unexpectedInput?.type}`);
}
}
// Normalize features
// Return an array of data objects, each of which have a type key
function normalizeInput(data, type) {
const features = [];
if (data.points) {
data.points.type = 'Point';
features.push(data.points);
}
if (data.lines) {
data.lines.type = 'LineString';
features.push(data.lines);
}
if (data.polygons) {
data.polygons.type = 'Polygon';
features.push(data.polygons);
}
return features;
const features = [];
if (data.points) {
data.points.type = 'Point';
features.push(data.points);
}
if (data.lines) {
data.lines.type = 'LineString';
features.push(data.lines);
}
if (data.polygons) {
data.polygons.type = 'Polygon';
features.push(data.polygons);
}
return features;
}
/** Parse input binary data and return an array of GeoJSON Features */
function parseFeatureCollection(dataArray) {
const features = [];
for (const data of dataArray) {
if (data.featureIds.value.length === 0) {
continue;
const features = [];
for (const data of dataArray) {
if (data.featureIds.value.length === 0) {
// eslint-disable-next-line no-continue
continue;
}
let lastIndex = 0;
let lastValue = data.featureIds.value[0];
// Need to deduce start, end indices of each feature
for (let i = 0; i < data.featureIds.value.length; i++) {
const currValue = data.featureIds.value[i];
if (currValue === lastValue) {
// eslint-disable-next-line no-continue
continue;
}
features.push(parseFeature(data, lastIndex, i));
lastIndex = i;
lastValue = currValue;
}
// Last feature
features.push(parseFeature(data, lastIndex, data.featureIds.value.length));
}
let lastIndex = 0;
let lastValue = data.featureIds.value[0];
for (let i = 0; i < data.featureIds.value.length; i++) {
const currValue = data.featureIds.value[i];
if (currValue === lastValue) {
continue;
}
features.push(parseFeature(data, lastIndex, i));
lastIndex = i;
lastValue = currValue;
}
features.push(parseFeature(data, lastIndex, data.featureIds.value.length));
}
return features;
return features;
}
/** Parse input binary data and return a single GeoJSON Feature */
function parseFeature(data, startIndex, endIndex) {
const geometry = binaryToGeometry(data, startIndex, endIndex);
const properties = parseProperties(data, startIndex, endIndex);
const fields = parseFields(data, startIndex, endIndex);
return {
type: 'Feature',
geometry,
properties,
...fields
};
const geometry = binaryToGeometry(data, startIndex, endIndex);
const properties = parseProperties(data, startIndex, endIndex);
const fields = parseFields(data, startIndex, endIndex);
return { type: 'Feature', geometry, properties, ...fields };
}
function parseFields(data) {
let startIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
let endIndex = arguments.length > 2 ? arguments[2] : undefined;
return data.fields && data.fields[data.featureIds.value[startIndex]];
/** Parse input binary data and return an object of fields */
function parseFields(data, startIndex = 0, endIndex) {
return data.fields && data.fields[data.featureIds.value[startIndex]];
}
function parseProperties(data) {
let startIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
let endIndex = arguments.length > 2 ? arguments[2] : undefined;
const properties = Object.assign({}, data.properties[data.featureIds.value[startIndex]]);
for (const key in data.numericProps) {
properties[key] = data.numericProps[key].value[startIndex];
}
return properties;
/** Parse input binary data and return an object of properties */
function parseProperties(data, startIndex = 0, endIndex) {
const properties = Object.assign({}, data.properties[data.featureIds.value[startIndex]]);
for (const key in data.numericProps) {
properties[key] = data.numericProps[key].value[startIndex];
}
return properties;
}
function polygonToGeoJson(data) {
let startIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -Infinity;
let endIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Infinity;
const {
positions
} = data;
const polygonIndices = data.polygonIndices.value.filter(x => x >= startIndex && x <= endIndex);
const primitivePolygonIndices = data.primitivePolygonIndices.value.filter(x => x >= startIndex && x <= endIndex);
const multi = polygonIndices.length > 2;
if (!multi) {
/** Parse binary data of type Polygon */
function polygonToGeoJson(data, startIndex = -Infinity, endIndex = Infinity) {
const { positions } = data;
const polygonIndices = data.polygonIndices.value.filter((x) => x >= startIndex && x <= endIndex);
const primitivePolygonIndices = data.primitivePolygonIndices.value.filter((x) => x >= startIndex && x <= endIndex);
const multi = polygonIndices.length > 2;
// Polygon
if (!multi) {
const coordinates = [];
for (let i = 0; i < primitivePolygonIndices.length - 1; i++) {
const startRingIndex = primitivePolygonIndices[i];
const endRingIndex = primitivePolygonIndices[i + 1];
const ringCoordinates = ringToGeoJson(positions, startRingIndex, endRingIndex);
coordinates.push(ringCoordinates);
}
return { type: 'Polygon', coordinates };
}
// MultiPolygon
const coordinates = [];
for (let i = 0; i < primitivePolygonIndices.length - 1; i++) {
const startRingIndex = primitivePolygonIndices[i];
const endRingIndex = primitivePolygonIndices[i + 1];
const ringCoordinates = ringToGeoJson(positions, startRingIndex, endRingIndex);
coordinates.push(ringCoordinates);
for (let i = 0; i < polygonIndices.length - 1; i++) {
const startPolygonIndex = polygonIndices[i];
const endPolygonIndex = polygonIndices[i + 1];
const polygonCoordinates = polygonToGeoJson(data, startPolygonIndex, endPolygonIndex).coordinates;
coordinates.push(polygonCoordinates);
}
return {
type: 'Polygon',
coordinates
};
}
const coordinates = [];
for (let i = 0; i < polygonIndices.length - 1; i++) {
const startPolygonIndex = polygonIndices[i];
const endPolygonIndex = polygonIndices[i + 1];
const polygonCoordinates = polygonToGeoJson(data, startPolygonIndex, endPolygonIndex).coordinates;
coordinates.push(polygonCoordinates);
}
return {
type: 'MultiPolygon',
coordinates
};
return { type: 'MultiPolygon', coordinates };
}
function lineStringToGeoJson(data) {
let startIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -Infinity;
let endIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Infinity;
const {
positions
} = data;
const pathIndices = data.pathIndices.value.filter(x => x >= startIndex && x <= endIndex);
const multi = pathIndices.length > 2;
if (!multi) {
const coordinates = ringToGeoJson(positions, pathIndices[0], pathIndices[1]);
return {
type: 'LineString',
coordinates
};
}
const coordinates = [];
for (let i = 0; i < pathIndices.length - 1; i++) {
const ringCoordinates = ringToGeoJson(positions, pathIndices[i], pathIndices[i + 1]);
coordinates.push(ringCoordinates);
}
return {
type: 'MultiLineString',
coordinates
};
/** Parse binary data of type LineString */
function lineStringToGeoJson(data, startIndex = -Infinity, endIndex = Infinity) {
const { positions } = data;
const pathIndices = data.pathIndices.value.filter((x) => x >= startIndex && x <= endIndex);
const multi = pathIndices.length > 2;
if (!multi) {
const coordinates = ringToGeoJson(positions, pathIndices[0], pathIndices[1]);
return { type: 'LineString', coordinates };
}
const coordinates = [];
for (let i = 0; i < pathIndices.length - 1; i++) {
const ringCoordinates = ringToGeoJson(positions, pathIndices[i], pathIndices[i + 1]);
coordinates.push(ringCoordinates);
}
return { type: 'MultiLineString', coordinates };
}
/** Parse binary data of type Point */
function pointToGeoJson(data, startIndex, endIndex) {
const {
positions
} = data;
const coordinates = ringToGeoJson(positions, startIndex, endIndex);
const multi = coordinates.length > 1;
if (multi) {
return {
type: 'MultiPoint',
coordinates
};
}
return {
type: 'Point',
coordinates: coordinates[0]
};
const { positions } = data;
const coordinates = ringToGeoJson(positions, startIndex, endIndex);
const multi = coordinates.length > 1;
if (multi) {
return { type: 'MultiPoint', coordinates };
}
return { type: 'Point', coordinates: coordinates[0] };
}
/**
* Parse a linear ring of positions to a GeoJSON linear ring
*
* @param positions Positions TypedArray
* @param startIndex Start index to include in ring
* @param endIndex End index to include in ring
* @returns GeoJSON ring
*/
function ringToGeoJson(positions, startIndex, endIndex) {
startIndex = startIndex || 0;
endIndex = endIndex || positions.value.length / positions.size;
const ringCoordinates = [];
for (let j = startIndex; j < endIndex; j++) {
const coord = Array();
for (let k = j * positions.size; k < (j + 1) * positions.size; k++) {
coord.push(Number(positions.value[k]));
startIndex = startIndex || 0;
endIndex = endIndex || positions.value.length / positions.size;
const ringCoordinates = [];
for (let j = startIndex; j < endIndex; j++) {
const coord = Array();
for (let k = j * positions.size; k < (j + 1) * positions.size; k++) {
coord.push(Number(positions.value[k]));
}
ringCoordinates.push(coord);
}
ringCoordinates.push(coord);
}
return ringCoordinates;
return ringCoordinates;
}
//# sourceMappingURL=binary-to-geojson.js.map

@@ -0,84 +1,92 @@

/**
* Initial scan over GeoJSON features
* Counts number of coordinates of each geometry type and
* keeps track of the max coordinate dimensions
*/
// eslint-disable-next-line complexity, max-statements
export function extractGeometryInfo(features) {
let pointPositionsCount = 0;
let pointFeaturesCount = 0;
let linePositionsCount = 0;
let linePathsCount = 0;
let lineFeaturesCount = 0;
let polygonPositionsCount = 0;
let polygonObjectsCount = 0;
let polygonRingsCount = 0;
let polygonFeaturesCount = 0;
const coordLengths = new Set();
for (const feature of features) {
const geometry = feature.geometry;
switch (geometry.type) {
case 'Point':
pointFeaturesCount++;
pointPositionsCount++;
coordLengths.add(geometry.coordinates.length);
break;
case 'MultiPoint':
pointFeaturesCount++;
pointPositionsCount += geometry.coordinates.length;
for (const point of geometry.coordinates) {
coordLengths.add(point.length);
// Counts the number of _positions_, so [x, y, z] counts as one
let pointPositionsCount = 0;
let pointFeaturesCount = 0;
let linePositionsCount = 0;
let linePathsCount = 0;
let lineFeaturesCount = 0;
let polygonPositionsCount = 0;
let polygonObjectsCount = 0;
let polygonRingsCount = 0;
let polygonFeaturesCount = 0;
const coordLengths = new Set();
for (const feature of features) {
const geometry = feature.geometry;
switch (geometry.type) {
case 'Point':
pointFeaturesCount++;
pointPositionsCount++;
coordLengths.add(geometry.coordinates.length);
break;
case 'MultiPoint':
pointFeaturesCount++;
pointPositionsCount += geometry.coordinates.length;
for (const point of geometry.coordinates) {
coordLengths.add(point.length);
}
break;
case 'LineString':
lineFeaturesCount++;
linePositionsCount += geometry.coordinates.length;
linePathsCount++;
for (const coord of geometry.coordinates) {
coordLengths.add(coord.length);
}
break;
case 'MultiLineString':
lineFeaturesCount++;
for (const line of geometry.coordinates) {
linePositionsCount += line.length;
linePathsCount++;
// eslint-disable-next-line max-depth
for (const coord of line) {
coordLengths.add(coord.length);
}
}
break;
case 'Polygon':
polygonFeaturesCount++;
polygonObjectsCount++;
polygonRingsCount += geometry.coordinates.length;
const flattened = geometry.coordinates.flat();
polygonPositionsCount += flattened.length;
for (const coord of flattened) {
coordLengths.add(coord.length);
}
break;
case 'MultiPolygon':
polygonFeaturesCount++;
for (const polygon of geometry.coordinates) {
polygonObjectsCount++;
polygonRingsCount += polygon.length;
const flattened = polygon.flat();
polygonPositionsCount += flattened.length;
// eslint-disable-next-line max-depth
for (const coord of flattened) {
coordLengths.add(coord.length);
}
}
break;
default:
throw new Error(`Unsupported geometry type: ${geometry.type}`);
}
break;
case 'LineString':
lineFeaturesCount++;
linePositionsCount += geometry.coordinates.length;
linePathsCount++;
for (const coord of geometry.coordinates) {
coordLengths.add(coord.length);
}
break;
case 'MultiLineString':
lineFeaturesCount++;
for (const line of geometry.coordinates) {
linePositionsCount += line.length;
linePathsCount++;
for (const coord of line) {
coordLengths.add(coord.length);
}
}
break;
case 'Polygon':
polygonFeaturesCount++;
polygonObjectsCount++;
polygonRingsCount += geometry.coordinates.length;
const flattened = geometry.coordinates.flat();
polygonPositionsCount += flattened.length;
for (const coord of flattened) {
coordLengths.add(coord.length);
}
break;
case 'MultiPolygon':
polygonFeaturesCount++;
for (const polygon of geometry.coordinates) {
polygonObjectsCount++;
polygonRingsCount += polygon.length;
const flattened = polygon.flat();
polygonPositionsCount += flattened.length;
for (const coord of flattened) {
coordLengths.add(coord.length);
}
}
break;
default:
throw new Error(`Unsupported geometry type: ${geometry.type}`);
}
}
return {
coordLength: coordLengths.size > 0 ? Math.max(...coordLengths) : 2,
pointPositionsCount,
pointFeaturesCount,
linePositionsCount,
linePathsCount,
lineFeaturesCount,
polygonPositionsCount,
polygonObjectsCount,
polygonRingsCount,
polygonFeaturesCount
};
return {
coordLength: coordLengths.size > 0 ? Math.max(...coordLengths) : 2,
pointPositionsCount,
pointFeaturesCount,
linePositionsCount,
linePathsCount,
lineFeaturesCount,
polygonPositionsCount,
polygonObjectsCount,
polygonRingsCount,
polygonFeaturesCount
};
}
//# sourceMappingURL=extract-geometry-info.js.map
export {};
//# sourceMappingURL=flat-geojson-to-binary-types.js.map
import type { BinaryFeatureCollection, FlatFeature, GeojsonGeometryInfo } from '@loaders.gl/schema';
import { PropArrayConstructor } from './flat-geojson-to-binary-types';
import { PropArrayConstructor } from "./flat-geojson-to-binary-types.js";
/**

@@ -4,0 +4,0 @@ * Convert binary features to flat binary arrays. Similar to

@@ -0,316 +1,381 @@

/* eslint-disable indent */
import { earcut } from '@math.gl/polygon';
/**
* Convert binary features to flat binary arrays. Similar to
* `geojsonToBinary` helper function, except that it expects
* a binary representation of the feature data, which enables
* 2X-3X speed increase in parse speed, compared to using
* geoJSON. See `binary-vector-tile/VectorTileFeature` for
* data format detais
*
* @param features
* @param geometryInfo
* @param options
* @returns filled arrays
*/
export function flatGeojsonToBinary(features, geometryInfo, options) {
const propArrayTypes = extractNumericPropTypes(features);
const numericPropKeys = Object.keys(propArrayTypes).filter(k => propArrayTypes[k] !== Array);
return fillArrays(features, {
propArrayTypes,
...geometryInfo
}, {
numericPropKeys: options && options.numericPropKeys || numericPropKeys,
PositionDataType: options ? options.PositionDataType : Float32Array,
triangulate: options ? options.triangulate : true
});
const propArrayTypes = extractNumericPropTypes(features);
const numericPropKeys = Object.keys(propArrayTypes).filter((k) => propArrayTypes[k] !== Array);
return fillArrays(features, {
propArrayTypes,
...geometryInfo
}, {
numericPropKeys: (options && options.numericPropKeys) || numericPropKeys,
PositionDataType: options ? options.PositionDataType : Float32Array,
triangulate: options ? options.triangulate : true
});
}
export const TEST_EXPORTS = {
extractNumericPropTypes
extractNumericPropTypes
};
/**
* Extracts properties that are always numeric
*
* @param features
* @returns object with numeric types
*/
function extractNumericPropTypes(features) {
const propArrayTypes = {};
for (const feature of features) {
if (feature.properties) {
for (const key in feature.properties) {
const val = feature.properties[key];
propArrayTypes[key] = deduceArrayType(val, propArrayTypes[key]);
}
const propArrayTypes = {};
for (const feature of features) {
if (feature.properties) {
for (const key in feature.properties) {
// If property has not been seen before, or if property has been numeric
// in all previous features, check if numeric in this feature
// If not numeric, Array is stored to prevent rechecking in the future
// Additionally, detects if 64 bit precision is required
const val = feature.properties[key];
propArrayTypes[key] = deduceArrayType(val, propArrayTypes[key]);
}
}
}
}
return propArrayTypes;
return propArrayTypes;
}
/**
* Fills coordinates into pre-allocated typed arrays
*
* @param features
* @param geometryInfo
* @param options
* @returns an accessor object with value and size keys
*/
// eslint-disable-next-line complexity, max-statements
function fillArrays(features, geometryInfo, options) {
const {
pointPositionsCount,
pointFeaturesCount,
linePositionsCount,
linePathsCount,
lineFeaturesCount,
polygonPositionsCount,
polygonObjectsCount,
polygonRingsCount,
polygonFeaturesCount,
propArrayTypes,
coordLength
} = geometryInfo;
const {
numericPropKeys = [],
PositionDataType = Float32Array,
triangulate = true
} = options;
const hasGlobalId = features[0] && 'id' in features[0];
const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
const points = {
type: 'Point',
positions: new PositionDataType(pointPositionsCount * coordLength),
globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
featureIds: pointFeaturesCount > 65535 ? new Uint32Array(pointPositionsCount) : new Uint16Array(pointPositionsCount),
numericProps: {},
properties: [],
fields: []
};
const lines = {
type: 'LineString',
pathIndices: linePositionsCount > 65535 ? new Uint32Array(linePathsCount + 1) : new Uint16Array(linePathsCount + 1),
positions: new PositionDataType(linePositionsCount * coordLength),
globalFeatureIds: new GlobalFeatureIdsDataType(linePositionsCount),
featureIds: lineFeaturesCount > 65535 ? new Uint32Array(linePositionsCount) : new Uint16Array(linePositionsCount),
numericProps: {},
properties: [],
fields: []
};
const polygons = {
type: 'Polygon',
polygonIndices: polygonPositionsCount > 65535 ? new Uint32Array(polygonObjectsCount + 1) : new Uint16Array(polygonObjectsCount + 1),
primitivePolygonIndices: polygonPositionsCount > 65535 ? new Uint32Array(polygonRingsCount + 1) : new Uint16Array(polygonRingsCount + 1),
positions: new PositionDataType(polygonPositionsCount * coordLength),
globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
featureIds: polygonFeaturesCount > 65535 ? new Uint32Array(polygonPositionsCount) : new Uint16Array(polygonPositionsCount),
numericProps: {},
properties: [],
fields: []
};
if (triangulate) {
polygons.triangles = [];
}
for (const object of [points, lines, polygons]) {
for (const propName of numericPropKeys) {
const T = propArrayTypes[propName];
object.numericProps[propName] = new T(object.positions.length / coordLength);
const { pointPositionsCount, pointFeaturesCount, linePositionsCount, linePathsCount, lineFeaturesCount, polygonPositionsCount, polygonObjectsCount, polygonRingsCount, polygonFeaturesCount, propArrayTypes, coordLength } = geometryInfo;
const { numericPropKeys = [], PositionDataType = Float32Array, triangulate = true } = options;
const hasGlobalId = features[0] && 'id' in features[0];
const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
const points = {
type: 'Point',
positions: new PositionDataType(pointPositionsCount * coordLength),
globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
featureIds: pointFeaturesCount > 65535
? new Uint32Array(pointPositionsCount)
: new Uint16Array(pointPositionsCount),
numericProps: {},
properties: [],
fields: []
};
const lines = {
type: 'LineString',
pathIndices: linePositionsCount > 65535
? new Uint32Array(linePathsCount + 1)
: new Uint16Array(linePathsCount + 1),
positions: new PositionDataType(linePositionsCount * coordLength),
globalFeatureIds: new GlobalFeatureIdsDataType(linePositionsCount),
featureIds: lineFeaturesCount > 65535
? new Uint32Array(linePositionsCount)
: new Uint16Array(linePositionsCount),
numericProps: {},
properties: [],
fields: []
};
const polygons = {
type: 'Polygon',
polygonIndices: polygonPositionsCount > 65535
? new Uint32Array(polygonObjectsCount + 1)
: new Uint16Array(polygonObjectsCount + 1),
primitivePolygonIndices: polygonPositionsCount > 65535
? new Uint32Array(polygonRingsCount + 1)
: new Uint16Array(polygonRingsCount + 1),
positions: new PositionDataType(polygonPositionsCount * coordLength),
globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
featureIds: polygonFeaturesCount > 65535
? new Uint32Array(polygonPositionsCount)
: new Uint16Array(polygonPositionsCount),
numericProps: {},
properties: [],
fields: []
};
if (triangulate) {
polygons.triangles = [];
}
}
lines.pathIndices[linePathsCount] = linePositionsCount;
polygons.polygonIndices[polygonObjectsCount] = polygonPositionsCount;
polygons.primitivePolygonIndices[polygonRingsCount] = polygonPositionsCount;
const indexMap = {
pointPosition: 0,
pointFeature: 0,
linePosition: 0,
linePath: 0,
lineFeature: 0,
polygonPosition: 0,
polygonObject: 0,
polygonRing: 0,
polygonFeature: 0,
feature: 0
};
for (const feature of features) {
const geometry = feature.geometry;
const properties = feature.properties || {};
switch (geometry.type) {
case 'Point':
handlePoint(geometry, points, indexMap, coordLength, properties);
points.properties.push(keepStringProperties(properties, numericPropKeys));
if (hasGlobalId) {
points.fields.push({
id: feature.id
});
// Instantiate numeric properties arrays; one value per vertex
for (const object of [points, lines, polygons]) {
for (const propName of numericPropKeys) {
// If property has been numeric in all previous features in which the property existed, check
// if numeric in this feature
const T = propArrayTypes[propName];
object.numericProps[propName] = new T(object.positions.length / coordLength);
}
indexMap.pointFeature++;
break;
case 'LineString':
handleLineString(geometry, lines, indexMap, coordLength, properties);
lines.properties.push(keepStringProperties(properties, numericPropKeys));
if (hasGlobalId) {
lines.fields.push({
id: feature.id
});
}
// Set last element of path/polygon indices as positions length
lines.pathIndices[linePathsCount] = linePositionsCount;
polygons.polygonIndices[polygonObjectsCount] = polygonPositionsCount;
polygons.primitivePolygonIndices[polygonRingsCount] = polygonPositionsCount;
const indexMap = {
pointPosition: 0,
pointFeature: 0,
linePosition: 0,
linePath: 0,
lineFeature: 0,
polygonPosition: 0,
polygonObject: 0,
polygonRing: 0,
polygonFeature: 0,
feature: 0
};
for (const feature of features) {
const geometry = feature.geometry;
const properties = feature.properties || {};
switch (geometry.type) {
case 'Point':
handlePoint(geometry, points, indexMap, coordLength, properties);
points.properties.push(keepStringProperties(properties, numericPropKeys));
if (hasGlobalId) {
points.fields.push({ id: feature.id });
}
indexMap.pointFeature++;
break;
case 'LineString':
handleLineString(geometry, lines, indexMap, coordLength, properties);
lines.properties.push(keepStringProperties(properties, numericPropKeys));
if (hasGlobalId) {
lines.fields.push({ id: feature.id });
}
indexMap.lineFeature++;
break;
case 'Polygon':
handlePolygon(geometry, polygons, indexMap, coordLength, properties);
polygons.properties.push(keepStringProperties(properties, numericPropKeys));
if (hasGlobalId) {
polygons.fields.push({ id: feature.id });
}
indexMap.polygonFeature++;
break;
default:
throw new Error('Invalid geometry type');
}
indexMap.lineFeature++;
break;
case 'Polygon':
handlePolygon(geometry, polygons, indexMap, coordLength, properties);
polygons.properties.push(keepStringProperties(properties, numericPropKeys));
if (hasGlobalId) {
polygons.fields.push({
id: feature.id
});
}
indexMap.polygonFeature++;
break;
default:
throw new Error('Invalid geometry type');
indexMap.feature++;
}
indexMap.feature++;
}
return makeAccessorObjects(points, lines, polygons, coordLength);
// Wrap each array in an accessor object with value and size keys
return makeAccessorObjects(points, lines, polygons, coordLength);
}
/**
* Fills (Multi)Point coordinates into points object of arrays
*
* @param geometry
* @param points
* @param indexMap
* @param coordLength
* @param properties
*/
function handlePoint(geometry, points, indexMap, coordLength, properties) {
points.positions.set(geometry.data, indexMap.pointPosition * coordLength);
const nPositions = geometry.data.length / coordLength;
fillNumericProperties(points, properties, indexMap.pointPosition, nPositions);
points.globalFeatureIds.fill(indexMap.feature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
points.featureIds.fill(indexMap.pointFeature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
indexMap.pointPosition += nPositions;
points.positions.set(geometry.data, indexMap.pointPosition * coordLength);
const nPositions = geometry.data.length / coordLength;
fillNumericProperties(points, properties, indexMap.pointPosition, nPositions);
points.globalFeatureIds.fill(indexMap.feature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
points.featureIds.fill(indexMap.pointFeature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
indexMap.pointPosition += nPositions;
}
/**
* Fills (Multi)LineString coordinates into lines object of arrays
*
* @param geometry
* @param lines
* @param indexMap
* @param coordLength
* @param properties
*/
function handleLineString(geometry, lines, indexMap, coordLength, properties) {
lines.positions.set(geometry.data, indexMap.linePosition * coordLength);
const nPositions = geometry.data.length / coordLength;
fillNumericProperties(lines, properties, indexMap.linePosition, nPositions);
lines.globalFeatureIds.fill(indexMap.feature, indexMap.linePosition, indexMap.linePosition + nPositions);
lines.featureIds.fill(indexMap.lineFeature, indexMap.linePosition, indexMap.linePosition + nPositions);
for (let i = 0, il = geometry.indices.length; i < il; ++i) {
const start = geometry.indices[i];
const end = i === il - 1 ? geometry.data.length : geometry.indices[i + 1];
lines.pathIndices[indexMap.linePath++] = indexMap.linePosition;
indexMap.linePosition += (end - start) / coordLength;
}
lines.positions.set(geometry.data, indexMap.linePosition * coordLength);
const nPositions = geometry.data.length / coordLength;
fillNumericProperties(lines, properties, indexMap.linePosition, nPositions);
lines.globalFeatureIds.fill(indexMap.feature, indexMap.linePosition, indexMap.linePosition + nPositions);
lines.featureIds.fill(indexMap.lineFeature, indexMap.linePosition, indexMap.linePosition + nPositions);
for (let i = 0, il = geometry.indices.length; i < il; ++i) {
// Extract range of data we are working with, defined by start
// and end indices (these index into the geometry.data array)
const start = geometry.indices[i];
const end = i === il - 1
? geometry.data.length // last line, so read to end of data
: geometry.indices[i + 1]; // start index for next line
lines.pathIndices[indexMap.linePath++] = indexMap.linePosition;
indexMap.linePosition += (end - start) / coordLength;
}
}
/**
* Fills (Multi)Polygon coordinates into polygons object of arrays
*
* @param geometry
* @param polygons
* @param indexMap
* @param coordLength
* @param properties
*/
function handlePolygon(geometry, polygons, indexMap, coordLength, properties) {
polygons.positions.set(geometry.data, indexMap.polygonPosition * coordLength);
const nPositions = geometry.data.length / coordLength;
fillNumericProperties(polygons, properties, indexMap.polygonPosition, nPositions);
polygons.globalFeatureIds.fill(indexMap.feature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
polygons.featureIds.fill(indexMap.polygonFeature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
for (let l = 0, ll = geometry.indices.length; l < ll; ++l) {
const startPosition = indexMap.polygonPosition;
polygons.polygonIndices[indexMap.polygonObject++] = startPosition;
const areas = geometry.areas[l];
const indices = geometry.indices[l];
const nextIndices = geometry.indices[l + 1];
for (let i = 0, il = indices.length; i < il; ++i) {
const start = indices[i];
const end = i === il - 1 ? nextIndices === undefined ? geometry.data.length : nextIndices[0] : indices[i + 1];
polygons.primitivePolygonIndices[indexMap.polygonRing++] = indexMap.polygonPosition;
indexMap.polygonPosition += (end - start) / coordLength;
polygons.positions.set(geometry.data, indexMap.polygonPosition * coordLength);
const nPositions = geometry.data.length / coordLength;
fillNumericProperties(polygons, properties, indexMap.polygonPosition, nPositions);
polygons.globalFeatureIds.fill(indexMap.feature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
polygons.featureIds.fill(indexMap.polygonFeature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
// Unlike Point & LineString geometry.indices is a 2D array
for (let l = 0, ll = geometry.indices.length; l < ll; ++l) {
const startPosition = indexMap.polygonPosition;
polygons.polygonIndices[indexMap.polygonObject++] = startPosition;
const areas = geometry.areas[l];
const indices = geometry.indices[l];
const nextIndices = geometry.indices[l + 1];
for (let i = 0, il = indices.length; i < il; ++i) {
const start = indices[i];
const end = i === il - 1
? // last line, so either read to:
nextIndices === undefined
? geometry.data.length // end of data (no next indices)
: nextIndices[0] // start of first line in nextIndices
: indices[i + 1]; // start index for next line
polygons.primitivePolygonIndices[indexMap.polygonRing++] = indexMap.polygonPosition;
indexMap.polygonPosition += (end - start) / coordLength;
}
const endPosition = indexMap.polygonPosition;
triangulatePolygon(polygons, areas, indices, { startPosition, endPosition, coordLength });
}
const endPosition = indexMap.polygonPosition;
triangulatePolygon(polygons, areas, indices, {
startPosition,
endPosition,
coordLength
});
}
}
function triangulatePolygon(polygons, areas, indices, _ref) {
let {
startPosition,
endPosition,
coordLength
} = _ref;
if (!polygons.triangles) {
return;
}
const start = startPosition * coordLength;
const end = endPosition * coordLength;
const polygonPositions = polygons.positions.subarray(start, end);
const offset = indices[0];
const holes = indices.slice(1).map(n => (n - offset) / coordLength);
const triangles = earcut(polygonPositions, holes, coordLength, areas);
for (let t = 0, tl = triangles.length; t < tl; ++t) {
polygons.triangles.push(startPosition + triangles[t]);
}
/**
* Triangulate polygon using earcut
*
* @param polygons
* @param areas
* @param indices
* @param param3
*/
function triangulatePolygon(polygons, areas, indices, { startPosition, endPosition, coordLength }) {
if (!polygons.triangles) {
return;
}
const start = startPosition * coordLength;
const end = endPosition * coordLength;
// Extract positions and holes for just this polygon
const polygonPositions = polygons.positions.subarray(start, end);
// Holes are referenced relative to outer polygon
const offset = indices[0];
const holes = indices.slice(1).map((n) => (n - offset) / coordLength);
// Compute triangulation
const triangles = earcut(polygonPositions, holes, coordLength, areas);
// Indices returned by triangulation are relative to start
// of polygon, so we need to offset
for (let t = 0, tl = triangles.length; t < tl; ++t) {
polygons.triangles.push(startPosition + triangles[t]);
}
}
/**
* Wraps an object containing array into accessors
*
* @param obj
* @param size
*/
function wrapProps(obj, size) {
const returnObj = {};
for (const key in obj) {
returnObj[key] = {
value: obj[key],
size
};
}
return returnObj;
const returnObj = {};
for (const key in obj) {
returnObj[key] = { value: obj[key], size };
}
return returnObj;
}
/**
* Wrap each array in an accessor object with value and size keys
*
* @param points
* @param lines
* @param polygons
* @param coordLength
* @returns object
*/
function makeAccessorObjects(points, lines, polygons, coordLength) {
const binaryFeatures = {
shape: 'binary-feature-collection',
points: {
...points,
positions: {
value: points.positions,
size: coordLength
},
globalFeatureIds: {
value: points.globalFeatureIds,
size: 1
},
featureIds: {
value: points.featureIds,
size: 1
},
numericProps: wrapProps(points.numericProps, 1)
},
lines: {
...lines,
positions: {
value: lines.positions,
size: coordLength
},
pathIndices: {
value: lines.pathIndices,
size: 1
},
globalFeatureIds: {
value: lines.globalFeatureIds,
size: 1
},
featureIds: {
value: lines.featureIds,
size: 1
},
numericProps: wrapProps(lines.numericProps, 1)
},
polygons: {
...polygons,
positions: {
value: polygons.positions,
size: coordLength
},
polygonIndices: {
value: polygons.polygonIndices,
size: 1
},
primitivePolygonIndices: {
value: polygons.primitivePolygonIndices,
size: 1
},
globalFeatureIds: {
value: polygons.globalFeatureIds,
size: 1
},
featureIds: {
value: polygons.featureIds,
size: 1
},
numericProps: wrapProps(polygons.numericProps, 1)
const binaryFeatures = {
shape: 'binary-feature-collection',
points: {
...points,
positions: { value: points.positions, size: coordLength },
globalFeatureIds: { value: points.globalFeatureIds, size: 1 },
featureIds: { value: points.featureIds, size: 1 },
numericProps: wrapProps(points.numericProps, 1)
},
lines: {
...lines,
positions: { value: lines.positions, size: coordLength },
pathIndices: { value: lines.pathIndices, size: 1 },
globalFeatureIds: { value: lines.globalFeatureIds, size: 1 },
featureIds: { value: lines.featureIds, size: 1 },
numericProps: wrapProps(lines.numericProps, 1)
},
polygons: {
...polygons,
positions: { value: polygons.positions, size: coordLength },
polygonIndices: { value: polygons.polygonIndices, size: 1 },
primitivePolygonIndices: { value: polygons.primitivePolygonIndices, size: 1 },
globalFeatureIds: { value: polygons.globalFeatureIds, size: 1 },
featureIds: { value: polygons.featureIds, size: 1 },
numericProps: wrapProps(polygons.numericProps, 1)
} // triangles not expected
};
if (binaryFeatures.polygons && polygons.triangles) {
binaryFeatures.polygons.triangles = { value: new Uint32Array(polygons.triangles), size: 1 };
}
};
if (binaryFeatures.polygons && polygons.triangles) {
binaryFeatures.polygons.triangles = {
value: new Uint32Array(polygons.triangles),
size: 1
};
}
return binaryFeatures;
return binaryFeatures;
}
/**
* Add numeric properties to object
*
* @param object
* @param properties
* @param index
* @param length
*/
function fillNumericProperties(object, properties, index, length) {
for (const numericPropName in object.numericProps) {
if (numericPropName in properties) {
const value = properties[numericPropName];
object.numericProps[numericPropName].fill(value, index, index + length);
for (const numericPropName in object.numericProps) {
if (numericPropName in properties) {
const value = properties[numericPropName];
object.numericProps[numericPropName].fill(value, index, index + length);
}
}
}
}
/**
* Keep string properties in object
*
* @param properties
* @param numericKeys
* @returns object
*/
function keepStringProperties(properties, numericKeys) {
const props = {};
for (const key in properties) {
if (!numericKeys.includes(key)) {
props[key] = properties[key];
const props = {};
for (const key in properties) {
if (!numericKeys.includes(key)) {
props[key] = properties[key];
}
}
}
return props;
return props;
}
/**
*
* Deduce correct array constructor to use for a given value
*
* @param x value to test
* @param constructor previous constructor deduced
* @returns PropArrayConstructor
*/
function deduceArrayType(x, constructor) {
if (constructor === Array || !Number.isFinite(x)) {
return Array;
}
return constructor === Float64Array || Math.fround(x) !== x ? Float64Array : Float32Array;
if (constructor === Array || !Number.isFinite(x)) {
return Array;
}
// If this or previous value required 64bits use Float64Array
return constructor === Float64Array || Math.fround(x) !== x ? Float64Array : Float32Array;
}
//# sourceMappingURL=flat-geojson-to-binary.js.map
import { extractGeometryInfo } from "./extract-geometry-info.js";
import { geojsonToFlatGeojson } from "./geojson-to-flat-geojson.js";
import { flatGeojsonToBinary } from "./flat-geojson-to-binary.js";
export function geojsonToBinary(features) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
fixRingWinding: true,
triangulate: true
};
const geometryInfo = extractGeometryInfo(features);
const coordLength = geometryInfo.coordLength;
const {
fixRingWinding
} = options;
const flatFeatures = geojsonToFlatGeojson(features, {
coordLength,
fixRingWinding
});
return flatGeojsonToBinary(flatFeatures, geometryInfo, {
numericPropKeys: options.numericPropKeys,
PositionDataType: options.PositionDataType || Float32Array,
triangulate: options.triangulate
});
/**
* Convert GeoJSON features to flat binary arrays
*
* @param features
* @param options
* @returns features in binary format, grouped by geometry type
*/
export function geojsonToBinary(features, options = { fixRingWinding: true, triangulate: true }) {
const geometryInfo = extractGeometryInfo(features);
const coordLength = geometryInfo.coordLength;
const { fixRingWinding } = options;
const flatFeatures = geojsonToFlatGeojson(features, { coordLength, fixRingWinding });
return flatGeojsonToBinary(flatFeatures, geometryInfo, {
numericPropKeys: options.numericPropKeys,
PositionDataType: options.PositionDataType || Float32Array,
triangulate: options.triangulate
});
}
//# sourceMappingURL=geojson-to-binary.js.map
import { getPolygonSignedArea } from '@math.gl/polygon';
export function geojsonToFlatGeojson(features) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
coordLength: 2,
fixRingWinding: true
};
return features.map(feature => flattenFeature(feature, options));
/**
* Convert GeoJSON features to Flat GeoJSON features
*
* @param features
* @param options
* @returns an Array of Flat GeoJSON features
*/
export function geojsonToFlatGeojson(features, options = { coordLength: 2, fixRingWinding: true }) {
return features.map((feature) => flattenFeature(feature, options));
}
/**
* Helper function to copy Point values from `coordinates` into `data` & `indices`
*
* @param coordinates
* @param data
* @param indices
* @param options
*/
function flattenPoint(coordinates, data, indices, options) {
indices.push(data.length);
data.push(...coordinates);
for (let i = coordinates.length; i < options.coordLength; i++) {
data.push(0);
}
indices.push(data.length);
data.push(...coordinates);
// Pad up to coordLength
for (let i = coordinates.length; i < options.coordLength; i++) {
data.push(0);
}
}
/**
* Helper function to copy LineString values from `coordinates` into `data` & `indices`
*
* @param coordinates
* @param data
* @param indices
* @param options
*/
function flattenLineString(coordinates, data, indices, options) {
indices.push(data.length);
for (const c of coordinates) {
data.push(...c);
for (let i = c.length; i < options.coordLength; i++) {
data.push(0);
indices.push(data.length);
for (const c of coordinates) {
data.push(...c);
// Pad up to coordLength
for (let i = c.length; i < options.coordLength; i++) {
data.push(0);
}
}
}
}
/**
* Helper function to copy Polygon values from `coordinates` into `data` & `indices` & `areas`
*
* @param coordinates
* @param data
* @param indices
* @param areas
* @param options
*/
function flattenPolygon(coordinates, data, indices, areas, options) {
let count = 0;
const ringAreas = [];
const polygons = [];
for (const lineString of coordinates) {
const lineString2d = lineString.map(p => p.slice(0, 2));
let area = getPolygonSignedArea(lineString2d.flat());
const ccw = area < 0;
if (options.fixRingWinding && (count === 0 && !ccw || count > 0 && ccw)) {
lineString.reverse();
area = -area;
let count = 0;
const ringAreas = [];
const polygons = [];
for (const lineString of coordinates) {
const lineString2d = lineString.map((p) => p.slice(0, 2));
let area = getPolygonSignedArea(lineString2d.flat());
const ccw = area < 0;
// Exterior ring must be CCW and interior rings CW
if (options.fixRingWinding && ((count === 0 && !ccw) || (count > 0 && ccw))) {
lineString.reverse();
area = -area;
}
ringAreas.push(area);
flattenLineString(lineString, data, polygons, options);
count++;
}
ringAreas.push(area);
flattenLineString(lineString, data, polygons, options);
count++;
}
if (count > 0) {
areas.push(ringAreas);
indices.push(polygons);
}
if (count > 0) {
areas.push(ringAreas);
indices.push(polygons);
}
}
/**
* Flatten single GeoJSON feature into Flat GeoJSON
*
* @param feature
* @param options
* @returns A Flat GeoJSON feature
*/
function flattenFeature(feature, options) {
const {
geometry
} = feature;
if (geometry.type === 'GeometryCollection') {
throw new Error('GeometryCollection type not supported');
}
const data = [];
const indices = [];
let areas;
let type;
switch (geometry.type) {
case 'Point':
type = 'Point';
flattenPoint(geometry.coordinates, data, indices, options);
break;
case 'MultiPoint':
type = 'Point';
geometry.coordinates.map(c => flattenPoint(c, data, indices, options));
break;
case 'LineString':
type = 'LineString';
flattenLineString(geometry.coordinates, data, indices, options);
break;
case 'MultiLineString':
type = 'LineString';
geometry.coordinates.map(c => flattenLineString(c, data, indices, options));
break;
case 'Polygon':
type = 'Polygon';
areas = [];
flattenPolygon(geometry.coordinates, data, indices, areas, options);
break;
case 'MultiPolygon':
type = 'Polygon';
areas = [];
geometry.coordinates.map(c => flattenPolygon(c, data, indices, areas, options));
break;
default:
throw new Error(`Unknown type: ${type}`);
}
return {
...feature,
geometry: {
type,
indices,
data,
areas
const { geometry } = feature;
if (geometry.type === 'GeometryCollection') {
throw new Error('GeometryCollection type not supported');
}
};
const data = [];
const indices = [];
let areas;
let type;
switch (geometry.type) {
case 'Point':
type = 'Point';
flattenPoint(geometry.coordinates, data, indices, options);
break;
case 'MultiPoint':
type = 'Point';
geometry.coordinates.map((c) => flattenPoint(c, data, indices, options));
break;
case 'LineString':
type = 'LineString';
flattenLineString(geometry.coordinates, data, indices, options);
break;
case 'MultiLineString':
type = 'LineString';
geometry.coordinates.map((c) => flattenLineString(c, data, indices, options));
break;
case 'Polygon':
type = 'Polygon';
areas = [];
flattenPolygon(geometry.coordinates, data, indices, areas, options);
break;
case 'MultiPolygon':
type = 'Polygon';
areas = [];
geometry.coordinates.map((c) => flattenPolygon(c, data, indices, areas, options));
break;
default:
throw new Error(`Unknown type: ${type}`);
}
return { ...feature, geometry: { type, indices, data, areas } };
}
//# sourceMappingURL=geojson-to-flat-geojson.js.map

@@ -0,40 +1,54 @@

/**
* Apply transformation to every coordinate of binary features
* @param binaryFeatures binary features
* @param transformCoordinate Function to call on each coordinate
* @return Transformed binary features
*/
export function transformBinaryCoords(binaryFeatures, transformCoordinate) {
if (binaryFeatures.points) {
transformBinaryGeometryPositions(binaryFeatures.points, transformCoordinate);
}
if (binaryFeatures.lines) {
transformBinaryGeometryPositions(binaryFeatures.lines, transformCoordinate);
}
if (binaryFeatures.polygons) {
transformBinaryGeometryPositions(binaryFeatures.polygons, transformCoordinate);
}
return binaryFeatures;
if (binaryFeatures.points) {
transformBinaryGeometryPositions(binaryFeatures.points, transformCoordinate);
}
if (binaryFeatures.lines) {
transformBinaryGeometryPositions(binaryFeatures.lines, transformCoordinate);
}
if (binaryFeatures.polygons) {
transformBinaryGeometryPositions(binaryFeatures.polygons, transformCoordinate);
}
return binaryFeatures;
}
/** Transform one binary geometry */
function transformBinaryGeometryPositions(binaryGeometry, fn) {
const {
positions
} = binaryGeometry;
for (let i = 0; i < positions.value.length; i += positions.size) {
const coord = Array.from(positions.value.subarray(i, i + positions.size));
const transformedCoord = fn(coord);
positions.value.set(transformedCoord, i);
}
const { positions } = binaryGeometry;
for (let i = 0; i < positions.value.length; i += positions.size) {
// @ts-ignore inclusion of bigint causes problems
const coord = Array.from(positions.value.subarray(i, i + positions.size));
const transformedCoord = fn(coord);
// @ts-ignore typescript typing for .set seems to require bigint?
positions.value.set(transformedCoord, i);
}
}
/**
* Apply transformation to every coordinate of GeoJSON features
*
* @param features Array of GeoJSON features
* @param fn Function to call on each coordinate
* @return Transformed GeoJSON features
*/
export function transformGeoJsonCoords(features, fn) {
for (const feature of features) {
feature.geometry.coordinates = coordMap(feature.geometry.coordinates, fn);
}
return features;
for (const feature of features) {
// @ts-ignore
feature.geometry.coordinates = coordMap(feature.geometry.coordinates, fn);
}
return features;
}
function coordMap(array, fn) {
if (isCoord(array)) {
return fn(array);
}
return array.map(item => {
return coordMap(item, fn);
});
if (isCoord(array)) {
return fn(array);
}
return array.map((item) => {
return coordMap(item, fn);
});
}
function isCoord(array) {
return Array.isArray(array) && Number.isFinite(array[0]) && Number.isFinite(array[1]);
return Array.isArray(array) && Number.isFinite(array[0]) && Number.isFinite(array[1]);
}
//# sourceMappingURL=transform.js.map

@@ -1,43 +0,70 @@

const GEOARROW_ENCODINGS = ['geoarrow.multipolygon', 'geoarrow.polygon', 'geoarrow.multilinestring', 'geoarrow.linestring', 'geoarrow.multipoint', 'geoarrow.point', 'geoarrow.wkb', 'geoarrow.wkt'];
// loaders.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
/** Array containing all encodings */
const GEOARROW_ENCODINGS = [
'geoarrow.multipolygon',
'geoarrow.polygon',
'geoarrow.multilinestring',
'geoarrow.linestring',
'geoarrow.multipoint',
'geoarrow.point',
'geoarrow.wkb',
'geoarrow.wkt'
];
const GEOARROW_COLUMN_METADATA_ENCODING = 'ARROW:extension:name';
const GEOARROW_COLUMN_METADATA_METADATA = 'ARROW:extension:metadata';
/**
* get geometry columns from arrow table
*/
export function getGeometryColumnsFromSchema(schema) {
const geometryColumns = {};
for (const field of schema.fields) {
const metadata = getGeometryMetadataForField(field);
if (metadata) {
geometryColumns[field.name] = metadata;
const geometryColumns = {};
for (const field of schema.fields) {
const metadata = getGeometryMetadataForField(field);
if (metadata) {
geometryColumns[field.name] = metadata;
}
}
}
return geometryColumns;
return geometryColumns;
}
/**
* Extracts GeoArrow metadata from a field
* @param field
* @returns
* @see https://github.com/geoarrow/geoarrow/blob/d2f56704414d9ae71e8a5170a8671343ed15eefe/extension-types.md
*/
export function getGeometryMetadataForField(field) {
var _field$metadata, _field$metadata2;
let metadata = null;
let geoEncoding = (_field$metadata = field.metadata) === null || _field$metadata === void 0 ? void 0 : _field$metadata[GEOARROW_COLUMN_METADATA_ENCODING];
if (geoEncoding) {
geoEncoding = geoEncoding.toLowerCase();
if (geoEncoding === 'wkb') {
geoEncoding = 'geoarrow.wkb';
let metadata = null;
// Check for GeoArrow column encoding
let geoEncoding = field.metadata?.[GEOARROW_COLUMN_METADATA_ENCODING];
if (geoEncoding) {
geoEncoding = geoEncoding.toLowerCase();
// at time of testing, ogr2ogr uses WKB/WKT for encoding.
if (geoEncoding === 'wkb') {
geoEncoding = 'geoarrow.wkb';
}
if (geoEncoding === 'wkt') {
geoEncoding = 'geoarrow.wkt';
}
if (!GEOARROW_ENCODINGS.includes(geoEncoding)) {
// eslint-disable-next-line no-console
console.warn(`Invalid GeoArrow encoding: ${geoEncoding}`);
}
else {
metadata = metadata || {};
metadata.encoding = geoEncoding;
}
}
if (geoEncoding === 'wkt') {
geoEncoding = 'geoarrow.wkt';
// Check for GeoArrow metadata
const columnMetadata = field.metadata?.[GEOARROW_COLUMN_METADATA_METADATA];
if (columnMetadata) {
try {
metadata = JSON.parse(columnMetadata);
}
catch (error) {
// eslint-disable-next-line no-console
console.warn('Failed to parse GeoArrow metadata', error);
}
}
if (!GEOARROW_ENCODINGS.includes(geoEncoding)) {
console.warn(`Invalid GeoArrow encoding: ${geoEncoding}`);
} else {
metadata = metadata || {};
metadata.encoding = geoEncoding;
}
}
const columnMetadata = (_field$metadata2 = field.metadata) === null || _field$metadata2 === void 0 ? void 0 : _field$metadata2[GEOARROW_COLUMN_METADATA_METADATA];
if (columnMetadata) {
try {
metadata = JSON.parse(columnMetadata);
} catch (error) {
console.warn('Failed to parse GeoArrow metadata', error);
}
}
return metadata || null;
return metadata || null;
}
//# sourceMappingURL=geoarrow-metadata.js.map

@@ -0,76 +1,69 @@

// loaders.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
/* eslint-disable camelcase */
/**
* Geoparquet JSON schema for geo metadata
* @see https://github.com/geoarrow/geoarrow/blob/main/metadata.md
* @see https://github.com/opengeospatial/geoparquet/blob/main/format-specs/geoparquet.md
*/
export const GEOPARQUET_METADATA_JSON_SCHEMA = {
$schema: 'http://json-schema.org/draft-07/schema#',
title: 'GeoParquet',
description: 'Parquet metadata included in the geo field.',
type: 'object',
required: ['version', 'primary_column', 'columns'],
properties: {
version: {
type: 'string',
const: '1.0.0-beta.1'
},
primary_column: {
type: 'string',
minLength: 1
},
columns: {
type: 'object',
minProperties: 1,
patternProperties: {
'.+': {
type: 'object',
required: ['encoding', 'geometry_types'],
properties: {
encoding: {
type: 'string',
const: 'WKB'
$schema: 'http://json-schema.org/draft-07/schema#',
title: 'GeoParquet',
description: 'Parquet metadata included in the geo field.',
type: 'object',
required: ['version', 'primary_column', 'columns'],
properties: {
version: { type: 'string', const: '1.0.0-beta.1' },
primary_column: { type: 'string', minLength: 1 },
columns: {
type: 'object',
minProperties: 1,
patternProperties: {
'.+': {
type: 'object',
required: ['encoding', 'geometry_types'],
properties: {
encoding: { type: 'string', const: 'WKB' },
geometry_types: {
type: 'array',
uniqueItems: true,
items: {
type: 'string',
pattern: '^(GeometryCollection|(Multi)?(Point|LineString|Polygon))( Z)?$'
}
},
crs: {
oneOf: [
{
$ref: 'https://proj.org/schemas/v0.5/projjson.schema.json'
},
{ type: 'null' }
]
},
edges: { type: 'string', enum: ['planar', 'spherical'] },
orientation: { type: 'string', const: 'counterclockwise' },
bbox: {
type: 'array',
items: { type: 'number' },
oneOf: [
{
description: '2D bbox consisting of (xmin, ymin, xmax, ymax)',
minItems: 4,
maxItems: 4
},
{
description: '3D bbox consisting of (xmin, ymin, zmin, xmax, ymax, zmax)',
minItems: 6,
maxItems: 6
}
]
},
epoch: { type: 'number' }
}
}
},
geometry_types: {
type: 'array',
uniqueItems: true,
items: {
type: 'string',
pattern: '^(GeometryCollection|(Multi)?(Point|LineString|Polygon))( Z)?$'
}
},
crs: {
oneOf: [{
$ref: 'https://proj.org/schemas/v0.5/projjson.schema.json'
}, {
type: 'null'
}]
},
edges: {
type: 'string',
enum: ['planar', 'spherical']
},
orientation: {
type: 'string',
const: 'counterclockwise'
},
bbox: {
type: 'array',
items: {
type: 'number'
},
oneOf: [{
description: '2D bbox consisting of (xmin, ymin, xmax, ymax)',
minItems: 4,
maxItems: 4
}, {
description: '3D bbox consisting of (xmin, ymin, zmin, xmax, ymax, zmax)',
minItems: 6,
maxItems: 6
}]
},
epoch: {
type: 'number'
}
}
additionalProperties: false
}
},
additionalProperties: false
}
}
};
//# sourceMappingURL=geoparquet-metadata-schema.js.map

@@ -0,97 +1,116 @@

// GEO METADATA
/**
* Reads the GeoMetadata object from the metadata
* @note geoarrow / parquet schema is stringified into a single key-value pair in the parquet metadata
*/
export function getGeoMetadata(schema) {
const geoMetadata = parseJSONStringMetadata(schema, 'geo');
if (!geoMetadata) {
return null;
}
for (const column of Object.values(geoMetadata.columns || {})) {
if (column.encoding) {
column.encoding = column.encoding.toLowerCase();
const geoMetadata = parseJSONStringMetadata(schema, 'geo');
if (!geoMetadata) {
return null;
}
}
return geoMetadata;
for (const column of Object.values(geoMetadata.columns || {})) {
if (column.encoding) {
column.encoding = column.encoding.toLowerCase();
}
}
return geoMetadata;
}
/**
* Stores a geoarrow / geoparquet geo metadata object in the schema
* @note geoarrow / geoparquet geo metadata is a single stringified JSON field
*/
export function setGeoMetadata(schema, geoMetadata) {
const stringifiedGeoMetadata = JSON.stringify(geoMetadata);
schema.metadata.geo = stringifiedGeoMetadata;
const stringifiedGeoMetadata = JSON.stringify(geoMetadata);
schema.metadata.geo = stringifiedGeoMetadata;
}
/**
* Unpacks geo metadata into separate metadata fields (parses the long JSON string)
* @note geoarrow / parquet schema is stringified into a single key-value pair in the parquet metadata
*/
export function unpackGeoMetadata(schema) {
const geoMetadata = getGeoMetadata(schema);
if (!geoMetadata) {
return;
}
const {
version,
primary_column,
columns
} = geoMetadata;
if (version) {
schema.metadata['geo.version'] = version;
}
if (primary_column) {
schema.metadata['geo.primary_column'] = primary_column;
}
schema.metadata['geo.columns'] = Object.keys(columns || {}).join('');
for (const [columnName, columnMetadata] of Object.entries(columns || {})) {
const field = schema.fields.find(field => field.name === columnName);
if (field) {
if (field.name === primary_column) {
setFieldMetadata(field, 'geo.primary_field', 'true');
}
unpackGeoFieldMetadata(field, columnMetadata);
const geoMetadata = getGeoMetadata(schema);
if (!geoMetadata) {
return;
}
}
// Store Parquet Schema Level Metadata
const { version, primary_column, columns } = geoMetadata;
if (version) {
schema.metadata['geo.version'] = version;
}
if (primary_column) {
schema.metadata['geo.primary_column'] = primary_column;
}
// store column names as comma separated list
schema.metadata['geo.columns'] = Object.keys(columns || {}).join('');
for (const [columnName, columnMetadata] of Object.entries(columns || {})) {
const field = schema.fields.find((field) => field.name === columnName);
if (field) {
if (field.name === primary_column) {
setFieldMetadata(field, 'geo.primary_field', 'true');
}
unpackGeoFieldMetadata(field, columnMetadata);
}
}
}
// eslint-disable-next-line complexity
function unpackGeoFieldMetadata(field, columnMetadata) {
for (const [key, value] of Object.entries(columnMetadata || {})) {
switch (key) {
case 'geometry_types':
setFieldMetadata(field, `geo.${key}`, value.join(','));
break;
case 'bbox':
setFieldMetadata(field, `geo.crs.${key}`, JSON.stringify(value));
break;
case 'crs':
for (const [crsKey, crsValue] of Object.entries(value || {})) {
switch (crsKey) {
case 'id':
const crsId = typeof crsValue === 'object' ? `${crsValue === null || crsValue === void 0 ? void 0 : crsValue.authority}:${crsValue === null || crsValue === void 0 ? void 0 : crsValue.code}` : JSON.stringify(crsValue);
setFieldMetadata(field, `geo.crs.${crsKey}`, crsId);
break;
for (const [key, value] of Object.entries(columnMetadata || {})) {
switch (key) {
case 'geometry_types':
setFieldMetadata(field, `geo.${key}`, value.join(','));
break;
case 'bbox':
setFieldMetadata(field, `geo.crs.${key}`, JSON.stringify(value));
break;
case 'crs':
// @ts-ignore
for (const [crsKey, crsValue] of Object.entries(value || {})) {
switch (crsKey) {
case 'id':
const crsId = typeof crsValue === 'object'
? // @ts-ignore
`${crsValue?.authority}:${crsValue?.code}`
: JSON.stringify(crsValue);
setFieldMetadata(field, `geo.crs.${crsKey}`, crsId);
break;
default:
setFieldMetadata(field, `geo.crs.${crsKey}`, typeof crsValue === 'string' ? crsValue : JSON.stringify(crsValue));
break;
}
}
break;
case 'edges':
default:
setFieldMetadata(field, `geo.crs.${crsKey}`, typeof crsValue === 'string' ? crsValue : JSON.stringify(crsValue));
break;
}
setFieldMetadata(field, `geo.${key}`, typeof value === 'string' ? value : JSON.stringify(value));
}
break;
case 'edges':
default:
setFieldMetadata(field, `geo.${key}`, typeof value === 'string' ? value : JSON.stringify(value));
}
}
}
function setFieldMetadata(field, key, value) {
field.metadata = field.metadata || {};
field.metadata[key] = value;
field.metadata = field.metadata || {};
field.metadata[key] = value;
}
// HELPERS
/** Parse a key with stringified arrow metadata */
export function parseJSONStringMetadata(schema, metadataKey) {
const stringifiedMetadata = schema.metadata[metadataKey];
if (!stringifiedMetadata) {
return null;
}
try {
const metadata = JSON.parse(stringifiedMetadata);
if (!metadata || typeof metadata !== 'object') {
return null;
const stringifiedMetadata = schema.metadata[metadataKey];
if (!stringifiedMetadata) {
return null;
}
return metadata;
} catch {
return null;
}
try {
const metadata = JSON.parse(stringifiedMetadata);
if (!metadata || typeof metadata !== 'object') {
return null;
}
return metadata;
}
catch {
return null;
}
}
export function unpackJSONStringMetadata(schema, metadataKey) {
const json = parseJSONStringMetadata(schema, metadataKey);
for (const [key, value] of Object.entries(json || {})) {
schema.metadata[`${metadataKey}.${key}`] = typeof value === 'string' ? value : JSON.stringify(value);
}
const json = parseJSONStringMetadata(schema, metadataKey);
for (const [key, value] of Object.entries(json || {})) {
schema.metadata[`${metadataKey}.${key}`] =
typeof value === 'string' ? value : JSON.stringify(value);
}
}
//# sourceMappingURL=geoparquet-metadata.js.map

@@ -0,48 +1,44 @@

// loaders.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import { getTableLength, getTableRowAsObject } from '@loaders.gl/schema';
import { getGeoMetadata } from "../geo/geoparquet-metadata.js";
/** TODO - move to loaders.gl/gis? */
export function convertWKBTableToGeoJSON(table, schema, loaders) {
const geoMetadata = getGeoMetadata(schema);
const primaryColumn = geoMetadata === null || geoMetadata === void 0 ? void 0 : geoMetadata.primary_column;
if (!primaryColumn) {
throw new Error('no geometry column');
}
const columnMetadata = geoMetadata.columns[primaryColumn];
const features = [];
const length = getTableLength(table);
for (let rowIndex = 0; rowIndex < length; rowIndex++) {
const row = getTableRowAsObject(table, rowIndex);
const geometry = parseGeometry(row[primaryColumn], columnMetadata, loaders);
delete row[primaryColumn];
const feature = {
type: 'Feature',
geometry: geometry,
properties: row
};
features.push(feature);
}
return {
shape: 'geojson-table',
schema,
type: 'FeatureCollection',
features
};
const geoMetadata = getGeoMetadata(schema);
const primaryColumn = geoMetadata?.primary_column;
if (!primaryColumn) {
throw new Error('no geometry column');
}
const columnMetadata = geoMetadata.columns[primaryColumn];
const features = [];
const length = getTableLength(table);
for (let rowIndex = 0; rowIndex < length; rowIndex++) {
const row = getTableRowAsObject(table, rowIndex);
const geometry = parseGeometry(row[primaryColumn], columnMetadata, loaders);
delete row[primaryColumn];
const feature = { type: 'Feature', geometry: geometry, properties: row };
features.push(feature);
}
return { shape: 'geojson-table', schema, type: 'FeatureCollection', features };
}
function parseGeometry(geometry, columnMetadata, loaders) {
var _wktLoader$parseTextS, _wkbLoader$parseSync;
switch (columnMetadata.encoding) {
case 'wkt':
const wktLoader = loaders.find(loader => loader.id === 'wkt');
return (wktLoader === null || wktLoader === void 0 ? void 0 : (_wktLoader$parseTextS = wktLoader.parseTextSync) === null || _wktLoader$parseTextS === void 0 ? void 0 : _wktLoader$parseTextS.call(wktLoader, geometry)) || null;
case 'wkb':
default:
const wkbLoader = loaders.find(loader => loader.id === 'wkb');
const arrayBuffer = ArrayBuffer.isView(geometry) ? geometry.buffer.slice(geometry.byteOffset, geometry.byteOffset + geometry.byteLength) : geometry;
const geojson = wkbLoader === null || wkbLoader === void 0 ? void 0 : (_wkbLoader$parseSync = wkbLoader.parseSync) === null || _wkbLoader$parseSync === void 0 ? void 0 : _wkbLoader$parseSync.call(wkbLoader, arrayBuffer, {
wkb: {
shape: 'geojson-geometry'
}
});
return geojson;
}
switch (columnMetadata.encoding) {
case 'wkt':
const wktLoader = loaders.find((loader) => loader.id === 'wkt');
return wktLoader?.parseTextSync?.(geometry) || null;
case 'wkb':
default:
const wkbLoader = loaders.find((loader) => loader.id === 'wkb');
const arrayBuffer = ArrayBuffer.isView(geometry)
? geometry.buffer.slice(geometry.byteOffset, geometry.byteOffset + geometry.byteLength)
: geometry;
const geojson = wkbLoader?.parseSync?.(arrayBuffer, {
wkb: { shape: 'geojson-geometry' }
});
return geojson; // binaryGeometry ? binaryToGeometry(binaryGeometry) : null;
// const binaryGeometry = WKBLoader.parseSync?.(geometry);
// ts-ignore
// return binaryGeometry ? binaryToGeometry(binaryGeometry) : null;
}
}
//# sourceMappingURL=convert-table-to-geojson.js.map
{
"name": "@loaders.gl/gis",
"description": "Helpers for GIS category data",
"version": "4.2.0-alpha.4",
"version": "4.2.0-alpha.5",
"license": "MIT",

@@ -35,4 +35,4 @@ "type": "module",

"dependencies": {
"@loaders.gl/loader-utils": "4.2.0-alpha.4",
"@loaders.gl/schema": "4.2.0-alpha.4",
"@loaders.gl/loader-utils": "4.2.0-alpha.5",
"@loaders.gl/schema": "4.2.0-alpha.5",
"@mapbox/vector-tile": "^1.3.1",

@@ -45,3 +45,6 @@ "@math.gl/polygon": "^4.0.0",

},
"gitHead": "6c52dee5c3f005648a394cc4aee7fc37005c8e83"
"peerDependencies": {
"@loaders.gl/core": "^4.0.0"
},
"gitHead": "32d95a81971f104e4dfeb88ab57065f05321a76a"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc