Skip to content

Commit

Permalink
feat: Remove reduce, update to non 6.5 types
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Dec 4, 2024
1 parent c8b816a commit 58ea0ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dependencies": {
"@turf/helpers": "^7.1.0",
"@turf/union": "^7.1.0",
"@types/geojson": "^7946.0.14",
"accessible-autocomplete": "^2.0.4",
"file-saver": "^2.0.5",
"govuk-frontend": "^5.7.1",
Expand Down
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 17 additions & 12 deletions src/components/my-map/os-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getServiceURL } from "../../lib/ordnanceSurvey";

import { hexToRgba } from "./utils";
import { featureCollection } from "@turf/helpers";
import { MultiPolygon, Polygon, Feature } from "geojson";

const featureSource = new VectorSource();

Expand Down Expand Up @@ -120,18 +121,22 @@ export function getFeaturesAtPoint(
}

outlineSource.clear();
outlineSource.addFeature(
// Merge all of the features into a single feature
geojson.readFeature(
featureSource.getFeatures().reduce((acc: any, curr) => {
const currentFeature = geojson.writeFeatureObject(curr);
if (!acc) return currentFeature;

const merged = union(featureCollection([currentFeature, acc]));
return merged;
}, null),
),
);

// Convert OL features to GeoJSON
const allFeatures = featureSource
.getFeatures()
.map((feature) => geojson.writeFeatureObject(feature))
.filter((feature): feature is Feature<Polygon | MultiPolygon> =>
["Polygon", "MultiPolygon"].includes(feature.geometry.type),
);

// Merge all GeoJSON features
const collection = featureCollection(allFeatures);
const mergedGeoJSON = union(collection);

// Convert back to OL feature and add to source
const mergedFeature = geojson.readFeature(mergedGeoJSON);
outlineSource.addFeature(mergedFeature);
})
.catch((error) => console.log(error));
}

0 comments on commit 58ea0ff

Please sign in to comment.