[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[1677] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[6474] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[9576] | 6 | import java.awt.Dimension;
|
---|
[1677] | 7 | import java.util.HashMap;
|
---|
[2540] | 8 | import java.util.Map;
|
---|
[12803] | 9 | import java.util.Objects;
|
---|
[12796] | 10 | import java.util.function.Supplier;
|
---|
[1677] | 11 |
|
---|
[4736] | 12 | import org.openstreetmap.josm.Main;
|
---|
[1677] | 13 | import org.openstreetmap.josm.data.Bounds;
|
---|
[11299] | 14 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
[1677] | 15 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[9230] | 16 | import org.openstreetmap.josm.data.projection.Ellipsoid;
|
---|
[11299] | 17 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
| 18 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
[1677] | 19 |
|
---|
[11329] | 20 | /**
|
---|
| 21 | * Parses various URL used in OpenStreetMap projects into {@link Bounds}.
|
---|
| 22 | */
|
---|
[6362] | 23 | public final class OsmUrlToBounds {
|
---|
[2540] | 24 | private static final String SHORTLINK_PREFIX = "http://osm.org/go/";
|
---|
[6474] | 25 |
|
---|
[12869] | 26 | private static volatile Supplier<Dimension> mapSize = () -> new Dimension(800, 600);
|
---|
[12796] | 27 |
|
---|
[6362] | 28 | private OsmUrlToBounds() {
|
---|
| 29 | // Hide default constructor for utils classes
|
---|
| 30 | }
|
---|
[2512] | 31 |
|
---|
[11329] | 32 | /**
|
---|
| 33 | * Parses an URL into {@link Bounds}
|
---|
| 34 | * @param url the URL to be parsed
|
---|
| 35 | * @return the parsed {@link Bounds}, or {@code null}
|
---|
| 36 | */
|
---|
[8291] | 37 | public static Bounds parse(String url) {
|
---|
[11329] | 38 | if (url.startsWith("geo:")) {
|
---|
| 39 | return GeoUrlToBounds.parse(url);
|
---|
| 40 | }
|
---|
[4298] | 41 | try {
|
---|
| 42 | // a percent sign indicates an encoded URL (RFC 1738).
|
---|
| 43 | if (url.contains("%")) {
|
---|
[8304] | 44 | url = Utils.decodeUrl(url);
|
---|
[4298] | 45 | }
|
---|
[11620] | 46 | } catch (IllegalArgumentException ex) {
|
---|
[12620] | 47 | Logging.error(ex);
|
---|
[4298] | 48 | }
|
---|
[2540] | 49 | Bounds b = parseShortLink(url);
|
---|
| 50 | if (b != null)
|
---|
| 51 | return b;
|
---|
[12090] | 52 | if (url.contains("#map") || url.contains("/#")) {
|
---|
[6155] | 53 | // probably it's a URL following the new scheme?
|
---|
| 54 | return parseHashURLs(url);
|
---|
| 55 | }
|
---|
[11299] | 56 | final int i = url.indexOf('?');
|
---|
[6118] | 57 | if (i == -1) {
|
---|
[6155] | 58 | return null;
|
---|
[6118] | 59 | }
|
---|
[1677] | 60 | String[] args = url.substring(i+1).split("&");
|
---|
[8338] | 61 | Map<String, String> map = new HashMap<>();
|
---|
[1677] | 62 | for (String arg : args) {
|
---|
| 63 | int eq = arg.indexOf('=');
|
---|
| 64 | if (eq != -1) {
|
---|
| 65 | map.put(arg.substring(0, eq), arg.substring(eq + 1));
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | try {
|
---|
| 70 | if (map.containsKey("bbox")) {
|
---|
[6085] | 71 | String[] bbox = map.get("bbox").split(",");
|
---|
[1677] | 72 | b = new Bounds(
|
---|
[6203] | 73 | Double.parseDouble(bbox[1]), Double.parseDouble(bbox[0]),
|
---|
| 74 | Double.parseDouble(bbox[3]), Double.parseDouble(bbox[2]));
|
---|
[1689] | 75 | } else if (map.containsKey("minlat")) {
|
---|
[6203] | 76 | double minlat = Double.parseDouble(map.get("minlat"));
|
---|
| 77 | double minlon = Double.parseDouble(map.get("minlon"));
|
---|
| 78 | double maxlat = Double.parseDouble(map.get("maxlat"));
|
---|
| 79 | double maxlon = Double.parseDouble(map.get("maxlon"));
|
---|
| 80 | b = new Bounds(minlat, minlon, maxlat, maxlon);
|
---|
[1677] | 81 | } else {
|
---|
[5005] | 82 | String z = map.get("zoom");
|
---|
[11847] | 83 | b = positionToBounds(parseDouble(map, "lat"), parseDouble(map, "lon"),
|
---|
[5005] | 84 | z == null ? 18 : Integer.parseInt(z));
|
---|
[1677] | 85 | }
|
---|
[11847] | 86 | } catch (IllegalArgumentException | ArrayIndexOutOfBoundsException ex) {
|
---|
[12620] | 87 | Logging.log(Logging.LEVEL_ERROR, url, ex);
|
---|
[1677] | 88 | }
|
---|
| 89 | return b;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[6118] | 92 | /**
|
---|
| 93 | * Openstreetmap.org changed it's URL scheme in August 2013, which breaks the URL parsing.
|
---|
| 94 | * The following function, called by the old parse function if necessary, provides parsing new URLs
|
---|
[6920] | 95 | * the new URLs follow the scheme https://www.openstreetmap.org/#map=18/51.71873/8.76164&layers=CN
|
---|
[6155] | 96 | * @param url string for parsing
|
---|
| 97 | * @return Bounds if hashurl, {@code null} otherwise
|
---|
[6118] | 98 | */
|
---|
[8291] | 99 | private static Bounds parseHashURLs(String url) {
|
---|
[12090] | 100 | int startIndex = url.indexOf('#');
|
---|
[6119] | 101 | if (startIndex == -1) return null;
|
---|
[6155] | 102 | int endIndex = url.indexOf('&', startIndex);
|
---|
[6119] | 103 | if (endIndex == -1) endIndex = url.length();
|
---|
[12090] | 104 | String coordPart = url.substring(startIndex+(url.contains("#map=") ? "#map=".length() : "#".length()), endIndex);
|
---|
[6474] | 105 | String[] parts = coordPart.split("/");
|
---|
| 106 | if (parts.length < 3) {
|
---|
[12620] | 107 | Logging.warn(tr("URL does not contain {0}/{1}/{2}", tr("zoom"), tr("latitude"), tr("longitude")));
|
---|
[6514] | 108 | return null;
|
---|
[6474] | 109 | }
|
---|
| 110 | int zoom;
|
---|
[6394] | 111 | try {
|
---|
[6474] | 112 | zoom = Integer.parseInt(parts[0]);
|
---|
| 113 | } catch (NumberFormatException e) {
|
---|
[12620] | 114 | Logging.warn(tr("URL does not contain valid {0}", tr("zoom")), e);
|
---|
[6514] | 115 | return null;
|
---|
[6119] | 116 | }
|
---|
[9062] | 117 | double lat, lon;
|
---|
[6474] | 118 | try {
|
---|
| 119 | lat = Double.parseDouble(parts[1]);
|
---|
| 120 | } catch (NumberFormatException e) {
|
---|
[12620] | 121 | Logging.warn(tr("URL does not contain valid {0}", tr("latitude")), e);
|
---|
[6514] | 122 | return null;
|
---|
[6474] | 123 | }
|
---|
| 124 | try {
|
---|
| 125 | lon = Double.parseDouble(parts[2]);
|
---|
| 126 | } catch (NumberFormatException e) {
|
---|
[12620] | 127 | Logging.warn(tr("URL does not contain valid {0}", tr("longitude")), e);
|
---|
[6514] | 128 | return null;
|
---|
[6474] | 129 | }
|
---|
| 130 | return positionToBounds(lat, lon, zoom);
|
---|
[6118] | 131 | }
|
---|
| 132 |
|
---|
[6317] | 133 | private static double parseDouble(Map<String, String> map, String key) {
|
---|
[1677] | 134 | if (map.containsKey(key))
|
---|
| 135 | return Double.parseDouble(map.get(key));
|
---|
[11847] | 136 | if (map.containsKey('m'+key))
|
---|
| 137 | return Double.parseDouble(map.get('m'+key));
|
---|
| 138 | throw new IllegalArgumentException(map.toString() + " does not contain " + key);
|
---|
[1677] | 139 | }
|
---|
[1722] | 140 |
|
---|
[2540] | 141 | private static final char[] SHORTLINK_CHARS = {
|
---|
| 142 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
---|
| 143 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
---|
| 144 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
---|
| 145 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
---|
| 146 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
---|
| 147 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
---|
| 148 | 'w', 'x', 'y', 'z', '0', '1', '2', '3',
|
---|
| 149 | '4', '5', '6', '7', '8', '9', '_', '@'
|
---|
| 150 | };
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
[5863] | 153 | * Parse OSM short link
|
---|
[2540] | 154 | *
|
---|
| 155 | * @param url string for parsing
|
---|
| 156 | * @return Bounds if shortlink, null otherwise
|
---|
[5863] | 157 | * @see <a href="http://trac.openstreetmap.org/browser/sites/rails_port/lib/short_link.rb">short_link.rb</a>
|
---|
[2540] | 158 | */
|
---|
| 159 | private static Bounds parseShortLink(final String url) {
|
---|
[2626] | 160 | if (!url.startsWith(SHORTLINK_PREFIX))
|
---|
[2540] | 161 | return null;
|
---|
| 162 | final String shortLink = url.substring(SHORTLINK_PREFIX.length());
|
---|
| 163 |
|
---|
[7005] | 164 | final Map<Character, Integer> array = new HashMap<>();
|
---|
[2540] | 165 |
|
---|
[8510] | 166 | for (int i = 0; i < SHORTLINK_CHARS.length; ++i) {
|
---|
[2540] | 167 | array.put(SHORTLINK_CHARS[i], i);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | // long is necessary (need 32 bit positive value is needed)
|
---|
| 171 | long x = 0;
|
---|
| 172 | long y = 0;
|
---|
| 173 | int zoom = 0;
|
---|
| 174 | int zoomOffset = 0;
|
---|
| 175 |
|
---|
| 176 | for (final char ch : shortLink.toCharArray()) {
|
---|
| 177 | if (array.containsKey(ch)) {
|
---|
| 178 | int val = array.get(ch);
|
---|
[8510] | 179 | for (int i = 0; i < 3; ++i) {
|
---|
[2540] | 180 | x <<= 1;
|
---|
| 181 | if ((val & 32) != 0) {
|
---|
| 182 | x |= 1;
|
---|
| 183 | }
|
---|
| 184 | val <<= 1;
|
---|
| 185 |
|
---|
| 186 | y <<= 1;
|
---|
| 187 | if ((val & 32) != 0) {
|
---|
| 188 | y |= 1;
|
---|
| 189 | }
|
---|
| 190 | val <<= 1;
|
---|
| 191 | }
|
---|
| 192 | zoom += 3;
|
---|
| 193 | } else {
|
---|
| 194 | zoomOffset--;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | x <<= 32 - zoom;
|
---|
| 199 | y <<= 32 - zoom;
|
---|
| 200 |
|
---|
| 201 | // 2**32 == 4294967296
|
---|
| 202 | return positionToBounds(y * 180.0 / 4294967296.0 - 90.0,
|
---|
[2626] | 203 | x * 360.0 / 4294967296.0 - 180.0,
|
---|
| 204 | // TODO: -2 was not in ruby code
|
---|
| 205 | zoom - 8 - (zoomOffset % 3) - 2);
|
---|
[2540] | 206 | }
|
---|
| 207 |
|
---|
[12796] | 208 | /**
|
---|
| 209 | * Sets the map size supplier.
|
---|
| 210 | * @param mapSizeSupplier returns the map size in pixels
|
---|
| 211 | * @since 12796
|
---|
| 212 | */
|
---|
| 213 | public static void setMapSizeSupplier(Supplier<Dimension> mapSizeSupplier) {
|
---|
[12803] | 214 | mapSize = Objects.requireNonNull(mapSizeSupplier, "mapSizeSupplier");
|
---|
[11302] | 215 | }
|
---|
| 216 |
|
---|
| 217 | private static final int TILE_SIZE_IN_PIXELS = 256;
|
---|
| 218 |
|
---|
[12382] | 219 | /**
|
---|
| 220 | * Compute the bounds for a given lat/lon position and the zoom level
|
---|
| 221 | * @param lat The latitude
|
---|
| 222 | * @param lon The longitude
|
---|
| 223 | * @param zoom The current zoom level
|
---|
| 224 | * @return The bounds the OSM server would display
|
---|
| 225 | */
|
---|
[11302] | 226 | public static Bounds positionToBounds(final double lat, final double lon, final int zoom) {
|
---|
[12796] | 227 | final Dimension screenSize = mapSize.get();
|
---|
[11302] | 228 | double scale = (1 << zoom) * TILE_SIZE_IN_PIXELS / (2 * Math.PI * Ellipsoid.WGS84.a);
|
---|
| 229 | double deltaX = screenSize.getWidth() / 2.0 / scale;
|
---|
| 230 | double deltaY = screenSize.getHeight() / 2.0 / scale;
|
---|
[11299] | 231 | final Projection mercator = Projections.getProjectionByCode("EPSG:3857");
|
---|
| 232 | final EastNorth projected = mercator.latlon2eastNorth(new LatLon(lat, lon));
|
---|
[9230] | 233 | return new Bounds(
|
---|
[11299] | 234 | mercator.eastNorth2latlon(projected.add(-deltaX, -deltaY)),
|
---|
| 235 | mercator.eastNorth2latlon(projected.add(deltaX, deltaY)));
|
---|
[2540] | 236 | }
|
---|
| 237 |
|
---|
[5863] | 238 | /**
|
---|
| 239 | * Return OSM Zoom level for a given area
|
---|
| 240 | *
|
---|
| 241 | * @param b bounds of the area
|
---|
| 242 | * @return matching zoom level for area
|
---|
| 243 | */
|
---|
[6889] | 244 | public static int getZoom(Bounds b) {
|
---|
[11302] | 245 | final Projection mercator = Projections.getProjectionByCode("EPSG:3857");
|
---|
| 246 | final EastNorth min = mercator.latlon2eastNorth(b.getMin());
|
---|
| 247 | final EastNorth max = mercator.latlon2eastNorth(b.getMax());
|
---|
| 248 | final double deltaX = max.getX() - min.getX();
|
---|
[12796] | 249 | final double scale = mapSize.get().getWidth() / deltaX;
|
---|
[11302] | 250 | final double x = scale * (2 * Math.PI * Ellipsoid.WGS84.a) / TILE_SIZE_IN_PIXELS;
|
---|
| 251 | return (int) Math.round(Math.log(x) / Math.log(2));
|
---|
[1722] | 252 | }
|
---|
| 253 |
|
---|
[5863] | 254 | /**
|
---|
[6453] | 255 | * Return OSM URL for given area.
|
---|
[5863] | 256 | *
|
---|
| 257 | * @param b bounds of the area
|
---|
| 258 | * @return link to display that area in OSM map
|
---|
| 259 | */
|
---|
[6453] | 260 | public static String getURL(Bounds b) {
|
---|
[1724] | 261 | return getURL(b.getCenter(), getZoom(b));
|
---|
[1722] | 262 | }
|
---|
| 263 |
|
---|
[5863] | 264 | /**
|
---|
[6453] | 265 | * Return OSM URL for given position and zoom.
|
---|
[5863] | 266 | *
|
---|
| 267 | * @param pos center position of area
|
---|
| 268 | * @param zoom zoom depth of display
|
---|
| 269 | * @return link to display that area in OSM map
|
---|
| 270 | */
|
---|
[6453] | 271 | public static String getURL(LatLon pos, int zoom) {
|
---|
| 272 | return getURL(pos.lat(), pos.lon(), zoom);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /**
|
---|
| 276 | * Return OSM URL for given lat/lon and zoom.
|
---|
| 277 | *
|
---|
| 278 | * @param dlat center latitude of area
|
---|
| 279 | * @param dlon center longitude of area
|
---|
| 280 | * @param zoom zoom depth of display
|
---|
| 281 | * @return link to display that area in OSM map
|
---|
[6830] | 282 | *
|
---|
[6453] | 283 | * @since 6453
|
---|
| 284 | */
|
---|
| 285 | public static String getURL(double dlat, double dlon, int zoom) {
|
---|
[1722] | 286 | // Truncate lat and lon to something more sensible
|
---|
[8364] | 287 | int decimals = (int) Math.pow(10, zoom / 3d);
|
---|
[8345] | 288 | double lat = Math.round(dlat * decimals);
|
---|
[1740] | 289 | lat /= decimals;
|
---|
[8345] | 290 | double lon = Math.round(dlon * decimals);
|
---|
[1740] | 291 | lon /= decimals;
|
---|
[8846] | 292 | return Main.getOSMWebsite() + "/#map="+zoom+'/'+lat+'/'+lon;
|
---|
[1722] | 293 | }
|
---|
[1307] | 294 | }
|
---|