Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Xun Li <[email protected]>
  • Loading branch information
lixun910 committed Sep 18, 2023
1 parent 44dc687 commit 672ef02
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 31 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
"@deck.gl/react": "^8.6.0",
"@deck.gl/test-utils": "^8.6.0",
"@luma.gl/test-utils": "^8.5.10",
"bson": "^5.5.0",
"browserslist": "^4.17.0",
"caniuse-lite": "^1.0.30001449",
"d3-array": "^2.8.0",
Expand Down
41 changes: 22 additions & 19 deletions src/layers/src/geojson-layer/geojson-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// THE SOFTWARE.

import {ListVector, FloatVector, BinaryVector, Utf8Vector} from 'apache-arrow';
import wktParser from 'wellknown';
import Console from 'global/console';
import normalize from '@mapbox/geojson-normalize';
import bbox from '@turf/bbox';
import {parseSync} from '@loaders.gl/core';
Expand Down Expand Up @@ -58,22 +58,26 @@ type FeatureTypeMap = {
};
/* eslint-enable */

export function parseGeoJsonRawFeature(rawFeature: unknown): Feature | null {
if (rawFeature && typeof rawFeature === 'object') {
if (typeof rawFeature['type'] === 'string') {
// Support GeoJson feature as object
// probably need to normalize it as well
const normalized = normalize(rawFeature);
if (!normalized || !Array.isArray(normalized.features)) {
// fail to normalize GeoJson
return null;
}
type RawArrowFeature = {
encoding?: string;
data: any;
};

return normalized.features[0];
} else if (rawFeature['encoding'].startsWith('geoarrow')) {
export function parseGeoJsonRawFeature(rawFeature: {} | Feature | RawArrowFeature): Feature | null {
if (rawFeature && typeof rawFeature === 'object') {
if ('encoding' in rawFeature && rawFeature.encoding?.startsWith('geoarrow')) {
// Support GeoArrow data
return parseGeometryFromArrow(rawFeature);
}
// Support GeoJson feature as object
// probably need to normalize it as well
const normalized = normalize(rawFeature);
if (!normalized || !Array.isArray(normalized.features)) {
// fail to normalize GeoJson
return null;
}

return normalized.features[0];
} else if (typeof rawFeature === 'string') {
return parseGeometryFromString(rawFeature);
} else if (Array.isArray(rawFeature)) {
Expand Down Expand Up @@ -284,7 +288,6 @@ function arrowPolygonToFeature(arrowPolygon: ListVector): Polygon {
ring.push(coords);
}
polygon.push(ring);
console.log(polygon);
}
const geometry: Polygon = {
type: 'Polygon',
Expand Down Expand Up @@ -387,7 +390,7 @@ function arrowWkbToFeature(arrowWkb: BinaryVector): Feature | null {
* convert Arrow wkt to geojson Geometry
*/
function arrowWktToFeature(arrowWkt: Utf8Vector): Feature | null {
const geometry = wktParser(arrowWkt.get(0));
const geometry = parseSync(arrowWkt.get(0) || '', WKTLoader);
const normalized = normalize(geometry);

if (!normalized || !Array.isArray(normalized.features)) {
Expand All @@ -405,9 +408,9 @@ function arrowWktToFeature(arrowWkt: Utf8Vector): Feature | null {
* @see processArrowData
* @returns
*/
export function parseGeometryFromArrow(rawData: object): Feature | null {
const encoding = rawData['encoding'];
const data = rawData['data'];
export function parseGeometryFromArrow(rawData: RawArrowFeature): Feature | null {
const encoding = rawData.encoding;
const data = rawData.data;
if (!encoding || !data) return null;

let geometry;
Expand Down Expand Up @@ -441,7 +444,7 @@ export function parseGeometryFromArrow(rawData: object): Feature | null {
}
default: {
// encoding is not supported, skip
console.error('GeoArrow encoding not supported');
Console.error('GeoArrow encoding not supported');
return null;
}
}
Expand Down
Loading

0 comments on commit 672ef02

Please sign in to comment.