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

Last change on this file since 14079 was 12624, checked in by Don-vip, 7 years ago

see #15182 - remove unused imports

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.data.Bounds;
10
11/**
12 * Parses a Geo URL (as specified in <a href="https://tools.ietf.org/html/rfc5870">RFC 5870</a>) into {@link Bounds}.
13 *
14 * Note that Geo URLs are also handled by {@link OsmUrlToBounds}.
15 */
16public final class GeoUrlToBounds {
17
18 /**
19 * The pattern of a geo: url, having named match groups.
20 */
21 public static final Pattern PATTERN = Pattern.compile("geo:(?<lat>[+-]?[0-9.]+),(?<lon>[+-]?[0-9.]+)(\\?z=(?<zoom>[0-9]+))?");
22
23 private GeoUrlToBounds() {
24 // Hide default constructor for utils classes
25 }
26
27 /**
28 * Parses a Geo URL (as specified in <a href="https://tools.ietf.org/html/rfc5870">RFC 5870</a>) into {@link Bounds}.
29 * @param url the URL to be parsed
30 * @return the parsed {@link Bounds}
31 */
32 public static Bounds parse(final String url) {
33 CheckParameterUtil.ensureParameterNotNull(url, "url");
34 final Matcher m = PATTERN.matcher(url);
35 if (m.matches()) {
36 final double lat;
37 final double lon;
38 final int zoom;
39 try {
40 lat = Double.parseDouble(m.group("lat"));
41 } catch (NumberFormatException e) {
42 Logging.warn(tr("URL does not contain valid {0}", tr("latitude")), e);
43 return null;
44 }
45 try {
46 lon = Double.parseDouble(m.group("lon"));
47 } catch (NumberFormatException e) {
48 Logging.warn(tr("URL does not contain valid {0}", tr("longitude")), e);
49 return null;
50 }
51 try {
52 zoom = m.group("zoom") != null ? Integer.parseInt(m.group("zoom")) : 18;
53 } catch (NumberFormatException e) {
54 Logging.warn(tr("URL does not contain valid {0}", tr("zoom")), e);
55 return null;
56 }
57 return OsmUrlToBounds.positionToBounds(lat, lon, zoom);
58 } else {
59 return null;
60 }
61 }
62}
Note: See TracBrowser for help on using the repository browser.