Statistics
| Revision:

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

History | View | Annotate | Download (11.5 KB)

1
/* 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
package org.gvsig.raster.util.process;
20

    
21
import java.io.IOException;
22
import java.util.ArrayList;
23

    
24
import javax.swing.SwingUtilities;
25

    
26
import org.gvsig.raster.Configuration;
27
import org.gvsig.raster.RasterProcess;
28
import org.gvsig.raster.beans.previewbase.ParamStruct;
29
import org.gvsig.raster.buffer.BufferFactory;
30
import org.gvsig.raster.buffer.RasterBuffer;
31
import org.gvsig.raster.buffer.WriterBufferServer;
32
import org.gvsig.raster.dataset.GeoRasterWriter;
33
import org.gvsig.raster.dataset.IBuffer;
34
import org.gvsig.raster.dataset.IRasterDataSource;
35
import org.gvsig.raster.dataset.NotSupportedExtensionException;
36
import org.gvsig.raster.dataset.RasterDataset;
37
import org.gvsig.raster.dataset.io.RasterDriverException;
38
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
39
import org.gvsig.raster.dataset.serializer.RmfSerializerException;
40
import org.gvsig.raster.datastruct.NoData;
41
import org.gvsig.raster.datastruct.Transparency;
42
import org.gvsig.raster.grid.Grid;
43
import org.gvsig.raster.grid.filter.FilterTypeException;
44
import org.gvsig.raster.grid.filter.IRasterFilterListManager;
45
import org.gvsig.raster.grid.filter.RasterFilterList;
46
import org.gvsig.raster.grid.filter.RasterFilterListManager;
47
import org.gvsig.raster.hierarchy.IRasterRendering;
48
import org.gvsig.raster.util.RasterToolsUtil;
49

    
50
import com.iver.andami.PluginServices;
51
/**
52
 * Clase donde se hara todo el proceso de aplicar una lista de filtros a una
53
 * capa. Muestra una ventana de dialogo de incremento informativa.
54
 *
55
 * @version 24/05/2007
56
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
57
 */
58
public class FilterProcess extends RasterProcess {
59
        private String            filename         = "";
60
        private IRasterDataSource rasterDataSource = null;
61
        private ArrayList         listFilterUsed   = null;
62

    
63
        private RasterFilterList  rasterFilterList = null;
64
        private GeoRasterWriter   geoRasterWriter  = null;
65
        private IRasterRendering  rendering        = null;
66

    
67
        /*
68
         * (non-Javadoc)
69
         * @see org.gvsig.rastertools.RasterProcess#init()
70
         */
71
        public void init() {
72
                rendering = (IRasterRendering) getParam("rendering");
73
                filename = getStringParam("filename");
74
                rasterDataSource = (IRasterDataSource) getParam("rasterdatasource");
75
                listFilterUsed = (ArrayList) getParam("listfilterused");
76
        }
77

    
78
        /**
79
         * Devuelve una interpretacion de color. Es usado cuando no conseguimos un
80
         * rendering y no podemos saber que interpretacion l?gica ten?a antes.
81
         * @param geoRasterWriter
82
         */
83
        private DatasetColorInterpretation getColorIntepretation(IBuffer buffer, Grid grid) {
84
                DatasetColorInterpretation colorInterpretation = null;
85

    
86
                do {
87
                        // Si tiene una tabla de color asignamos las tres bandas
88
                        if (grid.getFilterList().isActive("colortable")) {
89
                                colorInterpretation = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.RED_BAND, DatasetColorInterpretation.GREEN_BAND, DatasetColorInterpretation.BLUE_BAND });
90
                                break;
91
                        }
92

    
93
                        // Si el numero de bandas coincide asignamos la misma interpretacion que tenia antes
94
                        if ((rendering != null) && (buffer.getBandCount() == rasterDataSource.getBandCount())) {
95
                                colorInterpretation = rasterDataSource.getColorInterpretation();
96
                                break;
97
                        }
98

    
99
                        String[] colorInterp = new String[rasterDataSource.getColorInterpretation().getValues().length];
100

    
101
                        for (int i = 0; i < rasterDataSource.getColorInterpretation().getValues().length; i++) {
102
                                if (rasterDataSource.getColorInterpretation().getValues()[i].equals(DatasetColorInterpretation.UNDEF_BAND)) {
103
                                        colorInterp[i] = DatasetColorInterpretation.GRAY_BAND;
104
                                        continue;
105
                                }
106
                                colorInterp[i] = rasterDataSource.getColorInterpretation().getValues()[i];
107
                        }
108
                        colorInterpretation = new DatasetColorInterpretation(colorInterp);
109
                } while (false);
