Changeset 24300 in osm for applications/editors/josm
- Timestamp:
- 2010-11-17T18:51:59+01:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/LoadPictureCalibrationAction.java
r23190 r24300 38 38 39 39 /** 40 * Action for resetting properties of an image.40 * Action to load the calibration file. 41 41 * 42 * TODO Four almost identical classes. Refactoring needed.43 42 */ 44 43 public class LoadPictureCalibrationAction extends JosmAction { … … 47 46 PicLayerAbstract m_owner = null; 48 47 48 // Persistent FileChooser instance to remember last directory 49 JFileChooser m_filechooser = null; 50 49 51 /** 50 52 * Constructor 51 53 */ 52 54 public LoadPictureCalibrationAction( PicLayerAbstract owner ) { 53 super(tr("Load Picture Calibration..."), null, tr("Loads calibration data toa file"), null, false);55 super(tr("Load Picture Calibration..."), null, tr("Loads calibration data from a file"), null, false); 54 56 // Remember the owner... 55 57 m_owner = owner; 56 58 } 57 59 58 60 /** 59 61 * Action handler … … 61 63 public void actionPerformed(ActionEvent arg0) { 62 64 // Save dialog 63 finalJFileChooser fc = new JFileChooser();65 JFileChooser fc = new JFileChooser(); 64 66 fc.setAcceptAllFileFilterUsed( false ); 65 67 fc.setFileFilter( new CalibrationFileFilter() ); … … 71 73 // Load 72 74 try { 73 Properties props = new Properties(); 74 props.load(new FileInputStream(fc.getSelectedFile())); 75 m_owner.loadCalibration(props); 75 m_owner.loadCalibration(fc.getSelectedFile()); 76 76 } catch (Exception e) { 77 77 // Error … … 81 81 } 82 82 } 83 83 84 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/NewLayerFromFileAction.java
r23190 r24300 33 33 import org.openstreetmap.josm.Main; 34 34 import org.openstreetmap.josm.actions.JosmAction; 35 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 36 import org.openstreetmap.josm.gui.layer.Layer; 35 37 36 38 /** 37 * Action responsible for creation of a new layer based on 38 * an image file. 39 * Action responsible for creation of new layers based on image files. 39 40 */ 40 41 public class NewLayerFromFileAction extends JosmAction { 42 43 String m_lastdirprefname = "piclayer.lastdir"; 41 44 42 45 /** … … 80 83 81 84 // Choose a file 82 JFileChooser fc = new JFileChooser( );85 JFileChooser fc = new JFileChooser(Main.pref.get(m_lastdirprefname)); 83 86 fc.setAcceptAllFileFilterUsed( false ); 84 87 fc.setFileFilter( new ImageFileFilter() ); 88 fc.setMultiSelectionEnabled(true); 85 89 int result = fc.showOpenDialog( Main.parent ); 86 90 87 91 // Create a layer? 88 92 if ( result == JFileChooser.APPROVE_OPTION ) { 89 // Create layer from file 90 PicLayerFromFile layer = new PicLayerFromFile( fc.getSelectedFile() ); 91 // Add layer only if successfully initialized 92 try { 93 layer.initialize(); 93 // The first loaded layer will be placed at the top of any other layer of the same class, 94 // or at the bottom of the stack if there is no such layer yet 95 // The next layers we load will be placed one after the other after this first layer 96 int newLayerPos = Main.map.mapView.getAllLayers().size(); 97 for(Layer l : Main.map.mapView.getLayersOfType(PicLayerFromFile.class)) { 98 int pos = Main.map.mapView.getLayerPos(l); 99 if (pos < newLayerPos) newLayerPos = pos; 94 100 } 95 catch (IOException e) { 96 // Failed 97 System.out.println( "NewLayerFromFileAction::actionPerformed - " + e.getMessage() ); 98 JOptionPane.showMessageDialog(null, e.getMessage() ); 99 return; 101 102 for(File file : fc.getSelectedFiles() ) { 103 // TODO: we need a progress bar here, it can take quite some time 104 105 // Create layer from file 106 PicLayerFromFile layer = new PicLayerFromFile( file ); 107 // Add layer only if successfully initialized 108 try { 109 layer.initialize(); 110 } 111 catch (IOException e) { 112 // Failed 113 System.out.println( "NewLayerFromFileAction::actionPerformed - " + e.getMessage() ); 114 JOptionPane.showMessageDialog(null, e.getMessage() ); 115 return; 116 } 117 Main.pref.put(m_lastdirprefname, file.getParent()); 118 119 Main.main.addLayer( layer ); 120 Main.map.mapView.moveLayer(layer, newLayerPos++); 121 122 if ( fc.getSelectedFiles().length == 1 && Main.pref.getInteger("piclayer.zoom-on-load", 1) != 0 ) { 123 // if we are loading a single picture file, zoom on it, so that the user can see something 124 BoundingXYVisitor v = new BoundingXYVisitor(); 125 layer.visitBoundingBox(v); 126 Main.map.mapView.recalculateCenterScale(v); 127 } 128 100 129 } 101 // Add layer102 Main.main.addLayer( layer );103 130 } 104 105 131 } 106 132 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerAbstract.java
r24292 r24300 31 31 import java.awt.event.ActionEvent; 32 32 import java.awt.image.BufferedImage; 33 import java.io.File; 34 import java.io.FileInputStream; 35 import java.io.FileNotFoundException; 36 import java.io.FileOutputStream; 33 37 import java.io.IOException; 34 38 import java.util.List; … … 71 75 private double m_scaley = 1.0; 72 76 // The scale that was set on the map during image creation 73 private double m_initial_scale = 0;77 private double m_initial_scale = 1.0; 74 78 // Layer icon 75 79 private Icon m_layericon = null; … … 103 107 */ 104 108 public void initialize() throws IOException { 109 // First, we initialize the calibration, so that createImage() can rely on it 110 111 // If the map does not exist - we're screwed. We should not get into this situation in the first place! 112 if ( Main.map != null && Main.map.mapView != null ) { 113 // Geographical position of the image 114 // getCenter() documentation claims it returns a clone, but this is not in line with the code, 115 // which actually returns the same object upon subsequent calls. This messes things up 116 // when we loading several pictures and associated cal's in one go. 117 // So as a workaround, copy the object manually : 118 // TODO: not sure about this code below, probably there is a better way to clone the objects 119 EastNorth center = Main.map.mapView.getCenter(); 120 m_initial_position = new EastNorth(center.east(), center.north()); 121 m_position = new EastNorth(center.east(), center.north()); 122 // Initial scale at which the image was loaded 123 m_initial_scale = Main.map.mapView.getDist100Pixel(); 124 } else { 125 throw new IOException(tr("Could not find the map object.")); 126 } 105 127 106 128 // Create image … … 113 135 Graphics g = m_image.getGraphics(); 114 136 g.drawImage( image, 0, 0, null ); 115 116 // If the map does not exist - we're screwed. We should not get into this situation in the first place!117 if ( Main.map != null && Main.map.mapView != null ) {118 // Geographical position of the image119 m_initial_position = m_position = Main.map.mapView.getCenter();120 // Initial scale at which the image was loaded121 m_initial_scale = Main.map.mapView.getDist100Pixel();122 } else {123 throw new IOException(tr("Could not find the map object."));124 }125 137 } 126 138 … … 231 243 232 244 /** 233 * Scales the picture. Scaled in... don't know but works ok :)245 * Scales the picture. scalex and scaley will multiply the current factor 234 246 */ 235 247 public void scalePictureBy( double scalex, double scaley ) { 236 m_scalex += scalex;237 m_scaley += scaley;248 m_scalex *= scalex; 249 m_scaley *= scaley; 238 250 } 239 251 … … 268 280 269 281 @Override 282 /** 283 * Computes the (rough) bounding box. 284 * We ignore the rotation, the resulting bounding box contains any possible 285 * rotation. 286 */ 270 287 public void visitBoundingBox(BoundingXYVisitor arg0) { 271 // TODO Auto-generated method stub 272 288 if ( m_image == null ) 289 return; 290 String projcode = Main.proj.toCode(); 291 292 // TODO: bounding box only supported when coordinates are in meters 293 // The reason for that is that this .cal think makes us a hard time. 294 // The position is stored as a raw data (can be either in degrees or 295 // in meters, depending on the projection used at creation), but the 296 // initial scale is in m/100pix 297 // So for now, we support the bounding box only when everything is in meters 298 if ( projcode.equals("EPSG:3857") || projcode.equals("EPSG:4326") ) 299 return; 300 301 EastNorth center = m_position; 302 double w = m_image.getWidth(null); 303 double h = m_image.getHeight(null); 304 double diag_pix = Math.sqrt(w*w+h*h); 305 306 // m_initial_scale is a the scale (unit: m/100pix) at creation time 307 double diag_m = (diag_pix/100) * m_initial_scale; 308 309 double factor = Math.max(m_scalex, m_scaley); 310 double offset = factor * diag_m / 2.0; 311 312 EastNorth topleft = center.add(-offset, -offset); 313 EastNorth bottomright = center.add(offset, offset); 314 arg0.visit(topleft); 315 arg0.visit(bottomright); 273 316 } 274 317 … … 287 330 props.put(SCALEY, "" + m_scaley); 288 331 props.put(ANGLE, "" + m_angle); 332 } 333 334 /** 335 * Loads calibration data from file 336 * @param file The file to read from 337 * @return 338 */ 339 public void loadCalibration(File file) throws IOException { 340 Properties props = new Properties(); 341 props.load(new FileInputStream(file)); 342 loadCalibration(props); 289 343 } 290 344 -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerFromFile.java
r24291 r24300 21 21 package org.openstreetmap.josm.plugins.piclayer; 22 22 23 import static org.openstreetmap.josm.tools.I18n.tr; 24 25 import org.openstreetmap.josm.Main; 26 23 27 import java.awt.Image; 24 28 import java.io.File; 25 29 import java.io.IOException; 26 30 import javax.imageio.ImageIO; 27 31 import javax.swing.JDialog; 32 import javax.swing.JOptionPane; 28 33 /** 29 34 * Layer displaying a picture loaded from a file. … … 39 44 // Remember the file 40 45 m_file = file; 46 41 47 // Generate tooltip text 42 48 m_tooltiptext = m_file.getAbsolutePath(); … … 44 50 // Set the name of the layer as the base name of the file 45 51 setName(m_file.getName()); 46 System.out.println( "name="+m_file.getName() );47 52 } 48 53 … … 55 60 return calFile; 56 61 } 57 62 58 63 @Override 59 64 protected Image createImage() throws IOException { … … 61 66 Image image = null; 62 67 image = ImageIO.read( m_file ); 68 69 // Manage a potential existing calibration file 70 File calFile = getDefaultCalPath(); 71 if ( calFile.exists() ) { 72 String prefkey = "piclayer.autoloadcal"; 73 String policy = Main.pref.get(prefkey, ""); 74 policy = policy.trim().toLowerCase(); 75 boolean loadcal = false; 76 77 String msg = tr("A calibration file associated to the picture file was found:")+"\n"+calFile.getName(); 78 if ( policy.equals("yes") ) { 79 loadcal = true; 80 } 81 else if ( policy.equals("no") ) { 82 loadcal = false; 83 } 84 else if ( policy.equals("ask") ) { 85 msg += "\n" + tr("(set \"{0}\" to yes/no/ask in the preferences\n"+ 86 "to control the autoloading of calibration files)", prefkey); 87 msg += "\n" + tr("Do you want to apply it ?"); 88 int answer = JOptionPane.showConfirmDialog(Main.parent, msg, tr("Load calibration file ?"), JOptionPane.YES_NO_OPTION); 89 if (answer == JOptionPane.YES_OPTION) { 90 loadcal = true; 91 } 92 } 93 else { 94 msg += "\n" + tr("It will be applied automatically."); 95 msg += "\n" + tr("Also, frow now on, cal files will always be loaded automatically."); 96 msg += "\n" + tr("Set \"{0}\" to yes/no/ask in the preferences\n"+ 97 "to control the autoloading of calibration files.", prefkey); 98 // TODO: there should be here a yes/no dialog with a checkbox "do not ask again" 99 JOptionPane.showMessageDialog(Main.parent, msg, 100 "Automatic loading of the calibration", JOptionPane.INFORMATION_MESSAGE); 101 Main.pref.put(prefkey, "yes"); 102 loadcal = true; 103 } 104 if ( loadcal ) 105 loadCalibration(calFile); 106 } 107 63 108 return image; 64 109 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/ScalePictureActionAbstract.java
r24288 r24300 87 87 double factor; 88 88 if ( ( e.getModifiersEx() & e.SHIFT_DOWN_MASK ) != 0 ) { 89 factor = Main.pref.getDouble("piclayer.scalefactors.high_precision", 4000);89 factor = Main.pref.getDouble("piclayer.scalefactors.high_precision", 1.0001); 90 90 } 91 91 else { 92 factor = Main.pref.getDouble("piclayer.scalefactors.low_precision", 400);92 factor = Main.pref.getDouble("piclayer.scalefactors.low_precision", 1.015); 93 93 } 94 doTheScale( ( e.getY() - m_prevY ) / factor);94 doTheScale( Math.pow(factor, m_prevY - e.getY() ) ); 95 95 m_prevY = e.getY(); 96 96 Main.map.mapView.repaint(); -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/ScaleXPictureAction.java
r23190 r24300 44 44 45 45 public void doTheScale( double scale ) { 46 m_currentLayer.scalePictureBy( scale, 0.0 );46 m_currentLayer.scalePictureBy( scale, 1.0 ); 47 47 } 48 48 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/ScaleYPictureAction.java
r23190 r24300 44 44 45 45 public void doTheScale( double scale ) { 46 m_currentLayer.scalePictureBy( 0.0, scale );46 m_currentLayer.scalePictureBy( 1.0, scale ); 47 47 } 48 48 }
Note:
See TracChangeset
for help on using the changeset viewer.