Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
fix(processors): preserve integer GeoParquet fields (#3846)
Igor Dykhtad65c82761a81
diff --git a/src/processors/src/export/geoparquet-writer.ts b/src/processors/src/export/geoparquet-writer.tsindex 9a2dee705..562db0aa6 100644--- a/src/processors/src/export/geoparquet-writer.ts+++ b/src/processors/src/export/geoparquet-writer.ts@@ -1,6 +1,9 @@// SPDX-License-Identifier: MIT// Copyright contributors to the kepler.gl project+import {ALL_FIELD_TYPES} from '@kepler.gl/constants';+import type {Field} from '@kepler.gl/types';+import type {DataContainerInterface} from '@kepler.gl/utils';import {convertGeometryToWKB} from '@loaders.gl/gis';import * as arrow from 'apache-arrow';import type {FeatureCollection, Geometry} from 'geojson';@@ -57,7 +60,41 @@ function wkbBytes(geometry: Geometry | null): Uint8Array | null {return new Uint8Array(convertGeometryToWKB(geometry));}-function propertyVector(values: (string | number | boolean | null)[]): arrow.Vector {+const MIN_INT64 = -(1n << 63n);+const MAX_INT64 = (1n << 63n) - 1n;++function int64Vector(values: unknown[]): arrow.Vector | null {+ const integers: (bigint | null)[] = [];+ for (const value of values) {+ if (value == null) {+ integers.push(null);+ continue;+ }+ const integer =+ typeof value === 'bigint'+ ? value+ : typeof value === 'number' && Number.isSafeInteger(value)+ ? BigInt(value)+ : null;+ if (integer === null || integer < MIN_INT64 || integer > MAX_INT64) {+ return null;+ }+ integers.push(integer);+ }+ return arrow.vectorFromArray(integers, new arrow.Int64());+}++function propertyVector(+ values: (string | number | boolean | null)[],+ fieldType?: string,+ rawValues: unknown[] = values+): arrow.Vector {+ if (fieldType === ALL_FIELD_TYPES.integer) {+ const integers = int64Vector(rawValues);+ if (integers) {+ return integers;+ }+ }// tableFromArrays stores JS strings as Dictionary<Int32, Utf8>. Parquet import// rejects that type with "arrow type not supported: Dictionary".let kind: 'bool' | 'float' | 'string' = 'string';@@ -123,8 +160,13 @@ function propertyName(name: string, taken: Set<string>): string {/*** Write a GeoJSON FeatureCollection as GeoParquet 1.1 (WKB geometry, WGS84).*/-export async function writeGeoParquet(geojson: FeatureCollection): Promise<Uint8Array> {+export async function writeGeoParquet(+ geojson: FeatureCollection,+ fields: Field[] = [],+ data?: DataContainerInterface+): Promise<Uint8Array> {const features = geojson.features ?? [];+ const fieldsByName = new Map(fields.map((field, index) => [field.name, {field, index}]));const taken = new Set<string>(['geometry']);const propertyKeys: {source: string; name: string}[] = [];const seen = new Set<string>();@@ -144,9 +186,13 @@ export async function writeGeoParquet(geojson: FeatureCollection): Promise<Uint8)};for (const key of propertyKeys) {- columns[key.name] = propertyVector(- features.map(feature => parquetValue(feature.properties?.[key.source]))- );+ const values = features.map(feature => parquetValue(feature.properties?.[key.source]));+ const sourceField = fieldsByName.get(key.source);+ const rawValues =+ sourceField && data+ ? features.map((_, rowIndex) => data.valueAt(rowIndex, sourceField.index))+ : values;+ columns[key.name] = propertyVector(values, sourceField?.field.type, rawValues);}const table = new arrow.Table(columns);diff --git a/src/processors/src/export/vector-export.spec.ts b/src/processors/src/export/vector-export.spec.tsindex cd69e048b..2b9bbfde4 100644--- a/src/processors/src/export/vector-export.spec.ts+++ b/src/processors/src/export/vector-export.spec.ts@@ -168,6 +168,62 @@ describe('dataset export', () => {expect(schema.fields.find(field => field.name === 'name')?.type).toBe('utf8');});+ test('writes typed integer fields without reclassifying integer-valued real fields', async () => {+ const typedFields = [+ {name: 'count', type: ALL_FIELD_TYPES.integer},+ {name: 'measurement', type: ALL_FIELD_TYPES.real},+ {name: 'unsafe_count', type: ALL_FIELD_TYPES.integer},+ {name: 'out_of_range_count', type: ALL_FIELD_TYPES.integer},+ {name: 'lat', type: ALL_FIELD_TYPES.real},+ {name: 'lng', type: ALL_FIELD_TYPES.real}+ ] as Field[];+ const typedRows = [+ [1, 1, Number.MAX_SAFE_INTEGER + 1, 9223372036854775808n, 37.8, -122.4],+ [null, 2, null, null, 37.81, -122.41],+ [2147483648, 3, null, null, 37.82, -122.42],+ [Number.MAX_SAFE_INTEGER, 4, null, null, 37.83, -122.43]+ ];+ const geoparquet = await encodeDatasetExport({+ data: createDataContainer(typedRows),+ fields: typedFields,+ fieldPairs: [+ {+ defaultName: 'point',+ pair: {+ lat: {fieldIdx: 4, value: 'lat'},+ lng: {fieldIdx: 5, value: 'lng'}+ },+ suffix: ['lat', 'lng']+ }+ ],+ dataType: EXPORT_DATA_TYPE.GEOPARQUET,+ layerName: 'typed-numbers'+ });+ const specifier = 'parquet-wasm/node';+ const imported = (await import(specifier)) as {+ readParquet?: (bytes: Uint8Array) => {intoIPCStream(): Uint8Array};+ default?: {readParquet: (bytes: Uint8Array) => {intoIPCStream(): Uint8Array}};+ };+ const api = imported.readParquet ? imported : imported.default;+ if (!api?.readParquet) {+ throw new Error('parquet-wasm failed to load');+ }+ const wasmTable = api.readParquet(new Uint8Array(geoparquet.data as Uint8Array));+ const arrowTable = tableFromIPC(wasmTable.intoIPCStream());++ const count = arrowTable.getChild('count');+ expect(String(count?.type)).toBe('Int64');+ expect(Array.from({length: count?.length ?? 0}, (_, index) => count?.get(index))).toEqual([+ 1n,+ null,+ 2147483648n,+ BigInt(Number.MAX_SAFE_INTEGER)+ ]);+ expect(String(arrowTable.getChild('measurement')?.type)).toBe('Float64');+ expect(String(arrowTable.getChild('unsafe_count')?.type)).toBe('Float64');+ expect(String(arrowTable.getChild('out_of_range_count')?.type)).toBe('Utf8');+ });+test.each(['gpkg_contents', 'gpkg_geometry_columns', 'sqlite_sequence'])('sanitizes the reserved GeoPackage layer name %s',async layerName => {diff --git a/src/processors/src/export/vector-export.ts b/src/processors/src/export/vector-export.tsindex 87e2e0490..0363c3dfe 100644--- a/src/processors/src/export/vector-export.ts+++ b/src/processors/src/export/vector-export.ts@@ -98,7 +98,7 @@ export async function encodeDatasetExport(options: {};case EXPORT_DATA_TYPE.GEOPARQUET:return {- data: await writeGeoParquet(geojson),+ data: await writeGeoParquet(geojson, options.fields, options.data),extension: 'parquet',mimeType: 'application/vnd.apache.parquet'};