001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.util.regex.Matcher; 007import java.util.regex.Pattern; 008 009import org.openstreetmap.josm.Main; 010import org.openstreetmap.josm.data.Bounds; 011 012/** 013 * Parses a Geo URL (as specified in <a href="https://tools.ietf.org/html/rfc5870">RFC 5870</a>) into {@link Bounds}. 014 */ 015public final class GeoUrlToBounds { 016 017 public static final Pattern PATTERN = Pattern.compile("geo:(?<lat>[+-]?[0-9.]+),(?<lon>[+-]?[0-9.]+)(\\?z=(?<zoom>[0-9]+))?"); 018 019 private GeoUrlToBounds() { 020 // Hide default constructor for utils classes 021 } 022 023 /** 024 * Parses a Geo URL (as specified in <a href="https://tools.ietf.org/html/rfc5870">RFC 5870</a>) into {@link Bounds}. 025 * @param url the URL to be parsed 026 * @return the parsed {@link Bounds} 027 */ 028 public static Bounds parse(final String url) { 029 CheckParameterUtil.ensureParameterNotNull(url, "url"); 030 final Matcher m = PATTERN.matcher(url); 031 if (m.matches()) { 032 final double lat; 033 final double lon; 034 final int zoom; 035 try { 036 lat = Double.parseDouble(m.group("lat")); 037 } catch (NumberFormatException e) { 038 Main.warn(tr("URL does not contain valid {0}", tr("latitude")), e); 039 return null; 040 } 041 try { 042 lon = Double.parseDouble(m.group("lon")); 043 } catch (NumberFormatException e) { 044 Main.warn(tr("URL does not contain valid {0}", tr("longitude")), e); 045 return null; 046 } 047 try { 048 zoom = m.group("zoom") != null ? Integer.parseInt(m.group("zoom")) : 18; 049 } catch (NumberFormatException e) { 050 Main.warn(tr("URL does not contain valid {0}", tr("zoom")), e); 051 return null; 052 } 053 return OsmUrlToBounds.positionToBounds(lat, lon, zoom); 054 } else { 055 return null; 056 } 057 } 058}