Skip to content

Commit ddb9cec

Browse files
committed
improved(core): enhance robustness when handling invalid GeoJSON feature geometry
Signed-off-by: Tim Deubler <[email protected]>
1 parent ee4d241 commit ddb9cec

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

packages/core/src/features/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import {GeoJSONCoordinate, GeoJSONFeature} from './GeoJSON';
21+
2122
type Point = number[];
2223
type BBox = number[];
2324
type Coordinates = Array<Point>;
@@ -63,7 +64,9 @@ const calcBBox = (feature: GeoJSONFeature, bbox?: BBox): BBox | false => {
6364
if (bbox) {
6465
updatePointBBox(<[number, number]>coordinates, bbox);
6566
} else {
66-
bbox = [coordinates[0], coordinates[1], coordinates[0], coordinates[1]];
67+
const lon = coordinates[0];
68+
const lat = coordinates[1];
69+
bbox = [lon, lat, lon, lat];
6770
}
6871
} else {
6972
bbox = bbox || [Infinity, Infinity, -Infinity, -Infinity];
@@ -86,6 +89,10 @@ const calcBBox = (feature: GeoJSONFeature, bbox?: BBox): BBox | false => {
8689
}
8790
}
8891

92+
if (!Number.isFinite(bbox[0]) || !Number.isFinite(bbox[1])) {
93+
return false;
94+
}
95+
8996
return bbox;
9097
};
9198

packages/core/src/providers/EditableFeatureProvider.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
import {FeatureProvider as FeatureTileProvider} from './FeatureProvider';
2121
import {Feature} from '../features/Feature';
2222
import {JSUtils} from '@here/xyz-maps-common';
23-
import {NavlinkSplitHook, NavlinkDisconnectHook, FeatureRemoveHook, CoordinatesUpdateHook} from '@here/xyz-maps-editor';
23+
import {
24+
NavlinkSplitHook,
25+
NavlinkDisconnectHook,
26+
FeatureRemoveHook,
27+
CoordinatesUpdateHook
28+
} from '@here/xyz-maps-editor';
2429
import {GeoJSONCoordinate} from '../features/GeoJSON';
2530
import {TileProviderOptions} from './TileProvider/TileProviderOptions';
2631

@@ -186,7 +191,10 @@ export abstract class EditableFeatureProvider extends FeatureTileProvider {
186191
*
187192
* @returns true if turn is allowed, otherwise false.
188193
*/
189-
abstract readTurnRestriction(turnFrom: { link: Navlink, index: number }, turnTo: { link: Navlink, index: number }): boolean;
194+
abstract readTurnRestriction(turnFrom: { link: Navlink, index: number }, turnTo: {
195+
link: Navlink,
196+
index: number
197+
}): boolean;
190198

191199

192200
/**
@@ -336,13 +344,13 @@ export abstract class EditableFeatureProvider extends FeatureTileProvider {
336344
return true;
337345
};
338346

339-
prepareFeature(feature: Feature): Feature {
340-
if (!feature.properties) {
341-
feature.properties = {};
342-
}
343-
if (feature.id == undefined) {
344-
feature.id = JSUtils.String.random();
347+
prepareFeature(feature: Feature): Feature | false {
348+
feature.id ??= JSUtils.String.random();
349+
if (!super.prepareFeature(feature)) {
350+
return false;
345351
}
352+
feature.properties ??= {};
353+
346354
return feature;
347355
}
348356
}

packages/core/src/providers/FeatureProvider.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class FeatureProvider extends Provider {
9898
}
9999
} else {
100100
// unknown feature
101-
console.warn('unkown feature detected..', feature.geometry.type, feature);
101+
console.warn('Invalid feature skipped:', feature);
102102
}
103103
}
104104

@@ -238,7 +238,7 @@ export class FeatureProvider extends Provider {
238238
}
239239
} else {
240240
// unkown feature
241-
console.warn('unkown feature detected..', feature);
241+
console.warn('Invalid feature skipped:', feature);
242242

243243
feature = null;
244244
}
@@ -1084,12 +1084,10 @@ export class FeatureProvider extends Provider {
10841084
__type = 'FeatureProvider';
10851085

10861086
prepareFeature(feature: GeoJSONFeature): GeoJSONFeature | false {
1087-
if (feature['id'] == UNDEF) {
1088-
feature['id'] = Math.random() * 1e8 ^ 0;
1089-
}
1087+
feature.id ??= Math.random() * 1e8 ^ 0;
10901088
// calculates object bbox's
10911089
if (!feature.bbox) {
1092-
// false -> unkown feature -> no success
1090+
// false -> unknown feature -> no success
10931091
return this.updateBBox(feature) && feature;
10941092
} else if ((<number[]>feature.bbox).length === 6) { // convert to 2D bbox
10951093
feature.bbox = [feature.bbox[0], feature.bbox[1], feature.bbox[3], (<number[]>feature.bbox)[4]];

0 commit comments

Comments
 (0)