source: josm/trunk/src/org/openstreetmap/josm/tools/GeoUrlToBounds.java@ 12276

Last change on this file since 12276 was 11329, checked in by simon04, 7 years ago

see #13201 - OsmUrlToBounds: also take Geo URLs into account

This adds Geo URL support to various actions, such as:

  • Download dialog
  • Jump to Position
  • Commandline parameters (OSM or GPS download)
File size: 2.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.regex.Matcher;
7import java.util.regex.Pattern;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.data.Bounds;
11
12/**
13 * Parses a Geo URL (as specified in <a href="https://tools.ietf.org/html/rfc5870">RFC 5870</a>) into {@link Bounds}.
14 *
15 * Note that Geo URLs are also handled by {@link OsmUrlToBounds}.
16 */
17public final class GeoUrlToBounds {
18
19 public static final Pattern PATTERN = Pattern.compile("geo:(?<lat>[+-]?[0-9.]+),(?<lon>[+-]?[0-9.]+)(\\?z=(?<zoom>[0-9]+))?");
20
21 private GeoUrlToBounds() {
22 // Hide default constructor for utils classes
23 }
24
25 /**
26 * Parses a Geo URL (as specified in <a href="https://tools.ietf.org/html/rfc5870">RFC 5870</a>) into {@link Bounds}.
27 * @param url the URL to be parsed
28 * @return the parsed {@link Bounds}
29 */
30 public static Bounds parse(final String url) {
31 CheckParameterUtil.ensureParameterNotNull(url, "url");
32 final Matcher m = PATTERN.matcher(url);
33 if (m.matches()) {
34 final double lat;
35 final double lon;
36 final int zoom;
37 try {
38 lat = Double.parseDouble(m.group("lat"));
39 } catch (NumberFormatException e) {
40 Main.warn(tr("URL does not contain valid {0}", tr("latitude")), e);
41 return null;
42 }
43 try {
44 lon = Double.parseDouble(m.group("lon"));
45 } catch (NumberFormatException e) {
46 Main.warn(tr("URL does not contain valid {0}", tr("longitude")), e);
47 return null;
48 }
49 try {
50 zoom = m.group("zoom") != null ? Integer.parseInt(m.group("zoom")) : 18;
51 } catch (NumberFormatException e) {
52 Main.warn(tr("URL does not contain valid {0}", tr("zoom")), e);
53 return null;
54 }
55 return OsmUrlToBounds.positionToBounds(lat, lon, zoom);
56 } else {
57 return null;
58 }
59 }
60}
Note: See TracBrowser for help on using the repository browser.