1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
|
---|
2 | package cadastre_fr;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.image.BufferedImage;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.IOException;
|
---|
11 |
|
---|
12 | import javax.imageio.ImageIO;
|
---|
13 | import javax.swing.JFileChooser;
|
---|
14 | import javax.swing.filechooser.FileFilter;
|
---|
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;
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
28 |
|
---|
29 | public class MenuActionSaveRasterAs extends JosmAction {
|
---|
30 |
|
---|
31 | public static String name = marktr("Save image as...");
|
---|
32 |
|
---|
33 | private static final long serialVersionUID = 1L;
|
---|
34 |
|
---|
35 | private WMSLayer wmsLayer;
|
---|
36 |
|
---|
37 | public class FiltrePng extends FileFilter {
|
---|
38 | @Override
|
---|
39 | public boolean accept(File file) {
|
---|
40 | if (file.isDirectory()) {
|
---|
41 | return true;
|
---|
42 | }
|
---|
43 | return file.getName().toLowerCase().endsWith(".png");
|
---|
44 | }
|
---|
45 | @Override
|
---|
46 | public String getDescription() {
|
---|
47 | return tr("PNG files (*.png)");
|
---|
48 | }
|
---|
49 | }
|
---|
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();
|
---|
66 | FiltrePng filtrePng = new FiltrePng();
|
---|
67 |
|
---|
68 | public MenuActionSaveRasterAs(WMSLayer wmsLayer) {
|
---|
69 | super(tr(name), "save", tr("Export image (only raster images)"), null, false);
|
---|
70 | this.wmsLayer = wmsLayer;
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public void actionPerformed(ActionEvent arg0) {
|
---|
75 | File file;
|
---|
76 | JFileChooser fc = new JFileChooser();
|
---|
77 | fc.addChoosableFileFilter(filtreTiff);
|
---|
78 | fc.addChoosableFileFilter(filtrePng);
|
---|
79 | fc.setFileFilter(filtreTiff);
|
---|
80 | int returnVal = fc.showSaveDialog(Main.parent);
|
---|
81 | if (returnVal == JFileChooser.APPROVE_OPTION) {
|
---|
82 | file = fc.getSelectedFile();
|
---|
83 | BufferedImage bi = wmsLayer.getImage(0).image;
|
---|
84 | if (fc.getFileFilter().equals(filtrePng))
|
---|
85 | {
|
---|
86 | if (!file.getName().endsWith(".png"))
|
---|
87 | file = new File(file.getParent(), file.getName()+".png");
|
---|
88 | try {
|
---|
89 | ImageIO.write(bi, "png", file);
|
---|
90 | /*
|
---|
91 | FileOutputStream flux = new FileOutputStream(file);
|
---|
92 | BufferedOutputStream fluxBuf = new BufferedOutputStream(flux);
|
---|
93 | JPEGImageEncoder codec = JPEGCodec.createJPEGEncoder(fluxBuf, JPEGCodec.getDefaultJPEGEncodeParam(bi));
|
---|
94 | codec.encode(bi);
|
---|
95 | fluxBuf.close();
|
---|
96 | */
|
---|
97 | } catch (IOException e) {
|
---|
98 | e.printStackTrace();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | else if (fc.getFileFilter().equals(filtreTiff))
|
---|
102 | {
|
---|
103 | boolean alpha = bi.getColorModel().hasAlpha();
|
---|
104 | Main.info("image with alpha channel : " + alpha);
|
---|
105 | try {
|
---|
106 | double x = wmsLayer.getImage(0).min.east();
|
---|
107 | double y = wmsLayer.getImage(0).min.north();
|
---|
108 | Envelope2D bbox = new Envelope2D(CRS.decode("EPSG:27561"),
|
---|
109 | x, y,
|
---|
110 | wmsLayer.getImage(0).max.east()-x, wmsLayer.getImage(0).max.north()-y);
|
---|
111 | GridCoverageFactory factory = new GridCoverageFactory();
|
---|
112 | GridCoverage2D coverage = factory.create("tiff", bi, bbox);
|
---|
113 | final File output = new File(file.getParent(), file.getName()+".tif");
|
---|
114 | GeoTiffWriter gtwriter = new GeoTiffWriter(output);
|
---|
115 | GeoTiffWriteParams wp = new GeoTiffWriteParams();
|
---|
116 | wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT);
|
---|
117 | wp.setCompressionType("LZW");
|
---|
118 | wp.setCompressionQuality(0.75F);
|
---|
119 | final GeoTiffFormat format = new GeoTiffFormat();
|
---|
120 | final ParameterValueGroup params = format.getWriteParameters();
|
---|
121 | params.parameter(
|
---|
122 | AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString())
|
---|
123 | .setValue(wp);
|
---|
124 |
|
---|
125 | gtwriter.write(coverage, params.values().toArray(new GeneralParameterValue[1]));
|
---|
126 | gtwriter.dispose();
|
---|
127 | coverage.dispose(true);
|
---|
128 | } catch (Exception e) {
|
---|
129 | // TODO Auto-generated catch block
|
---|
130 | e.printStackTrace();
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | }
|
---|