Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

fix(processors): preserve GeoPackage altitude values (#3847)

Igor Dykhtafea21640ba7a
diff --git a/src/processors/src/export/dataset-to-geojson.ts b/src/processors/src/export/dataset-to-geojson.ts
index 55d6fbbe0..49a8de5af 100644
--- a/src/processors/src/export/dataset-to-geojson.ts
+++ b/src/processors/src/export/dataset-to-geojson.ts
@@ -99,8 +99,10 @@ function pointGeometry(
return null;
}
const altitudeIndex = pair.pair.altitude?.fieldIdx;
+ const altitudeValue =
+ altitudeIndex === undefined ? undefined : data.valueAt(rowIndex, altitudeIndex);
const altitude =
- altitudeIndex === undefined ? undefined : Number(data.valueAt(rowIndex, altitudeIndex));
+ altitudeValue == null || altitudeValue === '' ? undefined : Number(altitudeValue);
const coordinates: Position =
altitude !== undefined && Number.isFinite(altitude) ? [lng, lat, altitude] : [lng, lat];
return {type: 'Point', coordinates};
diff --git a/src/processors/src/export/geopackage-writer.ts b/src/processors/src/export/geopackage-writer.ts
index 0a35bfdef..076c6a94c 100644
--- a/src/processors/src/export/geopackage-writer.ts
+++ b/src/processors/src/export/geopackage-writer.ts
@@ -60,19 +60,7 @@ function emptyBoundingBox(): BoundingBox {
return {minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity};
}
-function extendBoundingBox(box: BoundingBox, geometry: Geometry): void {
- const visit = (position: Position) => {
- const x = Number(position[0]);
- const y = Number(position[1]);
- if (Number.isFinite(x)) {
- box.minX = Math.min(box.minX, x);
- box.maxX = Math.max(box.maxX, x);
- }
- if (Number.isFinite(y)) {
- box.minY = Math.min(box.minY, y);
- box.maxY = Math.max(box.maxY, y);
- }
- };
+function visitPositions(geometry: Geometry, visit: (position: Position) => void): void {
const walk = (geom: Geometry) => {
switch (geom.type) {
case 'Point':
@@ -99,6 +87,31 @@ function extendBoundingBox(box: BoundingBox, geometry: Geometry): void {
walk(geometry);
}
+function geometryHasZ(geometry: Geometry): boolean {
+ let hasPosition = false;
+ let hasZ = true;
+ visitPositions(geometry, position => {
+ hasPosition = true;
+ hasZ = hasZ && position.length >= 3;
+ });
+ return hasPosition && hasZ;
+}
+
+function extendBoundingBox(box: BoundingBox, geometry: Geometry): void {
+ visitPositions(geometry, position => {
+ const x = Number(position[0]);
+ const y = Number(position[1]);
+ if (Number.isFinite(x)) {
+ box.minX = Math.min(box.minX, x);
+ box.maxX = Math.max(box.maxX, x);
+ }
+ if (Number.isFinite(y)) {
+ box.minY = Math.min(box.minY, y);
+ box.maxY = Math.max(box.maxY, y);
+ }
+ });
+}
+
function quoteIdentifier(value: string): string {
return `"${value.replace(/"/g, '""')}"`;
}
@@ -164,7 +177,7 @@ function coerceValue(value: unknown, type: ColumnType): number | string | null {
*/
function geoPackageBlob(geometry: Geometry | null): Uint8Array | null {
if (!geometry) return null;
- const wkb = new Uint8Array(convertGeometryToWKB(geometry));
+ const wkb = new Uint8Array(convertGeometryToWKB(geometry, {hasZ: geometryHasZ(geometry)}));
const header = new Uint8Array(8);
const view = new DataView(header.buffer);
header[0] = 0x47; // 'G'
@@ -293,9 +306,11 @@ export function writeGeoPackageSync(
const box: BoundingBox = emptyBoundingBox();
const geometryTypes = new Set<string>();
+ const geometryZ = new Set<boolean>();
for (const feature of features) {
if (feature.geometry) {
geometryTypes.add(feature.geometry.type);
+ geometryZ.add(geometryHasZ(feature.geometry));
extendBoundingBox(box, feature.geometry);
}
}
@@ -355,11 +370,13 @@ export function writeGeoPackageSync(
contents.free();
}
- db.run(`INSERT INTO gpkg_geometry_columns VALUES (?, ?, ?, ?, 0, 0);`, [
+ const z = geometryZ.has(true) ? (geometryZ.has(false) ? 2 : 1) : 0;
+ db.run(`INSERT INTO gpkg_geometry_columns VALUES (?, ?, ?, ?, ?, 0);`, [
tableName,
GEOMETRY_COLUMN,
geometryTypeName(geometryTypes),
- SRS_ID
+ SRS_ID,
+ z
]);
db.run(`INSERT INTO gpkg_ogr_contents (table_name, feature_count) VALUES (?, ?);`, [
tableName,
diff --git a/src/processors/src/export/vector-export.spec.ts b/src/processors/src/export/vector-export.spec.ts
index 2b9bbfde4..db80a4f2a 100644
--- a/src/processors/src/export/vector-export.spec.ts
+++ b/src/processors/src/export/vector-export.spec.ts
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
+import {convertWKBToGeometry} from '@loaders.gl/gis';
import {convertArrowToSchema} from '@loaders.gl/schema-utils';
import {ALL_FIELD_TYPES, EXPORT_DATA_TYPE} from '@kepler.gl/constants';
import {Field} from '@kepler.gl/types';
@@ -112,6 +113,76 @@ describe('dataset export', () => {
expect(parquetBytes.includes(Buffer.from('hidden-value'))).toBe(false);
});
+ test.each([
+ {
+ dimension: '2D',
+ altitudes: [null, ''],
+ expectedZ: 0,
+ expectedCoordinates: [
+ [-122.4, 37.8],
+ [-122.41, 37.81]
+ ]
+ },
+ {
+ dimension: '3D',
+ altitudes: [12, 24],
+ expectedZ: 1,
+ expectedCoordinates: [
+ [-122.4, 37.8, 12],
+ [-122.41, 37.81, 24]
+ ]
+ },
+ {
+ dimension: 'mixed 2D and 3D',
+ altitudes: [12, null],
+ expectedZ: 2,
+ expectedCoordinates: [
+ [-122.4, 37.8, 12],
+ [-122.41, 37.81]
+ ]
+ }
+ ])(
+ 'writes $dimension GeoPackage points with matching Z metadata',
+ async ({altitudes, expectedZ, expectedCoordinates}) => {
+ const altitudeFields = [
+ ...fields,
+ {name: 'altitude', type: ALL_FIELD_TYPES.real}
+ ] as Field[];
+ const altitudeFieldPairs = [
+ {
+ defaultName: 'point',
+ pair: {
+ lat: {fieldIdx: 2, value: 'lat'},
+ lng: {fieldIdx: 3, value: 'lng'},
+ altitude: {fieldIdx: 4, value: 'altitude'}
+ },
+ suffix: ['lat', 'lng', 'altitude']
+ }
+ ];
+ const altitudeRows = rows.map((row, index) => [...row, altitudes[index]]);
+ const geopackage = await encodeDatasetExport({
+ data: createDataContainer(altitudeRows),
+ fields: altitudeFields,
+ fieldPairs: altitudeFieldPairs,
+ dataType: EXPORT_DATA_TYPE.GEOPACKAGE,
+ layerName: 'places'
+ });
+ const SQL = await loadSqlJs();
+ const db = new SQL.Database(new Uint8Array(geopackage.data as Uint8Array));
+ try {
+ const z = db.exec('SELECT z FROM gpkg_geometry_columns')[0].values[0][0];
+ const coordinates = db.exec('SELECT geom FROM places ORDER BY fid')[0].values.map(row => {
+ const blob = row[0] as Uint8Array;
+ const geometry = convertWKBToGeometry(blob.slice(8).buffer);
+ return geometry.type === 'Point' ? geometry.coordinates : null;
+ });
+ expect({z, coordinates}).toEqual({z: expectedZ, coordinates: expectedCoordinates});
+ } finally {
+ db.close();
+ }
+ }
+ );
+
test('rejects mixed geometry families when exporting a shapefile', async () => {
const geometryFields = [
{name: '_geojson', type: ALL_FIELD_TYPES.geojson},