[10630] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[10757] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[10630] | 5 |
|
---|
| 6 | import java.util.regex.Matcher;
|
---|
| 7 | import java.util.regex.Pattern;
|
---|
| 8 |
|
---|
[10757] | 9 | import org.openstreetmap.josm.Main;
|
---|
| 10 | import org.openstreetmap.josm.data.Bounds;
|
---|
[10630] | 11 |
|
---|
| 12 | /**
|
---|
| 13 | * Parses a Geo URL (as specified in <a href="https://tools.ietf.org/html/rfc5870">RFC 5870</a>) into {@link Bounds}.
|
---|
[11329] | 14 | *
|
---|
| 15 | * Note that Geo URLs are also handled by {@link OsmUrlToBounds}.
|
---|
[10630] | 16 | */
|
---|
| 17 | public final class GeoUrlToBounds {
|
---|
| 18 |
|
---|
[10635] | 19 | public static final Pattern PATTERN = Pattern.compile("geo:(?<lat>[+-]?[0-9.]+),(?<lon>[+-]?[0-9.]+)(\\?z=(?<zoom>[0-9]+))?");
|
---|
[10630] | 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()) {
|
---|
[10757] | 34 | final double lat;
|
---|
| 35 | final double lon;
|
---|
[10630] | 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 | }
|
---|