Changeset 29733 in osm for applications/editors/josm
- Timestamp:
- 2013-06-30T21:51:56+02:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionSaveRasterAs.java
r26382 r29733 14 14 import javax.swing.filechooser.FileFilter; 15 15 16 import org.geotools.coverage.grid.GridCoverage2D; 17 import org.geotools.coverage.grid.GridCoverageFactory; 18 import org.geotools.coverage.grid.io.AbstractGridFormat; 19 import org.geotools.gce.geotiff.GeoTiffFormat; 20 import org.geotools.gce.geotiff.GeoTiffWriteParams; 21 import org.geotools.gce.geotiff.GeoTiffWriter; 22 import org.geotools.geometry.Envelope2D; 23 import org.geotools.referencing.CRS; 24 import org.opengis.parameter.GeneralParameterValue; 25 import org.opengis.parameter.ParameterValueGroup; 16 26 import org.openstreetmap.josm.Main; 17 27 import org.openstreetmap.josm.actions.JosmAction; … … 19 29 public class MenuActionSaveRasterAs extends JosmAction { 20 30 21 public static String name = marktr("Save image as PNG");31 public static String name = marktr("Save image as..."); 22 32 23 33 private static final long serialVersionUID = 1L; … … 33 43 return file.getName().toLowerCase().endsWith(".png"); 34 44 } 35 36 45 @Override 37 46 public String getDescription() { 38 47 return tr("PNG files (*.png)"); 39 48 } 40 41 49 } 42 50 51 public class FiltreTiff extends FileFilter { 52 @Override 53 public boolean accept(File file) { 54 if (file.isDirectory()) { 55 return true; 56 } 57 return file.getName().toLowerCase().endsWith(".tif"); 58 } 59 @Override 60 public String getDescription() { 61 return tr("GeoTiff files (*.tif)"); 62 } 63 } 64 65 FiltreTiff filtreTiff = new FiltreTiff(); 43 66 FiltrePng filtrePng = new FiltrePng(); 44 67 45 68 public MenuActionSaveRasterAs(WMSLayer wmsLayer) { 46 super(tr(name), "save", tr("Export as PNG format(only raster images)"), null, false);69 super(tr(name), "save", tr("Export image (only raster images)"), null, false); 47 70 this.wmsLayer = wmsLayer; 48 71 } … … 51 74 File file; 52 75 JFileChooser fc = new JFileChooser(); 53 fc.setFileFilter(filtrePng); 76 fc.addChoosableFileFilter(filtreTiff); 77 fc.addChoosableFileFilter(filtrePng); 78 fc.setFileFilter(filtreTiff); 54 79 int returnVal = fc.showSaveDialog(Main.parent); 55 80 if (returnVal == JFileChooser.APPROVE_OPTION) { 56 81 file = fc.getSelectedFile(); 57 if (!file.getName().endsWith(".png"))58 file = new File(file.getParent(), file.getName()+".png");59 82 BufferedImage bi = wmsLayer.getImage(0).image; 60 try { 61 ImageIO.write(bi, "png", file); 83 if (fc.getFileFilter().equals(filtrePng)) 84 { 85 if (!file.getName().endsWith(".png")) 86 file = new File(file.getParent(), file.getName()+".png"); 87 try { 88 ImageIO.write(bi, "png", file); 62 89 /* 63 FileOutputStream flux = new FileOutputStream(file);64 BufferedOutputStream fluxBuf = new BufferedOutputStream(flux);65 JPEGImageEncoder codec = JPEGCodec.createJPEGEncoder(fluxBuf, JPEGCodec.getDefaultJPEGEncodeParam(bi));66 codec.encode(bi);67 fluxBuf.close();90 FileOutputStream flux = new FileOutputStream(file); 91 BufferedOutputStream fluxBuf = new BufferedOutputStream(flux); 92 JPEGImageEncoder codec = JPEGCodec.createJPEGEncoder(fluxBuf, JPEGCodec.getDefaultJPEGEncodeParam(bi)); 93 codec.encode(bi); 94 fluxBuf.close(); 68 95 */ 69 } catch (IOException e) { 70 e.printStackTrace(); 96 } catch (IOException e) { 97 e.printStackTrace(); 98 } 99 } 100 else if (fc.getFileFilter().equals(filtreTiff)) 101 { 102 boolean alpha = bi.getColorModel().hasAlpha(); 103 System.out.println("image with alpha channel : " + alpha); 104 try { 105 double x = wmsLayer.getImage(0).min.east(); 106 double y = wmsLayer.getImage(0).min.north(); 107 Envelope2D bbox = new Envelope2D(CRS.decode("EPSG:27561"), 108 x, y, 109 wmsLayer.getImage(0).max.east()-x, wmsLayer.getImage(0).max.north()-y); 110 GridCoverageFactory factory = new GridCoverageFactory(); 111 GridCoverage2D coverage = factory.create("tiff", bi, bbox); 112 final File output = new File(file.getParent(), file.getName()+".tif"); 113 GeoTiffWriter gtwriter = new GeoTiffWriter(output); 114 GeoTiffWriteParams wp = new GeoTiffWriteParams(); 115 wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT); 116 wp.setCompressionType("LZW"); 117 wp.setCompressionQuality(0.75F); 118 final GeoTiffFormat format = new GeoTiffFormat(); 119 final ParameterValueGroup params = format.getWriteParameters(); 120 params.parameter( 121 AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString()) 122 .setValue(wp); 123 124 gtwriter.write(coverage, (GeneralParameterValue[]) params.values().toArray(new GeneralParameterValue[1])); 125 gtwriter.dispose(); 126 coverage.dispose(true); 127 } catch (Exception e) { 128 // TODO Auto-generated catch block 129 e.printStackTrace(); 130 } 71 131 } 72 132 }
Note:
See TracChangeset
for help on using the changeset viewer.