@@ -8,6 +8,7 @@ import { getServiceURL } from "../../lib/ordnanceSurvey";
88
99import { hexToRgba } from "./utils" ;
1010import { featureCollection } from "@turf/helpers" ;
11+ import { MultiPolygon , Polygon , Feature } from "geojson" ;
1112
1213const 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