Skip to content

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

[fix] support Polygon and LineString mode in idToPolygonGeo (#2182)

Igor Dykhta79d8c7561ffe
diff --git a/src/utils/src/h3-utils.ts b/src/utils/src/h3-utils.ts
index f1b35da1f..e8a41be9f 100644
--- a/src/utils/src/h3-utils.ts
+++ b/src/utils/src/h3-utils.ts
@@ -42,13 +42,13 @@ export function idToPolygonGeo(object?: {id: H3Index}, properties?: any) {
if (!object?.id) {
return null;
}
-
const vertices = getVertices(object);
return {
+ type: 'Feature',
geometry: {
- coordinates: vertices,
- type: 'LineString'
+ coordinates: properties?.isClosed ? [vertices] : vertices,
+ type: properties?.isClosed ? 'Polygon' : 'LineString'
},
properties
};
diff --git a/test/browser/layer-tests/h3-hexagon-layer-specs.js b/test/browser/layer-tests/h3-hexagon-layer-specs.js
index 3d5f8e23c..523dae154 100644
--- a/test/browser/layer-tests/h3-hexagon-layer-specs.js
+++ b/test/browser/layer-tests/h3-hexagon-layer-specs.js
@@ -32,7 +32,7 @@ import {
hexagonIdLayerMeta
} from 'test/helpers/layer-utils';
import {KeplerGlLayers, h3DefaultElevation as defaultElevation} from '@kepler.gl/layers';
-import {getCentroid} from '@kepler.gl/utils';
+import {getCentroid, idToPolygonGeo} from '@kepler.gl/utils';
import {copyTableAndUpdate} from '@kepler.gl/table';
@@ -268,3 +268,54 @@ test('#H3Layer -> renderLayer', t => {
testRenderLayerCases(t, H3Layer, TEST_CASES);
t.end();
});
+
+test('#H3Layer -> idToPolygonGeo', t => {
+ const h3Object = {
+ index: 411,
+ id: '882a100d01fffff'
+ };
+
+ const hexagonOutline = idToPolygonGeo(h3Object);
+ const expectedCoordinates = [
+ [-73.9556604529423, 40.747207768842344],
+ [-73.96203986286912, 40.746155567377926],
+ [-73.96400718290255, 40.74169679631741],
+ [-73.95959600220597, 40.73829055843986],
+ [-73.95321761814058, 40.73934254051138],
+ [-73.9512493890226, 40.74380097982341],
+ [-73.9556604529423, 40.747207768842344]
+ ];
+
+ const expectedHexagonOutline = {
+ type: 'Feature',
+ geometry: {
+ coordinates: expectedCoordinates,
+ type: 'LineString'
+ },
+ properties: undefined
+ };
+ t.deepEqual(
+ hexagonOutline,
+ expectedHexagonOutline,
+ 'should generate geojson object of hexagon outline (LineString)'
+ );
+
+ const properties = {isClosed: true};
+ const hexagonPolygon = idToPolygonGeo(h3Object, properties);
+
+ const expectedHexagonPolygon = {
+ type: 'Feature',
+ geometry: {
+ coordinates: [expectedCoordinates],
+ type: 'Polygon'
+ },
+ properties: {isClosed: true}
+ };
+ t.deepEqual(
+ hexagonPolygon,
+ expectedHexagonPolygon,
+ 'should generate geojson object of hexagon outline (LineString)'
+ );
+
+ t.end();
+});