From 02f4584b3fd9a799dc4b573bbc9068974e700a23 Mon Sep 17 00:00:00 2001 From: Jonas Siedentop Date: Sat, 17 Feb 2024 14:08:42 +0100 Subject: [PATCH] Code review suggestions --- lib/src/booleans/boolean_contains.dart | 20 +++++++++--------- lib/src/booleans/boolean_helper.dart | 8 +++---- lib/src/booleans/discussion.md | 29 -------------------------- test/booleans/within_test.dart | 20 +++++++++--------- 4 files changed, 24 insertions(+), 53 deletions(-) delete mode 100644 lib/src/booleans/discussion.md diff --git a/lib/src/booleans/boolean_contains.dart b/lib/src/booleans/boolean_contains.dart index 12c68fe..13f77ed 100644 --- a/lib/src/booleans/boolean_contains.dart +++ b/lib/src/booleans/boolean_contains.dart @@ -12,22 +12,22 @@ import 'boolean_helper.dart'; /// [booleanContains] returns the exact opposite result of the [booleanWithin]. /// example: /// ```dart -/// var line = LineString(coordinates: [ +/// final line = LineString(coordinates: [ /// Position.of([1, 1]), /// Position.of([1, 2]), /// Position.of([1, 3]), /// Position.of([1, 4]) /// ]); -/// var point = Point(coordinates: Position.of([1, 2])); +/// final point = Point(coordinates: Position.of([1, 2])); /// booleanContains(line, point); /// //=true /// ``` bool booleanContains(GeoJSONObject feature1, GeoJSONObject feature2) { - var geom1 = getGeom(feature1); - var geom2 = getGeom(feature2); + final geom1 = getGeom(feature1); + final geom2 = getGeom(feature2); - var coords1 = (geom1 as GeometryType).coordinates; - var coords2 = (geom2 as GeometryType).coordinates; + final coords1 = (geom1 as GeometryType).coordinates; + final coords2 = (geom2 as GeometryType).coordinates; if (geom1 is Point) { if (geom2 is Point) { return coords1 == coords2; @@ -73,14 +73,14 @@ bool booleanContains(GeoJSONObject feature1, GeoJSONObject feature2) { /// Is Polygon2 in Polygon1 /// Only takes into account outer rings bool _isPolyInPoly(GeoJSONObject geom1, GeoJSONObject geom2) { - var poly1Bbox = bbox(geom1); - var poly2Bbox = bbox(geom2); + final poly1Bbox = bbox(geom1); + final poly2Bbox = bbox(geom2); if (!_doBBoxesOverlap(poly1Bbox, poly2Bbox)) { return false; } - for (var ring in (geom2 as GeometryType).coordinates) { - for (var coord in ring) { + for (final ring in (geom2 as GeometryType).coordinates) { + for (final coord in ring) { if (!booleanPointInPolygon(coord, geom1)) { return false; } diff --git a/lib/src/booleans/boolean_helper.dart b/lib/src/booleans/boolean_helper.dart index 974eb49..b121e4b 100644 --- a/lib/src/booleans/boolean_helper.dart +++ b/lib/src/booleans/boolean_helper.dart @@ -117,8 +117,8 @@ bool isLineInMultiPolygon(LineString line, MultiPolygon polygon) => _isLineInGeoJsonPolygon(line, polygon); bool _isLineInGeoJsonPolygon(LineString line, GeoJSONObject polygon) { - var boundingBoxOfPolygon = bbox(polygon); - var boundingBoxOfLine = bbox(line); + final boundingBoxOfPolygon = bbox(polygon); + final boundingBoxOfLine = bbox(line); if (!_doBBoxesOverlap(boundingBoxOfPolygon, boundingBoxOfLine)) { return false; @@ -197,8 +197,8 @@ bool _isPolygonInGeoJsonPolygon( Polygon polygon1, GeoJSONObject polygon2, ) { - var boundingBoxOfPolygon1 = bbox(polygon1); - var boundingBoxOfPolygon2 = bbox(polygon2); + final boundingBoxOfPolygon1 = bbox(polygon1); + final boundingBoxOfPolygon2 = bbox(polygon2); if (!_doBBoxesOverlap(boundingBoxOfPolygon2, boundingBoxOfPolygon1)) { return false; } diff --git a/lib/src/booleans/discussion.md b/lib/src/booleans/discussion.md deleted file mode 100644 index 8fd38af..0000000 --- a/lib/src/booleans/discussion.md +++ /dev/null @@ -1,29 +0,0 @@ - -``` - -// booleanWithin(Multipoint, LineString) -> at least one point not at start or end. -// booleanWithin(Point, LineString) -> ignore Vertices. -// booleanWithin(Point, Polygon) -> ignore Vertices. -// booleanWithin(MultiPoint, LineString) -> at least one inner point. -// booleanWithin(MultiPoint, Polygon) -> at least one inner point. -// booleanWithin(LineString, LineString) -> no check if there is a inner point. -// booleanWithin(LineString, Polygon) -> at least one point of the line or one -// point between two point of the line is not on the boundary. -// booleanWithin(Polygon, Polygon) -> no check if there is a inner point. - -// booleanWithin(LineString, Polygon): TurfJs seems to have an bug. When the last -// point of the line is outside of the polygon, the function returns true. -// turf-boolean-within -> isLineInPoly - -``` - - - -https://geojson.io/#map=4.84/4.83/2.16 - - -## Polygon is within polygon -file: examples/booleans/within/true/Polygon/Polygon/skip-PolygonIsWIthinPolygon.geojson - -This test fails, because the bounding box of the first polygon is not complete within the second polygon. Both polygons share one point. -I'm not sure, if this is the expected behavior. Other within functions allow it if one geometry is on the border of another geometry, if diff --git a/test/booleans/within_test.dart b/test/booleans/within_test.dart index 1c62af6..46debe4 100644 --- a/test/booleans/within_test.dart +++ b/test/booleans/within_test.dart @@ -45,8 +45,8 @@ void main() { (path, geoJson) { final multiPolygon = (geoJson as Feature); final pointInHole = point([-86.69208526611328, 36.20373274711739]); - var pointInPolygon = point([-86.72229766845702, 36.20258997094334]); - var pointInSecondPolygon = + final pointInPolygon = point([-86.72229766845702, 36.20258997094334]); + final pointInSecondPolygon = point([-86.75079345703125, 36.18527313913089]); expect(booleanWithin(pointInHole, multiPolygon), false, @@ -59,7 +59,7 @@ void main() { }); test("within - point in polygon", () { - var simplePolygon = polygon([ + final simplePolygon = polygon([ [ [0, 0], [0, 100], @@ -68,15 +68,15 @@ void main() { [0, 0], ], ]); - var pointIn = point([50, 50]); - var pointOut = point([140, 150]); + final pointIn = point([50, 50]); + final pointOut = point([140, 150]); expect(booleanWithin(pointIn, simplePolygon), true, reason: "point inside polygon"); expect(booleanWithin(pointOut, simplePolygon), false, reason: "point outside polygon"); - var concavePolygon = polygon([ + final concavePolygon = polygon([ [ [0, 0], [50, 50], @@ -87,8 +87,8 @@ void main() { ], ]); - var pointInConcave = point([75, 75]); - var pointOutConcave = point([25, 50]); + final pointInConcave = point([75, 75]); + final pointOutConcave = point([25, 50]); expect(booleanWithin(pointInConcave, concavePolygon), true, reason: "point inside concave polygon"); @@ -108,9 +108,9 @@ void loadGeoJson( void loadGeoJsonFiles( String path, void Function(String path, GeoJSONObject geoJson) test) { - var testDirectory = Directory(path); + final testDirectory = Directory(path); - for (var file in testDirectory.listSync(recursive: true)) { + for (final file in testDirectory.listSync(recursive: true)) { if (file is File && file.path.endsWith('.geojson')) { if (file.path.contains('skip')) continue;