Changeset 35967 in osm for applications/editors/josm
- Timestamp:
- 2022-04-28T21:26:26+02:00 (3 years ago)
- 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 20 20 import org.openstreetmap.josm.data.Preferences; 21 21 import org.openstreetmap.josm.data.coor.ILatLon; 22 import org.openstreetmap.josm.data.coor.LatLon;23 22 import org.openstreetmap.josm.io.Compression; 24 23 import org.openstreetmap.josm.tools.CheckParameterUtil; … … 83 82 } 84 83 85 public static Bounds read(File file) throws FileNotFoundException,IOException {84 public static Bounds read(File file) throws IOException { 86 85 String location = file.getName(); 87 86 for (String ext : COMPRESSION_EXT) { … … 91 90 // Overwrite the cache file (assume that is desired) 92 91 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})"); 94 93 Matcher matcher = pattern.matcher(location); 95 94 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)); 98 97 return new Bounds(lat, lon, lat + 1, lon + 1); 99 98 } … … 101 100 } 102 101 103 private static short[][] readHgtFile(String file) throws FileNotFoundException,IOException {102 private static short[][] readHgtFile(String file) throws IOException { 104 103 CheckParameterUtil.ensureParameterNotNull(file); 105 104 … … 135 134 * @return the elevation value or <code>Double.NaN</code>, if no value is present 136 135 */ 137 public static double readElevation( LatLon coor) {136 public static double readElevation(ILatLon coor) { 138 137 String tag = getHgtFileName(coor); 139 138 return readElevation(coor, tag); … … 205 204 double lonDegrees = latLon.lon(); 206 205 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); 210 209 if (latDegrees >= 0) 211 210 { 212 latitude = mapSize - 1 - latitude;211 latitude = mapSize - latitude - 1; 213 212 } 214 213 if (lonDegrees < 0) 215 214 { 216 longitude = mapSize - 1 - longitude;215 longitude = mapSize - longitude - 1; 217 216 } 218 217 return new int[] { latitude, longitude }; … … 243 242 } 244 243 245 return latPref + lat + lonPref + lon + HGT_EXT;244 return String.format("%s%2d%s%03d" + HGT_EXT, latPref, lat, lonPref, lon); 246 245 } 247 246 -
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; 1 package org.openstreetmap.josm.plugins.elevation;// License: GPL. For details, see LICENSE file. 3 2 4 import static org.junit. Assert.assertEquals;5 import static org.junit. Assert.assertNotNull;6 import static org.junit. Assert.assertTrue;3 import static org.junit.jupiter.api.Assertions.assertEquals; 4 import static org.junit.jupiter.api.Assertions.assertNotNull; 5 import static org.junit.jupiter.api.Assertions.assertTrue; 7 6 8 7 import java.awt.Color; 9 8 import java.util.List; 10 9 11 import org.junit.Rule; 12 import org.junit.Test; 10 import org.junit.jupiter.api.Test; 13 11 import org.openstreetmap.josm.data.coor.LatLon; 14 import org.openstreetmap.josm.plugins.elevation.ColorMap;15 12 import org.openstreetmap.josm.plugins.elevation.grid.EleCoordinate; 16 13 import org.openstreetmap.josm.plugins.elevation.grid.EleVertex; 17 import org.openstreetmap.josm.testutils.JOSMTestRules; 14 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 15 import org.openstreetmap.josm.tools.Logging; 18 16 19 public class EleVertexTest { 17 @BasicPreferences 18 class EleVertexTest { 20 19 21 20 private static final double EPS = 1e-10; 22 21 23 @Rule24 public JOSMTestRules rules = new JOSMTestRules().preferences();25 26 22 @Test 27 publicvoid testDivide() {23 void testDivide() { 28 24 EleCoordinate p1 = new EleCoordinate(30.0, 30.0, 100.0); 29 25 EleCoordinate p2 = new EleCoordinate(35.0, 30.0, 120.0); … … 52 48 53 49 @Test 54 publicvoid testSimpleRecurse() {50 void testSimpleRecurse() { 55 51 EleCoordinate c1 = new EleCoordinate(new LatLon(50.8328, 8.1337), 300); 56 52 EleCoordinate c2 = new EleCoordinate(new LatLon(50.8328, 7.9217), 200); … … 59 55 60 56 EleVertex v1 = new EleVertex(c1, c2, c3); 61 System.out.println("Start recurse");57 Logging.debug("Start recurse"); 62 58 recurse(v1, 0); 63 59 } … … 65 61 private void recurse(EleVertex v, int depth) { 66 62 if (!v.isFinished() && depth < 100) { 67 System.out.println("\tDivide: " + v);63 Logging.trace("\tDivide: " + v); 68 64 List<EleVertex> list = v.divide(); 69 65 assertNotNull(list); … … 72 68 for (EleVertex eleVertex : list) { 73 69 //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()); 75 71 recurse(eleVertex, depth + 1); 76 72 } 77 73 } else { 78 System.out.println("Finished: " + depth);74 Logging.debug("Finished: " + depth); 79 75 } 80 76 } -
applications/editors/josm/plugins/ElevationProfile/test/unit/org/openstreetmap/josm/plugins/elevation/HgtReaderTest.java
r35966 r35967 1 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.elevation .tests;2 package org.openstreetmap.josm.plugins.elevation; 3 3 4 import static org.junit. Assert.assertEquals;5 import static org.junit. Assert.assertFalse;4 import static org.junit.jupiter.api.Assertions.assertEquals; 5 import static org.junit.jupiter.api.Assertions.assertFalse; 6 6 7 7 import java.io.IOException; … … 11 11 import java.nio.file.Path; 12 12 import java.nio.file.Paths; 13 import java.util.stream.Stream; 13 14 14 import org.junit.Before; 15 import org.junit.Rule; 16 import org.junit.Test; 15 import org.junit.jupiter.api.BeforeEach; 16 import org.junit.jupiter.params.ParameterizedTest; 17 import org.junit.jupiter.params.provider.Arguments; 18 import org.junit.jupiter.params.provider.MethodSource; 17 19 import org.openstreetmap.josm.TestUtils; 18 20 import org.openstreetmap.josm.data.coor.LatLon; 19 import org.openstreetmap.josm.plugins.elevation.HgtReader;20 21 import org.openstreetmap.josm.spi.preferences.Config; 21 import org.openstreetmap.josm.testutils.JOSMTestRules; 22 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 23 import org.openstreetmap.josm.tools.Logging; 22 24 23 public class HgtReaderTest { 24 25 @Rule 26 public JOSMTestRules rules = new JOSMTestRules().preferences(); 25 @BasicPreferences 26 class HgtReaderTest { 27 27 28 28 /** … … 30 30 * @throws IOException if SRTM files cannot be installed 31 31 */ 32 @Before 33 publicvoid setUp() throws IOException {32 @BeforeEach 33 void setUp() throws IOException { 34 34 // Install SRTM files to plugin directory 35 35 try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(TestUtils.getTestDataRoot()), "*.hgt")) { … … 50 50 } 51 51 } 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 ); 61 64 } 62 65 63 private void testHgtData(final double lat, final double lon, 66 @ParameterizedTest 67 @MethodSource 68 void testHgtData(final double lat, final double lon, 64 69 final String expTag, final int expHeight) { 65 70 LatLon l = new LatLon(lat, lon); 66 HgtReader hr = new HgtReader(); 67 String text = hr.getHgtFileName(l); 71 String text = HgtReader.getHgtFileName(l); 68 72 69 73 assertEquals(expTag, text); 70 74 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); 74 78 75 assertEquals( (int) d, expHeight);79 assertEquals(expHeight, (int) d); 76 80 } 77 81 }
Note:
See TracChangeset
for help on using the changeset viewer.