1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import java.awt.Dimension;
|
---|
4 | import java.awt.GridBagLayout;
|
---|
5 | import java.awt.GridLayout;
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.ActionListener;
|
---|
8 | import java.awt.event.InputEvent;
|
---|
9 | import java.awt.event.KeyAdapter;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.KeyListener;
|
---|
12 | import java.io.FileNotFoundException;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.Map;
|
---|
16 |
|
---|
17 | import javax.swing.DefaultListModel;
|
---|
18 | import javax.swing.JButton;
|
---|
19 | import javax.swing.JCheckBox;
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JScrollPane;
|
---|
24 | import javax.swing.JTextField;
|
---|
25 | import javax.swing.KeyStroke;
|
---|
26 | import javax.swing.SwingUtilities;
|
---|
27 | import javax.swing.event.ListSelectionEvent;
|
---|
28 | import javax.swing.event.ListSelectionListener;
|
---|
29 |
|
---|
30 | import org.jdom.JDOMException;
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.data.Bounds;
|
---|
33 | import org.openstreetmap.josm.data.GeoPoint;
|
---|
34 | import org.openstreetmap.josm.data.Preferences.PreferencesException;
|
---|
35 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
36 | import org.openstreetmap.josm.gui.BookmarkList;
|
---|
37 | import org.openstreetmap.josm.gui.GBC;
|
---|
38 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
39 | import org.openstreetmap.josm.gui.MapView;
|
---|
40 | import org.openstreetmap.josm.gui.WorldChooser;
|
---|
41 | import org.openstreetmap.josm.gui.BookmarkList.Bookmark;
|
---|
42 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
43 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
44 | import org.openstreetmap.josm.gui.layer.RawGpsDataLayer;
|
---|
45 | import org.openstreetmap.josm.io.OsmServerReader;
|
---|
46 | import org.xml.sax.SAXException;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Action that opens a connection to the osm server and download map data.
|
---|
50 | *
|
---|
51 | * An dialog is displayed asking the user to specify a rectangle to grab.
|
---|
52 | * The url and account settings from the preferences are used.
|
---|
53 | *
|
---|
54 | * @author imi
|
---|
55 | */
|
---|
56 | public class DownloadAction extends JosmAction {
|
---|
57 |
|
---|
58 | private enum DownloadStatus {FINISHED, REDISPLAY}
|
---|
59 |
|
---|
60 | JTextField[] latlon = new JTextField[]{
|
---|
61 | new JTextField(9),
|
---|
62 | new JTextField(9),
|
---|
63 | new JTextField(9),
|
---|
64 | new JTextField(9)};
|
---|
65 | JCheckBox rawGps = new JCheckBox("Open as raw gps data", false);
|
---|
66 |
|
---|
67 | public DownloadAction() {
|
---|
68 | super("Download from OSM", "download", "Download map data from the OSM server.", KeyEvent.VK_D,
|
---|
69 | KeyStroke.getAWTKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
|
---|
70 | }
|
---|
71 |
|
---|
72 | public void actionPerformed(ActionEvent e) {
|
---|
73 |
|
---|
74 | //TODO: Remove this in later versions (temporary only)
|
---|
75 | if (Main.pref.osmDataServer.endsWith("/0.2") || Main.pref.osmDataServer.endsWith("/0.2/")) {
|
---|
76 | int answer = JOptionPane.showConfirmDialog(Main.main,
|
---|
77 | "You seem to have an outdated server entry in your preferences.\n" +
|
---|
78 | "\n" +
|
---|
79 | "As of JOSM Release 1.2, you must no longer specify the API version in\n" +
|
---|
80 | "the osm url. For the OSM standard server, use http://www.openstreetmap.org/api" +
|
---|
81 | "\n" +
|
---|
82 | "Fix settings and continue?", "Outdated server url detected.", JOptionPane.YES_NO_OPTION);
|
---|
83 | if (answer != JOptionPane.YES_OPTION)
|
---|
84 | return;
|
---|
85 | int cutPos = Main.pref.osmDataServer.endsWith("/0.2") ? 4 : 5;
|
---|
86 | Main.pref.osmDataServer = Main.pref.osmDataServer.substring(0, Main.pref.osmDataServer.length()-cutPos);
|
---|
87 | try {
|
---|
88 | Main.pref.save();
|
---|
89 | } catch (PreferencesException x) {
|
---|
90 | x.printStackTrace();
|
---|
91 | JOptionPane.showMessageDialog(Main.main, "Could not save the preferences chane:\n" +
|
---|
92 | x.getMessage());
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | JPanel dlg = new JPanel(new GridBagLayout());
|
---|
97 |
|
---|
98 | // World image
|
---|
99 | WorldChooser wc = new WorldChooser();
|
---|
100 | dlg.add(wc, GBC.eop());
|
---|
101 | wc.setToolTipText("Move and zoom the image like the main map. Select an area to download by dragging.");
|
---|
102 |
|
---|
103 | // Bounding box edits
|
---|
104 | dlg.add(new JLabel("Bounding box"), GBC.eol());
|
---|
105 | dlg.add(new JLabel("min lat"), GBC.std().insets(10,0,5,0));
|
---|
106 | dlg.add(latlon[0], GBC.std());
|
---|
107 | dlg.add(new JLabel("min lon"), GBC.std().insets(10,0,5,0));
|
---|
108 | dlg.add(latlon[1], GBC.eol());
|
---|
109 | dlg.add(new JLabel("max lat"), GBC.std().insets(10,0,5,0));
|
---|
110 | dlg.add(latlon[2], GBC.std());
|
---|
111 | dlg.add(new JLabel("max lon"), GBC.std().insets(10,0,5,0));
|
---|
112 | dlg.add(latlon[3], GBC.eol());
|
---|
113 | if (Main.main.getMapFrame() != null) {
|
---|
114 | MapView mv = Main.main.getMapFrame().mapView;
|
---|
115 | setEditBounds(new Bounds(
|
---|
116 | mv.getPoint(0, mv.getHeight(), true),
|
---|
117 | mv.getPoint(mv.getWidth(), 0, true)));
|
---|
118 | rawGps.setSelected(mv.getActiveLayer() instanceof RawGpsDataLayer);
|
---|
119 | }
|
---|
120 | dlg.add(rawGps, GBC.eop());
|
---|
121 |
|
---|
122 | // OSM url edit
|
---|
123 | dlg.add(new JLabel("URL from www.openstreetmap.org"), GBC.eol());
|
---|
124 | final JTextField osmUrl = new JTextField();
|
---|
125 | dlg.add(osmUrl, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
126 | final KeyListener osmUrlRefresher = new KeyAdapter(){
|
---|
127 | @Override
|
---|
128 | public void keyTyped(KeyEvent e) {
|
---|
129 | SwingUtilities.invokeLater(new Runnable() {
|
---|
130 | public void run() {
|
---|
131 | try {
|
---|
132 | double latMin = Double.parseDouble(latlon[0].getText());
|
---|
133 | double lonMin = Double.parseDouble(latlon[1].getText());
|
---|
134 | double latMax = Double.parseDouble(latlon[2].getText());
|
---|
135 | double lonMax = Double.parseDouble(latlon[3].getText());
|
---|
136 | double lat = (latMax+latMin)/2;
|
---|
137 | double lon = (lonMax+lonMin)/2;
|
---|
138 | // convert to mercator (for calculation of zoom only)
|
---|
139 | latMin = Math.log(Math.tan(Math.PI/4.0+latMin/180.0*Math.PI/2.0))*180.0/Math.PI;
|
---|
140 | latMax = Math.log(Math.tan(Math.PI/4.0+latMax/180.0*Math.PI/2.0))*180.0/Math.PI;
|
---|
141 | double size = Math.max(Math.abs(latMax-latMin), Math.abs(lonMax-lonMin));
|
---|
142 | int zoom = 0;
|
---|
143 | while (zoom <= 20) {
|
---|
144 | if (size >= 180)
|
---|
145 | break;
|
---|
146 | size *= 2;
|
---|
147 | zoom++;
|
---|
148 | }
|
---|
149 | osmUrl.setText("http://www.openstreetmap.org/index.html?lat="+lat+"&lon="+lon+"&zoom="+zoom);
|
---|
150 | } catch (NumberFormatException x) {
|
---|
151 | osmUrl.setText("");
|
---|
152 | }
|
---|
153 | osmUrl.setCaretPosition(0);
|
---|
154 | }
|
---|
155 | });
|
---|
156 | }
|
---|
157 | };
|
---|
158 | for (JTextField f : latlon)
|
---|
159 | f.addKeyListener(osmUrlRefresher);
|
---|
160 | SwingUtilities.invokeLater(new Runnable() {public void run() {osmUrlRefresher.keyTyped(null);}});
|
---|
161 | osmUrl.addKeyListener(new KeyAdapter(){
|
---|
162 | @Override
|
---|
163 | public void keyTyped(KeyEvent e) {
|
---|
164 | SwingUtilities.invokeLater(new Runnable() {
|
---|
165 | public void run() {
|
---|
166 | Map<String, Double> map = readArgs(osmUrl.getText());
|
---|
167 | try {
|
---|
168 | double size = 180.0 / Math.pow(2, map.get("zoom"));
|
---|
169 | Bounds b = new Bounds(
|
---|
170 | new GeoPoint(map.get("lat") - size/2, map.get("lon") - size),
|
---|
171 | new GeoPoint(map.get("lat") + size/2, map.get("lon") + size));
|
---|
172 | setEditBounds(b);
|
---|
173 | } catch (Exception x) { // NPE or IAE
|
---|
174 | for (JTextField f : latlon)
|
---|
175 | f.setText("");
|
---|
176 | }
|
---|
177 | }
|
---|
178 | });
|
---|
179 | }
|
---|
180 | });
|
---|
181 |
|
---|
182 | // Bookmarks
|
---|
183 | dlg.add(new JLabel("Bookmarks"), GBC.eol());
|
---|
184 | final BookmarkList bookmarks = new BookmarkList();
|
---|
185 | bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
|
---|
186 | public void valueChanged(ListSelectionEvent e) {
|
---|
187 | Bookmark b = (Bookmark)bookmarks.getSelectedValue();
|
---|
188 | for (int i = 0; i < 4; ++i) {
|
---|
189 | latlon[i].setText(b == null ? "" : ""+b.latlon[i]);
|
---|
190 | latlon[i].setCaretPosition(0);
|
---|
191 | }
|
---|
192 | rawGps.setSelected(b == null ? false : b.rawgps);
|
---|
193 | osmUrlRefresher.keyTyped(null);
|
---|
194 | }
|
---|
195 | });
|
---|
196 | wc.addListMarker(bookmarks);
|
---|
197 | dlg.add(new JScrollPane(bookmarks), GBC.eol().fill());
|
---|
198 |
|
---|
199 | JPanel buttons = new JPanel(new GridLayout(1,2));
|
---|
200 | JButton add = new JButton("Add");
|
---|
201 | add.addActionListener(new ActionListener(){
|
---|
202 | public void actionPerformed(ActionEvent e) {
|
---|
203 | Bookmark b = readBookmark();
|
---|
204 | if (b == null) {
|
---|
205 | JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates first.");
|
---|
206 | return;
|
---|
207 | }
|
---|
208 | b.name = JOptionPane.showInputDialog(Main.main, "Please enter a name for the location.");
|
---|
209 | if (!b.name.equals("")) {
|
---|
210 | ((DefaultListModel)bookmarks.getModel()).addElement(b);
|
---|
211 | bookmarks.save();
|
---|
212 | }
|
---|
213 | }
|
---|
214 | });
|
---|
215 | buttons.add(add);
|
---|
216 | JButton remove = new JButton("Remove");
|
---|
217 | remove.addActionListener(new ActionListener(){
|
---|
218 | public void actionPerformed(ActionEvent e) {
|
---|
219 | Object sel = bookmarks.getSelectedValue();
|
---|
220 | if (sel == null) {
|
---|
221 | JOptionPane.showMessageDialog(Main.main, "Select a bookmark first.");
|
---|
222 | return;
|
---|
223 | }
|
---|
224 | ((DefaultListModel)bookmarks.getModel()).removeElement(sel);
|
---|
225 | bookmarks.save();
|
---|
226 | }
|
---|
227 | });
|
---|
228 | buttons.add(remove);
|
---|
229 | dlg.add(buttons, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
230 |
|
---|
231 | Dimension d = dlg.getPreferredSize();
|
---|
232 | wc.setPreferredSize(new Dimension(d.width, d.width/2));
|
---|
233 | wc.addInputFields(latlon, osmUrl, osmUrlRefresher);
|
---|
234 |
|
---|
235 | // Finally: the dialog
|
---|
236 | while(true) {
|
---|
237 | int r = JOptionPane.showConfirmDialog(Main.main, dlg, "Choose an area",
|
---|
238 | JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
|
---|
239 | if (r != JOptionPane.OK_OPTION)
|
---|
240 | return;
|
---|
241 | if (startDownload() == DownloadStatus.FINISHED)
|
---|
242 | break;
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Read the values from the edit fields and start the download.
|
---|
248 | */
|
---|
249 | private DownloadStatus startDownload() {
|
---|
250 | Bookmark b = readBookmark();
|
---|
251 | if (b == null) {
|
---|
252 | JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates or click on a bookmark.");
|
---|
253 | return DownloadStatus.REDISPLAY;
|
---|
254 | }
|
---|
255 |
|
---|
256 | final OsmServerReader osmReader = new OsmServerReader(b.latlon[0], b.latlon[1], b.latlon[2], b.latlon[3]);
|
---|
257 | new Thread(new PleaseWaitRunnable("Downloading data"){
|
---|
258 | @Override
|
---|
259 | public void realRun() {
|
---|
260 | try {
|
---|
261 | String name = latlon[0].getText() + " "
|
---|
262 | + latlon[1].getText() + " x " + latlon[2].getText()
|
---|
263 | + " " + latlon[3].getText();
|
---|
264 |
|
---|
265 | Layer layer = null;
|
---|
266 | if (rawGps.isSelected()) {
|
---|
267 | layer = new RawGpsDataLayer(osmReader.parseRawGps(), name);
|
---|
268 | } else {
|
---|
269 | DataSet dataSet = osmReader.parseOsm();
|
---|
270 | if (dataSet == null)
|
---|
271 | return; // user cancelled download
|
---|
272 | if (dataSet.nodes.isEmpty()) {
|
---|
273 | closeDialog();
|
---|
274 | JOptionPane.showMessageDialog(Main.main,
|
---|
275 | "No data imported.");
|
---|
276 | }
|
---|
277 |
|
---|
278 | layer = new OsmDataLayer(dataSet, "Data Layer", false);
|
---|
279 | }
|
---|
280 |
|
---|
281 | if (Main.main.getMapFrame() == null)
|
---|
282 | Main.main.setMapFrame(new MapFrame(layer));
|
---|
283 | else
|
---|
284 | Main.main.getMapFrame().mapView.addLayer(layer);
|
---|
285 | } catch (SAXException x) {
|
---|
286 | closeDialog();
|
---|
287 | x.printStackTrace();
|
---|
288 | JOptionPane.showMessageDialog(Main.main, x.getMessage());
|
---|
289 | } catch (JDOMException x) {
|
---|
290 | closeDialog();
|
---|
291 | x.printStackTrace();
|
---|
292 | JOptionPane.showMessageDialog(Main.main, x.getMessage());
|
---|
293 | } catch (FileNotFoundException x) {
|
---|
294 | closeDialog();
|
---|
295 | x.printStackTrace();
|
---|
296 | JOptionPane.showMessageDialog(Main.main,
|
---|
297 | "URL nicht gefunden: " + x.getMessage());
|
---|
298 | } catch (IOException x) {
|
---|
299 | closeDialog();
|
---|
300 | x.printStackTrace();
|
---|
301 | JOptionPane.showMessageDialog(Main.main, x.getMessage());
|
---|
302 | }
|
---|
303 | }
|
---|
304 | }).start();
|
---|
305 | return DownloadStatus.FINISHED;
|
---|
306 | }
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Read a bookmark from the current set edit fields. If one of the fields is
|
---|
310 | * empty or contain illegal chars, <code>null</code> is returned.
|
---|
311 | * The name of the bookmark is <code>null</code>.
|
---|
312 | * @return A bookmark containing information from the edit fields and rawgps
|
---|
313 | * checkbox.
|
---|
314 | */
|
---|
315 | Bookmark readBookmark() {
|
---|
316 | try {
|
---|
317 | Bookmark b = new Bookmark();
|
---|
318 | for (int i = 0; i < 4; ++i) {
|
---|
319 | if (latlon[i].getText().equals(""))
|
---|
320 | return null;
|
---|
321 | b.latlon[i] = Double.parseDouble(latlon[i].getText());
|
---|
322 | }
|
---|
323 | b.rawgps = rawGps.isSelected();
|
---|
324 | return b;
|
---|
325 | } catch (NumberFormatException x) {
|
---|
326 | return null;
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Extrakt URL arguments.
|
---|
333 | */
|
---|
334 | private Map<String, Double> readArgs(String s) {
|
---|
335 | int i = s.indexOf('?');
|
---|
336 | if (i == -1)
|
---|
337 | return new HashMap<String, Double>();
|
---|
338 | String[] args = s.substring(i+1).split("&");
|
---|
339 | HashMap<String, Double> map = new HashMap<String, Double>();
|
---|
340 | for (String arg : args) {
|
---|
341 | int eq = arg.indexOf('=');
|
---|
342 | if (eq != -1) {
|
---|
343 | try {
|
---|
344 | map.put(arg.substring(0, eq), Double.parseDouble(arg.substring(eq + 1)));
|
---|
345 | } catch (NumberFormatException e) {
|
---|
346 | }
|
---|
347 | }
|
---|
348 | }
|
---|
349 | return map;
|
---|
350 | }
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Set the four edit fields to the given bounds coordinates.
|
---|
354 | */
|
---|
355 | private void setEditBounds(Bounds b) {
|
---|
356 | GeoPoint bottomLeft = b.min;
|
---|
357 | GeoPoint topRight = b.max;
|
---|
358 | if (bottomLeft.isOutSideWorld())
|
---|
359 | bottomLeft = new GeoPoint(-89.999, -179.999); // do not use the Projection constants, since this looks better.
|
---|
360 | if (topRight.isOutSideWorld())
|
---|
361 | topRight = new GeoPoint(89.999, 179.999);
|
---|
362 | latlon[0].setText(""+bottomLeft.lat);
|
---|
363 | latlon[1].setText(""+bottomLeft.lon);
|
---|
364 | latlon[2].setText(""+topRight.lat);
|
---|
365 | latlon[3].setText(""+topRight.lon);
|
---|
366 | for (JTextField f : latlon)
|
---|
367 | f.setCaretPosition(0);
|
---|
368 | }
|
---|
369 | }
|
---|