Skip to content

Commit 2888395

Browse files
committed
Add guard against rings with no points, log message
1 parent 23a9eb8 commit 2888395

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

‎src/lib/geo_location_utils.js‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,18 @@ function extractTraceFeature(calcTrace) {
242242
properties: {}
243243
};
244244

245-
// Compute centroid, add it to the properties
246-
if (fOut.geometry.coordinates.length > 0) {
247-
fOut.properties.ct = findCentroid(fOut);
248-
} else {
249-
fOut.properties.ct = [NaN, NaN];
245+
fOut.properties.ct = findCentroid(fOut);
246+
247+
if (isNaN(fOut.properties.ct[0])) {
248+
loggers.log(
249+
[
250+
'Location',
251+
cdi.loc,
252+
'has no polygon with positive area.',
253+
'Its centroid could not be computed,',
254+
'so hover and selection will not work for it.'
255+
].join(' ')
256+
);
250257
}
251258

252259
// Mutate in in/out features into calcdata
@@ -331,8 +338,10 @@ function findCentroid(feature) {
331338
poly = geometry;
332339
}
333340

334-
// Degenerate MultiPolygons may not contain a positive-area polygon.
335-
if (!poly) return [NaN, NaN];
341+
// Guard against MultiPolygons that don't contain a positive-area polygon
342+
// (collapsed rings measure zero, malformed ring ordering measures negative)
343+
// and when either geometry type has rings holding no points at all.
344+
if (!poly || !poly.coordinates.some((ring) => ring.length > 0)) return [NaN, NaN];
336345

337346
return turfCentroid(poly).geometry.coordinates;
338347
}

0 commit comments

Comments
 (0)