Changeset 29371 in osm for applications/editors/josm/plugins/imagery_offset_db/src/iodb/GetImageryOffsetAction.java
- Timestamp:
- 2013-03-18T21:58:17+01:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/imagery_offset_db/src/iodb/GetImageryOffsetAction.java
r28008 r29371 3 3 import java.awt.event.ActionEvent; 4 4 import java.awt.event.KeyEvent; 5 import java.io.IOException;6 5 import java.io.InputStream; 7 6 import java.io.UnsupportedEncodingException; 8 7 import java.net.*; 9 import java.util.ArrayList; 10 import java.util.Collections; 11 import java.util.HashMap; 12 import java.util.List; 13 import java.util.concurrent.Future; 14 import java.util.logging.Level; 15 import java.util.logging.Logger; 16 import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource; 8 import java.util.*; 9 import javax.swing.JOptionPane; 17 10 import org.openstreetmap.josm.Main; 11 import org.openstreetmap.josm.actions.AutoScaleAction; 12 import org.openstreetmap.josm.actions.DownloadPrimitiveAction; 18 13 import org.openstreetmap.josm.actions.JosmAction; 19 14 import org.openstreetmap.josm.data.coor.LatLon; 15 import org.openstreetmap.josm.data.osm.*; 20 16 import org.openstreetmap.josm.data.projection.Projection; 21 import org.openstreetmap.josm.gui.MapView;22 import org.openstreetmap.josm.gui.PleaseWaitRunnable;23 17 import org.openstreetmap.josm.gui.layer.ImageryLayer; 24 import org.openstreetmap.josm.gui.progress.ProgressMonitor;25 import org.openstreetmap.josm.io.OsmTransferException;26 18 import static org.openstreetmap.josm.tools.I18n.tr; 27 19 import org.openstreetmap.josm.tools.Shortcut; 28 import org.xml.sax.SAXException;29 20 30 21 /** … … 35 26 public class GetImageryOffsetAction extends JosmAction { 36 27 37 private List<ImageryOffsetBase> offsets;38 39 28 public GetImageryOffsetAction() { 40 29 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();30 Shortcut.registerShortcut("imageryoffset:get", tr("Imagery: {0}", tr("Get Imagery Offset...")), 31 KeyEvent.VK_I, Shortcut.ALT_CTRL), true); 43 32 } 44 33 45 34 public void actionPerformed(ActionEvent e) { 35 if( Main.map == null || Main.map.mapView == null || !Main.map.isVisible() ) 36 return; 46 37 Projection proj = Main.map.mapView.getProjection(); 47 38 LatLon center = proj.eastNorth2latlon(Main.map.mapView.getCenter()); … … 51 42 return; 52 43 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(); 44 DownloadOffsetsTask download = new DownloadOffsetsTask(center, layer, imagery); 45 Main.worker.submit(download); 46 } 47 48 @Override 49 protected void updateEnabledState() { 50 boolean state = true; 51 if( Main.map == null || Main.map.mapView == null || !Main.map.isVisible() ) 52 state = false; 53 ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer(); 54 if( ImageryOffsetTools.getImageryID(layer) == null ) 55 state = false; 56 setEnabled(state); 57 } 58 59 private void showOffsetDialog( List<ImageryOffsetBase> offsets, ImageryLayer layer ) { 60 if( offsets.isEmpty() ) { 61 JOptionPane.showMessageDialog(Main.parent, 62 tr("No data for this region. Please adjust imagery layer and upload an offset."), 63 ImageryOffsetTools.DIALOG_TITLE, JOptionPane.INFORMATION_MESSAGE); 60 64 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 } 66 final ImageryOffsetBase offset = new OffsetDialog(offsets).showDialog(); 65 67 if( offset != null ) { 66 // todo: use the chosen offset67 68 if( offset instanceof ImageryOffset ) { 68 69 ImageryOffsetTools.applyLayerOffset(layer, (ImageryOffset)offset); 70 Main.map.repaint(); 69 71 } else if( offset instanceof CalibrationObject ) { 70 // todo: select object 72 OsmPrimitive obj = ((CalibrationObject)offset).getObject(); 73 final List<PrimitiveId> ids = new ArrayList<PrimitiveId>(1); 74 ids.add(obj); 75 DownloadPrimitiveAction.processItems(false, ids, false, true); 76 Main.worker.submit(new AfterCalibrationDownloadTask((CalibrationObject)offset)); 77 } 78 } 79 } 80 81 class AfterCalibrationDownloadTask implements Runnable { 82 private CalibrationObject offset; 83 84 public AfterCalibrationDownloadTask( CalibrationObject offset ) { 85 this.offset = offset; 86 } 87 88 @Override 89 public void run() { 90 OsmPrimitive p = getCurrentDataSet().getPrimitiveById(offset.getObject()); 91 if( p == null ) { 92 return; 93 } 94 // check for last user 95 if( offset.getLastUserId() > 0 ) { 96 long uid = p.getUser().getId(); 97 Date ts = p.getTimestamp(); 98 if( p instanceof Way ) { 99 for( Node n : ((Way)p).getNodes() ) { 100 if( n.getTimestamp().after(ts) ) { 101 ts = n.getTimestamp(); 102 uid = n.getUser().getId(); 103 } 104 } 105 } 106 if( uid != offset.getLastUserId() ) { 107 int result = JOptionPane.showConfirmDialog(Main.parent, 108 tr("The calibration object has been changed in unknown way.\n" 109 + "It may be moved or extended, thus ceasing to be a reliable mark\n" 110 + "for imagery calibration. Do you want to notify the server of this?"), 111 ImageryOffsetTools.DIALOG_TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); 112 if( result == JOptionPane.YES_OPTION ) { 113 DeprecateOffsetAction.deprecateOffset(offset); 114 return; 115 } 116 } 117 } 118 Main.main.getCurrentDataSet().setSelected(p); 119 AutoScaleAction.zoomTo(Collections.singleton(p)); 120 if( !Main.pref.getBoolean("iodb.calibration.message", false) ) { 121 JOptionPane.showMessageDialog(Main.parent, 122 tr("An object has been selected on the map. Find the corresponding feature\n" 123 + "on the imagery layer and move that layer accordingly.\n" 124 + "DO NOT touch the selected object, so it can be used by others later."), 125 ImageryOffsetTools.DIALOG_TITLE, JOptionPane.INFORMATION_MESSAGE); 126 Main.pref.put("iodb.calibration.message", true); 71 127 } 72 128 } 73 129 } 74 130 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()); 131 class DownloadOffsetsTask extends SimpleOffsetQueryTask { 132 private ImageryLayer layer; 133 private List<ImageryOffsetBase> offsets; 134 135 public DownloadOffsetsTask( LatLon center, ImageryLayer layer, String imagery ) { 136 super(null, tr("Loading imagery offsets...")); 137 try { 138 String query = "get?lat=" + center.lat() + "&lon=" + center.lon() 139 + "&imagery=" + URLEncoder.encode(imagery, "UTF8"); 140 setQuery(query); 141 } catch( UnsupportedEncodingException e ) { 142 throw new IllegalArgumentException(e); 90 143 } 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; 144 this.layer = layer; 115 145 } 116 146 117 147 @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 148 protected void afterFinish() { 149 if( !cancelled && offsets != null ) 150 showOffsetDialog(offsets, layer); 130 151 } 131 152 132 153 @Override 133 protected void cancel() { 134 cancelled = true; 154 protected void processResponse( InputStream inp ) throws UploadException { 155 offsets = null; 156 try { 157 offsets = new IODBReader(inp).parse(); 158 } catch( Exception e ) { 159 throw new UploadException("Error processing XML response: " + e.getMessage()); 160 } 135 161 } 136 162 }
Note:
See TracChangeset
for help on using the changeset viewer.