1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection;
|
---|
3 |
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.Random;
|
---|
6 |
|
---|
7 | import org.junit.Assert;
|
---|
8 | import org.junit.Test;
|
---|
9 | import org.openstreetmap.josm.data.Bounds;
|
---|
10 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
12 |
|
---|
13 | public class ProjectionTest {
|
---|
14 |
|
---|
15 | private static Random rand = new Random(System.currentTimeMillis());
|
---|
16 |
|
---|
17 | boolean error;
|
---|
18 | String text;
|
---|
19 |
|
---|
20 | @Test
|
---|
21 | public void proj() {
|
---|
22 | error = false;
|
---|
23 | text = "";
|
---|
24 |
|
---|
25 | testProj(Projections.getProjectionByCode("EPSG:4326")); // WGS 84
|
---|
26 | testProj(Projections.getProjectionByCode("EPSG:3857")); // Mercator
|
---|
27 | if (!"yes".equals(System.getProperty("suppressPermanentFailure"))) {
|
---|
28 | testProj(Projections.getProjectionByCode("EPSG:3301")); // Lambert EST
|
---|
29 | }
|
---|
30 |
|
---|
31 | for (int i = 0; i <= 3; ++i) {
|
---|
32 | testProj(Projections.getProjectionByCode("EPSG:"+Integer.toString(27561+i))); // Lambert 4 Zones France
|
---|
33 | }
|
---|
34 |
|
---|
35 | for (int i = 0; i <= 4; ++i) {
|
---|
36 | testProj(Projections.getProjectionByCode("EPSG:"+Integer.toString(2176+i))); // PUWG Poland
|
---|
37 | }
|
---|
38 |
|
---|
39 | testProj(Projections.getProjectionByCode("EPSG:21781")); // Swiss grid
|
---|
40 |
|
---|
41 | for (int i = 0; i <= 60; ++i) {
|
---|
42 | testProj(Projections.getProjectionByCode("EPSG:"+Integer.toString(32601+i))); // UTM North
|
---|
43 | testProj(Projections.getProjectionByCode("EPSG:"+Integer.toString(32701+i))); // UTM South
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (!"yes".equals(System.getProperty("suppressPermanentFailure"))) {
|
---|
47 | for (String c : Arrays.asList("2969", "2970", "2972", "2973")) {
|
---|
48 | testProj(Projections.getProjectionByCode("EPSG:"+c)); // UTM France DOM
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | for (int i = 0; i <= 8; ++i) {
|
---|
53 | testProj(Projections.getProjectionByCode("EPSG:"+Integer.toString(3942+i))); // Lambert CC9 Zones France
|
---|
54 | }
|
---|
55 |
|
---|
56 | if (error) {
|
---|
57 | System.err.println(text);
|
---|
58 | Assert.fail();
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | private void testProj(Projection p) {
|
---|
63 | if (p != null) {
|
---|
64 | double maxErrLat = 0, maxErrLon = 0;
|
---|
65 | Bounds b = p.getWorldBoundsLatLon();
|
---|
66 |
|
---|
67 | text += String.format("*** %s %s%n", p.toString(), p.toCode());
|
---|
68 | for (int num = 0; num < 1000; ++num) {
|
---|
69 |
|
---|
70 | double lat = rand.nextDouble() * (b.getMax().lat() - b.getMin().lat()) + b.getMin().lat();
|
---|
71 | double lon = rand.nextDouble() * (b.getMax().lon() - b.getMin().lon()) + b.getMin().lon();
|
---|
72 |
|
---|
73 | LatLon ll = new LatLon(lat, lon);
|
---|
74 |
|
---|
75 | for (int i = 0; i < 10; ++i) {
|
---|
76 | EastNorth en = p.latlon2eastNorth(ll);
|
---|
77 | ll = p.eastNorth2latlon(en);
|
---|
78 | }
|
---|
79 | maxErrLat = Math.max(maxErrLat, Math.abs(lat - ll.lat()));
|
---|
80 | maxErrLon = Math.max(maxErrLon, Math.abs(lon - ll.lon()));
|
---|
81 | }
|
---|
82 |
|
---|
83 | String mark = "";
|
---|
84 | if (maxErrLat + maxErrLon > 1e-5) {
|
---|
85 | mark = "--FAILED-- ";
|
---|
86 | error = true;
|
---|
87 | }
|
---|
88 | text += String.format("%s errorLat: %s errorLon: %s%n", mark, maxErrLat, maxErrLon);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|