Changeset 9370 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/projection/Projections.java
r9240 r9370 44 44 45 45 /** 46 * Class to handle projections46 * Class to manage projections. 47 47 * 48 * Use this class to query available projection or register new projections 49 * from a plugin. 48 50 */ 49 51 public final class Projections { … … 157 159 } 158 160 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 */ 159 167 public static EastNorth project(LatLon ll) { 160 168 if (ll == null) return null; … … 162 170 } 163 171 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 */ 164 178 public static LatLon inverseProject(EastNorth en) { 165 179 if (en == null) return null; … … 184 198 } 185 199 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 */ 186 206 public static Proj getBaseProjection(String id) { 187 207 ProjFactory fac = projs.get(id); … … 190 210 } 191 211 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 */ 192 218 public static Ellipsoid getEllipsoid(String id) { 193 219 return ellipsoids.get(id); 194 220 } 195 221 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 */ 196 228 public static Datum getDatum(String id) { 197 229 return datums.get(id); 198 230 } 199 231 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 */ 200 237 public static NTV2GridShiftFileWrapper getNTV2Grid(String id) { 201 238 return nadgrids.get(id); … … 203 240 204 241 /** 205 * Get the projection definition string for the given id.206 * @param id the id242 * Get the projection definition string for the given code. 243 * @param code the code 207 244 * @return the string that can be processed by #{link CustomProjection}. 208 * Null, if the idisn'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)); 212 249 if (pd == null) return null; 213 250 return pd.definition; … … 260 297 } 261 298 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 */ 262 304 public static Projection getProjectionByCode(String code) { 263 305 Projection proj = projectionsByCode_cache.get(code); … … 293 335 } 294 336 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 295 347 private static String listKeys(Map<String, ?> map) { 296 348 List<String> keys = new ArrayList<>(map.keySet()); -
trunk/test/unit/org/openstreetmap/josm/data/projection/ProjectionTest.java
r9189 r9370 3 3 4 4 import java.util.Arrays; 5 import java.util.Collection; 6 import java.util.HashSet; 5 7 import java.util.Random; 6 8 … … 19 21 20 22 @Test 21 public void proj () {23 public void projections() { 22 24 error = false; 23 25 text = ""; 24 26 25 testProj (Projections.getProjectionByCode("EPSG:4326")); // WGS 8426 testProj (Projections.getProjectionByCode("EPSG:3857")); // Mercator27 testProj (Projections.getProjectionByCode("EPSG:3301")); // Lambert EST27 testProjection(Projections.getProjectionByCode("EPSG:4326")); // WGS 84 28 testProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator 29 testProjection(Projections.getProjectionByCode("EPSG:3301")); // Lambert EST 28 30 29 31 for (int i = 0; i <= 3; ++i) { 30 testProj (Projections.getProjectionByCode("EPSG:"+Integer.toString(27561+i))); // Lambert 4 Zones France32 testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(27561+i))); // Lambert 4 Zones France 31 33 } 32 34 33 35 for (int i = 0; i <= 4; ++i) { 34 testProj (Projections.getProjectionByCode("EPSG:"+Integer.toString(2176+i))); // PUWG Poland36 testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(2176+i))); // PUWG Poland 35 37 } 36 38 37 testProj (Projections.getProjectionByCode("EPSG:21781")); // Swiss grid39 testProjection(Projections.getProjectionByCode("EPSG:21781")); // Swiss grid 38 40 39 41 for (int i = 0; i <= 60; ++i) { 40 testProj (Projections.getProjectionByCode("EPSG:"+Integer.toString(32601+i))); // UTM North41 testProj (Projections.getProjectionByCode("EPSG:"+Integer.toString(32701+i))); // UTM South42 testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(32601+i))); // UTM North 43 testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(32701+i))); // UTM South 42 44 } 43 45 44 46 for (String c : Arrays.asList("2969", "2970", "2972", "2973")) { 45 testProj (Projections.getProjectionByCode("EPSG:"+c)); // UTM France DOM47 testProjection(Projections.getProjectionByCode("EPSG:"+c)); // UTM France DOM 46 48 } 47 49 48 50 for (int i = 0; i <= 8; ++i) { 49 testProj (Projections.getProjectionByCode("EPSG:"+Integer.toString(3942+i))); // Lambert CC9 Zones France51 testProjection(Projections.getProjectionByCode("EPSG:"+Integer.toString(3942+i))); // Lambert CC9 Zones France 50 52 } 51 53 … … 56 58 } 57 59 58 private void testProj (Projection p) {60 private void testProjection(Projection p) { 59 61 if (p != null) { 60 62 double maxErrLat = 0, maxErrLon = 0; … … 64 66 for (int num = 0; num < 1000; ++num) { 65 67 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; 70 70 71 71 for (int i = 0; i < 10; ++i) { … … 73 73 ll = p.eastNorth2latlon(en); 74 74 } 75 maxErrLat = Math.max(maxErrLat, Math.abs(l at- ll.lat()));76 maxErrLon = Math.max(maxErrLon, Math.abs(l on- ll.lon()));75 maxErrLat = Math.max(maxErrLat, Math.abs(ll0.lat() - ll.lat())); 76 maxErrLon = Math.max(maxErrLon, Math.abs(ll0.lon() - ll.lon())); 77 77 } 78 78 … … 85 85 } 86 86 } 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 } 87 149 }
Note:
See TracChangeset
for help on using the changeset viewer.