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

Last change on this file was 15184, checked in by Don-vip, 5 years ago

fix #17825 - support Geo URIs with WGS84 CRS and/or uncertainty

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.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(
22 "geo:(?<lat>[+-]?[0-9.]+),(?<lon>[+-]?[0-9.]+)(?<crs>;crs=wgs84)?(?<uncertainty>;u=[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 Logging.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 Logging.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 Logging.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.