source: osm/applications/editors/josm/plugins/imagery_offset_db/src/iodb/GetImageryOffsetAction.java@ 28008

Last change on this file since 28008 was 28008, checked in by zverik, 12 years ago

Something works :)

File size: 4.9 KB
Line 
1package iodb;
2
3import java.awt.event.ActionEvent;
4import java.awt.event.KeyEvent;
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.UnsupportedEncodingException;
8import java.net.*;
9import java.util.ArrayList;
10import java.util.Collections;
11import java.util.HashMap;
12import java.util.List;
13import java.util.concurrent.Future;
14import java.util.logging.Level;
15import java.util.logging.Logger;
16import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.actions.JosmAction;
19import org.openstreetmap.josm.data.coor.LatLon;
20import org.openstreetmap.josm.data.projection.Projection;
21import org.openstreetmap.josm.gui.MapView;
22import org.openstreetmap.josm.gui.PleaseWaitRunnable;
23import org.openstreetmap.josm.gui.layer.ImageryLayer;
24import org.openstreetmap.josm.gui.progress.ProgressMonitor;
25import org.openstreetmap.josm.io.OsmTransferException;
26import static org.openstreetmap.josm.tools.I18n.tr;
27import org.openstreetmap.josm.tools.Shortcut;
28import org.xml.sax.SAXException;
29
30/**
31 * Download a list of imagery offsets for the current position, let user choose which one to use.
32 *
33 * @author zverik
34 */
35public class GetImageryOffsetAction extends JosmAction {
36
37 private List<ImageryOffsetBase> offsets;
38
39 public GetImageryOffsetAction() {
40 super(tr("Get Imagery Offset..."), "getoffset", tr("Download offsets for current imagery from a server"),
41 Shortcut.registerShortcut("imageryoffset:get", tr("Imagery: {0}", tr("Get Imagery Offset...")), KeyEvent.VK_I, Shortcut.ALT+Shortcut.CTRL), true);
42 offsets = Collections.emptyList();
43 }
44
45 public void actionPerformed(ActionEvent e) {
46 Projection proj = Main.map.mapView.getProjection();
47 LatLon center = proj.eastNorth2latlon(Main.map.mapView.getCenter());
48 ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer();
49 String imagery = ImageryOffsetTools.getImageryID(layer);
50 if( imagery == null )
51 return;
52
53 List<ImageryOffsetBase> offsets = download(center, imagery); // todo: async
54 /*DownloadOffsets download = new DownloadOffsets();
55 Future<?> future = Main.worker.submit(download);
56 try {
57 future.get();
58 } catch( Exception ex ) {
59 ex.printStackTrace();
60 return;
61 }*/
62
63 // todo: show a dialog for selecting one of the offsets (without "update" flag)
64 ImageryOffsetBase offset = new OffsetDialog(offsets).showDialog();
65 if( offset != null ) {
66 // todo: use the chosen offset
67 if( offset instanceof ImageryOffset ) {
68 ImageryOffsetTools.applyLayerOffset(layer, (ImageryOffset)offset);
69 } else if( offset instanceof CalibrationObject ) {
70 // todo: select object
71 }
72 }
73 }
74
75 private List<ImageryOffsetBase> download( LatLon center, String imagery ) {
76 String base = Main.pref.get("iodb.server.url", "http://offsets.textual.ru/");
77 String query = "get?lat=" + center.getX() + "&lon=" + center.getY();
78 List<ImageryOffsetBase> result = null;
79 try {
80 query = query + "&imagery=" + URLEncoder.encode(imagery, "utf-8");
81 URL url = new URL(base + query);
82 System.out.println("url=" + url);
83 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
84 connection.connect();
85 int retCode = connection.getResponseCode();
86 InputStream inp = connection.getInputStream();
87 if( inp != null ) {
88 result = new IODBReader(inp).parse();
89 System.out.println("result.size() = " + result.size());
90 }
91 connection.disconnect();
92 } catch( MalformedURLException ex ) {
93 // ?
94 } catch( UnsupportedEncodingException e ) {
95 // do nothing. WTF is that?
96 } catch( IOException e ) {
97 e.printStackTrace();
98 // ?
99 } catch( SAXException e ) {
100 e.printStackTrace();
101 // ?
102 }
103 if( result == null )
104 result = new ArrayList<ImageryOffsetBase>();
105 return result;
106 }
107
108 class DownloadOffsets extends PleaseWaitRunnable {
109
110 private boolean cancelled;
111
112 public DownloadOffsets() {
113 super(tr("Downloading calibration data"));
114 cancelled = false;
115 }
116
117 @Override
118 protected void realRun() throws SAXException, IOException, OsmTransferException {
119 // todo: open httpconnection to server and read xml
120 if( cancelled )
121 return;
122
123 }
124
125 @Override
126 protected void finish() {
127 if( cancelled )
128 return;
129 // todo: parse xml and return an array of ImageryOffsetBase
130 }
131
132 @Override
133 protected void cancel() {
134 cancelled = true;
135 }
136 }
137}
Note: See TracBrowser for help on using the repository browser.