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

Last change on this file since 2001 was 1797, checked in by framm, 15 years ago
  • minor changes in conjunction with the Walking Papers plugin.
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
11 public static Bounds parse(String url) {
12 int i = url.indexOf('?');
13 if (i == -1)
14 return null;
15 String[] args = url.substring(i+1).split("&");
16 HashMap<String, String> map = new HashMap<String, String>();
17 for (String arg : args) {
18 int eq = arg.indexOf('=');
19 if (eq != -1) {
20 map.put(arg.substring(0, eq), arg.substring(eq + 1));
21 }
22 }
23
24 Bounds b = null;
25 try {
26 if (map.containsKey("bbox")) {
27 String bbox[] = map.get("bbox").split(",");
28 b = new Bounds(
29 new LatLon(Double.parseDouble(bbox[1]), Double.parseDouble(bbox[0])),
30 new LatLon(Double.parseDouble(bbox[3]), Double.parseDouble(bbox[2])));
31 } else if (map.containsKey("minlat")) {
32 String s = map.get("minlat");
33 Double minlat = Double.parseDouble(s);
34 s = map.get("minlon");
35 Double minlon = Double.parseDouble(s);
36 s = map.get("maxlat");
37 Double maxlat = Double.parseDouble(s);
38 s = map.get("maxlon");
39 Double maxlon = Double.parseDouble(s);
40 b = new Bounds(new LatLon(minlat, minlon), new LatLon(maxlat, maxlon));
41 } else {
42 double size = 180.0 / Math.pow(2, Integer.parseInt(map.get("zoom")));
43 b = new Bounds(
44 new LatLon(parseDouble(map, "lat") - size/2, parseDouble(map, "lon") - size),
45 new LatLon(parseDouble(map, "lat") + size/2, parseDouble(map, "lon") + size));
46 }
47 } catch (NumberFormatException x) {
48 } catch (NullPointerException x) {
49 }
50 return b;
51 }
52
53 private static double parseDouble(HashMap<String, String> map, String key) {
54 if (map.containsKey(key))
55 return Double.parseDouble(map.get(key));
56 return Double.parseDouble(map.get("m"+key));
57 }
58
59 static public int getZoom(Bounds b) {
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 return getURL(b.getCenter(), getZoom(b));
76 }
77
78 static public String getURL(LatLon pos, int zoom) {
79 // Truncate lat and lon to something more sensible
80 int decimals = (int) Math.pow(10, (zoom / 3));
81 double lat = (Math.round(pos.lat() * decimals));
82 lat /= decimals;
83 double lon = (Math.round(pos.lon() * decimals));
84 lon /= 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.