Skip to content

Commit 58ea0ff

Browse files
committed
feat: Remove reduce, update to non 6.5 types
1 parent c8b816a commit 58ea0ff

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"dependencies": {
2929
"@turf/helpers": "^7.1.0",
3030
"@turf/union": "^7.1.0",
31+
"@types/geojson": "^7946.0.14",
3132
"accessible-autocomplete": "^2.0.4",
3233
"file-saver": "^2.0.5",
3334
"govuk-frontend": "^5.7.1",

pnpm-lock.yaml

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/my-map/os-features.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getServiceURL } from "../../lib/ordnanceSurvey";
88

99
import { hexToRgba } from "./utils";
1010
import { featureCollection } from "@turf/helpers";
11+
import { MultiPolygon, Polygon, Feature } from "geojson";
1112

1213
const featureSource = new VectorSource();
1314

@@ -120,18 +121,22 @@ export function getFeaturesAtPoint(
120121
}
121122

122123
outlineSource.clear();
123-
outlineSource.addFeature(
124-
// Merge all of the features into a single feature
125-
geojson.readFeature(
126-
featureSource.getFeatures().reduce((acc: any, curr) => {
127-
const currentFeature = geojson.writeFeatureObject(curr);
128-
if (!acc) return currentFeature;
129-
130-
const merged = union(featureCollection([currentFeature, acc]));
131-
return merged;
132-
}, null),
133-
),
134-
);
124+
125+
// Convert OL features to GeoJSON
126+
const allFeatures = featureSource
127+
.getFeatures()
128+
.map((feature) => geojson.writeFeatureObject(feature))
129+
.filter((feature): feature is Feature<Polygon | MultiPolygon> =>
130+
["Polygon", "MultiPolygon"].includes(feature.geometry.type),
131+
);
132+
133+
// Merge all GeoJSON features
134+
const collection = featureCollection(allFeatures);
135+
const mergedGeoJSON = union(collection);
136+
137+
// Convert back to OL feature and add to source
138+
const mergedFeature = geojson.readFeature(mergedGeoJSON);
139+
outlineSource.addFeature(mergedFeature);
135140
})
136141
.catch((error) => console.log(error));
137142
}

0 commit comments

Comments
 (0)