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

Last change on this file since 12524 was 12382, checked in by michael2402, 7 years ago

More documentation for the tools package

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