Changeset 26498 in osm for applications/editors/josm
- Timestamp:
- 2011-08-09T11:43:43+02:00 (13 years ago)
- Location:
- applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/LoadPictureCalibrationAction.java
r24300 r26498 73 73 // Load 74 74 try { 75 m_owner.loadCalibration( fc.getSelectedFile());75 m_owner.loadCalibration(new FileInputStream(fc.getSelectedFile())); 76 76 } catch (Exception e) { 77 77 // Error -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/NewLayerFromFileAction.java
r24308 r26498 54 54 return true; 55 55 56 String fileExtension = f.getName().substring(f.getName().lastIndexOf('.')+1); 56 int dotIdx = f.getName().lastIndexOf('.'); 57 if (dotIdx == -1) return false; 58 String fileExtension = f.getName().substring(dotIdx+1); 57 59 String[] supportedExtensions = ImageIO.getReaderFormatNames(); 58 60 61 if ("zip".equalsIgnoreCase(fileExtension)) return true; 59 62 // Unfortunately, getReaderFormatNames does not always return ALL extensions in 60 63 // both lower and upper case, so we can not do a search in the array -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerAbstract.java
r26493 r26498 34 34 import java.io.FileReader; 35 35 import java.io.IOException; 36 import java.io.InputStream; 37 import java.io.InputStreamReader; 38 import java.io.Reader; 36 39 import java.util.List; 37 40 import java.util.Properties; … … 51 54 import org.openstreetmap.josm.gui.MapView; 52 55 import org.openstreetmap.josm.gui.layer.Layer; 56 import org.openstreetmap.josm.tools.Utils; 53 57 54 58 /** … … 406 410 * @return 407 411 */ 408 public void loadCalibration( File file) throws IOException {412 public void loadCalibration(InputStream is) throws IOException { 409 413 Properties props = new Properties(); 410 props.load( new FileInputStream(file));414 props.load(is); 411 415 loadCalibration(props); 412 416 } … … 441 445 } 442 446 443 public void loadWorldfile(File file) throws IOException { 444 FileReader reader = new FileReader(file); 445 BufferedReader br = new BufferedReader(reader); 446 double e[] = new double[6]; 447 for (int i=0; i<6; ++i) { 448 String line = br.readLine(); 449 e[i] = Double.parseDouble(line); 450 } 451 double sx=e[0], ry=e[1], rx=e[2], sy=e[3], dx=e[4], dy=e[5]; 452 int w = m_image.getWidth(null); 453 int h = m_image.getHeight(null); 454 m_position.setLocation( 455 dx + w/2*sx + h/2*rx, 456 dy + w/2*ry + h/2*sy 457 ); 458 m_initial_position.setLocation(m_position); 459 m_angle = 0; 460 m_scalex = 100*sx*getMetersPerEasting(m_position); 461 m_scaley = -100*sy*getMetersPerNorthing(m_position); 462 m_shearx = rx / sx; 463 m_sheary = ry / sy; 464 m_initial_scale = 1; 465 Main.map.mapView.repaint(); 447 public void loadWorldfile(InputStream is) throws IOException { 448 BufferedReader br = null; 449 try { 450 Reader reader = new InputStreamReader(is); 451 br = new BufferedReader(reader); 452 double e[] = new double[6]; 453 for (int i=0; i<6; ++i) { 454 String line = br.readLine(); 455 e[i] = Double.parseDouble(line); 456 } 457 double sx=e[0], ry=e[1], rx=e[2], sy=e[3], dx=e[4], dy=e[5]; 458 int w = m_image.getWidth(null); 459 int h = m_image.getHeight(null); 460 m_position.setLocation( 461 dx + w/2*sx + h/2*rx, 462 dy + w/2*ry + h/2*sy 463 ); 464 m_initial_position.setLocation(m_position); 465 m_angle = 0; 466 m_scalex = 100*sx*getMetersPerEasting(m_position); 467 m_scaley = -100*sy*getMetersPerNorthing(m_position); 468 m_shearx = rx / sx; 469 m_sheary = ry / sy; 470 m_initial_scale = 1; 471 Main.map.mapView.repaint(); 472 } finally { 473 Utils.close(br); 474 } 466 475 } 467 476 -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerFromFile.java
r26493 r26498 27 27 import java.awt.Image; 28 28 import java.io.File; 29 import java.io.FileInputStream; 29 30 import java.io.IOException; 31 import java.io.InputStream; 30 32 import java.util.Arrays; 33 import java.util.Enumeration; 34 import java.util.zip.ZipEntry; 35 import java.util.zip.ZipFile; 36 31 37 import javax.imageio.ImageIO; 32 38 import javax.swing.JOptionPane; 39 40 import org.openstreetmap.josm.tools.Utils; 33 41 /** 34 42 * Layer displaying a picture loaded from a file. … … 38 46 // File to load from. 39 47 private File m_file; 48 49 // whether the file is a zip archive 50 private boolean isZip; 51 // if so, what is the name of the image inside the archive? 52 private String imgNameInZip; 53 40 54 // Tooltip text 41 55 private String m_tooltiptext; … … 45 59 m_file = file; 46 60 61 if ("zip".equalsIgnoreCase(getFileExtension(file))) { 62 isZip = true; 63 } 64 47 65 // Generate tooltip text 48 66 m_tooltiptext = m_file.getAbsolutePath(); 49 67 50 68 // Set the name of the layer as the base name of the file 51 69 setName(m_file.getName()); 52 70 } 53 71 54 protected String getFilePath() {55 return m_file.getAbsolutePath();56 }57 58 public File getDefaultCalPath() {59 File calFile = new File(m_file + CalibrationFileFilter.EXTENSION);60 return calFile;61 }62 63 72 @Override 64 73 protected Image createImage() throws IOException { 65 74 // Try to load file 66 75 Image image = null; 67 image = ImageIO.read( m_file ); 68 return image; 69 } 70 76 77 if (isZip) { 78 ZipFile zipFile = null; 79 try 80 { 81 zipFile = new ZipFile(m_file); 82 ZipEntry imgEntry = null; 83 Enumeration<? extends ZipEntry> entries = zipFile.entries(); 84 String[] supportedImageExtensions = ImageIO.getReaderFormatNames(); 85 86 while_loop: 87 while (entries.hasMoreElements()) { 88 ZipEntry entry = entries.nextElement(); 89 for (String extension : supportedImageExtensions) { 90 if (entry.getName().endsWith("." + extension)) { 91 imgEntry = entry; 92 break while_loop; 93 } 94 } 95 } 96 if (imgEntry != null) { 97 imgNameInZip = imgEntry.getName(); 98 InputStream is = null; 99 try { 100 is = zipFile.getInputStream(imgEntry); 101 image = ImageIO.read(is); 102 return image; 103 } finally { 104 Utils.close(is); 105 } 106 } 107 System.err.println("Warning: no image in zip file found"); 108 return null; 109 } catch (Exception e) { 110 System.err.println(tr("Warning: failed to handle zip file ''{0}''. Exception was: {1}", m_file.getName(), e.toString())); 111 return null; 112 } finally { 113 if (zipFile != null) { 114 try { 115 zipFile.close(); 116 } catch (IOException ex) { 117 } 118 } 119 } 120 } else { 121 image = ImageIO.read( m_file ); 122 return image; 123 } 124 } 125 126 public enum CalibrationType { CAL, WORLDFILE } 127 128 public static class CalData { 129 public String[] imgExt; 130 public String[] calExt; 131 public CalibrationType type; 132 133 public CalData(String[] imgExt, String[] calExt, CalibrationType type) { 134 this.imgExt = imgExt; 135 this.calExt = calExt; 136 this.type = type; 137 } 138 } 139 71 140 @Override 72 141 protected void lookForCalibration() throws IOException { 73 142 // Manage a potential existing calibration file 74 File calFile = getDefaultCalPath(); 75 if ( calFile.exists() ) { 76 String prefkey = "piclayer.autoloadcal"; 77 String policy = Main.pref.get(prefkey, ""); 78 policy = policy.trim().toLowerCase(); 79 boolean loadcal = false; 80 81 String msg = tr("A calibration file associated to the picture file was found:")+"\n"+calFile.getName(); 82 if ( policy.equals("yes") ) { 143 144 String[][] imgExtensions = new String[][] { 145 { ".jpg", ".jpeg" }, 146 { ".png" }, 147 { ".tif", ".tiff" }, 148 { ".bmp" }, 149 }; 150 String[][] wldExtensions = new String[][] { 151 { ".wld", ".jgw", ".jpgw" }, 152 { ".wld", ".pgw", ".pngw" }, 153 { ".wld", ".tfw", ".tifw" }, 154 { ".wld", ".bmpw", ".bpw"}, 155 }; 156 157 if (isZip) { 158 ZipFile zipFile = null; 159 try 160 { 161 zipFile = new ZipFile(m_file); 162 String calFileStr = imgNameInZip + CalibrationFileFilter.EXTENSION; 163 ZipEntry calEntry = zipFile.getEntry(calFileStr); 164 if (calEntry != null) { 165 if (confirmCalibrationLoading(calFileStr)) { 166 InputStream is = zipFile.getInputStream(calEntry); 167 loadCalibration(is); 168 return; 169 } 170 } else { 171 int dotIdx = imgNameInZip.lastIndexOf("."); 172 if (dotIdx == -1) return; 173 String extension = imgNameInZip.substring(dotIdx); 174 String namepart = imgNameInZip.substring(0, dotIdx); 175 for (int i=0; i<imgExtensions.length; ++i) { 176 if (Arrays.asList(imgExtensions[i]).contains(extension.toLowerCase())) { 177 for (String wldExtension : wldExtensions[i]) { 178 String wldName = namepart+wldExtension; 179 ZipEntry wldEntry = zipFile.getEntry(wldName); 180 if (wldEntry != null) { 181 if (confirmCalibrationLoading(wldName)) { 182 InputStream is = zipFile.getInputStream(wldEntry); 183 loadWorldfile(is); 184 return; 185 } 186 } 187 } 188 } 189 } 190 } 191 } catch (Exception e) { 192 System.err.println(tr("Warning: failed to handle zip file ''{0}''. Exception was: {1}", m_file.getName(), e.toString())); 193 return; 194 } finally { 195 if (zipFile != null) { 196 try { 197 zipFile.close(); 198 } catch (IOException ex) { 199 } 200 } 201 } 202 } else { 203 File calFile = new File(m_file + CalibrationFileFilter.EXTENSION); 204 if (calFile.exists()) { 205 if (confirmCalibrationLoading(calFile.getName())) { 206 loadCalibration(new FileInputStream(calFile)); 207 } 208 } else { 209 int dotIdx = m_file.getName().lastIndexOf("."); 210 if (dotIdx == -1) return; 211 String extension = m_file.getName().substring(dotIdx); 212 String namepart = m_file.getName().substring(0, dotIdx); 213 for (int i=0; i<imgExtensions.length; ++i) { 214 if (Arrays.asList(imgExtensions[i]).contains(extension.toLowerCase())) { 215 for (String wldExtension : wldExtensions[i]) { 216 File wldFile = new File(m_file.getParentFile(), namepart+wldExtension); 217 if (wldFile.exists()) { 218 loadWorldfile(new FileInputStream(wldFile)); 219 return; 220 } 221 } 222 } 223 } 224 } 225 } 226 } 227 228 protected boolean confirmCalibrationLoading(String fileName) { 229 String prefkey = "piclayer.autoloadcal"; 230 String policy = Main.pref.get(prefkey, ""); 231 policy = policy.trim().toLowerCase(); 232 boolean loadcal = false; 233 234 String msg = tr("A calibration file associated to the picture file was found:")+"\n"+fileName; 235 if ( policy.equals("yes") ) { 236 loadcal = true; 237 } 238 else if ( policy.equals("no") ) { 239 loadcal = false; 240 } 241 else if ( policy.equals("ask") ) { 242 msg += "\n" + tr("(set \"{0}\" to yes/no/ask in the preferences\n"+ 243 "to control the autoloading of calibration files)", prefkey); 244 msg += "\n" + tr("Do you want to apply it ?"); 245 int answer = JOptionPane.showConfirmDialog(Main.parent, msg, tr("Load calibration file ?"), JOptionPane.YES_NO_OPTION); 246 if (answer == JOptionPane.YES_OPTION) { 83 247 loadcal = true; 84 248 } 85 else if ( policy.equals("no") ) { 86 loadcal = false; 87 } 88 else if ( policy.equals("ask") ) { 89 msg += "\n" + tr("(set \"{0}\" to yes/no/ask in the preferences\n"+ 90 "to control the autoloading of calibration files)", prefkey); 91 msg += "\n" + tr("Do you want to apply it ?"); 92 int answer = JOptionPane.showConfirmDialog(Main.parent, msg, tr("Load calibration file ?"), JOptionPane.YES_NO_OPTION); 93 if (answer == JOptionPane.YES_OPTION) { 94 loadcal = true; 95 } 96 } 97 else { 98 msg += "\n" + tr("It will be applied automatically."); 99 msg += "\n" + tr("Also, frow now on, cal files will always be loaded automatically."); 100 msg += "\n" + tr("Set \"{0}\" to yes/no/ask in the preferences\n"+ 101 "to control the autoloading of calibration files.", prefkey); 102 // TODO: there should be here a yes/no dialog with a checkbox "do not ask again" 103 JOptionPane.showMessageDialog(Main.parent, msg, 104 "Automatic loading of the calibration", JOptionPane.INFORMATION_MESSAGE); 105 Main.pref.put(prefkey, "yes"); 106 loadcal = true; 107 } 108 if ( loadcal ) 109 loadCalibration(calFile); 110 } else { 111 112 // try to find and load world file 113 int dotIdx = m_file.getName().lastIndexOf("."); 114 if (dotIdx == -1) return; 115 String extension = m_file.getName().substring(dotIdx); 116 String namepart = m_file.getName().substring(0, dotIdx); 117 String[][] imgExtensions = new String[][] { 118 { ".jpg", ".jpeg" }, 119 { ".png" }, 120 { ".tif", ".tiff" }, 121 { ".bmp" }, 122 }; 123 String[][] wldExtensions = new String[][] { 124 { ".wld", ".jgw", ".jpgw" }, 125 { ".wld", ".pgw", ".pngw" }, 126 { ".wld", ".tfw", ".tifw" }, 127 { ".wld", ".bmpw", ".bpw"}, 128 }; 129 for (int i=0; i<imgExtensions.length; ++i) { 130 if (Arrays.asList(imgExtensions[i]).contains(extension.toLowerCase())) { 131 for (String wldExtension : wldExtensions[i]) { 132 File wldFile = new File(m_file.getParentFile(), namepart+wldExtension); 133 if (wldFile.exists()) { 134 System.out.println("Loading world file: "+wldFile); 135 loadWorldfile(wldFile); 136 return; 137 } 138 } 139 } 140 } 141 } 249 } 250 else { 251 msg += "\n" + tr("It will be applied automatically."); 252 msg += "\n" + tr("Also, frow now on, cal files will always be loaded automatically."); 253 msg += "\n" + tr("Set \"{0}\" to yes/no/ask in the preferences\n"+ 254 "to control the autoloading of calibration files.", prefkey); 255 // TODO: there should be here a yes/no dialog with a checkbox "do not ask again" 256 JOptionPane.showMessageDialog(Main.parent, msg, 257 "Automatic loading of the calibration", JOptionPane.INFORMATION_MESSAGE); 258 Main.pref.put(prefkey, "yes"); 259 loadcal = true; 260 } 261 return loadcal; 142 262 } 143 263 … … 146 266 return m_tooltiptext; 147 267 } 268 269 /** 270 * Get the file extension 271 * @param f the file 272 * @return everything after the last '.' 273 * the empty string, if there is no extension 274 */ 275 public static String getFileExtension(File f) { 276 int dotIdx = f.getName().lastIndexOf('.'); 277 if (dotIdx == -1) return ""; 278 return f.getName().substring(dotIdx+1); 279 } 280 148 281 }
Note:
See TracChangeset
for help on using the changeset viewer.