Skip to content

Commit

Permalink
Add geometry check to filter out invalid geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Caldwell committed Apr 8, 2021
1 parent 4f79738 commit ceae6d2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mbDrawModes[DRAW_CIRCLE] = DrawCircle;

export interface Props {
drawType?: DRAW_TYPE;
onDraw: (event: { features: Feature[] }) => void;
onDraw: (drawControl: MapboxDraw) => (event: { features: Feature[] }) => void;
onFeaturesSelected?: (drawControl: MapboxDraw) => (event: { features: Feature[] }) => void;
mbMap: MbMap;
drawActive: boolean;
Expand Down Expand Up @@ -112,7 +112,7 @@ export class DrawControl extends Component<Props> {
this._mbDrawControlAdded = true;
this.props.mbMap.getCanvas().style.cursor = 'crosshair';
this.props.mbMap.on('draw.modechange', this._onModeChange);
this.props.mbMap.on('draw.create', this.props.onDraw);
this.props.mbMap.on('draw.create', this.props.onDraw(this._mbDrawControl));
if (this.props.onFeaturesSelected) {
this.props.mbMap.on(
'draw.selectionchange',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import { Map as MbMap } from 'mapbox-gl';
import MapboxDraw from '@mapbox/mapbox-gl-draw';
import { Feature } from 'geojson';
import { i18n } from '@kbn/i18n';
// @ts-expect-error
import * as jsts from 'jsts';
import { getToasts } from '../../../../kibana_services';
import { DrawControl } from '../';
import { DRAW_TYPE } from '../../../../../common';

const geoJSONReader = new jsts.io.GeoJSONReader();

export interface Props {
disableDrawState: () => void;
addFeaturesToIndexQueue: (features: Feature[]) => void;
Expand All @@ -31,9 +35,15 @@ export class DrawFeatureControl extends Component<Props, {}> {
}
};

_onDraw = async (e: { features: Feature[] }) => {
_onDraw = (mbDrawControl: MapboxDraw) => async (e: { features: Feature[] }) => {
try {
console.log(e);
e.features.forEach((feature: Feature) => {
const { geometry } = geoJSONReader.read(feature);
if (!geometry.isSimple() || !geometry.isValid()) {
mbDrawControl.delete(feature.id);
throw new Error('Invalid geometry detected');
}
});
this.props.addFeaturesToIndexQueue(e.features);
} catch (error) {
getToasts().addWarning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface Props {
}

export class DrawFilterControl extends Component<Props, {}> {
_onDraw = async (e: { features: Feature[] }) => {
_onDraw = () => async (e: { features: Feature[] }) => {
if (
!e.features.length ||
!this.props.drawState ||
Expand Down

0 comments on commit ceae6d2

Please sign in to comment.