Changeset 8366 in josm
- Timestamp:
- 2015-05-16T15:44:07+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/LatLonDialog.java
r8285 r8366 56 56 private static final char W_TR = LatLon.WEST.charAt(0); 57 57 58 private static final Pattern p= Pattern.compile(58 private static final Pattern P = Pattern.compile( 59 59 "([+|-]?\\d+[.,]\\d+)|" // (1) 60 60 + "([+|-]?\\d+)|" // (2) … … 66 66 + "\\s+|" 67 67 + "(.+)"); 68 69 private static final Pattern P_XML = Pattern.compile( 70 "lat=[\"']([+|-]?\\d+[.,]\\d+)[\"']\\s+lon=[\"']([+|-]?\\d+[.,]\\d+)[\"']"); 68 71 69 72 protected JPanel buildLatLon() { … … 336 339 } 337 340 @Override 338 public void focusLost(FocusEvent e) {} 341 public void focusLost(FocusEvent e) { 342 // Not used 343 } 339 344 } 340 345 341 346 public static LatLon parseLatLon(final String coord) { 342 final Matcher m = p.matcher(coord); 343 344 final StringBuilder sb = new StringBuilder(); 345 final List<Object> list = new ArrayList<>(); 346 347 while (m.find()) { 348 if (m.group(1) != null) { 349 sb.append('R'); // floating point number 350 list.add(Double.parseDouble(m.group(1).replace(',', '.'))); 351 } else if (m.group(2) != null) { 352 sb.append('Z'); // integer number 353 list.add(Double.parseDouble(m.group(2))); 354 } else if (m.group(3) != null) { 355 sb.append('o'); // degree sign 356 } else if (m.group(4) != null) { 357 sb.append('\''); // seconds sign 358 } else if (m.group(5) != null) { 359 sb.append('"'); // minutes sign 360 } else if (m.group(6) != null) { 361 sb.append(','); // separator 362 } else if (m.group(7) != null) { 363 sb.append("x"); // cardinal direction 364 String c = m.group(7).toUpperCase(); 365 if ("N".equals(c) || "S".equals(c) || "E".equals(c) || "W".equals(c)) { 366 list.add(c); 367 } else { 368 list.add(c.replace(N_TR, 'N').replace(S_TR, 'S') 369 .replace(E_TR, 'E').replace(W_TR, 'W')); 347 final LatLonHolder latLon = new LatLonHolder(); 348 final Matcher mXml = P_XML.matcher(coord); 349 if (mXml.matches()) { 350 setLatLonObj(latLon, 351 Double.parseDouble(mXml.group(1).replace(',', '.')), ZERO, ZERO, "N", 352 Double.parseDouble(mXml.group(2).replace(',', '.')), ZERO, ZERO, "E"); 353 } else { 354 final Matcher m = P.matcher(coord); 355 356 final StringBuilder sb = new StringBuilder(); 357 final List<Object> list = new ArrayList<>(); 358 359 while (m.find()) { 360 if (m.group(1) != null) { 361 sb.append('R'); // floating point number 362 list.add(Double.parseDouble(m.group(1).replace(',', '.'))); 363 } else if (m.group(2) != null) { 364 sb.append('Z'); // integer number 365 list.add(Double.parseDouble(m.group(2))); 366 } else if (m.group(3) != null) { 367 sb.append('o'); // degree sign 368 } else if (m.group(4) != null) { 369 sb.append('\''); // seconds sign 370 } else if (m.group(5) != null) { 371 sb.append('"'); // minutes sign 372 } else if (m.group(6) != null) { 373 sb.append(','); // separator 374 } else if (m.group(7) != null) { 375 sb.append("x"); // cardinal direction 376 String c = m.group(7).toUpperCase(); 377 if ("N".equals(c) || "S".equals(c) || "E".equals(c) || "W".equals(c)) { 378 list.add(c); 379 } else { 380 list.add(c.replace(N_TR, 'N').replace(S_TR, 'S') 381 .replace(E_TR, 'E').replace(W_TR, 'W')); 382 } 383 } else if (m.group(8) != null) { 384 throw new IllegalArgumentException("invalid token: " + m.group(8)); 370 385 } 371 } else if (m.group(8) != null) {372 throw new IllegalArgumentException("invalid token: " + m.group(8));373 386 } 374 } 375 376 final String pattern = sb.toString(); 377 378 final Object[] params = list.toArray(); 379 final LatLonHolder latLon = new LatLonHolder(); 380 381 if (pattern.matches("Ro?,?Ro?")) { 382 setLatLonObj(latLon, 383 params[0], ZERO, ZERO, "N", 384 params[1], ZERO, ZERO, "E"); 385 } else if (pattern.matches("xRo?,?xRo?")) { 386 setLatLonObj(latLon, 387 params[1], ZERO, ZERO, params[0], 388 params[3], ZERO, ZERO, params[2]); 389 } else if (pattern.matches("Ro?x,?Ro?x")) { 390 setLatLonObj(latLon, 391 params[0], ZERO, ZERO, params[1], 392 params[2], ZERO, ZERO, params[3]); 393 } else if (pattern.matches("Zo[RZ]'?,?Zo[RZ]'?|Z[RZ],?Z[RZ]")) { 394 setLatLonObj(latLon, 395 params[0], params[1], ZERO, "N", 396 params[2], params[3], ZERO, "E"); 397 } else if (pattern.matches("xZo[RZ]'?,?xZo[RZ]'?|xZo?[RZ],?xZo?[RZ]")) { 398 setLatLonObj(latLon, 399 params[1], params[2], ZERO, params[0], 400 params[4], params[5], ZERO, params[3]); 401 } else if (pattern.matches("Zo[RZ]'?x,?Zo[RZ]'?x|Zo?[RZ]x,?Zo?[RZ]x")) { 402 setLatLonObj(latLon, 403 params[0], params[1], ZERO, params[2], 404 params[3], params[4], ZERO, params[5]); 405 } else if (pattern.matches("ZoZ'[RZ]\"?x,?ZoZ'[RZ]\"?x|ZZ[RZ]x,?ZZ[RZ]x")) { 406 setLatLonObj(latLon, 407 params[0], params[1], params[2], params[3], 408 params[4], params[5], params[6], params[7]); 409 } else if (pattern.matches("xZoZ'[RZ]\"?,?xZoZ'[RZ]\"?|xZZ[RZ],?xZZ[RZ]")) { 410 setLatLonObj(latLon, 411 params[1], params[2], params[3], params[0], 412 params[5], params[6], params[7], params[4]); 413 } else if (pattern.matches("ZZ[RZ],?ZZ[RZ]")) { 414 setLatLonObj(latLon, 415 params[0], params[1], params[2], "N", 416 params[3], params[4], params[5], "E"); 417 } else { 418 throw new IllegalArgumentException("invalid format: " + pattern); 387 388 final String pattern = sb.toString(); 389 390 final Object[] params = list.toArray(); 391 392 if (pattern.matches("Ro?,?Ro?")) { 393 setLatLonObj(latLon, 394 params[0], ZERO, ZERO, "N", 395 params[1], ZERO, ZERO, "E"); 396 } else if (pattern.matches("xRo?,?xRo?")) { 397 setLatLonObj(latLon, 398 params[1], ZERO, ZERO, params[0], 399 params[3], ZERO, ZERO, params[2]); 400 } else if (pattern.matches("Ro?x,?Ro?x")) { 401 setLatLonObj(latLon, 402 params[0], ZERO, ZERO, params[1], 403 params[2], ZERO, ZERO, params[3]); 404 } else if (pattern.matches("Zo[RZ]'?,?Zo[RZ]'?|Z[RZ],?Z[RZ]")) { 405 setLatLonObj(latLon, 406 params[0], params[1], ZERO, "N", 407 params[2], params[3], ZERO, "E"); 408 } else if (pattern.matches("xZo[RZ]'?,?xZo[RZ]'?|xZo?[RZ],?xZo?[RZ]")) { 409 setLatLonObj(latLon, 410 params[1], params[2], ZERO, params[0], 411 params[4], params[5], ZERO, params[3]); 412 } else if (pattern.matches("Zo[RZ]'?x,?Zo[RZ]'?x|Zo?[RZ]x,?Zo?[RZ]x")) { 413 setLatLonObj(latLon, 414 params[0], params[1], ZERO, params[2], 415 params[3], params[4], ZERO, params[5]); 416 } else if (pattern.matches("ZoZ'[RZ]\"?x,?ZoZ'[RZ]\"?x|ZZ[RZ]x,?ZZ[RZ]x")) { 417 setLatLonObj(latLon, 418 params[0], params[1], params[2], params[3], 419 params[4], params[5], params[6], params[7]); 420 } else if (pattern.matches("xZoZ'[RZ]\"?,?xZoZ'[RZ]\"?|xZZ[RZ],?xZZ[RZ]")) { 421 setLatLonObj(latLon, 422 params[1], params[2], params[3], params[0], 423 params[5], params[6], params[7], params[4]); 424 } else if (pattern.matches("ZZ[RZ],?ZZ[RZ]")) { 425 setLatLonObj(latLon, 426 params[0], params[1], params[2], "N", 427 params[3], params[4], params[5], "E"); 428 } else { 429 throw new IllegalArgumentException("invalid format: " + pattern); 430 } 419 431 } 420 432
Note:
See TracChangeset
for help on using the changeset viewer.