-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtransform_rotate.dart
44 lines (38 loc) · 1.35 KB
/
transform_rotate.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
39
40
41
42
43
44
import 'package:turf/bearing.dart';
import 'package:turf/distance.dart';
import 'package:turf/src/centroid.dart';
import 'package:turf/src/invariant.dart';
import 'package:turf/src/meta/coord.dart';
import 'package:turf/src/rhumb_destination.dart';
/// Rotates any [GeoJSONObject] of a specified angle, around its `centroid` or a given `pivot` [Point].
///
/// example:
/// ```dart
/// final line = Feature<LineString>(geometry: LineString.fromJson({'coordinates': [[10, 10],[12, 15]]}));
/// final rotated = transformRotate(line, 100);
/// ```
GeoJSONObject transformRotate(
GeoJSONObject geoJSON,
num angle, {
Point? pivot,
bool mutate = false,
}) {
if (angle == 0) {
return geoJSON;
}
// Use centroid of GeoJSON if pivot is not provided
pivot ??= centroid(geoJSON).geometry!;
// Clone geojson to avoid side effects
if (mutate == false) geoJSON = geoJSON.clone();
// Rotate each coordinate
coordEach(geoJSON, (pointCoords, _, __, ___, ____) {
final currentPoint = Point(coordinates: pointCoords!);
final initialAngle = rhumbBearing(pivot!, currentPoint);
final finalAngle = initialAngle + angle;
final distance = rhumbDistance(pivot, currentPoint);
final newCoords = getCoord(rhumbDestination(pivot, distance, finalAngle));
pointCoords[0] = newCoords[0]!;
pointCoords[1] = newCoords[1]!;
});
return geoJSON;
}