location: fixed conversion from decimal degree to DMS

This commit is contained in:
Thibault Deckers 2020-08-21 11:00:21 +09:00
parent d83d0e353a
commit f8928f557f
2 changed files with 19 additions and 15 deletions

View file

@ -3,8 +3,7 @@ import 'dart:math' as math;
import 'package:intl/intl.dart';
import 'package:tuple/tuple.dart';
// adapted from Mike Mitterer's dart-latlong library
String _decimal2sexagesimal(final double dec) {
String _decimal2sexagesimal(final double degDecimal) {
double _round(final double value, {final int decimals = 6}) => (value * math.pow(10, decimals)).round() / math.pow(10, decimals);
List<int> _split(final double value) {
@ -17,22 +16,15 @@ String _decimal2sexagesimal(final double dec) {
];
}
final parts = _split(dec);
final integerPart = parts[0];
final fractionalPart = parts[1];
final deg = _split(degDecimal)[0];
final minDecimal = (degDecimal.abs() - deg) * 60;
final min = _split(minDecimal)[0];
final sec = (minDecimal - min) * 60;
final deg = integerPart;
final min = double.parse('0.$fractionalPart') * 60;
final minParts = _split(min);
final minFractionalPart = minParts[1];
final sec = double.parse('0.$minFractionalPart') * 60;
return '$deg° ${min.floor()} ${_round(sec, decimals: 2).toStringAsFixed(2)}';
return '$deg° $min ${_round(sec, decimals: 2).toStringAsFixed(2)}';
}
// return coordinates formatted as DMS, e.g. ['41°2412.2″ N', '1026.5″E']
// return coordinates formatted as DMS, e.g. ['41° 24 12.2″ N', '2° 10 26.5″ E']
List<String> toDMS(Tuple2<double, double> latLng) {
if (latLng == null) return [];
final lat = latLng.item1;

View file

@ -0,0 +1,12 @@
import 'package:aves/utils/geo_utils.dart';
import 'package:test/test.dart';
import 'package:tuple/tuple.dart';
void main() {
test('Decimal degrees to DMS (sexagesimal)', () {
expect(toDMS(Tuple2(37.496667, 127.0275)), ['37° 29 48.00″ N', '127° 1 39.00″ E']); // Gangnam
expect(toDMS(Tuple2(78.9243503, 11.9230465)), ['78° 55 27.66″ N', '11° 55 22.97″ E']); // Ny-Ålesund
expect(toDMS(Tuple2(-38.6965891, 175.9830047)), ['38° 41 47.72″ S', '175° 58 58.82″ E']); // Taupo
expect(toDMS(Tuple2(-64.249391, -56.6556145)), ['64° 14 57.81″ S', '56° 39 20.21″ W']); // Marambio
});
}