| 59 | | tr("Enter the coordinates for the new node.") + |
| 60 | | "<br>" + tr("Use decimal degrees.") + |
| 61 | | "<br>" + tr("Negative values denote Western/Southern hemisphere.")), |
| | 62 | tr("Enter the coordinates for the new node.<br/>You can separate longitude and latitude with space, comma or semicolon.<br/>" + |
| | 63 | "Use positive numbers or N, E characters to indicate North or East cardinal direction.<br/>" + |
| | 64 | "For South and West cardinal directions you can use either negative numbers or S, W characters.<br/>" + |
| | 65 | "Coordinate value can be in one of three formats:<ul>" + |
| | 66 | "<li><i>degrees</i><tt>°</tt></li>" + |
| | 67 | "<li><i>degrees</i><tt>°</tt> <i>minutes</i><tt>'</tt></li>" + |
| | 68 | "<li><i>degrees</i><tt>°</tt> <i>minutes</i><tt>'</tt> <i>seconds</i><tt>"</tt></li>" + |
| | 69 | "</ul>" + |
| | 70 | "Symbols <tt>°</tt>, <tt>'</tt>, <tt>′</tt>, <tt>"</tt>, <tt>″</tt> are optional.<br/><br/>" + |
| | 71 | "Some examples:<ul>" + |
| | 72 | "<li>49.29918° 19.24788°</li>" + |
| | 73 | "<li>N 49.29918 E 19.24788</li>" + |
| | 74 | "<li>W 49°29.918' S 19°24.788'</li>" + |
| | 75 | "<li>N 49°29'04" E 19°24'43"</li>" + |
| | 76 | "<li>49.29918 N, 19.24788 E</li>" + |
| | 77 | "<li>49°29'21" N 19°24'38" E</li>" + |
| | 78 | "<li>49 29 51, 19 24 18</li>" + |
| | 79 | "<li>49 29, 19 24</li>" + |
| | 80 | "<li>E 49 29, N 19 24</li>" + |
| | 81 | "<li>49° 29; 19° 24</li>" + |
| | 82 | "<li>N 49° 29, W 19° 24</li>" + |
| | 83 | "<li>49° 29.5 S, 19° 24.6 E</li>" + |
| | 84 | "<li>N 49 29.918 E 19 15.88</li>" + |
| | 85 | "<li>49 29.4 19 24.5</li>" + |
| | 86 | "<li>-49 29.4 N -19 24.5 W</li></ul>" + |
| | 87 | "<li>48 deg 42' 52.13\" N, 21 deg 11' 47.60\" E</li></ul>" |
| | 88 | )), |
| | 302 | |
| | 303 | |
| | 304 | private static final double ZERO = 0.0; |
| | 305 | |
| | 306 | private static final Pattern p = Pattern.compile("([+|-]?\\d+[.,]\\d+)|([+|-]?\\d+)|(\u00B0|o|deg)|('|\u2032|min)|(\"|\u2033|sec)|(,|;)|([NSEWO])|\\s+|(.+)", Pattern.CASE_INSENSITIVE); |
| | 307 | |
| | 308 | |
| | 309 | private static LatLon parse(final String coord) { |
| | 310 | final Matcher m = p.matcher(coord); |
| | 311 | |
| | 312 | final StringBuilder sb = new StringBuilder(); |
| | 313 | final List<Object> list = new ArrayList<Object>(); |
| | 314 | |
| | 315 | while (m.find()) { |
| | 316 | if (m.group(1) != null) { |
| | 317 | sb.append('R'); |
| | 318 | list.add(Double.parseDouble(m.group(1).replace(',', '.'))); |
| | 319 | } else if (m.group(2) != null) { |
| | 320 | sb.append('Z'); |
| | 321 | list.add(Double.parseDouble(m.group(2))); |
| | 322 | } else if (m.group(3) != null) { |
| | 323 | sb.append('°'); |
| | 324 | } else if (m.group(4) != null) { |
| | 325 | sb.append('\''); |
| | 326 | } else if (m.group(5) != null) { |
| | 327 | sb.append('"'); |
| | 328 | } else if (m.group(6) != null) { |
| | 329 | sb.append(','); |
| | 330 | } else if (m.group(7) != null) { |
| | 331 | sb.append("x"); |
| | 332 | list.add(m.group(7).toUpperCase().replace('O', 'E')); |
| | 333 | } else if (m.group(8) != null) { |
| | 334 | throw new IllegalArgumentException("invalid token: " + m.group(8)); |
| | 335 | } |
| | 336 | } |
| | 337 | |
| | 338 | final String pattern = sb.toString(); |
| | 339 | |
| | 340 | final Object[] params = list.toArray(); |
| | 341 | final LatLonHolder latLon = new LatLonHolder(); |
| | 342 | |
| | 343 | if (pattern.matches("R°?,?R°?")) { |
| | 344 | setLatLon(latLon, |
| | 345 | params[0], ZERO, ZERO, "N", |
| | 346 | params[1], ZERO, ZERO, "E"); |
| | 347 | } else if (pattern.matches("xR°?,?xR°?")) { |
| | 348 | setLatLon(latLon, |
| | 349 | params[1], ZERO, ZERO, params[0], |
| | 350 | params[3], ZERO, ZERO, params[2]); |
| | 351 | } else if (pattern.matches("R°?x,?R°?x")) { |
| | 352 | setLatLon(latLon, |
| | 353 | params[0], ZERO, ZERO, params[1], |
| | 354 | params[2], ZERO, ZERO, params[3]); |
| | 355 | } else if (pattern.matches("Z°[RZ]'?,?Z°[RZ]'?|Z[RZ],?Z[RZ]")) { |
| | 356 | setLatLon(latLon, |
| | 357 | params[0], params[1], ZERO, "N", |
| | 358 | params[2], params[3], ZERO, "E"); |
| | 359 | } else if (pattern.matches("xZ°[RZ]'?,?xZ°[RZ]'?|xZ°?[RZ],?xZ°?[RZ]")) { |
| | 360 | setLatLon(latLon, |
| | 361 | params[1], params[2], ZERO, params[0], |
| | 362 | params[4], params[5], ZERO, params[3]); |
| | 363 | } else if (pattern.matches("Z°[RZ]'?x,?Z°[RZ]'?x|Z°?[RZ]x,?Z°?[RZ]x")) { |
| | 364 | setLatLon(latLon, |
| | 365 | params[0], params[1], ZERO, params[2], |
| | 366 | params[3], params[4], ZERO, params[5]); |
| | 367 | } else if (pattern.matches("Z°Z'[RZ]\"?x,?Z°Z'[RZ]\"?x|ZZ[RZ]x,?ZZ[RZ]x")) { |
| | 368 | setLatLon(latLon, |
| | 369 | params[0], params[1], params[2], params[3], |
| | 370 | params[4], params[5], params[6], params[7]); |
| | 371 | } else if (pattern.matches("xZ°Z'[RZ]\"?,?xZ°Z'[RZ]\"?|xZZ[RZ],?xZZ[RZ]")) { |
| | 372 | setLatLon(latLon, |
| | 373 | params[1], params[2], params[3], params[0], |
| | 374 | params[5], params[6], params[7], params[4]); |
| | 375 | } else if (pattern.matches("ZZ[RZ],?ZZ[RZ]")) { |
| | 376 | setLatLon(latLon, |
| | 377 | params[0], params[1], params[2], "N", |
| | 378 | params[3], params[4], params[5], "E"); |
| | 379 | } else { |
| | 380 | throw new IllegalArgumentException("invalid format: " + pattern); |
| | 381 | } |
| | 382 | |
| | 383 | return new LatLon(latLon.lat, latLon.lon); |
| | 384 | } |
| | 385 | |
| | 386 | |
| | 387 | private static class LatLonHolder { |
| | 388 | double lat, lon; |
| | 389 | } |
| | 390 | |
| | 391 | |
| | 392 | private static void setLatLon(final LatLonHolder latLon, |
| | 393 | final Object coord1deg, final Object coord1min, final Object coord1sec, final Object card1, |
| | 394 | final Object coord2deg, final Object coord2min, final Object coord2sec, final Object card2) { |
| | 395 | |
| | 396 | setLatLon(latLon, |
| | 397 | (double) (Double) coord1deg, (double) (Double) coord1min, (double) (Double) coord1sec, (String) card1, |
| | 398 | (double) (Double) coord2deg, (double) (Double) coord2min, (double) (Double) coord2sec, (String) card2); |
| | 399 | } |
| | 400 | |
| | 401 | |
| | 402 | private static void setLatLon(final LatLonHolder latLon, |
| | 403 | final double coord1deg, final double coord1min, final double coord1sec, final String card1, |
| | 404 | final double coord2deg, final double coord2min, final double coord2sec, final String card2) { |
| | 405 | |
| | 406 | setLatLon(latLon, coord1deg, coord1min, coord1sec, card1); |
| | 407 | setLatLon(latLon, coord2deg, coord2min, coord2sec, card2); |
| | 408 | } |
| | 409 | |
| | 410 | |
| | 411 | private static void setLatLon(final LatLonHolder latLon, final double coordDeg, final double coordMin, final double coordSec, final String card) { |
| | 412 | if (coordDeg < -180 || coordDeg > 180 || coordMin < 0 || coordMin >= 60 || coordSec < 0 || coordSec > 60) { |
| | 413 | throw new IllegalArgumentException("out of range"); |
| | 414 | } |
| | 415 | |
| | 416 | double coord = (coordDeg < 0 ? -1 : 1) * (Math.abs(coordDeg) + coordMin / 60 + coordSec / 3600); |
| | 417 | coord = card.equals("N") || card.equals("E") ? coord : -coord; |
| | 418 | if (card.equals("N") || card.equals("S")) { |
| | 419 | latLon.lat = coord; |
| | 420 | } else { |
| | 421 | latLon.lon = coord; |
| | 422 | } |
| | 423 | } |
| | 424 | |
| | 425 | |
| | 426 | public String getText() { |
| | 427 | return tfLatLon.getText(); |
| | 428 | } |
| | 429 | |
| | 430 | |
| | 431 | public void setText(String text) { |
| | 432 | tfLatLon.setText(text); |
| | 433 | } |
| | 434 | |
| | 435 | |