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

Last change on this file since 1728 was 1724, checked in by stoecker, 15 years ago

some more changes and bug fixes related to new projection stuff - GPX should now work also

File size: 3.3 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import java.util.HashMap;
5
6import org.openstreetmap.josm.data.Bounds;
7import org.openstreetmap.josm.data.coor.LatLon;
8
9public class OsmUrlToBounds {
10 public static Bounds parse(String url) {
11 int i = url.indexOf('?');
12 if (i == -1)
13 return null;
14 String[] args = url.substring(i+1).split("&");
15 HashMap<String, String> map = new HashMap<String, String>();
16 for (String arg : args) {
17 int eq = arg.indexOf('=');
18 if (eq != -1) {
19 map.put(arg.substring(0, eq), arg.substring(eq + 1));
20 }
21 }
22
23 Bounds b = null;
24 try {
25 if (map.containsKey("bbox")) {
26 String bbox[] = map.get("bbox").split(",");
27 b = new Bounds(
28 new LatLon(Double.parseDouble(bbox[1]), Double.parseDouble(bbox[0])),
29 new LatLon(Double.parseDouble(bbox[3]), Double.parseDouble(bbox[2])));
30 } else if (map.containsKey("minlat")) {
31 String s = map.get("minlat");
32 Double minlat = Double.parseDouble(s);
33 s = map.get("minlon");
34 Double minlon = Double.parseDouble(s);
35 s = map.get("maxlat");
36 Double maxlat = Double.parseDouble(s);
37 s = map.get("maxlon");
38 Double maxlon = Double.parseDouble(s);
39 b = new Bounds(new LatLon(minlat, minlon), new LatLon(maxlat, maxlon));
40 } else {
41 double size = 180.0 / Math.pow(2, Integer.parseInt(map.get("zoom")));
42 b = new Bounds(
43 new LatLon(parseDouble(map, "lat") - size/2, parseDouble(map, "lon") - size),
44 new LatLon(parseDouble(map, "lat") + size/2, parseDouble(map, "lon") + size));
45 }
46 } catch (NumberFormatException x) {
47 } catch (NullPointerException x) {
48 }
49 return b;
50 }
51
52 private static double parseDouble(HashMap<String, String> map, String key) {
53 if (map.containsKey(key))
54 return Double.parseDouble(map.get(key));
55 return Double.parseDouble(map.get("m"+key));
56 }
57
58 static public int getZoom(Bounds b)
59 {
60 // convert to mercator (for calculation of zoom only)
61 double latMin = Math.log(Math.tan(Math.PI/4.0+b.min.lat()/180.0*Math.PI/2.0))*180.0/Math.PI;
62 double latMax = Math.log(Math.tan(Math.PI/4.0+b.max.lat()/180.0*Math.PI/2.0))*180.0/Math.PI;
63 double size = Math.max(Math.abs(latMax-latMin), Math.abs(b.max.lon()-b.min.lon()));
64 int zoom = 0;
65 while (zoom <= 20) {
66 if (size >= 180)
67 break;
68 size *= 2;
69 zoom++;
70 }
71 return zoom;
72 }
73
74 static public String getURL(Bounds b)
75 {
76 return getURL(b.getCenter(), getZoom(b));
77 }
78
79 static public String getURL(LatLon pos, int zoom)
80 {
81 // Truncate lat and lon to something more sensible
82 int decimals = (int) Math.pow(10, (zoom / 3));
83 double lat = (Math.round(pos.lat() * decimals))/decimals;
84 double lon = (Math.round(pos.lon() * decimals))/decimals;
85 return new String("http://www.openstreetmap.org/?lat="+lat+"&lon="+lon+"&zoom="+zoom);
86 }
87}
Note: See TracBrowser for help on using the repository browser.