Changeset 35967 in osm for applications/editors/josm


Ignore:
Timestamp:
2022-04-28T21:26:26+02:00 (3 years ago)
Author:
taylor.smock
Message:

ElevationProfile: fix tests

Location:
applications/editors/josm/plugins/ElevationProfile
Files:
1 deleted
1 edited
2 copied

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/HgtReader.java

    r35964 r35967  
    2020import org.openstreetmap.josm.data.Preferences;
    2121import org.openstreetmap.josm.data.coor.ILatLon;
    22 import org.openstreetmap.josm.data.coor.LatLon;
    2322import org.openstreetmap.josm.io.Compression;
    2423import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    8382    }
    8483
    85     public static Bounds read(File file) throws FileNotFoundException, IOException {
     84    public static Bounds read(File file) throws IOException {
    8685        String location = file.getName();
    8786        for (String ext : COMPRESSION_EXT) {
     
    9190        // Overwrite the cache file (assume that is desired)
    9291        cache.put(location, sb);
    93         Pattern pattern = Pattern.compile("(N|S)([0-9]{2})(E|W)([0-9]{3})");
     92        Pattern pattern = Pattern.compile("([NS])(\\d{2})([EW])(\\d{3})");
    9493        Matcher matcher = pattern.matcher(location);
    9594        if (matcher.lookingAt()) {
    96             int lat = (matcher.group(1) == "S" ? -1 : 1) * Integer.parseInt(matcher.group(2));
    97             int lon = (matcher.group(3) == "W" ? -1 : 1) * Integer.parseInt(matcher.group(4));
     95            int lat = ("S".equals(matcher.group(1)) ? -1 : 1) * Integer.parseInt(matcher.group(2));
     96            int lon = ("W".equals(matcher.group(3)) ? -1 : 1) * Integer.parseInt(matcher.group(4));
    9897            return new Bounds(lat, lon, lat + 1, lon + 1);
    9998        }
     
    101100    }
    102101
    103     private static short[][] readHgtFile(String file) throws FileNotFoundException, IOException {
     102    private static short[][] readHgtFile(String file) throws IOException {
    104103        CheckParameterUtil.ensureParameterNotNull(file);
    105104
     
    135134     * @return the elevation value or <code>Double.NaN</code>, if no value is present
    136135     */
    137     public static double readElevation(LatLon coor) {
     136    public static double readElevation(ILatLon coor) {
    138137        String tag = getHgtFileName(coor);
    139138        return readElevation(coor, tag);
     
    205204        double lonDegrees = latLon.lon();
    206205
    207         float fraction = ((float) SRTM_EXTENT) / mapSize;
    208         int latitude = (int) Math.floor(Math.abs(latDegrees - (int) latDegrees) / fraction);
    209         int longitude = (int) Math.floor(Math.abs(lonDegrees - (int) lonDegrees) / fraction);
     206        float fraction = ((float) SRTM_EXTENT) / (mapSize - 1);
     207        int latitude = (int) Math.round(frac(latDegrees) / fraction);
     208        int longitude = (int) Math.round(frac(lonDegrees) / fraction);
    210209        if (latDegrees >= 0)
    211210        {
    212             latitude = mapSize - 1 - latitude;
     211            latitude = mapSize - latitude - 1;
    213212        }
    214213        if (lonDegrees < 0)
    215214        {
    216             longitude = mapSize - 1 - longitude;
     215            longitude = mapSize - longitude - 1;
    217216        }
    218217        return new int[] { latitude, longitude };
     
    243242        }
    244243
    245         return latPref + lat + lonPref + lon + HGT_EXT;
     244        return String.format("%s%2d%s%03d" + HGT_EXT, latPref, lat, lonPref, lon);
    246245    }
    247246
  • applications/editors/josm/plugins/ElevationProfile/test/unit/org/openstreetmap/josm/plugins/elevation/EleVertexTest.java

    r35966 r35967  
    1 // License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.plugins.elevation.tests;
     1package org.openstreetmap.josm.plugins.elevation;// License: GPL. For details, see LICENSE file.
    32
    4 import static org.junit.Assert.assertEquals;
    5 import static org.junit.Assert.assertNotNull;
    6 import static org.junit.Assert.assertTrue;
     3import static org.junit.jupiter.api.Assertions.assertEquals;
     4import static org.junit.jupiter.api.Assertions.assertNotNull;
     5import static org.junit.jupiter.api.Assertions.assertTrue;
    76
    87import java.awt.Color;
    98import java.util.List;
    109
    11 import org.junit.Rule;
    12 import org.junit.Test;
     10import org.junit.jupiter.api.Test;
    1311import org.openstreetmap.josm.data.coor.LatLon;
    14 import org.openstreetmap.josm.plugins.elevation.ColorMap;
    1512import org.openstreetmap.josm.plugins.elevation.grid.EleCoordinate;
    1613import org.openstreetmap.josm.plugins.elevation.grid.EleVertex;
    17 import org.openstreetmap.josm.testutils.JOSMTestRules;
     14import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
     15import org.openstreetmap.josm.tools.Logging;
    1816
    19 public class EleVertexTest {
     17@BasicPreferences
     18class EleVertexTest {
    2019
    2120    private static final double EPS = 1e-10;
    2221
    23     @Rule
    24     public JOSMTestRules rules = new JOSMTestRules().preferences();
    25 
    2622    @Test
    27     public void testDivide() {
     23    void testDivide() {
    2824        EleCoordinate p1 = new EleCoordinate(30.0, 30.0, 100.0);
    2925        EleCoordinate p2 = new EleCoordinate(35.0, 30.0, 120.0);
     
    5248
    5349    @Test
    54     public void testSimpleRecurse() {
     50    void testSimpleRecurse() {
    5551        EleCoordinate c1 = new EleCoordinate(new LatLon(50.8328, 8.1337), 300);
    5652        EleCoordinate c2 = new EleCoordinate(new LatLon(50.8328, 7.9217), 200);
     
    5955
    6056        EleVertex v1 = new EleVertex(c1, c2, c3);
    61         System.out.println("Start recurse");
     57        Logging.debug("Start recurse");
    6258        recurse(v1, 0);
    6359    }
     
    6561    private void recurse(EleVertex v, int depth) {
    6662        if (!v.isFinished() && depth < 100) {
    67             System.out.println("\tDivide: " + v);
     63            Logging.trace("\tDivide: " + v);
    6864            List<EleVertex> list = v.divide();
    6965            assertNotNull(list);
     
    7268            for (EleVertex eleVertex : list) {
    7369                //System.out.println("\t\tRecurse: " + eleVertex);
    74                 assertTrue("Area is larger " + v.getArea() + " > " + eleVertex.getArea(), eleVertex.getArea() < v.getArea());
     70                assertTrue(eleVertex.getArea() < v.getArea(), "Area is larger " + v.getArea() + " > " + eleVertex.getArea());
    7571                recurse(eleVertex, depth + 1);
    7672            }
    7773        } else {
    78             System.out.println("Finished: " + depth);
     74            Logging.debug("Finished: " + depth);
    7975        }
    8076    }
  • applications/editors/josm/plugins/ElevationProfile/test/unit/org/openstreetmap/josm/plugins/elevation/HgtReaderTest.java

    r35966 r35967  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.plugins.elevation.tests;
     2package org.openstreetmap.josm.plugins.elevation;
    33
    4 import static org.junit.Assert.assertEquals;
    5 import static org.junit.Assert.assertFalse;
     4import static org.junit.jupiter.api.Assertions.assertEquals;
     5import static org.junit.jupiter.api.Assertions.assertFalse;
    66
    77import java.io.IOException;
     
    1111import java.nio.file.Path;
    1212import java.nio.file.Paths;
     13import java.util.stream.Stream;
    1314
    14 import org.junit.Before;
    15 import org.junit.Rule;
    16 import org.junit.Test;
     15import org.junit.jupiter.api.BeforeEach;
     16import org.junit.jupiter.params.ParameterizedTest;
     17import org.junit.jupiter.params.provider.Arguments;
     18import org.junit.jupiter.params.provider.MethodSource;
    1719import org.openstreetmap.josm.TestUtils;
    1820import org.openstreetmap.josm.data.coor.LatLon;
    19 import org.openstreetmap.josm.plugins.elevation.HgtReader;
    2021import org.openstreetmap.josm.spi.preferences.Config;
    21 import org.openstreetmap.josm.testutils.JOSMTestRules;
     22import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
     23import org.openstreetmap.josm.tools.Logging;
    2224
    23 public class HgtReaderTest {
    24 
    25     @Rule
    26     public JOSMTestRules rules = new JOSMTestRules().preferences();
     25@BasicPreferences
     26class HgtReaderTest {
    2727
    2828    /**
     
    3030     * @throws IOException if SRTM files cannot be installed
    3131     */
    32     @Before
    33     public void setUp() throws IOException {
     32    @BeforeEach
     33    void setUp() throws IOException {
    3434        // Install SRTM files to plugin directory
    3535        try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(TestUtils.getTestDataRoot()), "*.hgt")) {
     
    5050        }
    5151    }
    52 
    53     @Test
    54     public void testGetElevationFromHgt() {
    55         // Staufenberg, Hessen
    56         testHgtData(50.6607106, 8.7337029, "N50E008.hgt", 199);
    57         // Ulrichstein, Hessen
    58         testHgtData(50.5767627, 9.1938483, "N50E009.hgt", 560);
    59         // Fujijama
    60         //testHgtData(35.360555, 138.727777, "N35E138.hgt", 3741);
     52    static Stream<Arguments> testHgtData() {
     53        return Stream.of(
     54            // Staufenberg, Hessen
     55            Arguments.of(50.6607106, 8.7337029, "N50E008.hgt", 199),
     56            // Ulrichstein, Hessen
     57            Arguments.of(50.5767627, 9.1938483, "N50E009.hgt", 560),
     58            // Fujijama
     59            //testHgtData(35.360555, 138.727777, "N35E138.hgt", 3741),
     60            // Some random location in the middle of a file
     61            Arguments.of(50.5, 8.5, "N50E008.hgt", 274),
     62            Arguments.of(50.0000001, 8.999999, "N50E008.hgt", 132)
     63        );
    6164    }
    6265
    63     private void testHgtData(final double lat, final double lon,
     66    @ParameterizedTest
     67    @MethodSource
     68    void testHgtData(final double lat, final double lon,
    6469            final String expTag, final int expHeight) {
    6570        LatLon l = new LatLon(lat, lon);
    66         HgtReader hr = new HgtReader();
    67         String text = hr.getHgtFileName(l);
     71        String text = HgtReader.getHgtFileName(l);
    6872
    6973        assertEquals(expTag, text);
    7074
    71         double d = hr.getElevationFromHgt(l);
    72         System.out.println(d);
    73         assertFalse("Data missing or void for coor " + l, Double.isNaN(d));
     75        double d = HgtReader.getElevationFromHgt(l);
     76        Logging.trace(Double.toString(d));
     77        assertFalse(Double.isNaN(d), "Data missing or void for coor " + l);
    7478
    75         assertEquals((int) d, expHeight);
     79        assertEquals(expHeight, (int) d);
    7680    }
    7781}
Note: See TracChangeset for help on using the changeset viewer.