Skip to content

Commit

Permalink
[fix] support Polygon and LineString mode in idToPolygonGeo (#2182)
Browse files Browse the repository at this point in the history
Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com>
Co-authored-by: Xun Li <lixun910@gmail.com>
  • Loading branch information
igorDykhta and lixun910 committed Apr 4, 2023
1 parent 8589730 commit 79d8c75
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/utils/src/h3-utils.ts
Expand Up @@ -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
};
Expand Down
53 changes: 52 additions & 1 deletion test/browser/layer-tests/h3-hexagon-layer-specs.js
Expand Up @@ -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';

Expand Down Expand Up @@ -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();
});

0 comments on commit 79d8c75

Please sign in to comment.