Skip to content

Commit

Permalink
Add utility methods for GPS units conversion (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: Levi Lesches <[email protected]>
  • Loading branch information
Gold872 and Levi-Lesches authored Nov 28, 2024
1 parent 61c1952 commit 7ec8005
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "dart:async";
import "dart:math";

import "package:burt_network/generated.dart";

Expand Down Expand Up @@ -46,3 +47,37 @@ String? deviceToDataName(Device device) => switch (device) {
Device.BASE_STATION => BaseStationData().messageName,
_ => null,
};

/// Helpful extension methods to convert [GpsCoordinates] into meters
extension GpsToMeters on GpsCoordinates {
/// Number of meters per degree of latitude
static const metersPerLatitude = 111.32 * 1000;
/// Number of radians per degree.
static const radiansPerDegree = pi / 180;

/// Number of meters per degree longitude at the given latitude.
///
/// While the distance between rings of latitude remains constant all over the Earth, rings of
/// longitude stretch as you near the equator, and come to a single point at the poles. For that
/// reason, we need to calculate this number based on your current latitude.
///
/// Source: https://stackoverflow.com/a/39540339/9392211
static double metersPerLongitude(double latitude) =>
40075 * cos(latitude * radiansPerDegree ) / 360 * 1000;

/// Converts [GpsCoordinates] into (lat, long) in meters.
({double lat, double long}) get inMeters => (
lat: latitude * metersPerLatitude,
long: longitude * metersPerLongitude(latitude),
);
}

/// Extension to convert coordinates in meters into [GpsCoordinates].
extension MetersToGps on ({num lat, num long}) {
/// Convert a record of (lat, long) in meters into [GpsCoordinates].
GpsCoordinates toGps() {
final degreeLatitude = lat / GpsToMeters.metersPerLatitude;
final degreeLongitude = long / GpsToMeters.metersPerLongitude(degreeLatitude);
return GpsCoordinates(latitude: degreeLatitude, longitude: degreeLongitude);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: burt_network
description: A starting point for Dart libraries or applications.
version: 2.1.0
version: 2.2.0
publish_to: none
repository: https://github.com/BinghamtonRover/Networking

Expand Down

0 comments on commit 7ec8005

Please sign in to comment.