Skip to content

Commit

Permalink
fix: PolylineLayer throws exception: "The west longitude can't be s…
Browse files Browse the repository at this point in the history
…maller than the east longitude" (#1879)
  • Loading branch information
josxha authored May 15, 2024
1 parent 4f6b524 commit f68190c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
78 changes: 48 additions & 30 deletions lib/src/geo/latlng_bounds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import 'package:vector_math/vector_math_64.dart';
/// Data structure representing rectangular bounding box constrained by its
/// northwest and southeast corners
class LatLngBounds {
/// minimum latitude value, south
static const double minLatitude = -90;

/// maximum latitude value, north
static const double maxLatitude = 90;

/// minimum longitude value, west
static const double minLongitude = -180;

/// maximum longitude value, east
static const double maxLongitude = 180;

/// The latitude north edge of the bounds
double north;

Expand Down Expand Up @@ -61,26 +73,32 @@ class LatLngBounds {
required this.south,
required this.east,
required this.west,
}) : assert(
north <= 90, "The north latitude can't be bigger than 90: $north"),
assert(north >= -90,
"The north latitude can't be smaller than -90: $north"),
assert(
south <= 90, "The south latitude can't be bigger than 90: $south"),
assert(south >= -90,
"The south latitude can't be smaller than -90: $south"),
}) : assert(north <= maxLatitude,
"The north latitude can't be bigger than $maxLatitude: $north"),
assert(north >= minLatitude,
"The north latitude can't be smaller than $minLatitude: $north"),
assert(south <= maxLatitude,
"The south latitude can't be bigger than $maxLatitude: $south"),
assert(south >= minLatitude,
"The south latitude can't be smaller than $minLatitude: $south"),
assert(east <= maxLongitude,
"The east longitude can't be bigger than $maxLongitude: $east"),
assert(east >= minLongitude,
"The east longitude can't be smaller than $minLongitude: $east"),
assert(west <= maxLongitude,
"The west longitude can't be bigger than $maxLongitude: $west"),
assert(west >= minLongitude,
"The west longitude can't be smaller than $minLongitude: $west"),
assert(
east <= 180, "The east longitude can't be bigger than 180: $east"),
assert(east >= -180,
"The east longitude can't be smaller than -180: $east"),
north >= south,
"The north latitude ($north) can't be smaller than the "
'south latitude ($south)',
),
assert(
west <= 180, "The west longitude can't be bigger than 180: $west"),
assert(west >= -180,
"The west longitude can't be smaller than -180: $west"),
assert(north >= south,
"The north latitude can't be smaller than the south latitude"),
assert(east >= west,
"The west longitude can't be smaller than the east longitude");
east >= west,
"The west longitude ($west) can't be smaller than the "
'east longitude ($east)',
);

/// Create a new [LatLngBounds] from a list of [LatLng] points. This
/// calculates the bounding box of the provided points.
Expand All @@ -90,10 +108,10 @@ class LatLngBounds {
'LatLngBounds cannot be created with an empty List of LatLng',
);
// initialize bounds with max values.
double minX = 180;
double maxX = -180;
double minY = 90;
double maxY = -90;
double minX = maxLongitude;
double maxX = minLongitude;
double minY = maxLatitude;
double maxY = minLatitude;
// find the largest and smallest latitude and longitude
for (final point in points) {
if (point.longitude < minX) minX = point.longitude;
Expand All @@ -112,20 +130,20 @@ class LatLngBounds {
/// Expands bounding box by [latLng] coordinate point. This method mutates
/// the bounds object on which it is called.
void extend(LatLng latLng) {
north = min(90, max(north, latLng.latitude));
south = max(-90, min(south, latLng.latitude));
east = min(180, max(east, latLng.longitude));
west = max(-180, min(west, latLng.longitude));
north = min(maxLatitude, max(north, latLng.latitude));
south = max(minLatitude, min(south, latLng.latitude));
east = min(maxLongitude, max(east, latLng.longitude));
west = max(minLongitude, min(west, latLng.longitude));
}

/// Expands bounding box by other [bounds] object. If provided [bounds] object
/// is smaller than current one, it is not shrunk. This method mutates
/// the bounds object on which it is called.
void extendBounds(LatLngBounds bounds) {
north = min(90, max(north, bounds.north));
south = max(-90, min(south, bounds.south));
east = min(180, max(east, bounds.east));
west = max(-180, min(west, bounds.west));
north = min(maxLatitude, max(north, bounds.north));
south = max(minLatitude, min(south, bounds.south));
east = min(maxLongitude, max(east, bounds.east));
west = max(minLongitude, min(west, bounds.west));
}

/// Obtain coordinates of southwest corner of the bounds.
Expand Down
8 changes: 4 additions & 4 deletions lib/src/layer/polyline_layer/polyline_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {
// The min(-90), max(180), ... are used to get around the limits of LatLng
// the value cannot be greater or smaller than that
final boundsAdjusted = LatLngBounds.unsafe(
west: math.max(-180, bounds.west - margin),
east: math.min(90, bounds.east + margin),
south: math.max(-90, bounds.south - margin),
north: math.min(180, bounds.north + margin),
west: math.max(LatLngBounds.minLongitude, bounds.west - margin),
east: math.min(LatLngBounds.maxLongitude, bounds.east + margin),
south: math.max(LatLngBounds.minLatitude, bounds.south - margin),
north: math.min(LatLngBounds.maxLatitude, bounds.north + margin),
);

// segment is visible
Expand Down

0 comments on commit f68190c

Please sign in to comment.