Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / util / process / FilterProcess.java @ 20867

History | View | Annotate | Download (10 KB)

1 11803 bsanchez
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*/
19 19568 bsanchez
package org.gvsig.raster.util.process;
20 11803 bsanchez
21 17882 bsanchez
import java.io.FileNotFoundException;
22 11856 bsanchez
import java.io.IOException;
23 11864 bsanchez
import java.util.ArrayList;
24 11856 bsanchez
25 18014 bsanchez
import org.gvsig.raster.Configuration;
26 18538 nbrodin
import org.gvsig.raster.RasterProcess;
27 19567 bsanchez
import org.gvsig.raster.beans.previewbase.ParamStruct;
28 11856 bsanchez
import org.gvsig.raster.buffer.BufferFactory;
29 16544 nbrodin
import org.gvsig.raster.buffer.RasterBuffer;
30 18857 nbrodin
import org.gvsig.raster.buffer.WriterBufferServer;
31 11856 bsanchez
import org.gvsig.raster.dataset.GeoRasterWriter;
32
import org.gvsig.raster.dataset.IBuffer;
33 13328 nacho
import org.gvsig.raster.dataset.IRasterDataSource;
34 11856 bsanchez
import org.gvsig.raster.dataset.NotSupportedExtensionException;
35 16591 nbrodin
import org.gvsig.raster.dataset.io.RasterDriverException;
36 17882 bsanchez
import org.gvsig.raster.dataset.io.rmf.RmfBlocksManager;
37 18368 bsanchez
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
38 17882 bsanchez
import org.gvsig.raster.datastruct.serializer.NoDataRmfSerializer;
39 11856 bsanchez
import org.gvsig.raster.grid.Grid;
40 18368 bsanchez
import org.gvsig.raster.grid.GridTransparency;
41 17110 nbrodin
import org.gvsig.raster.grid.filter.FilterTypeException;
42 11897 bsanchez
import org.gvsig.raster.grid.filter.IRasterFilterListManager;
43 11815 bsanchez
import org.gvsig.raster.grid.filter.RasterFilterList;
44 11897 bsanchez
import org.gvsig.raster.grid.filter.RasterFilterListManager;
45 18368 bsanchez
import org.gvsig.raster.hierarchy.IRasterRendering;
46 13987 nacho
import org.gvsig.raster.util.RasterToolsUtil;
47 17882 bsanchez
import org.gvsig.raster.util.RasterUtilities;
48 11856 bsanchez
49
import com.iver.andami.PluginServices;
50 11803 bsanchez
/**
51 19568 bsanchez
 * Clase donde se hara todo el proceso de aplicar una lista de filtros a una
52
 * capa. Muestra una ventana de dialogo de incremento informativa.
53 11803 bsanchez
 *
54
 * @version 24/05/2007
55 12369 bsanchez
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
56 11803 bsanchez
 */
57 17718 bsanchez
public class FilterProcess extends RasterProcess {
58
        private String            filename         = "";
59
        private IRasterDataSource rasterDataSource = null;
60
        private ArrayList         listFilterUsed   = null;
61 11803 bsanchez
62 17718 bsanchez
        private RasterFilterList  rasterFilterList = null;
63
        private GeoRasterWriter   geoRasterWriter  = null;
64 18368 bsanchez
        private IRasterRendering  rendering        = null;
65 11815 bsanchez
66 17718 bsanchez
        /*
67
         * (non-Javadoc)
68
         * @see org.gvsig.rastertools.RasterProcess#init()
69 11815 bsanchez
         */
70 17718 bsanchez
        public void init() {
71 18368 bsanchez
                rendering = (IRasterRendering) getParam("rendering");
72 17718 bsanchez
                filename = getStringParam("filename");
73
                rasterDataSource = (IRasterDataSource) getParam("rasterdatasource");
74
                listFilterUsed = (ArrayList) getParam("listfilterused");
75 11815 bsanchez
        }
76 18368 bsanchez
77
        /**
78
         *
79
         * @param geoRasterWriter
80
         */
81 20610 bsanchez
        private DatasetColorInterpretation getColorIntepretation(IBuffer buffer, Grid grid) {
82 18368 bsanchez
83
                DatasetColorInterpretation colorInterpretation = null;
84
85
                do {
86
                        // Si tiene una tabla de color asignamos las tres bandas
87
                        if (grid.getFilterList().isActive("colortable")) {
88
                                colorInterpretation = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.RED_BAND, DatasetColorInterpretation.GREEN_BAND, DatasetColorInterpretation.BLUE_BAND });
89
                                break;
90
                        }
91
92
                        // Si el numero de bandas coincide asignamos la misma interpretacion que tenia antes
93 20721 bsanchez
                        if ((rendering != null) && (buffer.getBandCount() == rasterDataSource.getBandCount())) {
94 20865 nbrodin
                                colorInterpretation = rasterDataSource.getColorInterpretation(0);
95 18368 bsanchez
                                break;
96
                        }
97
98
                        String[] colorInterp = new String[rasterDataSource.getColorInterpretation(0).getValues().length];
99
100
                        for (int i = 0; i<rasterDataSource.getColorInterpretation(0).getValues().length; i++) {
101
                                if (rasterDataSource.getColorInterpretation(0).getValues()[i].equals(DatasetColorInterpretation.UNDEF_BAND)) {
102
                                        colorInterp[i] = DatasetColorInterpretation.GRAY_BAND;
103
                                        continue;
104
                                }
105
                                colorInterp[i] = rasterDataSource.getColorInterpretation(0).getValues()[i];
106
                        }
107
                        colorInterpretation = new DatasetColorInterpretation(colorInterp);
108
                } while (false);
