@@ -2,7 +2,7 @@ import { FeatureModelType } from '../models/feature-model.type';
22import { Feature } from 'ol' ;
33import { GeoJSON , WKT } from 'ol/format' ;
44import { FeatureModel , FeatureModelAttributes } from '@tailormap-viewer/api' ;
5- import { Circle , Geometry , Point } from 'ol/geom' ;
5+ import { Circle , Geometry , LineString , MultiLineString , MultiPoint , MultiPolygon , Point , Polygon } from 'ol/geom' ;
66import { fromCircle , fromExtent } from 'ol/geom/Polygon' ;
77import { MapSizeHelper } from '../helpers/map-size.helper' ;
88import { MapUnitEnum } from '../models/map-unit.enum' ;
@@ -209,4 +209,47 @@ export class FeatureHelper {
209209 const circle = new Circle ( point . getCoordinates ( ) , radius ) ;
210210 return FeatureHelper . getWKT ( circle ) ;
211211 }
212+
213+ public static appendMultiGeometryWKT ( currentWkt : string | null , newGeometryWkt : string ) {
214+ const newGeom = FeatureHelper . fromWKT ( newGeometryWkt ) ;
215+ if ( ! currentWkt ) {
216+ return newGeom ;
217+ }
218+ const currentMultiGeom = this . ensureMultiGeometry ( FeatureHelper . fromWKT ( currentWkt ) ) ;
219+
220+ if ( currentMultiGeom instanceof MultiPoint && newGeom instanceof Point ) {
221+ currentMultiGeom . appendPoint ( newGeom ) ;
222+ } else if ( currentMultiGeom instanceof MultiLineString && newGeom instanceof LineString ) {
223+ currentMultiGeom . appendLineString ( newGeom ) ;
224+ } else if ( currentMultiGeom instanceof MultiPolygon && newGeom instanceof Polygon ) {
225+ currentMultiGeom . appendPolygon ( newGeom ) ;
226+ } else {
227+ // In case of incompatible geometry types, just return the new geometry
228+ // TODO, maybe create a GeometryCollection?
229+ return newGeom ;
230+ }
231+ return currentMultiGeom ;
232+ }
233+
234+ private static ensureMultiGeometry ( geometry : Geometry ) {
235+ if ( geometry instanceof MultiPoint || geometry instanceof MultiLineString || geometry instanceof MultiPolygon ) {
236+ return geometry ;
237+ }
238+ if ( geometry instanceof Point ) {
239+ const multiPoint = new MultiPoint ( [ ] ) ;
240+ multiPoint . appendPoint ( geometry ) ;
241+ return multiPoint ;
242+ }
243+ if ( geometry instanceof LineString ) {
244+ const multiLineString = new MultiLineString ( [ ] ) ;
245+ multiLineString . appendLineString ( geometry ) ;
246+ return multiLineString ;
247+ }
248+ if ( geometry instanceof Polygon ) {
249+ const multiPolygon = new MultiPolygon ( [ ] ) ;
250+ multiPolygon . appendPolygon ( geometry ) ;
251+ return multiPolygon ;
252+ }
253+ throw new Error ( 'Unsupported geometry type' ) ;
254+ }
212255}
0 commit comments