Skip to content

Commit

Permalink
review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Jan 7, 2021
1 parent db1daf5 commit b9d28da
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
"@loaders.gl/json": "^2.3.1",
"@slack/webhook": "^5.0.0",
"@storybook/addons": "^6.0.16",
"@turf/along": "6.0.1",
"@turf/area": "6.0.1",
"@turf/bbox": "6.0.1",
"@turf/bbox-polygon": "6.0.1",
Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/maps/common/get_centroid_features.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ test('should not create centroid for too many features polygon', () => {
test('should create centroid feature for line (even number of points)', () => {
const lineFeature: Feature = {
type: 'Feature',
id: 'myfeature',
geometry: {
type: 'LineString',
coordinates: [
Expand All @@ -97,9 +98,10 @@ test('should create centroid feature for line (even number of points)', () => {
expect(centroidFeatures.length).toBe(1);
expect(centroidFeatures[0]).toEqual({
type: 'Feature',
id: 'myfeature',
geometry: {
type: 'Point',
coordinates: [103.0, 1.0],
coordinates: [103.50003808007737, 0.5000190382261022],
},
properties: {
__kbn_is_centroid_feature__: true,
Expand Down Expand Up @@ -179,7 +181,7 @@ test('should create centroid feature for multi line', () => {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [30, 30],
coordinates: [35.56701982106548, 24.717594944805672],
},
properties: {
__kbn_is_centroid_feature__: true,
Expand Down
18 changes: 8 additions & 10 deletions x-pack/plugins/maps/common/get_centroid_features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
MultiPolygon,
Position,
} from 'geojson';
import turfAlong from '@turf/along';
import turfArea from '@turf/area';
// @ts-expect-error
import turfCenterOfMass from '@turf/center-of-mass';
Expand All @@ -36,7 +37,7 @@ export function getCentroidFeatures(featureCollection: FeatureCollection): Featu

let centroidGeometry: Geometry | null = null;
if (feature.geometry.type === GEO_JSON_TYPE.LINE_STRING) {
centroidGeometry = getLineCentroid((feature.geometry as LineString).coordinates);
centroidGeometry = getLineCentroid(feature);
} else if (feature.geometry.type === GEO_JSON_TYPE.MULTI_LINE_STRING) {
const coordinates = (feature.geometry as MultiLineString).coordinates;
let longestLine = coordinates[0];
Expand All @@ -49,7 +50,7 @@ export function getCentroidFeatures(featureCollection: FeatureCollection): Featu
longestLength = nextLength;
}
}
centroidGeometry = getLineCentroid(longestLine);
centroidGeometry = getLineCentroid(lineString(longestLine));
} else if (feature.geometry.type === GEO_JSON_TYPE.POLYGON) {
centroidGeometry = turfCenterOfMass(feature).geometry;
} else if (feature.geometry.type === GEO_JSON_TYPE.MULTI_POLYGON) {
Expand All @@ -69,7 +70,8 @@ export function getCentroidFeatures(featureCollection: FeatureCollection): Featu

if (centroidGeometry) {
centroidFeatures.push({
...feature,
type: 'Feature',
id: feature.id,
properties: {
...feature.properties,
[KBN_IS_CENTROID_FEATURE]: true,
Expand All @@ -81,11 +83,7 @@ export function getCentroidFeatures(featureCollection: FeatureCollection): Featu
return centroidFeatures;
}

// To ensure centroid is placed on line and fast calculations, centroid is middle point
function getLineCentroid(lineCoordinates: Position[]): Geometry {
const centerPointIndex = Math.ceil(lineCoordinates.length / 2) - 1;
return {
type: GEO_JSON_TYPE.POINT as 'Point',
coordinates: lineCoordinates[centerPointIndex],
};
function getLineCentroid(lineStringFeature: LineString): Geometry {
const length = turfLength(lineStringFeature);
return turfAlong(lineStringFeature, length / 2).geometry;
}
21 changes: 20 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4329,6 +4329,17 @@
dependencies:
"@babel/runtime" "^7.10.2"

"@turf/[email protected]":
version "6.0.1"
resolved "https://registry.yarnpkg.com/@turf/along/-/along-6.0.1.tgz#595cecdc48fc7fcfa83c940a8e3eb24d4c2e04d4"
integrity sha512-6PptAcrsFR3o0Flpktk8Vo68W2txEVTh14zjoTVu+H5docd2+pv5/upA77bg3YFBoJgAxmUFt1leDdjReJ44BQ==
dependencies:
"@turf/bearing" "6.x"
"@turf/destination" "6.x"
"@turf/distance" "6.x"
"@turf/helpers" "6.x"
"@turf/invariant" "6.x"

"@turf/[email protected]":
version "6.0.1"
resolved "https://registry.yarnpkg.com/@turf/area/-/area-6.0.1.tgz#50ed63c70ef2bdb72952384f1594319d94f3b051"
Expand All @@ -4352,6 +4363,14 @@
"@turf/helpers" "6.x"
"@turf/meta" "6.x"

"@turf/[email protected]":
version "6.0.1"
resolved "https://registry.yarnpkg.com/@turf/bearing/-/bearing-6.0.1.tgz#8da5d17092e571f170cde7bfb2e5b0d74923c92d"
integrity sha512-mXY1NozqV9EFfBTbUItujwfqfQF0G/Xe2fzvnZle90ekPEUfhi4Dgf5JswJTd96J9LiT8kcd6Jonp5khnx0wIg==
dependencies:
"@turf/helpers" "6.x"
"@turf/invariant" "6.x"

"@turf/[email protected]":
version "6.0.1"
resolved "https://registry.yarnpkg.com/@turf/boolean-contains/-/boolean-contains-6.0.1.tgz#c3c583215fc5bda47ede51cf52d735ffdc1006a5"
Expand Down Expand Up @@ -22657,7 +22676,7 @@ puppeteer@^2.0.0:
integrity sha512-I4JbNmQHZkE72TPNdipND8GnsEBnqzuksxPSAT25qvudShuuzdY9TwNBQ65IJwPD/pjlpx7fUIUmFyvTHwlxhQ==
dependencies:
debug "^4.1.0"
devtools-protocol "0.0.818844"
devtools-protocol "0.0.809251"
extract-zip "^2.0.0"
https-proxy-agent "^4.0.0"
node-fetch "^2.6.1"
Expand Down

0 comments on commit b9d28da

Please sign in to comment.