109
110
                return colorInterpretation;
111
        }
112
113 11803 bsanchez
        /*
114
         * (non-Javadoc)
115 17718 bsanchez
         * @see org.gvsig.rastertools.RasterProcess#process()
116 11803 bsanchez
         */
117 17718 bsanchez
        public void process() throws InterruptedException {
118
                WriterBufferServer writerBufferServer = null;
119 16580 bsanchez
120 16544 nbrodin
                IRasterDataSource dsetCopy = null;
121 12325 bsanchez
                try {
122 18014 bsanchez
                        insertLineLog(RasterToolsUtil.getText(this, "leyendo_raster"));
123 17882 bsanchez
                        dsetCopy = rasterDataSource.newDataset();
124 16544 nbrodin
                        BufferFactory bufferFactory = new BufferFactory(dsetCopy);
125 17718 bsanchez
                        if (!RasterBuffer.loadInMemory(dsetCopy))
126 16544 nbrodin
                                bufferFactory.setReadOnly(true);
127 12325 bsanchez
                        bufferFactory.setAllDrawableBands();
128 11856 bsanchez
129 12325 bsanchez
                        IBuffer buffer = null;
130 12283 bsanchez
131 12325 bsanchez
                        writerBufferServer = new WriterBufferServer();
132 11856 bsanchez
133 12325 bsanchez
                        bufferFactory.setAreaOfInterest();
134 16580 bsanchez
135 12325 bsanchez
                        Grid grid = new Grid(bufferFactory, true);
136 18368 bsanchez
137 18804 nbrodin
                        //Obtenemos la lista de filtros
138 12325 bsanchez
                        rasterFilterList = grid.getFilterList();
139 18804 nbrodin
140
                        //Aplicamos los filtros usados en el panel
141 12325 bsanchez
                        addSelectedFilters(rasterFilterList, listFilterUsed);
142 18804 nbrodin
143 18014 bsanchez
                        insertLineLog(RasterToolsUtil.getText(this, "aplicando_filtros"));
144 16580 bsanchez
145 20120 bsanchez
                        grid.setNoDataValue(rasterDataSource.getNoDataValue());
146 17882 bsanchez
147 18804 nbrodin
                        //Asignamos el n?mero de banda alpha. Si no tiene asignar? -1 y no se usar?
148
                        grid.getFilterList().addEnvParam("alphaBandNumber", new Integer(rasterDataSource.getTransparencyFilesStatus().getAlphaBandNumber()));
149
150 12325 bsanchez
                        grid.applyFilters();
151 18368 bsanchez
152
                        GridTransparency transparency = (GridTransparency) grid.getFilterList().getEnv().get("Transparency");
153 11856 bsanchez
154 18014 bsanchez
                        insertLineLog(RasterToolsUtil.getText(this, "guardando_capa"));
155 11945 bsanchez
156 12325 bsanchez
                        buffer = grid.getRasterBuf();
157 11897 bsanchez
158 12325 bsanchez
                        writerBufferServer.setBuffer(buffer, -1);
159 17882 bsanchez
                        // TODO: FUNCIONALIDAD: Poner los getWriter con la proyecci?n del fichero fuente
160 18804 nbrodin
161 19357 bsanchez
                        //En el caso de que la imagen no tuviera una banda alpha creamos una y la asignamos en el WriteBufferServer
162 19476 nbrodin
                        if (rasterDataSource.getTransparencyFilesStatus().getAlphaBandNumber() < 0 && transparency.getAlphaBand() != null) {
163 19357 bsanchez
                                writerBufferServer.setAlphaBuffer(transparency.getAlphaBand());
164
                                geoRasterWriter = GeoRasterWriter.getWriter(writerBufferServer, filename, buffer.getBandCount() + 1, rasterDataSource.getAffineTransform(), buffer.getWidth(), buffer.getHeight(), buffer.getDataType(), GeoRasterWriter.getWriter(filename).getParams(), null);
165
                        } else //Si ya tiene una banda de transparencia se seguir? usando la que ten?a
166
                                geoRasterWriter = GeoRasterWriter.getWriter(writerBufferServer, filename, buffer.getBandCount(), rasterDataSource.getAffineTransform(), buffer.getWidth(), buffer.getHeight(), buffer.getDataType(), GeoRasterWriter.getWriter(filename).getParams(), null);
167 18368 bsanchez
168 18804 nbrodin
                        //Asignamos la interpretaci?n de color al escritor
169 20610 bsanchez
                        DatasetColorInterpretation colorInterpretation = getColorIntepretation(buffer, grid);
170 18368 bsanchez
                        if (transparency.isTransparencyActive()) {
171
                                DatasetColorInterpretation alphaI;
172
                                alphaI = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.ALPHA_BAND });
173
                                colorInterpretation.addColorInterpretation(alphaI);
174
                        }
