From 5c863e05f5882b040bd4001a83cd096c7a3bacee Mon Sep 17 00:00:00 2001 From: armantorkzaban Date: Fri, 8 Jul 2022 01:28:25 +0200 Subject: [PATCH] moving forward --- lib/src/booleans/boolean_equal.dart | 20 +++++-- test/booleans/equal_test.dart | 58 +++++++++++-------- .../test/true/different-initials-poly.geojson | 48 +++++++++++---- .../equal/test/true/reverse-polygons.geojson | 47 ++++++++++++--- 4 files changed, 125 insertions(+), 48 deletions(-) diff --git a/lib/src/booleans/boolean_equal.dart b/lib/src/booleans/boolean_equal.dart index e903205..f461249 100644 --- a/lib/src/booleans/boolean_equal.dart +++ b/lib/src/booleans/boolean_equal.dart @@ -5,7 +5,11 @@ import '../clean_coords.dart'; /// Determine whether two geometries of the same type have identical X,Y coordinate values. /// See http://edndoc.esri.com/arcsde/9.0/general_topics/understand_spatial_relations.htm -/// [precision]=6 sets decimal precision to use when comparing coordinates +/// [precision]=6 sets decimal precision to use when comparing coordinates. +/// With [direction] set to true, even if the [LineStrings] are reverse versions +/// of each other but the have similar [Position]s, they will be considered the same. +/// If [shiftedPolygon] is true, two [Polygon]s with shifted [Position]s are +/// considered the same. /// Returns true if the objects are equal, false otherwise /// example: /// var pt1 = Point(coordinates: Position.of([0, 0])); @@ -15,8 +19,13 @@ import '../clean_coords.dart'; /// //= true /// booleanEqual(pt2, pt3); /// //= false -bool booleanEqual(GeoJSONObject feature1, GeoJSONObject feature2, - {int precision = 6}) { +bool booleanEqual( + GeoJSONObject feature1, + GeoJSONObject feature2, { + int precision = 6, + bool direction = false, + bool shiftedPolygon = false, +}) { if (!(precision >= 0)) { throw Exception("precision must be a positive number"); } @@ -27,6 +36,9 @@ bool booleanEqual(GeoJSONObject feature1, GeoJSONObject feature2, var type2 = geom2!.type; if (type1 != type2) return false; - var equality = Equality(precision: precision); + var equality = Equality( + precision: precision, + shiftedPolygon: shiftedPolygon, + direction: direction); return equality.compare(cleanCoords(feature1), cleanCoords(feature2)); } diff --git a/test/booleans/equal_test.dart b/test/booleans/equal_test.dart index d8e7a0f..ad766a6 100644 --- a/test/booleans/equal_test.dart +++ b/test/booleans/equal_test.dart @@ -12,41 +12,49 @@ main() { var inDir = Directory('./test/examples/booleans/equal/test/true'); for (var file in inDir.listSync(recursive: true)) { if (file is File && file.path.endsWith('.geojson')) { - test(file.path, () { - // True Fixtures - var inSource = file.readAsStringSync(); - var inGeom = GeoJSONObject.fromJson(jsonDecode(inSource)); + test( + file.path, + () { + // True Fixtures + var inSource = file.readAsStringSync(); + var inGeom = GeoJSONObject.fromJson(jsonDecode(inSource)); - var feature1 = (inGeom as FeatureCollection).features[0]; - var feature2 = inGeom.features[1]; - Map json = jsonDecode(inSource); - var options = json['properties']; - var result = booleanEqual(feature1, feature2, - precision: options?['precision'] ?? 6); - - expect(result, true); - }); + var feature1 = (inGeom as FeatureCollection).features[0]; + var feature2 = inGeom.features[1]; + Map json = jsonDecode(inSource); + var options = json['properties']; + print(json['properties']?['direction']); + var result = booleanEqual(feature1, feature2, + precision: options?['precision'] ?? 6, + shiftedPolygon: options?['shiftedPolygon'] ?? false, + direction: options?['direction'] ?? false); + expect(result, true); + }, + ); } } // False Fixtures var inDir1 = Directory('./test/examples/booleans/equal/test/false'); for (var file in inDir1.listSync(recursive: true)) { if (file is File && file.path.endsWith('.geojson')) { - test(file.path, () { - // True Fixtures - var inSource = file.readAsStringSync(); - var inGeom = GeoJSONObject.fromJson(jsonDecode(inSource)); + test( + file.path, + () { + // True Fixtures + var inSource = file.readAsStringSync(); + var inGeom = GeoJSONObject.fromJson(jsonDecode(inSource)); - var feature1 = (inGeom as FeatureCollection).features[0]; - var feature2 = inGeom.features[1]; + var feature1 = (inGeom as FeatureCollection).features[0]; + var feature2 = inGeom.features[1]; - Map json = jsonDecode(inSource); - var options = json['properties']; - var result = booleanEqual(feature1, feature2, - precision: options?['precision'] ?? 6); + Map json = jsonDecode(inSource); + var options = json['properties']; + var result = booleanEqual(feature1, feature2, + precision: options?['precision'] ?? 6); - expect(result, false); - }); + expect(result, false); + }, + ); } } var pt = Point(coordinates: Position.of([9, 50])); diff --git a/test/examples/booleans/equal/test/true/different-initials-poly.geojson b/test/examples/booleans/equal/test/true/different-initials-poly.geojson index fcd1cdb..929e7f6 100644 --- a/test/examples/booleans/equal/test/true/different-initials-poly.geojson +++ b/test/examples/booleans/equal/test/true/different-initials-poly.geojson @@ -1,17 +1,33 @@ { "type": "FeatureCollection", + "properties": { + "precision": 6, + "shiftedPolygon": true, + "direction": false + }, "features": [ { "type": "Feature", - "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ - [1, 1], - [1, 0], - [0, 0], - [1, 1] + [ + 1, + 1 + ], + [ + 1, + 0 + ], + [ + 0, + 0 + ], + [ + 1, + 1 + ] ] ] } @@ -23,13 +39,25 @@ "type": "Polygon", "coordinates": [ [ - [0, 0], - [1, 1], - [1, 0], - [0, 0] + [ + 0, + 0 + ], + [ + 1, + 1 + ], + [ + 1, + 0 + ], + [ + 0, + 0 + ] ] ] } } ] -} +} \ No newline at end of file diff --git a/test/examples/booleans/equal/test/true/reverse-polygons.geojson b/test/examples/booleans/equal/test/true/reverse-polygons.geojson index 6f8b9a9..8c7530d 100644 --- a/test/examples/booleans/equal/test/true/reverse-polygons.geojson +++ b/test/examples/booleans/equal/test/true/reverse-polygons.geojson @@ -1,5 +1,10 @@ { "type": "FeatureCollection", + "properties": { + "precision": 6, + "shiftedPolygon": true, + "direction": true + }, "features": [ { "type": "Feature", @@ -8,10 +13,22 @@ "type": "Polygon", "coordinates": [ [ - [-53.57, 28.28], - [-53.33, 28.29], - [-53.34, 28.43], - [-53.57, 28.28] + [ + -53.57, + 28.28 + ], + [ + -53.33, + 28.29 + ], + [ + -53.34, + 28.43 + ], + [ + -53.57, + 28.28 + ] ] ] } @@ -23,13 +40,25 @@ "type": "Polygon", "coordinates": [ [ - [-53.57, 28.28], - [-53.34, 28.43], - [-53.33, 28.29], - [-53.57, 28.28] + [ + -53.57, + 28.28 + ], + [ + -53.34, + 28.43 + ], + [ + -53.33, + 28.29 + ], + [ + -53.57, + 28.28 + ] ] ] } } ] -} +} \ No newline at end of file