From 04e3a9921cf53b05ed23970d5c3df62d7b4224ab Mon Sep 17 00:00:00 2001 From: NithyaNN3 Date: Sun, 4 May 2025 21:40:53 +1000 Subject: [PATCH 1/2] added intersection feature --- lib/src/intersect.dart | 74 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 lib/src/intersect.dart diff --git a/lib/src/intersect.dart b/lib/src/intersect.dart new file mode 100644 index 0000000..b95cd36 --- /dev/null +++ b/lib/src/intersect.dart @@ -0,0 +1,74 @@ +import 'package:geotypes/geotypes.dart'; +import 'package:turf/meta.dart'; +import 'package:polyclip-dart/polyclip.dart'; // Polyclip-dart import (to be fixed) + +// Coded the same as intersect on turf.js. Subject to change based on how polyclip-dart will read geojson objects. + +/* Takes [Polygon] or [MultiPolygon] geometries and + finds their polygonal intersection. If they don't intersect, returns null. + + @param features the features to intersect + @param [properties={}] Optional Properties to translate to Feature + @returns returns a feature representing the area they share (either a [Polygon] or + [MultiPolygon]). If they do not share any area, returns `null`. + + @example + ```dart + var poly1 = polygon([ + [ + [-122.801742, 45.48565], + [-122.801742, 45.60491], + [-122.584762, 45.60491], + [-122.584762, 45.48565], + [-122.801742, 45.48565] + ] + ]); + + var poly2 = polygon([ + [ + [-122.520217, 45.535693], + [-122.64038, 45.553967], + [-122.720031, 45.526554], + [-122.669906, 45.507309], + [-122.723464, 45.446643], + [-122.532577, 45.408574], + [-122.487258, 45.477466], + [-122.520217, 45.535693] + ] + ]); + + var intersection = intersect(featureCollection([poly1, poly2])); +*/ +Feature? intersect(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 specify at least 2 geometries'); + } + + // Use polyclip library to find intersection + final intersection = Polyclip.intersection(geoms[0], [...geoms.sublist(1)]); + + if (intersection.isEmpty) { + return null; + } + + if (intersection.length == 1) { + // Create a polygon feature + final polygonGeometry = Polygon(coordinates: intersection[0] as List>); + return Feature(geometry: polygonGeometry, properties: properties ?? {}); + } + + // Create a multipolygon feature + final multiPolygonGeometry = MultiPolygon( + coordinates: intersection as List>> + ); + return Feature(geometry: multiPolygonGeometry, properties: properties ?? {}); +} \ No newline at end of file From d30a1e60467305fb49ce76b333c2ab2b6c8eec8f Mon Sep 17 00:00:00 2001 From: NithyaNN3 Date: Sun, 4 May 2025 22:09:57 +1000 Subject: [PATCH 2/2] added export file and export --- lib/intersect.dart | 4 ++++ lib/turf.dart | 1 + 2 files changed, 5 insertions(+) create mode 100644 lib/intersect.dart diff --git a/lib/intersect.dart b/lib/intersect.dart new file mode 100644 index 0000000..45e9bd3 --- /dev/null +++ b/lib/intersect.dart @@ -0,0 +1,4 @@ +library turf_intersect; + +export 'package:geotypes/geotypes.dart'; +export 'src/intersect.dart'; diff --git a/lib/turf.dart b/lib/turf.dart index 1ee09fc..a7e9fea 100644 --- a/lib/turf.dart +++ b/lib/turf.dart @@ -16,6 +16,7 @@ export 'distance.dart'; export 'explode.dart'; export 'extensions.dart'; export 'helpers.dart'; +export 'intersect.dart'; export 'invariant.dart'; export 'length.dart'; export 'line_intersect.dart';