-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbbox.dart
38 lines (34 loc) · 1.12 KB
/
bbox.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import 'package:turf/meta.dart';
/// Calculates the bounding box for any [geoJson] object, including [FeatureCollection].
/// If [recompute] is not set and the [bbox] is not null, the function uses the [bbox] of the given [GeoJSONObject].
BBox bbox(GeoJSONObject geoJson, {bool recompute = false}) {
if (geoJson.bbox != null && !recompute) {
return geoJson.bbox!;
}
var result = BBox.named(
lat1: double.infinity,
lng1: double.infinity,
lat2: double.negativeInfinity,
lng2: double.negativeInfinity,
);
coordEach(
geoJson,
(Position? currentCoord, _, __, ___, ____) {
if (currentCoord != null) {
if (result.lng1 > currentCoord.lng) {
result = result.copyWith(lng1: currentCoord.lng);
}
if (result.lat1 > currentCoord.lat) {
result = result.copyWith(lat1: currentCoord.lat);
}
if (result.lng2 < currentCoord.lng) {
result = result.copyWith(lng2: currentCoord.lng);
}
if (result.lat2 < currentCoord.lat) {
result = result.copyWith(lat2: currentCoord.lat);
}
}
},
);
return result;
}