Changeset 9370 in josm


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

unit test & javadoc

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/projection/Projections.java

    r9240 r9370  
    4444
    4545/**
    46  * Class to handle projections
     46 * Class to manage projections.
    4747 *
     48 * Use this class to query available projection or register new projections
     49 * from a plugin.
    4850 */
    4951public final class Projections {
     
    157159    }
    158160
     161    /**
     162     * Convert from lat/lon to easting/northing using the current projection.
     163     *
     164     * @param ll the geographical point to convert (in WGS84 lat/lon)
     165     * @return the corresponding east/north coordinates
     166     */
    159167    public static EastNorth project(LatLon ll) {
    160168        if (ll == null) return null;
     
    162170    }
    163171
     172    /**
     173     * Convert from easting/norting to lat/lon using the current projection.
     174     *
     175     * @param en the geographical point to convert (in projected coordinates)
     176     * @return the corresponding lat/lon (WGS84)
     177     */
    164178    public static LatLon inverseProject(EastNorth en) {
    165179        if (en == null) return null;
     
    184198    }
    185199
     200    /**
     201     * Get a base projection by id.
     202     *
     203     * @param id the id, for example "lonlat" or "tmerc"
     204     * @return the corresponding base projection if the id is known, null otherwise
     205     */
    186206    public static Proj getBaseProjection(String id) {
    187207        ProjFactory fac = projs.get(id);
     
    190210    }
    191211
     212    /**
     213     * Get an ellipsoid by id.
     214     *
     215     * @param id the id, for example "bessel" or "WGS84"
     216     * @return the corresponding ellipsoid if the id is known, null otherwise
     217     */
    192218    public static Ellipsoid getEllipsoid(String id) {
    193219        return ellipsoids.get(id);
    194220    }
    195221
     222    /**
     223     * Get a geodetic datum by id.
     224     *
     225     * @param id the id, for example "potsdam" or "WGS84"
     226     * @return the corresponding datum if the id is known, null otherwise
     227     */
    196228    public static Datum getDatum(String id) {
    197229        return datums.get(id);
    198230    }
    199231
     232    /**
     233     * Get a NTV2 grid database by id.
     234     * @param id the id
     235     * @return the corresponding NTV2 grid if the id is known, null otherwise
     236     */
    200237    public static NTV2GridShiftFileWrapper getNTV2Grid(String id) {
    201238        return nadgrids.get(id);
     
    203240
    204241    /**
    205      * Get the projection definition string for the given id.
    206      * @param id the id
     242     * Get the projection definition string for the given code.
     243     * @param code the code
    207244     * @return the string that can be processed by #{link CustomProjection}.
    208      * Null, if the id isn't supported.
    209      */
    210     public static String getInit(String id) {
    211         ProjectionDefinition pd = inits.get(id.toUpperCase(Locale.ENGLISH));
     245     * Null, if the code isn't supported.
     246     */
     247    public static String getInit(String code) {
     248        ProjectionDefinition pd = inits.get(code.toUpperCase(Locale.ENGLISH));
    212249        if (pd == null) return null;
    213250        return pd.definition;
     
    260297    }
    261298
     299    /**
     300     * Get a projection by code.
     301     * @param code the code, e.g. "EPSG:2026"
     302     * @return the corresponding projection, if the code is known, null otherwise
     303     */
    262304    public static Projection getProjectionByCode(String code) {
    263305        Projection proj = projectionsByCode_cache.get(code);
     
    293335    }
    294336
     337    /**
     338     * Get a list of ids of all registered base projections.
     339     *
     340     * @return all registered base projection ids
     341     * @see #getBaseProjection(java.lang.String)
     342     */
     343    public static Collection<String> getAllBaseProjectionIds() {
     344        return projs.keySet();
     345    }
     346
    295347    private static String listKeys(Map<String, ?> map) {
    296348        List<String> keys = new ArrayList<>(map.keySet());
  • 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.