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

Last change on this file since 10684 was 10635, checked in by simon04, 8 years ago

fix #13201: Support Geo URLs with negative values

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