source: josm/src/org/openstreetmap/josm/gui/download/BoundingBoxSelection.java@ 298

Last change on this file since 298 was 298, checked in by imi, 17 years ago
  • added license description to head of each source file
File size: 6.9 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.download;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Color;
7import java.awt.Dimension;
8import java.awt.Font;
9import java.awt.GridBagLayout;
10import java.awt.event.FocusAdapter;
11import java.awt.event.FocusEvent;
12import java.awt.event.FocusListener;
13import java.awt.event.KeyAdapter;
14import java.awt.event.KeyEvent;
15import java.awt.event.KeyListener;
16import java.util.HashMap;
17
18import javax.swing.JLabel;
19import javax.swing.JPanel;
20import javax.swing.JTextArea;
21import javax.swing.JTextField;
22import javax.swing.SwingUtilities;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.data.Bounds;
26import org.openstreetmap.josm.data.coor.LatLon;
27import org.openstreetmap.josm.tools.GBC;
28/**
29 * Bounding box selector.
30 *
31 * Provides max/min lat/lon input fields as well as the "URL from www.openstreetmap.org" text field.
32 *
33 * @author Frederik Ramm <frederik@remote.org>
34 *
35 */
36public class BoundingBoxSelection implements DownloadSelection {
37
38 private JTextField[] latlon = new JTextField[] {
39 new JTextField(11),
40 new JTextField(11),
41 new JTextField(11),
42 new JTextField(11) };
43 final JTextArea osmUrl = new JTextArea();
44
45 final JLabel sizeCheck = new JLabel();
46
47 public void addGui(final DownloadDialog gui) {
48
49 JPanel dlg = new JPanel(new GridBagLayout());
50 osmUrl.setText(tr("You can paste an URL here to download the area."));
51
52 final FocusListener dialogUpdater = new FocusAdapter() {
53 @Override public void focusLost(FocusEvent e) {
54 SwingUtilities.invokeLater(new Runnable() {
55 public void run() {
56 try {
57 double minlat = Double.parseDouble(latlon[0].getText());
58 double minlon = Double.parseDouble(latlon[1].getText());
59 double maxlat = Double.parseDouble(latlon[2].getText());
60 double maxlon = Double.parseDouble(latlon[3].getText());
61 if (minlat != gui.minlat || minlon != gui.minlon || maxlat != gui.maxlat || maxlon != gui.maxlon) {
62 gui.minlat = minlat; gui.minlon = minlon;
63 gui.maxlat = maxlat; gui.maxlon = maxlon;
64 gui.boundingBoxChanged(BoundingBoxSelection.this);
65 }
66 } catch (NumberFormatException x) {
67 // ignore
68 }
69 updateUrl(gui);
70 updateSizeCheck(gui);
71 }
72 });
73 }
74 };
75
76 for (JTextField f : latlon) {
77 f.setMinimumSize(new Dimension(100,new JTextField().getMinimumSize().height));
78 f.addFocusListener(dialogUpdater);
79 }
80
81 final KeyListener osmUrlRefresher = new KeyAdapter() {
82 @Override public void keyTyped(KeyEvent e) {
83 SwingUtilities.invokeLater(new Runnable() {
84 public void run() {
85 Bounds b = osmurl2bounds(osmUrl.getText());
86 if (b != null) {
87 gui.minlon = b.min.lon();
88 gui.minlat = b.min.lat();
89 gui.maxlon = b.max.lon();
90 gui.maxlat = b.max.lat();
91 gui.boundingBoxChanged(BoundingBoxSelection.this);
92 updateBboxFields(gui);
93 updateSizeCheck(gui);
94 }
95 }
96 });
97 }
98 };
99
100 osmUrl.addKeyListener(osmUrlRefresher);
101 osmUrl.setLineWrap(true);
102 osmUrl.setBorder(latlon[0].getBorder());
103
104 Font labelFont = sizeCheck.getFont();
105 sizeCheck.setFont(labelFont.deriveFont(Font.PLAIN, labelFont.getSize()));
106
107 dlg.add(new JLabel(tr("min lat")), GBC.std().insets(10,20,5,0));
108 dlg.add(latlon[0], GBC.std().insets(0,20,0,0));
109 dlg.add(new JLabel(tr("min lon")), GBC.std().insets(10,20,5,0));
110 dlg.add(latlon[1], GBC.eol().insets(0,20,0,0));
111 dlg.add(new JLabel(tr("max lat")), GBC.std().insets(10,0,5,0));
112 dlg.add(latlon[2], GBC.std());
113 dlg.add(new JLabel(tr("max lon")), GBC.std().insets(10,0,5,0));
114 dlg.add(latlon[3], GBC.eol());
115
116 dlg.add(new JLabel(tr("URL from www.openstreetmap.org")), GBC.eol().insets(10,20,5,0));
117 dlg.add(osmUrl, GBC.eop().insets(10,0,5,0).fill());
118 dlg.add(sizeCheck, GBC.eop().insets(10,0,5,20));
119
120 gui.tabpane.addTab("Bounding Box", dlg);
121 }
122
123 /**
124 * Called when bounding box is changed by one of the other download dialog tabs.
125 */
126 public void boundingBoxChanged(DownloadDialog gui) {
127 updateBboxFields(gui);
128 updateUrl(gui);
129 updateSizeCheck(gui);
130 }
131
132 private void updateBboxFields(DownloadDialog gui) {
133 latlon[0].setText(Double.toString(gui.minlat));
134 latlon[1].setText(Double.toString(gui.minlon));
135 latlon[2].setText(Double.toString(gui.maxlat));
136 latlon[3].setText(Double.toString(gui.maxlon));
137 for (JTextField f : latlon)
138 f.setCaretPosition(0);
139 }
140
141 private void updateUrl(DownloadDialog gui) {
142 double lat = (gui.minlat + gui.maxlat)/2;
143 double lon = (gui.minlon + gui.maxlon)/2;
144 // convert to mercator (for calculation of zoom only)
145 double latMin = Math.log(Math.tan(Math.PI/4.0+gui.minlat/180.0*Math.PI/2.0))*180.0/Math.PI;
146 double latMax = Math.log(Math.tan(Math.PI/4.0+gui.maxlat/180.0*Math.PI/2.0))*180.0/Math.PI;
147 double size = Math.max(Math.abs(latMax-latMin), Math.abs(gui.maxlon-gui.minlon));
148 int zoom = 0;
149 while (zoom <= 20) {
150 if (size >= 180)
151 break;
152 size *= 2;
153 zoom++;
154 }
155 osmUrl.setText("http://www.openstreetmap.org/index.html?mlat="+lat+"&mlon="+lon+"&zoom="+zoom);
156 }
157
158 private void updateSizeCheck(DownloadDialog gui) {
159 double squareDegrees = (gui.maxlon-gui.minlon)*(gui.maxlat-gui.minlat);
160 double maxBboxSize = 0.25;
161 try {
162 Double.parseDouble(Main.pref.get("osm-server.max-request-area", "0.25"));
163 } catch (NumberFormatException nfe) {
164 maxBboxSize = 0.25;
165 }
166 if (squareDegrees > maxBboxSize) {
167 sizeCheck.setText(tr("Download area too large; will probably be rejected by server"));
168 sizeCheck.setForeground(Color.red);
169 } else {
170 sizeCheck.setText(tr("Download area ok, size probably acceptable to server"));
171 sizeCheck.setForeground(Color.darkGray);
172 }
173 }
174
175 public static Bounds osmurl2bounds(String url) {
176 int i = url.indexOf('?');
177 if (i == -1)
178 return null;
179 String[] args = url.substring(i+1).split("&");
180 HashMap<String, String> map = new HashMap<String, String>();
181 for (String arg : args) {
182 int eq = arg.indexOf('=');
183 if (eq != -1) {
184 map.put(arg.substring(0, eq), arg.substring(eq + 1));
185 }
186 }
187
188 Bounds b = null;
189 try {
190 if (map.containsKey("bbox")) {
191 String bbox[] = map.get("bbox").split(",");
192 b = new Bounds(
193 new LatLon(Double.parseDouble(bbox[1]), Double.parseDouble(bbox[0])),
194 new LatLon(Double.parseDouble(bbox[3]), Double.parseDouble(bbox[2])));
195
196 } else {
197 double size = 180.0 / Math.pow(2, Integer.parseInt(map.get("zoom")));
198 b = new Bounds(
199 new LatLon(parseDouble(map, "lat") - size/2, parseDouble(map, "lon") - size),
200 new LatLon(parseDouble(map, "lat") + size/2, parseDouble(map, "lon") + size));
201 }
202 } catch (NumberFormatException x) {
203 } catch (NullPointerException x) {
204 }
205 return b;
206 }
207
208 private static double parseDouble(HashMap<String, String> map, String key) {
209 if (map.containsKey(key))
210 return Double.parseDouble(map.get(key));
211 return Double.parseDouble(map.get("m"+key));
212 }
213}
Note: See TracBrowser for help on using the repository browser.