110

    
111
                return colorInterpretation;
112
        }
113
        
114
        /*
115
         * (non-Javadoc)
116
         * @see org.gvsig.rastertools.RasterProcess#process()
117
         */
118
        public void process() throws InterruptedException {
119
                WriterBufferServer writerBufferServer = null;
120

    
121
                IRasterDataSource dsetCopy = null;
122
                BufferFactory bufferFactory = null;
123
                IBuffer buffer = null;
124
                
125
                try {
126
                        insertLineLog(RasterToolsUtil.getText(this, "leyendo_raster"));
127
                        
128
                        //Creaci?n del BufferFactory
129
                        dsetCopy = rasterDataSource.newDataset();
130
                        bufferFactory = new BufferFactory(dsetCopy);
131
                        if (!RasterBuffer.loadInMemory(dsetCopy))
132
                                bufferFactory.setReadOnly(true);
133

    
134
                        //Asignaci?n de bandas
135
                        if (rendering != null) {
136
                                int[] renderBands = rendering.getRenderBands();
137
                                // Si es gris, se reduce a una sola banda
138
                                if ((renderBands.length == 3) && (renderBands[0] == renderBands[1]) && (renderBands[1] == renderBands[2])) 
139
                                        renderBands = new int[] { renderBands[0] };
140
                                bufferFactory.setDrawableBands(renderBands);
141
                        } else
142
                                bufferFactory.setAllDrawableBands();
143

    
144
                        bufferFactory.setAreaOfInterest();
145
                        Grid grid = new Grid(bufferFactory, true);
146

    
147
                        //Obtenemos la lista de filtros
148
                        rasterFilterList = grid.getFilterList();
149
                        
150
                        //Aplicamos los filtros usados en el panel
151
                        addSelectedFilters(rasterFilterList, listFilterUsed);
152
                        
153
                        insertLineLog(RasterToolsUtil.getText(this, "aplicando_filtros"));
154

    
155
                        grid.setNoDataValue(rasterDataSource.getNoDataValue());
156
                        grid.applyFilters();
157
                        
158
                        insertLineLog(RasterToolsUtil.getText(this, "guardando_capa"));
159

    
160
                        buffer = grid.getRasterBuf();
161

    
162
                        writerBufferServer = new WriterBufferServer();
163
                        writerBufferServer.setBuffer(buffer, -1);
164
                        // TODO: FUNCIONALIDAD: Poner los getWriter con la proyecci?n del fichero fuente
165

    
166
                        //Calculo de la interpretaci?n de color
167
                        DatasetColorInterpretation colorInterpretation = null;
168
                        if((rendering != null && rendering.existColorTable()) || buffer.getBandCount() > 1)
169
                                colorInterpretation = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.RED_BAND, DatasetColorInterpretation.GREEN_BAND, DatasetColorInterpretation.BLUE_BAND });
170
                        else if(rendering != null && buffer.getBandCount() == 2) {
171
                                int[] renderBands = rendering.getRenderBands();
172
                                String[] ci = new String[renderBands.length];
173
                                for (int i = 0; i < renderBands.length; i++) {
174
                                        switch (renderBands[i]) {
175
                                        case 0:ci[i] = DatasetColorInterpretation.RED_BAND; break;
176
                                        case 1:ci[i] = DatasetColorInterpretation.GREEN_BAND; break;
177
                                        case 2:ci[i] = DatasetColorInterpretation.BLUE_BAND; break;
178
                                        default: ci[i] = DatasetColorInterpretation.UNDEF_BAND; 
179
                                        }
180
                                }
181
                                colorInterpretation = new DatasetColorInterpretation(ci);
182
                        } else
183
                                colorInterpretation = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.GRAY_BAND });
184
                        
185
                        //Si la imagen original ten?a una banda de transparencia se asignar? esta. Si los filtros generados
186
                        //crean una banda se mezclar? con la original. 
187
                        int nbands = buffer.getBandCount();
188
                        if(rasterDataSource.getTransparencyFilesStatus().getAlphaBandNumber() >= 0) {
189
                                bufferFactory.setDrawableBands(new int[]{rasterDataSource.getTransparencyFilesStatus().getAlphaBandNumber()});
190
                                bufferFactory.setAreaOfInterest();
191
                                IBuffer alpha = bufferFactory.getRasterBuf();
192
                                if(grid.getFilterList().getAlphaBand() != null)
193
                                        alpha = Transparency.merge(alpha, grid.getFilterList().getAlphaBand());
194
                                writerBufferServer.setAlphaBuffer(alpha);
195
                                //Asignamos la interpretaci?n de color de la banda alpha                                
196
                                DatasetColorInterpretation alphaI = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.ALPHA_BAND });
