From a6290269b0aa68a006e4dbd189101cc60e2288ea Mon Sep 17 00:00:00 2001 From: NithyaNN3 Date: Thu, 15 May 2025 23:39:20 +1000 Subject: [PATCH 1/2] added imports --- lib/src/bboxclip.dart | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 lib/src/bboxclip.dart diff --git a/lib/src/bboxclip.dart b/lib/src/bboxclip.dart new file mode 100644 index 00000000..35af4469 --- /dev/null +++ b/lib/src/bboxclip.dart @@ -0,0 +1,12 @@ +// This code considers the dart port of lineclip from Mapbox +// https://github.com/Zverik/flutter_country_coder/blob/eccf7afbc13746d864b01fd581c7b785499340f0/lib/src/lineclip.dart#L8 + +import 'package:geotypes/geotypes.dart' show + Feature, + LineString, + MultiLineString, + MultiPolygon, + Polygon; + +import 'package:turf/bbox.dart'; +import 'package:turf/lineclip.dart'; \ No newline at end of file From 86faeb72ff8effdf18bc9b76d19ded83333246b7 Mon Sep 17 00:00:00 2001 From: NithyaNN3 Date: Wed, 21 May 2025 23:54:59 +1000 Subject: [PATCH 2/2] basic bboxclip implementation --- lib/src/bboxclip.dart | 94 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 82 insertions(+), 12 deletions(-) diff --git a/lib/src/bboxclip.dart b/lib/src/bboxclip.dart index 35af4469..7394ba03 100644 --- a/lib/src/bboxclip.dart +++ b/lib/src/bboxclip.dart @@ -1,12 +1,82 @@ -// This code considers the dart port of lineclip from Mapbox -// https://github.com/Zverik/flutter_country_coder/blob/eccf7afbc13746d864b01fd581c7b785499340f0/lib/src/lineclip.dart#L8 - -import 'package:geotypes/geotypes.dart' show - Feature, - LineString, - MultiLineString, - MultiPolygon, - Polygon; - -import 'package:turf/bbox.dart'; -import 'package:turf/lineclip.dart'; \ No newline at end of file +import 'package:geotypes/geotypes.dart'; +import 'package:turf/helpers.dart'; +import 'package:turf/bbox.dart'; // for BBox +import 'package:turf/lineclip.dart'; // your polygonclip and lineclip implementations + +Feature bboxClip(Feature feature, BBox bbox) { + final geometry = feature.geometry; + + switch (geometry) { + case LineString(): + final clippedLines = lineclip(geometry.coordinates, bbox); + if (clippedLines.length == 1) { + return Feature( + geometry: LineString(clippedLines.first), + properties: feature.properties, + ); + } + return Feature( + geometry: MultiLineString(clippedLines), + properties: feature.properties, + ); + + case MultiLineString(): + final List> resultLines = []; + for (final line in geometry.coordinates) { + lineclip(line, bbox, resultLines); + } + if (resultLines.length == 1) { + return Feature( + geometry: LineString(resultLines.first), + properties: feature.properties, + ); + } + return Feature( + geometry: MultiLineString(resultLines), + properties: feature.properties, + ); + + case Polygon(): + final clippedRings = clipPolygon(geometry.coordinates, bbox); + return Feature( + geometry: Polygon(clippedRings), + properties: feature.properties, + ); + + case MultiPolygon(): + final clippedPolygons = geometry.coordinates + .map((rings) => clipPolygon(rings, bbox)) + .where((poly) => poly.isNotEmpty) + .toList(); + + return Feature( + geometry: MultiPolygon(clippedPolygons), + properties: feature.properties, + ); + + default: + throw UnsupportedError('Geometry type ${geometry.runtimeType} not supported'); + } +} + +List> clipPolygon(List> rings, BBox bbox) { + final List> outRings = []; + + for (final ring in rings) { + final clipped = polygonclip(ring, bbox); + + if (clipped.isNotEmpty) { + // Ensure the ring is closed + if (clipped.first != clipped.last) { + clipped.add(clipped.first); + } + + // Minimum 4 points to form a valid ring + if (clipped.length >= 4) { + outRings.add(clipped); + } + } + } + + return outRings; +}