diff --git a/package.json b/package.json index e35de83..836b25f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 507bcf8..fa9c5c8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ dependencies: '@turf/union': specifier: ^7.1.0 version: 7.1.0 + '@types/geojson': + specifier: ^7946.0.14 + version: 7946.0.14 accessible-autocomplete: specifier: ^2.0.4 version: 2.0.4 @@ -228,7 +231,7 @@ packages: '@babel/core': 7.24.7 '@babel/helper-compilation-targets': 7.24.7 '@babel/helper-plugin-utils': 7.24.7 - debug: 4.3.5 + debug: 4.3.7 lodash.debounce: 4.0.8 resolve: 1.22.8 transitivePeerDependencies: @@ -6675,7 +6678,7 @@ packages: resolution: {integrity: sha512-v67WcEouB5GxbTWL/4NeToqcZiAWEq90N888fczVArY8A79J0L4FD7vj5hm3eUMua5EpoQ59wa/oovY6TLvRUA==} engines: {node: '>= 18'} dependencies: - debug: 4.3.5 + debug: 4.3.7 destroy: 1.2.0 encodeurl: 2.0.0 escape-html: 1.0.3 @@ -6943,7 +6946,7 @@ packages: /spdy-transport@3.0.0: resolution: {integrity: sha512-hsLVFE5SjA6TCisWeJXFKniGGOpBgMLmerfO2aCyCU5s7nJ/rpAepqmFifv/GCbSbueEeAJJnmSQ2rKC/g8Fcw==} dependencies: - debug: 4.3.5 + debug: 4.3.7 detect-node: 2.1.0 hpack.js: 2.1.6 obuf: 1.1.2 diff --git a/src/components/my-map/os-features.ts b/src/components/my-map/os-features.ts index b0474d3..499b835 100644 --- a/src/components/my-map/os-features.ts +++ b/src/components/my-map/os-features.ts @@ -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(); @@ -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"].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)); }