197
                                colorInterpretation.addColorInterpretation(alphaI);
198
                                nbands ++;
199
                        } else if(grid.getFilterList().getAlphaBand() != null) {
200
                                writerBufferServer.setAlphaBuffer(grid.getFilterList().getAlphaBand());
201
                                //Asignamos la interpretaci?n de color de la banda alpha
202
                                DatasetColorInterpretation alphaI = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.ALPHA_BAND });
203
                                colorInterpretation.addColorInterpretation(alphaI);
204
                                nbands ++;
205
                        }
206
                        
207
                        geoRasterWriter = GeoRasterWriter.getWriter(writerBufferServer, filename, nbands, rasterDataSource.getAffineTransform(), buffer.getWidth(), buffer.getHeight(), buffer.getDataType(), GeoRasterWriter.getWriter(filename).getParams(), null);
208
                        
209
                        if (rendering == null)
210
                                colorInterpretation = getColorIntepretation(buffer, grid);
211

    
212
                        geoRasterWriter.setColorBandsInterpretation(colorInterpretation.getValues());
213

    
214
                        geoRasterWriter.dataWrite();
215
                        geoRasterWriter.writeClose();
216
                        geoRasterWriter = null;
217
                        
218
                        // Guardamos en el RMF del fichero el valor NoData
219
                        if (Configuration.getValue("nodata_transparency_enabled", Boolean.FALSE).booleanValue()) {
220
                                try {
221
                                        RasterDataset.saveObjectToRmfFile(filename, NoData.class, new NoData(rasterDataSource.getNoDataValue(), rasterDataSource.isNoDataEnabled()?2:0, rasterDataSource.getDataType()[0]));
222
                                } catch (RmfSerializerException e) {
223
                                        RasterToolsUtil.messageBoxError(PluginServices.getText(this,"error_salvando_rmf"), this, e);
224
                                }
225
                        }
226
                        
227
                        SwingUtilities.invokeLater(new Runnable() {
228
                                public void run() {
229
                                        if (externalActions != null)
230
                                                externalActions.end(filename);
231
                                }
232
                        });
233
                } catch (NotSupportedExtensionException e) {
234
                        RasterToolsUtil.messageBoxError("error_writer_notsupportedextension", this, e);
235
                } catch (RasterDriverException e) {
236
                        RasterToolsUtil.messageBoxError("error_writer", this, e);
237
                } catch (IOException e) {
238
                        RasterToolsUtil.messageBoxError("error_writer", this, e);
239
                } catch (FilterTypeException e) {
240
                        RasterToolsUtil.messageBoxError("error_adding_filters", this, e);
241
                } finally {
242
                        rasterDataSource = null;
243
                        if(bufferFactory != null)
244
                                bufferFactory.free();
245
                        if(buffer != null)
246
                                buffer.free();
247
                }
248
        }
249

    
250
        /**
251
         * Sustituye la lista de filtros de filterList por la que le pasamos en forma
252
         * de ArrayList
253
         * @param filterList
254
         * @param listFilterUsed
255
         * @throws FilterTypeException 
256
         */
257
        public static void addSelectedFilters(RasterFilterList filterList, ArrayList listFilterUsed) throws FilterTypeException {
258
                filterList.clear();
259
                RasterFilterListManager stackManager = new RasterFilterListManager(filterList);
260

    
261
                for (int i = 0; i < listFilterUsed.size(); i++) {
262
                        ParamStruct aux = (ParamStruct) listFilterUsed.get(i);
263
                        IRasterFilterListManager filterManager = stackManager.getManagerByFilterClass(aux.getFilterClass());
264
                        filterManager.addFilter(aux.getFilterClass(), aux.getFilterParam());
265
                }
266

    
267
                filterList.resetPercent();
268
        }
269

    
270
        /*
271
         * (non-Javadoc)
272
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
273
         */
274
        public int getPercent() {
275
                if (rasterFilterList == null)
276
                        return 0;
277

    
278
                if (rasterFilterList.getPercent() < 100)
279
                        return rasterFilterList.getPercent();
280

    
281
                if (geoRasterWriter == null)
282
                        return 0;
283

    
284
                return geoRasterWriter.getPercent();
285
        }
286

    
287
        /*
288
         * (non-Javadoc)
289
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
290
         */
291
        public String getTitle() {
292
                return PluginServices.getText(this, "aplicando_filtros");
293
        }
294
}