175 11897 bsanchez
176 18368 bsanchez
                        geoRasterWriter.setColorBandsInterpretation(colorInterpretation.getValues());
177
178 17718 bsanchez
                        geoRasterWriter.dataWrite();
179
                        geoRasterWriter.writeClose();
180 17882 bsanchez
181
                        // Guardamos en el RMF del fichero el valor NoData
182 17977 bsanchez
                        if (Configuration.getValue("nodata_transparency_enabled", Boolean.FALSE).booleanValue()) {
183
                                String filenameRMF = RasterUtilities.getNameWithoutExtension(filename) + ".rmf";
184
                                RmfBlocksManager manager = dsetCopy.getDataset(0)[0].getRmfBlocksManager();
185
                                NoDataRmfSerializer ser;
186 20867 bsanchez
                                ser = new NoDataRmfSerializer(rasterDataSource.getNoDataValue(), rasterDataSource.isNoDataEnabled()?2:0, rasterDataSource.getDataType()[0]);
187 17977 bsanchez
188
                                manager.setPath(filenameRMF);
189
190
                                if (!manager.checkRmf())
191
                                        return;
192
193
                                manager.addClient(ser);
194
                                try {
195
                                        manager.write(true);
196
                                } catch (FileNotFoundException e) {
197 18014 bsanchez
                                        RasterToolsUtil.messageBoxError("error_salvando_rmf", this, e);
198 17977 bsanchez
                                } catch (IOException e) {
199 18014 bsanchez
                                        RasterToolsUtil.messageBoxError("error_salvando_rmf", this, e);
200 17977 bsanchez
                                }
201
                                manager.removeClient(ser.getClass());
202 17882 bsanchez
                        }
203
204 20721 bsanchez
                        if (externalActions != null)
205
                                externalActions.end(filename);
206 17882 bsanchez
207 11856 bsanchez
                } catch (NotSupportedExtensionException e) {
208 18014 bsanchez
                        RasterToolsUtil.messageBoxError("error_writer_notsupportedextension", this, e);
209 11856 bsanchez
                } catch (RasterDriverException e) {
210 18014 bsanchez
                        RasterToolsUtil.messageBoxError("error_writer", this, e);
211 11856 bsanchez
                } catch (IOException e) {
212 18014 bsanchez
                        RasterToolsUtil.messageBoxError("error_writer", this, e);
213 17110 nbrodin
                } catch (FilterTypeException e) {
214 18014 bsanchez
                        RasterToolsUtil.messageBoxError("error_adding_filters", this, e);
215 11815 bsanchez
                }
216 12325 bsanchez
        }
217 11856 bsanchez
218 11803 bsanchez
        /**
219 12344 bsanchez
         * Sustituye la lista de filtros de filterList por la que le pasamos en forma
220
         * de ArrayList
221
         * @param filterList
222
         * @param listFilterUsed
223 17110 nbrodin
         * @throws FilterTypeException
224 12344 bsanchez
         */
225 17110 nbrodin
        public static void addSelectedFilters(RasterFilterList filterList, ArrayList listFilterUsed) throws FilterTypeException {
226 11897 bsanchez
                filterList.clear();
227
                RasterFilterListManager stackManager = new RasterFilterListManager(filterList);
228
229 17718 bsanchez
                for (int i = 0; i < listFilterUsed.size(); i++) {
230 11897 bsanchez
                        ParamStruct aux = (ParamStruct) listFilterUsed.get(i);
231
                        IRasterFilterListManager filterManager = stackManager.getManagerByFilterClass(aux.getFilterClass());
232
                        filterManager.addFilter(aux.getFilterClass(), aux.getFilterParam());
233
                }
234
235
                filterList.resetPercent();
236
        }
237
238 11803 bsanchez
        /*
239
         * (non-Javadoc)
240
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
241
         */
242
        public int getPercent() {
243 11897 bsanchez
                if (rasterFilterList == null)
244
                        return 0;
245
246 11856 bsanchez
                if (rasterFilterList.getPercent() < 100)
247
                        return rasterFilterList.getPercent();
248 11897 bsanchez
249 17718 bsanchez
                if (geoRasterWriter == null)
250 11897 bsanchez
                        return 0;
251
252 17718 bsanchez
                return geoRasterWriter.getPercent();
253 11803 bsanchez
        }
254
255
        /*
256
         * (non-Javadoc)
257
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
258
         */
259
        public String getTitle() {
260 11856 bsanchez
                return PluginServices.getText(this, "aplicando_filtros");
261 11803 bsanchez
        }
262
}