From 65eddf84855180448453207687eb94f114b65e31 Mon Sep 17 00:00:00 2001 From: armantorkzaban Date: Tue, 30 Aug 2022 01:52:02 +0200 Subject: [PATCH] #88 crosses, disjoint, equal, intersects, pip @lukas-h --- lib/src/booleans/boolean_crosses.dart | 3 +- lib/src/booleans/boolean_disjoint.dart | 9 +-- lib/src/booleans/boolean_equal.dart | 4 +- lib/src/booleans/boolean_intersect.dart | 3 +- .../booleans/boolean_point_in_polygon.dart | 12 +-- lib/src/line_intersect.dart | 77 ++++++++----------- pubspec.yaml | 4 + test/booleans/disjoint_test.dart | 7 +- test/booleans/intersect_test.dart | 2 +- 9 files changed, 57 insertions(+), 64 deletions(-) diff --git a/lib/src/booleans/boolean_crosses.dart b/lib/src/booleans/boolean_crosses.dart index e51e421..c7dc406 100644 --- a/lib/src/booleans/boolean_crosses.dart +++ b/lib/src/booleans/boolean_crosses.dart @@ -1,5 +1,4 @@ import '../../helpers.dart'; -import '../invariant.dart'; import '../line_intersect.dart'; import '../polygon_to_line.dart'; import 'boolean_point_in_polygon.dart'; @@ -115,7 +114,7 @@ bool doLineStringsCross(LineString lineString1, LineString lineString2) { } bool doLineStringAndPolygonCross(LineString lineString, Polygon polygon) { - LineString line = polygonToLine(polygon); + Feature line = polygonToLine(polygon) as Feature; var doLinesIntersect = lineIntersect(lineString, line); if (doLinesIntersect.features.isNotEmpty) { return true; diff --git a/lib/src/booleans/boolean_disjoint.dart b/lib/src/booleans/boolean_disjoint.dart index 80342b3..f754618 100644 --- a/lib/src/booleans/boolean_disjoint.dart +++ b/lib/src/booleans/boolean_disjoint.dart @@ -27,8 +27,8 @@ bool booleanDisjoint(GeoJSONObject feature1, GeoJSONObject feature2) { flattenEach( feature2, (flatten2, featureIndex, multiFeatureIndex) { - if (bool == false) { - return false; + if (!bool) { + return bool; } bool = _disjoint(flatten1.geometry!, flatten2.geometry!); }, @@ -39,7 +39,7 @@ bool booleanDisjoint(GeoJSONObject feature1, GeoJSONObject feature2) { } /// Disjoint operation for simple Geometries (Point/LineString/Polygon) -_disjoint(GeometryType geom1, GeometryType geom2) { +bool _disjoint(GeometryType geom1, GeometryType geom2) { if (geom1 is Point) { if (geom2 is Point) { return !_compareCoords(geom1.coordinates, geom2.coordinates); @@ -64,9 +64,8 @@ _disjoint(GeometryType geom1, GeometryType geom2) { } else if (geom2 is Polygon) { return !_isPolyInPoly(geom2, geom1); } - } else { - return false; } + return false; } // http://stackoverflow.com/a/11908158/1979085 diff --git a/lib/src/booleans/boolean_equal.dart b/lib/src/booleans/boolean_equal.dart index 4f26bc7..283602e 100644 --- a/lib/src/booleans/boolean_equal.dart +++ b/lib/src/booleans/boolean_equal.dart @@ -32,7 +32,7 @@ bool booleanEqual( var equality = Equality( precision: precision, - shiftedPolygon: shiftedPolygon, - direction: direction); + shiftedPolygons: shiftedPolygon, + reversedGeometries: direction); return equality.compare(cleanCoords(feature1), cleanCoords(feature2)); } diff --git a/lib/src/booleans/boolean_intersect.dart b/lib/src/booleans/boolean_intersect.dart index 9e230d6..7a31cca 100644 --- a/lib/src/booleans/boolean_intersect.dart +++ b/lib/src/booleans/boolean_intersect.dart @@ -2,8 +2,7 @@ import '../../helpers.dart'; import '../../meta.dart'; import 'boolean_disjoint.dart'; - -Boolean-intersects returns (TRUE) two geometries intersect. +/// returns (TRUE) when two geometries intersect. /// @name booleanIntersects /// @param {Geometry|Feature} feature1 GeoJSON Feature or Geometry /// @param {Geometry|Feature} feature2 GeoJSON Feature or Geometry diff --git a/lib/src/booleans/boolean_point_in_polygon.dart b/lib/src/booleans/boolean_point_in_polygon.dart index cb0da21..d97576c 100644 --- a/lib/src/booleans/boolean_point_in_polygon.dart +++ b/lib/src/booleans/boolean_point_in_polygon.dart @@ -1,10 +1,10 @@ // http://en.wikipedia.org/wiki/Even%E2%80%93odd_rule // modified from: https://github.com/substack/point-in-polygon/blob/master/index.js // which was modified from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html -import 'package:pip/pip.dart'; + +import 'package:turf_pip/turf_pip.dart'; import '../../helpers.dart'; -import '../invariant.dart'; /// Takes a [Point], and a [Polygon] or [MultiPolygon]and determines if the /// [Point] resides inside the [Polygon]. The polygon can be convex or concave. @@ -58,11 +58,11 @@ bool booleanPointInPolygon(Position point, GeoJSONObject polygon, var result = false; for (var i = 0; i < polys!.length; ++i) { - var polyResult = - pip(Point(coordinates: point), Polygon(coordinates: polys[i])); - if (polyResult == 0) { + var polyResult = pointInPolygon( + Point(coordinates: point), Polygon(coordinates: polys[i])); + if (polyResult == PointInPolygonResult.isOnEdge) { return ignoreBoundary ? false : true; - } else if (polyResult) { + } else if (polyResult == PointInPolygonResult.isInside) { result = true; } } diff --git a/lib/src/line_intersect.dart b/lib/src/line_intersect.dart index 48a84e7..29f047d 100644 --- a/lib/src/line_intersect.dart +++ b/lib/src/line_intersect.dart @@ -1,3 +1,4 @@ +import 'package:sweepline_intersections/sweepline_intersections.dart'; import '../helpers.dart'; @@ -8,71 +9,61 @@ import '../helpers.dart'; /// example: /// ```dart /// var line1 = LineString(coordinates:[ -/// Position.of([126, -11]), +/// Position.of([126, -11]), /// Position.of([129, -21]), /// ]); /// var line2 = LineString(coordinates:[ -/// Position.of([123, -18]), +/// Position.of([123, -18]), /// Position.of([131, -14]), /// ]); /// var intersects = lineIntersect(line1, line2); /// //addToMap /// var addToMap = [line1, line2, intersects] -FeatureCollection lineIntersect( - GeoJSONObject line1, - GeoJSONObject line2, -{ - bool removeDuplicates = true, - bool ignoreSelfIntersections = false - } -){ - var features= []; - if (line1 is FeatureCollection) -{ features = features..addAll(line1.features); -} else if (line1 is Feature) {features.add(line1);} - else if ( - line1 is LineString || - line1 is Polygon || - line1 is MultiLineString || - line1 is MultiPolygon - ) { +FeatureCollection lineIntersect(GeoJSONObject line1, GeoJSONObject line2, + {bool removeDuplicates = true, bool ignoreSelfIntersections = false}) { + var features = []; + if (line1 is FeatureCollection) { + features = features..addAll(line1.features); + } else if (line1 is Feature) { + features.add(line1); + } else if (line1 is LineString || + line1 is Polygon || + line1 is MultiLineString || + line1 is MultiPolygon) { features.add(Feature(geometry: line1 as GeometryObject)); } - if (line2 is FeatureCollection) - {features = features..addAll(line2.features);} - else if (line2 is Feature) {features.add(line2);} - else if ( - line2 is LineString || - line2 is Polygon || - line2 is MultiLineString || - line2 is MultiPolygon - ) { + if (line2 is FeatureCollection) { + features = features..addAll(line2.features); + } else if (line2 is Feature) { + features.add(line2); + } else if (line2 is LineString || + line2 is Polygon || + line2 is MultiLineString || + line2 is MultiPolygon) { features.add(Feature(geometry: line2 as GeometryObject)); } - var intersections = findIntersections( - FeatureCollection(features: features), - ignoreSelfIntersections - ); + var intersections = sweeplineIntersections( + FeatureCollection(features: features), ignoreSelfIntersections); - var results: Intersection[] = []; + var results = []; if (removeDuplicates) { - const unique: Record = {}; - intersections.forEach((intersection) => { - var key = intersection.join(","); - if (!unique[key]) { - unique[key] = true; - results.push(intersection); + Set unique = {}; + intersections.forEach((intersection) { + if (!unique.contains(intersection)) { + unique.add(intersection); + results.add(intersection); } }); } else { results = intersections; } - return FeatureCollection(features: results.map((r) => Feature(geometry: Point(coordinates:r)))); + return FeatureCollection( + features: results + .map((r) => Feature(geometry: Point(coordinates: r))) + .toList()); } - - /** * import { feature, featureCollection, point } from "@turf/helpers"; import { diff --git a/pubspec.yaml b/pubspec.yaml index 0529e9f..3d3f055 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,6 +10,10 @@ publish_to: none dependencies: json_annotation: ^4.4.0 turf_equality: ^0.0.1 + sweepline_intersections: + git: https://github.com/dartclub/sweepline_intersections.git + turf_pip: ^0.0.1+1 + dev_dependencies: dartclub_lint: ^0.4.2 test: ^1.19.3 diff --git a/test/booleans/disjoint_test.dart b/test/booleans/disjoint_test.dart index 369df33..df6c808 100644 --- a/test/booleans/disjoint_test.dart +++ b/test/booleans/disjoint_test.dart @@ -7,9 +7,9 @@ import 'package:turf/src/booleans/boolean_disjoint.dart'; main() { group( - 'boolean_crosses', + 'boolean_disjoint', () { - var inDir = Directory('./test/examples/booleans/disjoint/true'); + var inDir = Directory('./test/examples/booleans/disjoint/test/true'); for (var file in inDir.listSync(recursive: true)) { if (file is File && file.path.endsWith('.geojson')) { test( @@ -26,7 +26,8 @@ main() { }, ); // False Fixtures - var inDir1 = Directory('./test/examples/booleans/disjoint/false'); + var inDir1 = + Directory('./test/examples/booleans/disjoint/test/false'); for (var file in inDir1.listSync(recursive: true)) { if (file is File && file.path.endsWith('.geojson')) { test( diff --git a/test/booleans/intersect_test.dart b/test/booleans/intersect_test.dart index 4540ac4..f062ff7 100644 --- a/test/booleans/intersect_test.dart +++ b/test/booleans/intersect_test.dart @@ -2,7 +2,7 @@ import 'package:test/test.dart'; import 'package:turf/helpers.dart'; import 'package:turf/src/booleans/boolean_intersect.dart'; -// main() { +main() { var featureCollection = FeatureCollection(features: [ Feature( properties: {"fill": "#ff0000"},