64 | | pnl.add(new JLabel(tr("Latitude")), GBC.std().insets(0,10,5,0)); |
65 | | tfLat = new JTextField(12); |
66 | | pnl.add(tfLat, GBC.eol().insets(0,10,0,0)); |
67 | | pnl.add(new JLabel(tr("Longitude")), GBC.std().insets(0,0,5,10)); |
68 | | tfLon = new JTextField(12); |
69 | | pnl.add(tfLon, GBC.eol().insets(0,0,0,10)); |
| 68 | pnl.add(new JLabel(tr("Coordinates")), GBC.std().insets(0,10,5,0)); |
| 69 | tfLatLon = new JTextField(24); |
| 70 | pnl.add(tfLatLon, GBC.eol().insets(0,10,0,0)); |
| 71 | // pnl.add(new JLabel(tr("Longitude")), GBC.std().insets(0,0,5,10)); |
| 72 | // tfLon = new JTextField(12); |
| 73 | // pnl.add(tfLon, GBC.eol().insets(0,0,0,10)); |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | private static final double ZERO = 0.0; |
| 306 | |
| 307 | private static final Pattern p = Pattern.compile("([+|-]?\\d+\\.\\d*)|([+|-]?\\d+)|(°|o|deg)|('|′|min)|(\"|″|sec)|(,|;)|([NSEW])|\\s+|(.+)", Pattern.CASE_INSENSITIVE); |
| 308 | |
| 309 | |
| 310 | private static LatLon parse(final String coord) { |
| 311 | // System.out.println("======================="); |
| 312 | // System.out.println("Input: " + coord); |
| 313 | |
| 314 | final Matcher m = p.matcher(coord); |
| 315 | |
| 316 | final StringBuilder sb = new StringBuilder(); |
| 317 | final List<Object> list = new ArrayList<Object>(); |
| 318 | |
| 319 | while (m.find()) { |
| 320 | if (m.group(1) != null) { |
| 321 | sb.append('R'); |
| 322 | list.add(Double.parseDouble(m.group(1))); |
| 323 | } else if (m.group(2) != null) { |
| 324 | sb.append('Z'); |
| 325 | list.add(Double.parseDouble(m.group(2))); |
| 326 | } else if (m.group(3) != null) { |
| 327 | sb.append('°'); |
| 328 | } else if (m.group(4) != null) { |
| 329 | sb.append('\''); |
| 330 | } else if (m.group(5) != null) { |
| 331 | sb.append('"'); |
| 332 | } else if (m.group(6) != null) { |
| 333 | sb.append(','); |
| 334 | } else if (m.group(7) != null) { |
| 335 | sb.append("x"); |
| 336 | list.add(m.group(7).toUpperCase()); |
| 337 | } else if (m.group(8) != null) { |
| 338 | throw new IllegalArgumentException("invalid token: " + m.group(8)); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | final String pattern = sb.toString(); |
| 343 | |
| 344 | // System.out.println("Pattern: " + sb.toString()); |
| 345 | |
| 346 | final Object[] params = list.toArray(); |
| 347 | final LatLonHolder latLon = new LatLonHolder(); |
| 348 | |
| 349 | if (pattern.matches("R°?,?R°?")) { |
| 350 | setLatLon(latLon, |
| 351 | params[0], ZERO, ZERO, "N", |
| 352 | params[1], ZERO, ZERO, "E"); |
| 353 | } else if (pattern.matches("xR°?,?xR°?")) { |
| 354 | setLatLon(latLon, |
| 355 | params[1], ZERO, ZERO, params[0], |
| 356 | params[3], ZERO, ZERO, params[2]); |
| 357 | } else if (pattern.matches("R°?x,?R°?x")) { |
| 358 | setLatLon(latLon, |
| 359 | params[0], ZERO, ZERO, params[1], |
| 360 | params[2], ZERO, ZERO, params[3]); |
| 361 | } else if (pattern.matches("Z°[RZ]'?,?Z°[RZ]'?")) { |
| 362 | setLatLon(latLon, |
| 363 | params[0], params[1], ZERO, "N", |
| 364 | params[2], params[3], ZERO, "E"); |
| 365 | } else if (pattern.matches("Z[RZ],?Z[RZ]")) { |
| 366 | setLatLon(latLon, |
| 367 | params[0], params[1], ZERO, "N", |
| 368 | params[2], params[3], ZERO, "E"); |
| 369 | } else if (pattern.matches("xZ°[RZ]'?,?xZ°[RZ]'?|xZ°?[RZ],?xZ°?[RZ]")) { |
| 370 | setLatLon(latLon, |
| 371 | params[1], params[2], ZERO, params[0], |
| 372 | params[4], params[5], ZERO, params[3]); |
| 373 | } else if (pattern.matches("Z°[RZ]'?x,?Z°[RZ]'?x|Z°?[RZ]x,?Z°?[RZ]x")) { |
| 374 | setLatLon(latLon, |
| 375 | params[0], params[1], ZERO, params[2], |
| 376 | params[3], params[4], ZERO, params[5]); |
| 377 | } else if (pattern.matches("Z°Z'[RZ]\"?x,?Z°Z'[RZ]\"?x|ZZ[RZ]x,?ZZ[RZ]x")) { |
| 378 | setLatLon(latLon, |
| 379 | params[0], params[1], params[2], params[3], |
| 380 | params[4], params[5], params[6], params[7]); |
| 381 | } else if (pattern.matches("xZ°Z'[RZ]\"?,?xZ°Z'[RZ]\"?|xZZ[RZ],?xZZ[RZ]")) { |
| 382 | setLatLon(latLon, |
| 383 | params[1], params[2], params[3], params[0], |
| 384 | params[5], params[6], params[7], params[4]); |
| 385 | } else if (pattern.matches("ZZ[RZ],?ZZ[RZ]")) { |
| 386 | setLatLon(latLon, |
| 387 | params[0], params[1], params[2], "N", |
| 388 | params[3], params[4], params[5], "E"); |
| 389 | } else { |
| 390 | throw new IllegalArgumentException("invalid format: " + pattern); |
| 391 | } |
| 392 | |
| 393 | return new LatLon(latLon.lat, latLon.lon); |
| 394 | } |
| 395 | |
| 396 | |
| 397 | private static class LatLonHolder { |
| 398 | double lat, lon; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | private static void setLatLon(final LatLonHolder latLon, |
| 403 | final Object coord1deg, final Object coord1min, final Object coord1sec, final Object card1, |
| 404 | final Object coord2deg, final Object coord2min, final Object coord2sec, final Object card2) { |
| 405 | |
| 406 | setLatLon(latLon, |
| 407 | (double) (Double) coord1deg, (double) (Double) coord1min, (double) (Double) coord1sec, (String) card1, |
| 408 | (double) (Double) coord2deg, (double) (Double) coord2min, (double) (Double) coord2sec, (String) card2); |
| 409 | } |
| 410 | |
| 411 | |
| 412 | private static void setLatLon(final LatLonHolder latLon, |
| 413 | final double coord1deg, final double coord1min, final double coord1sec, final String card1, |
| 414 | final double coord2deg, final double coord2min, final double coord2sec, final String card2) { |
| 415 | |
| 416 | setLatLon(latLon, coord1deg, coord1min, coord1sec, card1); |
| 417 | setLatLon(latLon, coord2deg, coord2min, coord2sec, card2); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | private static void setLatLon(final LatLonHolder latLon, final double coordDeg, final double coordMin, final double coordSec, final String card) { |
| 422 | if (coordDeg < -180 || coordDeg > 180 || coordMin < 0 || coordMin >= 60 || coordSec < 0 || coordSec > 60) { |
| 423 | throw new IllegalArgumentException("out of range"); |
| 424 | } |
| 425 | |
| 426 | double coord = Math.signum(coordDeg) * (Math.abs(coordDeg) + coordMin / 60 + coordSec / 3600); |
| 427 | coord = card.equals("N") || card.equals("E") ? coord : -coord; |
| 428 | if (card.equals("N") || card.equals("S")) { |
| 429 | latLon.lat = coord; |
| 430 | } else { |
| 431 | latLon.lon = coord; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | |
| 436 | |