Ignore:
Timestamp:
2016-01-09T22:33:40+01:00 (8 years ago)
Author:
bastiK
Message:

unit test & javadoc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/projection/ProjectionTest.java

    r9189 r9370  
    33
    44import java.util.Arrays;
     5import java.util.Collection;
     6import java.util.HashSet;
    57import java.util.Random;
    68
     
    1921
    2022    @Test
    21     public void proj() {
     23    public void projections() {
    2224        error = false;
    2325        text = "";
    2426
    25         testProj(Projections.getProjectionByCode("EPSG:4326")); // WGS 84
    26         testProj(Projections.getProjectionByCode("EPSG:3857")); // Mercator
    27         testProj(Projections.getProjectionByCode("EPSG:3301")); // Lambert EST
     27        testProjection(Projections.getProjectionByCode("EPSG:4326")); // WGS 84
     28        testProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
     29        testProjection(Projections.getProjectionByCode("EPSG:3301")); // Lambert EST
    2830
    2931        for (int i = 0; i <= 3; ++i) {
    30             testProj(Projections.getProjectionByCode("EPSG:"+Integer.toString(27561+i))); // Lambert 4 Zones France
     32            testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(27561+i))); // Lambert 4 Zones France
    3133        }
    3234
    3335        for (int i = 0; i <= 4; ++i) {
    34             testProj(Projections.getProjectionByCode("EPSG:"+Integer.toString(2176+i))); // PUWG Poland
     36            testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(2176+i))); // PUWG Poland
    3537        }
    3638
    37         testProj(Projections.getProjectionByCode("EPSG:21781")); // Swiss grid
     39        testProjection(Projections.getProjectionByCode("EPSG:21781")); // Swiss grid
    3840
    3941        for (int i = 0; i <= 60; ++i) {
    40             testProj(Projections.getProjectionByCode("EPSG:"+Integer.toString(32601+i))); // UTM North
    41             testProj(Projections.getProjectionByCode("EPSG:"+Integer.toString(32701+i))); // UTM South
     42            testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(32601+i))); // UTM North
     43            testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(32701+i))); // UTM South
    4244        }
    4345
    4446        for (String c : Arrays.asList("2969", "2970", "2972", "2973")) {
    45             testProj(Projections.getProjectionByCode("EPSG:"+c)); // UTM France DOM
     47            testProjection(Projections.getProjectionByCode("EPSG:"+c)); // UTM France DOM
    4648        }
    4749
    4850        for (int i = 0; i <= 8; ++i) {
    49             testProj(Projections.getProjectionByCode("EPSG:"+Integer.toString(3942+i))); // Lambert CC9 Zones France
     51            testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(3942+i))); // Lambert CC9 Zones France
    5052        }
    5153
     
    5658    }
    5759
    58     private void testProj(Projection p) {
     60    private void testProjection(Projection p) {
    5961        if (p != null) {
    6062            double maxErrLat = 0, maxErrLon = 0;
     
    6466            for (int num = 0; num < 1000; ++num) {
    6567
    66                 double lat = rand.nextDouble() * (b.getMax().lat() - b.getMin().lat()) + b.getMin().lat();
    67                 double lon = rand.nextDouble() * (b.getMax().lon() - b.getMin().lon()) + b.getMin().lon();
    68 
    69                 LatLon ll = new LatLon(lat, lon);
     68                LatLon ll0 = random(b);
     69                LatLon ll = ll0;
    7070
    7171                for (int i = 0; i < 10; ++i) {
     
    7373                    ll = p.eastNorth2latlon(en);
    7474                }
    75                 maxErrLat = Math.max(maxErrLat, Math.abs(lat - ll.lat()));
    76                 maxErrLon = Math.max(maxErrLon, Math.abs(lon - ll.lon()));
     75                maxErrLat = Math.max(maxErrLat, Math.abs(ll0.lat() - ll.lat()));
     76                maxErrLon = Math.max(maxErrLon, Math.abs(ll0.lon() - ll.lon()));
    7777            }
    7878
     
    8585        }
    8686    }
     87
     88    private LatLon random(Bounds b) {
     89        for (int i=0; i<20; i++) {
     90            double lat = rand.nextDouble() * (b.getMax().lat() - b.getMin().lat()) + b.getMin().lat();
     91            double lon = rand.nextDouble() * (b.getMax().lon() - b.getMin().lon()) + b.getMin().lon();
     92            LatLon result = new LatLon(lat, lon);
     93            if (result.isValid()) return result;
     94        }
     95        throw new RuntimeException();
     96    }
     97
     98    boolean error2;
     99    String text2;
     100    Collection<String> projIds;
     101
     102    @Test
     103    public void projs() {
     104        error2 = false;
     105        text2 = "";
     106
     107        projIds = new HashSet<>(Projections.getAllBaseProjectionIds());
     108
     109        final double EPS = 1e-6;
     110        testProj("lonlat", EPS, "");
     111        testProj("josm:smerc", EPS, "");
     112        testProj("lcc", EPS, "+lat_0=34");
     113        testProj("lcc", EPS, "+lat_1=87 +lat_2=83.6 +lat_0=85.43");
     114        testProj("somerc", EPS, "+lat_0=47");
     115        testProj("tmerc", 2e-3, "");
     116        testProj("sterea", EPS, "+lat_0=52");
     117
     118        if (error2) {
     119            System.err.println(text2);
     120            Assert.fail();
     121        }
     122        Assert.assertTrue("missing test: "+projIds, projIds.isEmpty());
     123    }
     124
     125    private void testProj(String id, double eps, String prefAdd) {
     126        final int NUM_IT = 1000;
     127        projIds.remove(id);
     128        String pref = String.format("+proj=%s +ellps=WGS84 +nadgrids=null "+prefAdd, id);
     129        CustomProjection p = new CustomProjection();
     130        try {
     131            p.update(pref);
     132        } catch (ProjectionConfigurationException ex) {
     133            throw new RuntimeException(ex);
     134        }
     135        Bounds b = p.getWorldBoundsLatLon();
     136        for (int i=0; i<NUM_IT; i++) {
     137            LatLon ll1 = random(b);
     138            EastNorth en = p.latlon2eastNorth(ll1);
     139            LatLon ll2 = p.eastNorth2latlon(en);
     140            Assert.assertTrue(p.toCode() + " at " + ll1 + " is " + ll2, ll2.isValid());
     141            double dist = ll1.greatCircleDistance(ll2);
     142            if (dist > eps) {
     143                error2 = true;
     144                text2 += id + ": dist " + dist + " at " + ll1 + "\n";
     145                return;
     146            }
     147        }
     148    }
    87149}
Note: See TracChangeset for help on using the changeset viewer.