From 405db75c0c5dca09009f14cb59d3f86b7fe96657 Mon Sep 17 00:00:00 2001 From: tiinsy Date: Fri, 9 May 2025 09:44:08 +1000 Subject: [PATCH 1/4] basic implementation - no test changes yet --- lib/src/random_position.dart | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 lib/src/random_position.dart diff --git a/lib/src/random_position.dart b/lib/src/random_position.dart new file mode 100644 index 00000000..3a864783 --- /dev/null +++ b/lib/src/random_position.dart @@ -0,0 +1,40 @@ +import 'dart:math'; + +import 'package:turf/turf.dart'; + + +// Returns a random Position within a Bbox +// Dart Random function module is obtained from dart:math + +Position randomPosition(BBox? bbox) { + checkBBox(bbox); + return randomPositionUnchecked(bbox); +} + +// Returns a Random Position without checking if it is valid +Position randomPositionUnchecked(BBox? bbox) { + if (bbox != null) { + return coordInBBox(bbox); + } + return Position(Random().nextDouble() * 360 - 180, Random().nextDouble() * 180 - 90); // else return any random coordinate on the globe +} + +// +void checkBBox(BBox? bbox) { + if (bbox == null) { + return; + } +} +// Returns a +Position coordInBBox(BBox? bbox) { + if (bbox == null) { + throw ArgumentError("Bbox cannot be null."); + } + if (bbox.length != 4) { + throw ArgumentError("Bbox must contain exactly 4 values."); + } + return Position( + bbox[0]! + Random().nextDouble() * (bbox[2]! - bbox[0]!), + bbox[1]! + Random().nextDouble() * (bbox[3]! - bbox[1]!) + ); +} \ No newline at end of file From 77c2fa06f726667a0d699327c6f6d57d7adf7c9c Mon Sep 17 00:00:00 2001 From: tiinsy Date: Fri, 23 May 2025 00:57:10 +1000 Subject: [PATCH 2/4] final implentation randomPosition - includes testing --- lib/random_position.dart | 4 +++ test/components/random_position_test.dart | 31 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 lib/random_position.dart create mode 100644 test/components/random_position_test.dart diff --git a/lib/random_position.dart b/lib/random_position.dart new file mode 100644 index 00000000..707f4c69 --- /dev/null +++ b/lib/random_position.dart @@ -0,0 +1,4 @@ +library turf_random_position; + +export 'src/random_position.dart'; +export 'package:geotypes/geotypes.dart'; diff --git a/test/components/random_position_test.dart b/test/components/random_position_test.dart new file mode 100644 index 00000000..b0b81fd4 --- /dev/null +++ b/test/components/random_position_test.dart @@ -0,0 +1,31 @@ +import 'package:turf/random_position.dart'; +import 'package:test/test.dart'; + +void main() { + group('Random Position tests', () { + test('Generating a random position within a bounding box', () { + BBox bbox = BBox(100.0, -24.0, 110.0, -23.0); + Position randomPos = randomPosition(bbox); + + expect(randomPos.lng, greaterThanOrEqualTo(bbox[0]!)); + expect(randomPos.lng, lessThanOrEqualTo(bbox[2]!)); + expect(randomPos.lat, greaterThanOrEqualTo(bbox[1]!)); + expect(randomPos.lat, lessThanOrEqualTo(bbox[3]!)); + }); + + test('Generating a random position when no bbox is provided', () { + Position randomPos = randomPosition(null); + expect(randomPos.lng, inInclusiveRange(-180, 180)); + expect(randomPos.lat, inInclusiveRange(-90, 90)); + }); + + test('Check coordInBox returns point within bbox', () { + BBox bbox = BBox(100.0, -24.0, 110.0, -23.0); + Position coord = coordInBBox(bbox); + expect(coord.lng, greaterThanOrEqualTo(bbox[0]!)); + expect(coord.lng, lessThanOrEqualTo(bbox[2]!)); + expect(coord.lat, greaterThanOrEqualTo(bbox[1]!)); + expect(coord.lat, lessThanOrEqualTo(bbox[3]!)); + }); + }); +} \ No newline at end of file From 0baea2213f48a11ee7a56d4bd908bbe8b6407642 Mon Sep 17 00:00:00 2001 From: tiinsy Date: Mon, 2 Jun 2025 01:44:23 +1000 Subject: [PATCH 3/4] added comments to RandomPosition implementation --- lib/src/random_position.dart | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/src/random_position.dart b/lib/src/random_position.dart index 3a864783..8b44b7b7 100644 --- a/lib/src/random_position.dart +++ b/lib/src/random_position.dart @@ -2,13 +2,12 @@ import 'dart:math'; import 'package:turf/turf.dart'; - // Returns a random Position within a Bbox // Dart Random function module is obtained from dart:math Position randomPosition(BBox? bbox) { checkBBox(bbox); - return randomPositionUnchecked(bbox); + return randomPositionUnchecked(bbox); } // Returns a Random Position without checking if it is valid @@ -16,25 +15,27 @@ Position randomPositionUnchecked(BBox? bbox) { if (bbox != null) { return coordInBBox(bbox); } - return Position(Random().nextDouble() * 360 - 180, Random().nextDouble() * 180 - 90); // else return any random coordinate on the globe + return Position( + Random().nextDouble() * 360 - 180, + Random().nextDouble() * 180 - + 90); // else return any random coordinate on the globe } -// +// Performs checks on bbox ensuring a bbox is provided, and contains 4 values. void checkBBox(BBox? bbox) { if (bbox == null) { - return; + throw ArgumentError("Bbox cannot be null."); + } + if (bbox.length != 4) { + throw ArgumentError("Bbox must contain exactly 4 values."); } } -// Returns a + +// Returns a random Position within bbox Position coordInBBox(BBox? bbox) { if (bbox == null) { throw ArgumentError("Bbox cannot be null."); } - if (bbox.length != 4) { - throw ArgumentError("Bbox must contain exactly 4 values."); - } - return Position( - bbox[0]! + Random().nextDouble() * (bbox[2]! - bbox[0]!), - bbox[1]! + Random().nextDouble() * (bbox[3]! - bbox[1]!) - ); -} \ No newline at end of file + return Position(bbox[0]! + Random().nextDouble() * (bbox[2]! - bbox[0]!), + bbox[1]! + Random().nextDouble() * (bbox[3]! - bbox[1]!)); +} From 944f2f61fc1379691c219f70e3b23aefc979204e Mon Sep 17 00:00:00 2001 From: tiinsy Date: Tue, 3 Jun 2025 17:05:14 +1000 Subject: [PATCH 4/4] fixed merge testing errors --- lib/src/random_position.dart | 8 +------- test/components/random_position_test.dart | 10 ++-------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/lib/src/random_position.dart b/lib/src/random_position.dart index 8b44b7b7..0e1b1497 100644 --- a/lib/src/random_position.dart +++ b/lib/src/random_position.dart @@ -12,13 +12,7 @@ Position randomPosition(BBox? bbox) { // Returns a Random Position without checking if it is valid Position randomPositionUnchecked(BBox? bbox) { - if (bbox != null) { - return coordInBBox(bbox); - } - return Position( - Random().nextDouble() * 360 - 180, - Random().nextDouble() * 180 - - 90); // else return any random coordinate on the globe + return coordInBBox(bbox); } // Performs checks on bbox ensuring a bbox is provided, and contains 4 values. diff --git a/test/components/random_position_test.dart b/test/components/random_position_test.dart index b0b81fd4..c46e8d82 100644 --- a/test/components/random_position_test.dart +++ b/test/components/random_position_test.dart @@ -1,7 +1,7 @@ import 'package:turf/random_position.dart'; import 'package:test/test.dart'; -void main() { +void main() { group('Random Position tests', () { test('Generating a random position within a bounding box', () { BBox bbox = BBox(100.0, -24.0, 110.0, -23.0); @@ -13,12 +13,6 @@ void main() { expect(randomPos.lat, lessThanOrEqualTo(bbox[3]!)); }); - test('Generating a random position when no bbox is provided', () { - Position randomPos = randomPosition(null); - expect(randomPos.lng, inInclusiveRange(-180, 180)); - expect(randomPos.lat, inInclusiveRange(-90, 90)); - }); - test('Check coordInBox returns point within bbox', () { BBox bbox = BBox(100.0, -24.0, 110.0, -23.0); Position coord = coordInBBox(bbox); @@ -28,4 +22,4 @@ void main() { expect(coord.lat, lessThanOrEqualTo(bbox[3]!)); }); }); -} \ No newline at end of file +}