diff --git a/lib/src/union.dart b/lib/src/union.dart new file mode 100644 index 00000000..adf97610 --- /dev/null +++ b/lib/src/union.dart @@ -0,0 +1,68 @@ +import 'package:geotypes/geotypes.dart'; +import 'package:turf/meta.dart'; +import 'package:polyclip-dart/polyclip.dart'; // TSubject to change as per polyclip-dart changes + +/* Takes a collection of input polygons and returns a combined polygon. If the + input polygons are not contiguous, this function returns a multi-polygon + feature. + + @param features input polygon features + @param [properties={}] Optional Properties to assign to output feature + @returns a combined polygon or multi-polygon feature, or null if there were no input polygons to combine + + @example + var poly1 = polygon([ + [ + [-82.574787, 35.594087], + [-82.574787, 35.615581], + [-82.545261, 35.615581], + [-82.545261, 35.594087], + [-82.574787, 35.594087], + ] + ], properties: { 'fill': '#0f0' }); + + var poly2 = polygon([ + [ + [-82.560024, 35.585153], + [-82.560024, 35.602602], + [-82.52964, 35.602602], + [-82.52964, 35.585153], + [-82.560024, 35.585153], + ] + ]); + + var union = turf.union(featureCollection([poly1, poly2])); + */ +Feature? union(FeatureCollection features, {Map? properties}) { + final geoms = >[]; + + // Extract geometries from features + geomEach(features, (geom, featureIndex, featureProperties, featureBBox, featureId) { + if (geom != null && geom.coordinates != null) { + geoms.add(geom.coordinates as List); + } + }); + + if (geoms.length < 2) { + throw Exception('Must have at least 2 geometries'); + } + + // Use polyclip library to find union + final unioned = Polyclip.union(geoms[0], [...geoms.sublist(1)]); + + if (unioned.isEmpty) { + return null; + } + + if (unioned.length == 1) { + // Create a polygon feature + final polygonGeometry = Polygon(coordinates: unioned[0] as List>); + return Feature(geometry: polygonGeometry, properties: properties ?? {}); + } + + // Create a multipolygon feature + final multiPolygonGeometry = MultiPolygon( + coordinates: unioned as List>> + ); + return Feature(geometry: multiPolygonGeometry, properties: properties ?? {}); +} \ No newline at end of file diff --git a/lib/turf.dart b/lib/turf.dart index 1ee09fcc..7c82c7ed 100644 --- a/lib/turf.dart +++ b/lib/turf.dart @@ -35,3 +35,4 @@ export 'polyline.dart'; export 'square.dart'; export 'transform.dart'; export 'truncate.dart'; +export 'union.dart'; diff --git a/lib/union.dart b/lib/union.dart new file mode 100644 index 00000000..be3af022 --- /dev/null +++ b/lib/union.dart @@ -0,0 +1,4 @@ +library turf_union; + +export 'package:geotypes/geotypes.dart'; +export 'src/union.dart';