Skip to content

Commit 0cffa57

Browse files
committed
fix bug where multi-geometries errored in lasso
1 parent e5ba9ba commit 0cffa57

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

packages/plugins/Draw/src/store/createInteractions/createLassoInteractions.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import { PolarActionContext } from '@polar/lib-custom-types'
77
import { Feature } from 'ol'
88
import { Polygon } from 'ol/geom'
99
import { parseWfsResponse } from '@polar/lib-get-features/wfs/parse'
10-
import { FeatureCollection, Feature as GeoJsonFeature } from 'geojson'
10+
import {
11+
FeatureCollection,
12+
Feature as GeoJsonFeature,
13+
Geometry,
14+
GeometryCollection,
15+
} from 'geojson'
1116
import { getVectorFeaturesByFeatureRequest } from '@polar/lib-get-features'
1217
import { DrawGetters, DrawState } from '../../types'
1318

@@ -27,22 +32,42 @@ const internalError = () => ({
2732
const buildAddFeaturesPayload = (
2833
featureCollections: FeatureCollection[],
2934
drawnLasso: Feature
30-
) => ({
31-
geoJSON: {
32-
type: 'FeatureCollection',
33-
features: featureCollections
34-
.reduce(
35-
(accumulator, { features }) => accumulator.concat(features),
36-
[] as GeoJsonFeature[]
37-
)
38-
.filter((feature) =>
39-
booleanContains(
40-
JSON.parse(new GeoJSON().writeFeature(drawnLasso)),
41-
feature
35+
) => {
36+
const drawnLassoGeoJson = JSON.parse(new GeoJSON().writeFeature(drawnLasso))
37+
38+
console.error(featureCollections)
39+
40+
return {
41+
geoJSON: {
42+
type: 'FeatureCollection',
43+
features: featureCollections
44+
.reduce(
45+
(accumulator, { features }) => accumulator.concat(features),
46+
[] as GeoJsonFeature[]
4247
)
43-
),
44-
},
45-
})
48+
.filter((feature) => {
49+
if (feature.geometry.type.startsWith('Multi')) {
50+
return (
51+
// since .type on GeometryCollection doesn't start with 'Multi'
52+
(
53+
feature.geometry as Exclude<Geometry, GeometryCollection>
54+
).coordinates.every((partialCoordinates) =>
55+
booleanContains(drawnLassoGeoJson, {
56+
type: 'Feature',
57+
geometry: {
58+
type: feature.geometry.type.slice(5), // un«Multi»ed
59+
coordinates: partialCoordinates,
60+
},
61+
properties: {},
62+
})
63+
)
64+
)
65+
}
66+
return booleanContains(drawnLassoGeoJson, feature)
67+
}),
68+
},
69+
}
70+
}
4671

4772
// should be fine, surrounding method is only unpacking/packing, also see below
4873
// eslint-disable-next-line max-lines-per-function

0 commit comments

Comments
 (0)