1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.download;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.Font;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.event.FocusAdapter;
|
---|
11 | import java.awt.event.FocusEvent;
|
---|
12 | import java.awt.event.FocusListener;
|
---|
13 | import java.util.HashMap;
|
---|
14 |
|
---|
15 | import javax.swing.JLabel;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 | import javax.swing.JTextArea;
|
---|
18 | import javax.swing.JTextField;
|
---|
19 | import javax.swing.SwingUtilities;
|
---|
20 | import javax.swing.event.DocumentListener;
|
---|
21 | import javax.swing.event.DocumentEvent;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.data.Bounds;
|
---|
25 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
26 | import org.openstreetmap.josm.tools.GBC;
|
---|
27 | /**
|
---|
28 | * Bounding box selector.
|
---|
29 | *
|
---|
30 | * Provides max/min lat/lon input fields as well as the "URL from www.openstreetmap.org" text field.
|
---|
31 | *
|
---|
32 | * @author Frederik Ramm <frederik@remote.org>
|
---|
33 | *
|
---|
34 | */
|
---|
35 | public class BoundingBoxSelection implements DownloadSelection {
|
---|
36 |
|
---|
37 | private JTextField[] latlon = new JTextField[] {
|
---|
38 | new JTextField(11),
|
---|
39 | new JTextField(11),
|
---|
40 | new JTextField(11),
|
---|
41 | new JTextField(11) };
|
---|
42 | final JTextArea osmUrl = new JTextArea();
|
---|
43 | String oldUrl;
|
---|
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 | class osmUrlRefresher implements DocumentListener {
|
---|
81 | public void changedUpdate(DocumentEvent e) { dowork(); }
|
---|
82 | public void insertUpdate(DocumentEvent e) { dowork(); }
|
---|
83 | public void removeUpdate(DocumentEvent e) { dowork(); }
|
---|
84 | private void dowork() {
|
---|
85 | if(!oldUrl.equals(osmUrl.getText()))
|
---|
86 | {
|
---|
87 | Bounds b = osmurl2bounds(osmUrl.getText());
|
---|
88 | if (b != null) {
|
---|
89 | gui.minlon = b.min.lon();
|
---|
90 | gui.minlat = b.min.lat();
|
---|
91 | gui.maxlon = b.max.lon();
|
---|
92 | gui.maxlat = b.max.lat();
|
---|
93 | gui.boundingBoxChanged(BoundingBoxSelection.this);
|
---|
94 | updateBboxFields(gui);
|
---|
95 | updateSizeCheck(gui);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | };
|
---|
100 |
|
---|
101 | osmUrl.getDocument().addDocumentListener(new osmUrlRefresher());
|
---|
102 |
|
---|
103 | // select content on receiving focus. this seems to be the default in the
|
---|
104 | // windows look+feel but not for others. needs invokeLater to avoid strange
|
---|
105 | // side effects that will cancel out the newly made selection otherwise.
|
---|
106 | osmUrl.addFocusListener(new FocusAdapter() {
|
---|
107 | @Override public void focusGained(FocusEvent e) {
|
---|
108 | SwingUtilities.invokeLater(new Runnable() {
|
---|
109 | public void run() {
|
---|
110 | osmUrl.selectAll();
|
---|
111 | }
|
---|
112 | });
|
---|
113 | }
|
---|
114 | });
|
---|
115 | osmUrl.setLineWrap(true);
|
---|
116 | osmUrl.setBorder(latlon[0].getBorder());
|
---|
117 |
|
---|
118 | Font labelFont = sizeCheck.getFont();
|
---|
119 | sizeCheck.setFont(labelFont.deriveFont(Font.PLAIN, labelFont.getSize()));
|
---|
120 |
|
---|
121 | dlg.add(new JLabel(tr("min lat")), GBC.std().insets(10,20,5,0));
|
---|
122 | dlg.add(latlon[0], GBC.std().insets(0,20,0,0));
|
---|
123 | dlg.add(new JLabel(tr("min lon")), GBC.std().insets(10,20,5,0));
|
---|
124 | dlg.add(latlon[1], GBC.eol().insets(0,20,0,0));
|
---|
125 | dlg.add(new JLabel(tr("max lat")), GBC.std().insets(10,0,5,0));
|
---|
126 | dlg.add(latlon[2], GBC.std());
|
---|
127 | dlg.add(new JLabel(tr("max lon")), GBC.std().insets(10,0,5,0));
|
---|
128 | dlg.add(latlon[3], GBC.eol());
|
---|
129 |
|
---|
130 | dlg.add(new JLabel(tr("URL from www.openstreetmap.org")), GBC.eol().insets(10,20,5,0));
|
---|
131 | dlg.add(osmUrl, GBC.eop().insets(10,0,5,0).fill());
|
---|
132 | dlg.add(sizeCheck, GBC.eop().insets(10,0,5,20));
|
---|
133 |
|
---|
134 | gui.tabpane.addTab("Bounding Box", dlg);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Called when bounding box is changed by one of the other download dialog tabs.
|
---|
139 | */
|
---|
140 | public void boundingBoxChanged(DownloadDialog gui) {
|
---|
141 | updateBboxFields(gui);
|
---|
142 | updateUrl(gui);
|
---|
143 | updateSizeCheck(gui);
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void updateBboxFields(DownloadDialog gui) {
|
---|
147 | latlon[0].setText(Double.toString(gui.minlat));
|
---|
148 | latlon[1].setText(Double.toString(gui.minlon));
|
---|
149 | latlon[2].setText(Double.toString(gui.maxlat));
|
---|
150 | latlon[3].setText(Double.toString(gui.maxlon));
|
---|
151 | for (JTextField f : latlon)
|
---|
152 | f.setCaretPosition(0);
|
---|
153 | }
|
---|
154 |
|
---|
155 | private void updateUrl(DownloadDialog gui) {
|
---|
156 | double lat = (gui.minlat + gui.maxlat)/2;
|
---|
157 | double lon = (gui.minlon + gui.maxlon)/2;
|
---|
158 | // convert to mercator (for calculation of zoom only)
|
---|
159 | double latMin = Math.log(Math.tan(Math.PI/4.0+gui.minlat/180.0*Math.PI/2.0))*180.0/Math.PI;
|
---|
160 | double latMax = Math.log(Math.tan(Math.PI/4.0+gui.maxlat/180.0*Math.PI/2.0))*180.0/Math.PI;
|
---|
161 | double size = Math.max(Math.abs(latMax-latMin), Math.abs(gui.maxlon-gui.minlon));
|
---|
162 | int zoom = 0;
|
---|
163 | while (zoom <= 20) {
|
---|
164 | if (size >= 180)
|
---|
165 | break;
|
---|
166 | size *= 2;
|
---|
167 | zoom++;
|
---|
168 | }
|
---|
169 | // setting old URL prevents refresh based on this URL
|
---|
170 | oldUrl = "http://www.openstreetmap.org/index.html?mlat="+lat+"&mlon="+lon+"&zoom="+zoom;
|
---|
171 | osmUrl.setText(oldUrl);
|
---|
172 | }
|
---|
173 |
|
---|
174 | private void updateSizeCheck(DownloadDialog gui) {
|
---|
175 | double squareDegrees = (gui.maxlon-gui.minlon)*(gui.maxlat-gui.minlat);
|
---|
176 | double maxBboxSize = 0.25;
|
---|
177 | try {
|
---|
178 | Double.parseDouble(Main.pref.get("osm-server.max-request-area", "0.25"));
|
---|
179 | } catch (NumberFormatException nfe) {
|
---|
180 | maxBboxSize = 0.25;
|
---|
181 | }
|
---|
182 | if (squareDegrees > maxBboxSize) {
|
---|
183 | sizeCheck.setText(tr("Download area too large; will probably be rejected by server"));
|
---|
184 | sizeCheck.setForeground(Color.red);
|
---|
185 | } else {
|
---|
186 | sizeCheck.setText(tr("Download area ok, size probably acceptable to server"));
|
---|
187 | sizeCheck.setForeground(Color.darkGray);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | public static Bounds osmurl2bounds(String url) {
|
---|
192 | int i = url.indexOf('?');
|
---|
193 | if (i == -1)
|
---|
194 | return null;
|
---|
195 | String[] args = url.substring(i+1).split("&");
|
---|
196 | HashMap<String, String> map = new HashMap<String, String>();
|
---|
197 | for (String arg : args) {
|
---|
198 | int eq = arg.indexOf('=');
|
---|
199 | if (eq != -1) {
|
---|
200 | map.put(arg.substring(0, eq), arg.substring(eq + 1));
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | Bounds b = null;
|
---|
205 | try {
|
---|
206 | if (map.containsKey("bbox")) {
|
---|
207 | String bbox[] = map.get("bbox").split(",");
|
---|
208 | b = new Bounds(
|
---|
209 | new LatLon(Double.parseDouble(bbox[1]), Double.parseDouble(bbox[0])),
|
---|
210 | new LatLon(Double.parseDouble(bbox[3]), Double.parseDouble(bbox[2])));
|
---|
211 |
|
---|
212 | } else {
|
---|
213 | double size = 180.0 / Math.pow(2, Integer.parseInt(map.get("zoom")));
|
---|
214 | b = new Bounds(
|
---|
215 | new LatLon(parseDouble(map, "lat") - size/2, parseDouble(map, "lon") - size),
|
---|
216 | new LatLon(parseDouble(map, "lat") + size/2, parseDouble(map, "lon") + size));
|
---|
217 | }
|
---|
218 | } catch (NumberFormatException x) {
|
---|
219 | } catch (NullPointerException x) {
|
---|
220 | }
|
---|
221 | return b;
|
---|
222 | }
|
---|
223 |
|
---|
224 | private static double parseDouble(HashMap<String, String> map, String key) {
|
---|
225 | if (map.containsKey(key))
|
---|
226 | return Double.parseDouble(map.get(key));
|
---|
227 | return Double.parseDouble(map.get("m"+key));
|
---|
228 | }
|
---|
229 | }
|
---|