source: josm/trunk/src/org/openstreetmap/josm/tools/OsmUrlToBounds.java@ 6113

Last change on this file since 6113 was 6085, checked in by Don-vip, 11 years ago

see #8902 - c-like array definitions changed to java-like (patch by shinigami)

  • Property svn:eol-style set to native
File size: 8.4 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import java.awt.Toolkit;
5import java.io.UnsupportedEncodingException;
6import java.net.URLDecoder;
7import java.util.HashMap;
8import java.util.Map;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.Bounds;
12import org.openstreetmap.josm.data.coor.LatLon;
13
14public class OsmUrlToBounds {
15 private static final String SHORTLINK_PREFIX = "http://osm.org/go/";
16
17 public static Bounds parse(String url) {
18 try {
19 // a percent sign indicates an encoded URL (RFC 1738).
20 if (url.contains("%")) {
21 url = URLDecoder.decode(url, "UTF-8");
22 }
23 } catch (UnsupportedEncodingException x) {
24 } catch (IllegalArgumentException x) {
25 }
26 Bounds b = parseShortLink(url);
27 if (b != null)
28 return b;
29 int i = url.indexOf('?');
30 if (i == -1)
31 return null;
32 String[] args = url.substring(i+1).split("&");
33 HashMap<String, String> map = new HashMap<String, String>();
34 for (String arg : args) {
35 int eq = arg.indexOf('=');
36 if (eq != -1) {
37 map.put(arg.substring(0, eq), arg.substring(eq + 1));
38 }
39 }
40
41 try {
42 if (map.containsKey("bbox")) {
43 String[] bbox = map.get("bbox").split(",");
44 b = new Bounds(
45 new LatLon(Double.parseDouble(bbox[1]), Double.parseDouble(bbox[0])),
46 new LatLon(Double.parseDouble(bbox[3]), Double.parseDouble(bbox[2])));
47 } else if (map.containsKey("minlat")) {
48 String s = map.get("minlat");
49 Double minlat = Double.parseDouble(s);
50 s = map.get("minlon");
51 Double minlon = Double.parseDouble(s);
52 s = map.get("maxlat");
53 Double maxlat = Double.parseDouble(s);
54 s = map.get("maxlon");
55 Double maxlon = Double.parseDouble(s);
56 b = new Bounds(new LatLon(minlat, minlon), new LatLon(maxlat, maxlon));
57 } else {
58 String z = map.get("zoom");
59 b = positionToBounds(parseDouble(map, "lat"),
60 parseDouble(map, "lon"),
61 z == null ? 18 : Integer.parseInt(z));
62 }
63 } catch (NumberFormatException x) {
64 x.printStackTrace();
65 } catch (NullPointerException x) {
66 x.printStackTrace();
67 } catch (ArrayIndexOutOfBoundsException x) {
68 x.printStackTrace();
69 }
70 return b;
71 }
72
73 private static double parseDouble(HashMap<String, String> map, String key) {
74 if (map.containsKey(key))
75 return Double.parseDouble(map.get(key));
76 return Double.parseDouble(map.get("m"+key));
77 }
78
79 private static final char[] SHORTLINK_CHARS = {
80 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
81 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
82 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
83 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
84 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
85 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
86 'w', 'x', 'y', 'z', '0', '1', '2', '3',
87 '4', '5', '6', '7', '8', '9', '_', '@'
88 };
89
90 /**
91 * Parse OSM short link
92 *
93 * @param url string for parsing
94 * @return Bounds if shortlink, null otherwise
95 * @see <a href="http://trac.openstreetmap.org/browser/sites/rails_port/lib/short_link.rb">short_link.rb</a>
96 */
97 private static Bounds parseShortLink(final String url) {
98 if (!url.startsWith(SHORTLINK_PREFIX))
99 return null;
100 final String shortLink = url.substring(SHORTLINK_PREFIX.length());
101
102 final Map<Character, Integer> array = new HashMap<Character, Integer>();
103
104 for (int i=0; i<SHORTLINK_CHARS.length; ++i) {
105 array.put(SHORTLINK_CHARS[i], i);
106 }
107
108 // long is necessary (need 32 bit positive value is needed)
109 long x = 0;
110 long y = 0;
111 int zoom = 0;
112 int zoomOffset = 0;
113
114 for (final char ch : shortLink.toCharArray()) {
115 if (array.containsKey(ch)) {
116 int val = array.get(ch);
117 for (int i=0; i<3; ++i) {
118 x <<= 1;
119 if ((val & 32) != 0) {
120 x |= 1;
121 }
122 val <<= 1;
123
124 y <<= 1;
125 if ((val & 32) != 0) {
126 y |= 1;
127 }
128 val <<= 1;
129 }
130 zoom += 3;
131 } else {
132 zoomOffset--;
133 }
134 }
135
136 x <<= 32 - zoom;
137 y <<= 32 - zoom;
138
139 // 2**32 == 4294967296
140 return positionToBounds(y * 180.0 / 4294967296.0 - 90.0,
141 x * 360.0 / 4294967296.0 - 180.0,
142 // TODO: -2 was not in ruby code
143 zoom - 8 - (zoomOffset % 3) - 2);
144 }
145
146 /** radius of the earth */
147 public static final double R = 6378137.0;
148
149 public static Bounds positionToBounds(final double lat, final double lon, final int zoom) {
150 int tileSizeInPixels = 256;
151 int height = Toolkit.getDefaultToolkit().getScreenSize().height;
152 int width = Toolkit.getDefaultToolkit().getScreenSize().width;
153 if (Main.isDisplayingMapView()) {
154 height = Main.map.mapView.getHeight();
155 width = Main.map.mapView.getWidth();
156 }
157 double scale = (1 << zoom) * tileSizeInPixels / (2 * Math.PI * R);
158 double deltaX = width / 2.0 / scale;
159 double deltaY = height / 2.0 / scale;
160 double x = Math.toRadians(lon) * R;
161 double y = mercatorY(lat);
162 return new Bounds(invMercatorY(y - deltaY), Math.toDegrees(x - deltaX) / R, invMercatorY(y + deltaY), Math.toDegrees(x + deltaX) / R);
163 }
164
165 public static double mercatorY(double lat) {
166 return Math.log(Math.tan(Math.PI/4 + Math.toRadians(lat)/2)) * R;
167 }
168
169 public static double invMercatorY(double north) {
170 return Math.toDegrees(Math.atan(Math.sinh(north / R)));
171 }
172
173 public static Pair<Double, Double> getTileOfLatLon(double lat, double lon, double zoom) {
174 double x = Math.floor((lon + 180) / 360 * Math.pow(2.0, zoom));
175 double y = Math.floor((1 - Math.log(Math.tan(Math.toRadians(lat)) + 1 / Math.cos(Math.toRadians(lat))) / Math.PI)
176 / 2 * Math.pow(2.0, zoom));
177 return new Pair<Double, Double>(x, y);
178 }
179
180 public static LatLon getLatLonOfTile(double x, double y, double zoom) {
181 double lon = x / Math.pow(2.0, zoom) * 360.0 - 180;
182 double lat = Math.toDegrees(Math.atan(Math.sinh(Math.PI - (2.0 * Math.PI * y) / Math.pow(2.0, zoom))));
183 return new LatLon(lat, lon);
184 }
185
186 /**
187 * Return OSM Zoom level for a given area
188 *
189 * @param b bounds of the area
190 * @return matching zoom level for area
191 */
192 static public int getZoom(Bounds b) {
193 // convert to mercator (for calculation of zoom only)
194 double latMin = Math.log(Math.tan(Math.PI/4.0+b.getMin().lat()/180.0*Math.PI/2.0))*180.0/Math.PI;
195 double latMax = Math.log(Math.tan(Math.PI/4.0+b.getMax().lat()/180.0*Math.PI/2.0))*180.0/Math.PI;
196 double size = Math.max(Math.abs(latMax-latMin), Math.abs(b.getMax().lon()-b.getMin().lon()));
197 int zoom = 0;
198 while (zoom <= 20) {
199 if (size >= 180) {
200 break;
201 }
202 size *= 2;
203 zoom++;
204 }
205 return zoom;
206 }
207
208 /**
209 * Return OSM URL for given area
210 *
211 * @param b bounds of the area
212 * @return link to display that area in OSM map
213 */
214 static public String getURL(Bounds b) {
215 return getURL(b.getCenter(), getZoom(b));
216 }
217
218 /**
219 * Return OSM URL for given position and zoom
220 *
221 * @param pos center position of area
222 * @param zoom zoom depth of display
223 * @return link to display that area in OSM map
224 */
225 static public String getURL(LatLon pos, int zoom) {
226 // Truncate lat and lon to something more sensible
227 int decimals = (int) Math.pow(10, (zoom / 3));
228 double lat = (Math.round(pos.lat() * decimals));
229 lat /= decimals;
230 double lon = (Math.round(pos.lon() * decimals));
231 lon /= decimals;
232 return "http://www.openstreetmap.org/?lat="+lat+"&lon="+lon+"&zoom="+zoom;
233 }
234}
Note: See TracBrowser for help on using the repository browser.