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

Last change on this file since 4736 was 4736, checked in by bastiK, 12 years ago

fixed #7198 - latitude problem for south-west areas

  • Property svn:eol-style set to native
File size: 7.8 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 b = positionToBounds(parseDouble(map, "lat"),
59 parseDouble(map, "lon"),
60 Integer.parseInt(map.get("zoom")));
61 }
62 } catch (NumberFormatException x) {
63 x.printStackTrace();
64 } catch (NullPointerException x) {
65 x.printStackTrace();
66 } catch (ArrayIndexOutOfBoundsException x) {
67 x.printStackTrace();
68 }
69 return b;
70 }
71
72 private static double parseDouble(HashMap<String, String> map, String key) {
73 if (map.containsKey(key))
74 return Double.parseDouble(map.get(key));
75 return Double.parseDouble(map.get("m"+key));
76 }
77
78 private static final char[] SHORTLINK_CHARS = {
79 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
80 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
81 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
82 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
83 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
84 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
85 'w', 'x', 'y', 'z', '0', '1', '2', '3',
86 '4', '5', '6', '7', '8', '9', '_', '@'
87 };
88
89 /**
90 * p
91 *
92 * @param url string for parsing
93 *
94 * @return Bounds if shortlink, null otherwise
95 *
96 * @see http://trac.openstreetmap.org/browser/sites/rails_port/lib/short_link.rb
97 */
98 private static Bounds parseShortLink(final String url) {
99 if (!url.startsWith(SHORTLINK_PREFIX))
100 return null;
101 final String shortLink = url.substring(SHORTLINK_PREFIX.length());
102
103 final Map<Character, Integer> array = new HashMap<Character, Integer>();
104
105 for (int i=0; i<SHORTLINK_CHARS.length; ++i) {
106 array.put(SHORTLINK_CHARS[i], i);
107 }
108
109 // long is necessary (need 32 bit positive value is needed)
110 long x = 0;
111 long y = 0;
112 int zoom = 0;
113 int zoomOffset = 0;
114
115 for (final char ch : shortLink.toCharArray()) {
116 if (array.containsKey(ch)) {
117 int val = array.get(ch);
118 for (int i=0; i<3; ++i) {
119 x <<= 1;
120 if ((val & 32) != 0) {
121 x |= 1;
122 }
123 val <<= 1;
124
125 y <<= 1;
126 if ((val & 32) != 0) {
127 y |= 1;
128 }
129 val <<= 1;
130 }
131 zoom += 3;
132 } else {
133 zoomOffset--;
134 }
135 }
136
137 x <<= 32 - zoom;
138 y <<= 32 - zoom;
139
140 // 2**32 == 4294967296
141 return positionToBounds(y * 180.0 / 4294967296.0 - 90.0,
142 x * 360.0 / 4294967296.0 - 180.0,
143 // TODO: -2 was not in ruby code
144 zoom - 8 - (zoomOffset % 3) - 2);
145 }
146
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.map != null && Main.map.mapView != null) {
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 static public int getZoom(Bounds b) {
187 // convert to mercator (for calculation of zoom only)
188 double latMin = Math.log(Math.tan(Math.PI/4.0+b.getMin().lat()/180.0*Math.PI/2.0))*180.0/Math.PI;
189 double latMax = Math.log(Math.tan(Math.PI/4.0+b.getMax().lat()/180.0*Math.PI/2.0))*180.0/Math.PI;
190 double size = Math.max(Math.abs(latMax-latMin), Math.abs(b.getMax().lon()-b.getMin().lon()));
191 int zoom = 0;
192 while (zoom <= 20) {
193 if (size >= 180) {
194 break;
195 }
196 size *= 2;
197 zoom++;
198 }
199 return zoom;
200 }
201
202 static public String getURL(Bounds b) {
203 return getURL(b.getCenter(), getZoom(b));
204 }
205
206 static public String getURL(LatLon pos, int zoom) {
207 // Truncate lat and lon to something more sensible
208 int decimals = (int) Math.pow(10, (zoom / 3));
209 double lat = (Math.round(pos.lat() * decimals));
210 lat /= decimals;
211 double lon = (Math.round(pos.lon() * decimals));
212 lon /= decimals;
213 return "http://www.openstreetmap.org/?lat="+lat+"&lon="+lon+"&zoom="+zoom;
214 }
215}
Note: See TracBrowser for help on using the repository browser.