| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.data.projection;
|
|---|
| 3 |
|
|---|
| 4 | import java.io.BufferedReader;
|
|---|
| 5 | import java.io.BufferedWriter;
|
|---|
| 6 | import java.io.File;
|
|---|
| 7 | import java.io.IOException;
|
|---|
| 8 | import java.io.OutputStreamWriter;
|
|---|
| 9 | import java.nio.charset.StandardCharsets;
|
|---|
| 10 | import java.nio.file.Files;
|
|---|
| 11 | import java.nio.file.Paths;
|
|---|
| 12 | import java.security.SecureRandom;
|
|---|
| 13 | import java.util.ArrayList;
|
|---|
| 14 | import java.util.HashMap;
|
|---|
| 15 | import java.util.List;
|
|---|
| 16 | import java.util.Map;
|
|---|
| 17 | import java.util.Random;
|
|---|
| 18 | import java.util.Set;
|
|---|
| 19 | import java.util.TreeSet;
|
|---|
| 20 | import java.util.stream.Collectors;
|
|---|
| 21 |
|
|---|
| 22 | import org.junit.BeforeClass;
|
|---|
| 23 | import org.junit.Test;
|
|---|
| 24 | import org.openstreetmap.josm.JOSMFixture;
|
|---|
| 25 | import org.openstreetmap.josm.data.Bounds;
|
|---|
| 26 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 27 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 28 | import org.openstreetmap.josm.tools.Pair;
|
|---|
| 29 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * This test is used to monitor changes in projection code.
|
|---|
| 33 | *
|
|---|
| 34 | * It keeps a record of test data in the file nodist/data/projection/projection-regression-test-data.
|
|---|
| 35 | * This record is generated from the current Projection classes available in JOSM. It needs to
|
|---|
| 36 | * be updated, whenever a projection is added / removed or an algorithm is changed, such that
|
|---|
| 37 | * the computed values are numerically different. There is no error threshold, every change is reported.
|
|---|
| 38 | *
|
|---|
| 39 | * So when this test fails, first check if the change is intended. Then update the regression
|
|---|
| 40 | * test data, by running the main method of this class and commit the new data file.
|
|---|
| 41 | */
|
|---|
| 42 | public class ProjectionRegressionTest {
|
|---|
| 43 |
|
|---|
| 44 | private static final String PROJECTION_DATA_FILE = "nodist/data/projection/projection-regression-test-data";
|
|---|
| 45 |
|
|---|
| 46 | private static class TestData {
|
|---|
| 47 | public String code;
|
|---|
| 48 | public LatLon ll;
|
|---|
| 49 | public EastNorth en;
|
|---|
| 50 | public LatLon ll2;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Program entry point to update reference projection file.
|
|---|
| 55 | * @param args not used
|
|---|
| 56 | * @throws IOException if any I/O errors occurs
|
|---|
| 57 | */
|
|---|
| 58 | public static void main(String[] args) throws IOException {
|
|---|
| 59 | setUp();
|
|---|
| 60 |
|
|---|
| 61 | Map<String, Projection> supportedCodesMap = new HashMap<>();
|
|---|
| 62 | for (String code : Projections.getAllProjectionCodes()) {
|
|---|
| 63 | supportedCodesMap.put(code, Projections.getProjectionByCode(code));
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | List<TestData> prevData = new ArrayList<>();
|
|---|
| 67 | if (new File(PROJECTION_DATA_FILE).exists()) {
|
|---|
| 68 | prevData = readData();
|
|---|
| 69 | }
|
|---|
| 70 | Map<String, TestData> prevCodesMap = new HashMap<>();
|
|---|
| 71 | for (TestData data : prevData) {
|
|---|
| 72 | prevCodesMap.put(data.code, data);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | Set<String> codesToWrite = new TreeSet<>();
|
|---|
| 76 | for (TestData data : prevData) {
|
|---|
| 77 | if (supportedCodesMap.containsKey(data.code)) {
|
|---|
| 78 | codesToWrite.add(data.code);
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | for (String code : supportedCodesMap.keySet()) {
|
|---|
| 82 | if (!codesToWrite.contains(code)) {
|
|---|
| 83 | codesToWrite.add(code);
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | Random rand = new SecureRandom();
|
|---|
| 88 | try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
|
|---|
| 89 | Files.newOutputStream(Paths.get(PROJECTION_DATA_FILE)), StandardCharsets.UTF_8))) {
|
|---|
| 90 | out.write("# Data for test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java\n");
|
|---|
| 91 | out.write("# Format: 1. Projection code; 2. lat/lon; 3. lat/lon projected -> east/north; 4. east/north (3.) inverse projected\n");
|
|---|
| 92 | for (String code : codesToWrite) {
|
|---|
| 93 | Projection proj = supportedCodesMap.get(code);
|
|---|
| 94 | Bounds b = proj.getWorldBoundsLatLon();
|
|---|
| 95 | double lat, lon;
|
|---|
| 96 | TestData prev = prevCodesMap.get(proj.toCode());
|
|---|
| 97 | if (prev != null) {
|
|---|
| 98 | lat = prev.ll.lat();
|
|---|
| 99 | lon = prev.ll.lon();
|
|---|
| 100 | } else {
|
|---|
| 101 | lat = b.getMin().lat() + rand.nextDouble() * (b.getMax().lat() - b.getMin().lat());
|
|---|
| 102 | lon = b.getMin().lon() + rand.nextDouble() * (b.getMax().lon() - b.getMin().lon());
|
|---|
| 103 | }
|
|---|
| 104 | EastNorth en = proj.latlon2eastNorth(new LatLon(lat, lon));
|
|---|
| 105 | LatLon ll2 = proj.eastNorth2latlon(en);
|
|---|
| 106 | out.write(String.format(
|
|---|
| 107 | "%s%n ll %s %s%n en %s %s%n ll2 %s %s%n", proj.toCode(), lat, lon, en.east(), en.north(), ll2.lat(), ll2.lon()));
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 | System.out.println("Update successful.");
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | private static List<TestData> readData() throws IOException {
|
|---|
| 114 | try (BufferedReader in = Files.newBufferedReader(Paths.get(PROJECTION_DATA_FILE), StandardCharsets.UTF_8)) {
|
|---|
| 115 | List<TestData> result = new ArrayList<>();
|
|---|
| 116 | String line;
|
|---|
| 117 | while ((line = in.readLine()) != null) {
|
|---|
| 118 | if (line.startsWith("#")) {
|
|---|
| 119 | continue;
|
|---|
| 120 | }
|
|---|
| 121 | TestData next = new TestData();
|
|---|
| 122 |
|
|---|
| 123 | Pair<Double, Double> ll = readLine("ll", in.readLine());
|
|---|
| 124 | Pair<Double, Double> en = readLine("en", in.readLine());
|
|---|
| 125 | Pair<Double, Double> ll2 = readLine("ll2", in.readLine());
|
|---|
| 126 |
|
|---|
| 127 | next.code = line;
|
|---|
| 128 | next.ll = new LatLon(ll.a, ll.b);
|
|---|
| 129 | next.en = new EastNorth(en.a, en.b);
|
|---|
| 130 | next.ll2 = new LatLon(ll2.a, ll2.b);
|
|---|
| 131 |
|
|---|
| 132 | result.add(next);
|
|---|
| 133 | }
|
|---|
| 134 | return result;
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | private static Pair<Double, Double> readLine(String expectedName, String input) {
|
|---|
| 139 | String[] fields = input.trim().split("[ ]+");
|
|---|
| 140 | if (fields.length != 3) throw new AssertionError();
|
|---|
| 141 | if (!fields[0].equals(expectedName)) throw new AssertionError();
|
|---|
| 142 | double a = Double.parseDouble(fields[1]);
|
|---|
| 143 | double b = Double.parseDouble(fields[2]);
|
|---|
| 144 | return Pair.create(a, b);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /**
|
|---|
| 148 | * Setup test.
|
|---|
| 149 | */
|
|---|
| 150 | @BeforeClass
|
|---|
| 151 | public static void setUp() {
|
|---|
| 152 | JOSMFixture.createUnitTestFixture().init();
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * Non-regression unit test.
|
|---|
| 157 | * @throws IOException if any I/O error occurs
|
|---|
| 158 | */
|
|---|
| 159 | @Test
|
|---|
| 160 | public void testNonRegression() throws IOException {
|
|---|
| 161 | List<TestData> allData = readData();
|
|---|
| 162 | Set<String> dataCodes = allData.stream().map(data -> data.code).collect(Collectors.toSet());
|
|---|
| 163 |
|
|---|
| 164 | StringBuilder fail = new StringBuilder();
|
|---|
| 165 |
|
|---|
| 166 | for (String code : Projections.getAllProjectionCodes()) {
|
|---|
| 167 | if (!dataCodes.contains(code)) {
|
|---|
| 168 | fail.append("Did not find projection "+code+" in test data!\n");
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | final boolean java9 = Utils.getJavaVersion() >= 9;
|
|---|
| 173 | for (TestData data : allData) {
|
|---|
| 174 | Projection proj = Projections.getProjectionByCode(data.code);
|
|---|
| 175 | if (proj == null) {
|
|---|
| 176 | fail.append("Projection "+data.code+" from test data was not found!\n");
|
|---|
| 177 | continue;
|
|---|
| 178 | }
|
|---|
| 179 | EastNorth en = proj.latlon2eastNorth(data.ll);
|
|---|
| 180 | LatLon ll2 = proj.eastNorth2latlon(data.en);
|
|---|
| 181 | if (!(java9 ? equalsJava9(en, data.en) : en.equals(data.en))) {
|
|---|
| 182 | String error = String.format("%s (%s): Projecting latlon(%s,%s):%n" +
|
|---|
| 183 | " expected: eastnorth(%s,%s),%n" +
|
|---|
| 184 | " but got: eastnorth(%s,%s)!%n",
|
|---|
| 185 | proj.toString(), data.code, data.ll.lat(), data.ll.lon(), data.en.east(), data.en.north(), en.east(), en.north());
|
|---|
| 186 | fail.append(error);
|
|---|
| 187 | }
|
|---|
| 188 | if (!(java9 ? equalsJava9(ll2, data.ll2) : ll2.equals(data.ll2))) {
|
|---|
| 189 | String error = String.format("%s (%s): Inverse projecting eastnorth(%s,%s):%n" +
|
|---|
| 190 | " expected: latlon(%s,%s),%n" +
|
|---|
| 191 | " but got: latlon(%s,%s)!%n",
|
|---|
| 192 | proj.toString(), data.code, data.en.east(), data.en.north(), data.ll2.lat(), data.ll2.lon(), ll2.lat(), ll2.lon());
|
|---|
| 193 | fail.append(error);
|
|---|
| 194 | }
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | if (fail.length() > 0) {
|
|---|
| 198 | System.err.println(fail.toString());
|
|---|
| 199 | throw new AssertionError(fail.toString());
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | private static boolean equalsDoubleMaxUlp(double d1, double d2) {
|
|---|
| 204 | // Due to error accumulation in projection computation, the difference can reach hundreds of ULPs
|
|---|
| 205 | // The worst error is 1168 ULP (followed by 816 ULP then 512 ULP) with:
|
|---|
| 206 | // NAD83 / Colorado South (EPSG:26955): Projecting latlon(32.24604527892822,-125.93039495227096):
|
|---|
| 207 | // expected: eastnorth(-1004398.8994415681,24167.8944844745),
|
|---|
| 208 | // but got: eastnorth(-1004398.8994415683,24167.894484478747)!
|
|---|
| 209 | return Math.abs(d1 - d2) <= 1200 * Math.ulp(d1);
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | private static boolean equalsJava9(EastNorth en1, EastNorth en2) {
|
|---|
| 213 | return equalsDoubleMaxUlp(en1.east(), en2.east()) &&
|
|---|
| 214 | equalsDoubleMaxUlp(en1.north(), en2.north());
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | private static boolean equalsJava9(LatLon ll1, LatLon ll2) {
|
|---|
| 218 | return equalsDoubleMaxUlp(ll1.lat(), ll2.lat()) &&
|
|---|
| 219 | equalsDoubleMaxUlp(ll1.lon(), ll2.lon());
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|