From 79d8c7561ffe9db51315452fe310e9a5444b4081 Mon Sep 17 00:00:00 2001 From: Igor Dykhta Date: Tue, 4 Apr 2023 20:10:36 +0300 Subject: [PATCH] [fix] support Polygon and LineString mode in idToPolygonGeo (#2182) Signed-off-by: Ihor Dykhta Co-authored-by: Xun Li --- src/utils/src/h3-utils.ts | 6 +-- .../layer-tests/h3-hexagon-layer-specs.js | 53 ++++++++++++++++++- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/utils/src/h3-utils.ts b/src/utils/src/h3-utils.ts index f1b35da1f6..e8a41be9fc 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 3d5f8e23c6..523dae1549 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(); +});