|
1 | 1 | import 'dart:math';
|
2 | 2 | import 'package:test/test.dart';
|
3 | 3 | import 'package:turf/helpers.dart';
|
| 4 | +import 'package:geotypes/geotypes.dart'; |
4 | 5 |
|
5 | 6 | void main() {
|
6 | 7 | test('radiansToLength', () {
|
@@ -77,4 +78,98 @@ void main() {
|
77 | 78 | expect(convertArea(100, Unit.meters, Unit.feet), equals(1076.3910417));
|
78 | 79 | expect(convertArea(100000, Unit.feet), equals(0.009290303999749462));
|
79 | 80 | });
|
| 81 | + |
| 82 | + test('toMercator', () { |
| 83 | + // Test with San Francisco coordinates |
| 84 | + final wgs84 = [-122.4194, 37.7749]; |
| 85 | + final mercator = toMercator(wgs84); |
| 86 | + |
| 87 | + // Expected values (approximate) |
| 88 | + final expectedX = -13627665.0; |
| 89 | + final expectedY = 4547675.0; |
| 90 | + |
| 91 | + // Check conversion produces results within an acceptable range |
| 92 | + expect(mercator[0], closeTo(expectedX, 50.0)); |
| 93 | + expect(mercator[1], closeTo(expectedY, 50.0)); |
| 94 | + |
| 95 | + // Test with error case |
| 96 | + expect(() => toMercator([]), throwsException); |
| 97 | + }); |
| 98 | + |
| 99 | + test('toWGS84', () { |
| 100 | + // Test with San Francisco Mercator coordinates |
| 101 | + final mercator = [-13627695.092862014, 4547675.345836067]; |
| 102 | + final wgs84 = toWGS84(mercator); |
| 103 | + |
| 104 | + // Expected values (approximate) |
| 105 | + final expectedLon = -122.42; |
| 106 | + final expectedLat = 37.77; |
| 107 | + |
| 108 | + // Check conversion produces results within an acceptable range |
| 109 | + expect(wgs84[0], closeTo(expectedLon, 0.01)); |
| 110 | + expect(wgs84[1], closeTo(expectedLat, 0.01)); |
| 111 | + |
| 112 | + // Test with error case |
| 113 | + expect(() => toWGS84([]), throwsException); |
| 114 | + }); |
| 115 | + |
| 116 | + test('Round-trip conversion WGS84-Mercator-WGS84', () { |
| 117 | + // Test coordinates for various cities |
| 118 | + final cities = [ |
| 119 | + [-122.4194, 37.7749], // San Francisco |
| 120 | + [139.6917, 35.6895], // Tokyo |
| 121 | + [151.2093, -33.8688], // Sydney |
| 122 | + [-0.1278, 51.5074], // London |
| 123 | + ]; |
| 124 | + |
| 125 | + for (final original in cities) { |
| 126 | + final mercator = toMercator(original); |
| 127 | + final roundTrip = toWGS84(mercator); |
| 128 | + |
| 129 | + // Round-trip should return to the original value within a small delta |
| 130 | + expect(roundTrip[0], closeTo(original[0], 0.00001)); |
| 131 | + expect(roundTrip[1], closeTo(original[1], 0.00001)); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + test('convertCoordinates', () { |
| 136 | + // Test WGS84 to Mercator conversion |
| 137 | + final wgs84 = [-122.4194, 37.7749]; // San Francisco |
| 138 | + final mercator = convertCoordinates( |
| 139 | + wgs84, |
| 140 | + CoordinateSystem.wgs84, |
| 141 | + CoordinateSystem.mercator |
| 142 | + ); |
| 143 | + |
| 144 | + // Should match toMercator result |
| 145 | + final directMercator = toMercator(wgs84); |
| 146 | + expect(mercator[0], equals(directMercator[0])); |
| 147 | + expect(mercator[1], equals(directMercator[1])); |
| 148 | + |
| 149 | + // Test Mercator to WGS84 conversion |
| 150 | + final backToWgs84 = convertCoordinates( |
| 151 | + mercator, |
| 152 | + CoordinateSystem.mercator, |
| 153 | + CoordinateSystem.wgs84 |
| 154 | + ); |
| 155 | + |
| 156 | + // Should match toWGS84 result and be close to original |
| 157 | + expect(backToWgs84[0], closeTo(wgs84[0], 0.00001)); |
| 158 | + expect(backToWgs84[1], closeTo(wgs84[1], 0.00001)); |
| 159 | + |
| 160 | + // Test same system conversion (should return same values) |
| 161 | + final sameSystem = convertCoordinates( |
| 162 | + wgs84, |
| 163 | + CoordinateSystem.wgs84, |
| 164 | + CoordinateSystem.wgs84 |
| 165 | + ); |
| 166 | + expect(sameSystem[0], equals(wgs84[0])); |
| 167 | + expect(sameSystem[1], equals(wgs84[1])); |
| 168 | + |
| 169 | + // Test error case |
| 170 | + expect( |
| 171 | + () => convertCoordinates([], CoordinateSystem.wgs84, CoordinateSystem.mercator), |
| 172 | + throwsException |
| 173 | + ); |
| 174 | + }); |
80 | 175 | }
|
0 commit comments