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