1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection;
|
---|
3 |
|
---|
4 | import java.security.SecureRandom;
|
---|
5 | import java.util.Random;
|
---|
6 |
|
---|
7 | import org.junit.Assert;
|
---|
8 | import org.junit.jupiter.api.Test;
|
---|
9 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Unit tests for class {@link Ellipsoid}.
|
---|
13 | */
|
---|
14 | class EllipsoidTest {
|
---|
15 |
|
---|
16 | private static final double EPSILON = 1e-8;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * convert latlon to cartesian coordinates back and forth
|
---|
20 | */
|
---|
21 | @Test
|
---|
22 | void testLatLon2Cart2LatLon() {
|
---|
23 | Random r = new SecureRandom();
|
---|
24 | double maxErrLat = 0, maxErrLon = 0;
|
---|
25 | Ellipsoid ellips = Ellipsoid.WGS84;
|
---|
26 | for (int num = 0; num < 1000; ++num) {
|
---|
27 |
|
---|
28 | double lat = r.nextDouble() * 180.0 - 90.0;
|
---|
29 | double lon = r.nextDouble() * 360.0 - 180.0;
|
---|
30 | LatLon ll = new LatLon(lat, lon);
|
---|
31 |
|
---|
32 | for (int i = 0; i < 1000; ++i) {
|
---|
33 | double[] cart = ellips.latLon2Cart(ll);
|
---|
34 | ll = ellips.cart2LatLon(cart);
|
---|
35 |
|
---|
36 | if (!(Math.abs(lat - ll.lat()) < EPSILON && Math.abs(lon - ll.lon()) < EPSILON)) {
|
---|
37 | String error = String.format("point: %s iterations: %s current: %s errorLat: %s errorLon %s",
|
---|
38 | new LatLon(lat, lon), i, ll, Math.abs(lat - ll.lat()), Math.abs(lon - ll.lon()));
|
---|
39 | System.err.println(error);
|
---|
40 | Assert.fail();
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | maxErrLat = Math.max(maxErrLat, Math.abs(lat - ll.lat()));
|
---|
45 | maxErrLon = Math.max(maxErrLon, Math.abs(lon - ll.lon()));
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|