Statistics
| Revision:

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

History | View | Annotate | Download (10 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.FileNotFoundException;
22
import java.io.IOException;
23
import java.util.ArrayList;
24

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

    
49
import com.iver.andami.PluginServices;
50
/**
51
 * 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
 *
54
 * @version 24/05/2007
55
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
56
 */
57
public class FilterProcess extends RasterProcess {
58
        private String            filename         = "";
59
        private IRasterDataSource rasterDataSource = null;
60
        private ArrayList         listFilterUsed   = null;
61

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

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

    
77
        /**
78
         * 
79
         * @param geoRasterWriter
80
         */
81
        private DatasetColorInterpretation getColorIntepretation(IBuffer buffer, Grid grid) {
82
                
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
                        if ((rendering != null) && (buffer.getBandCount() == rasterDataSource.getBandCount())) {
94
                                colorInterpretation = rasterDataSource.getColorInterpretation(0);
95
                                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
        /*
114
         * (non-Javadoc)
115
         * @see org.gvsig.rastertools.RasterProcess#process()
116
         */
117
        public void process() throws InterruptedException {
118
                WriterBufferServer writerBufferServer = null;
119

    
120
                IRasterDataSource dsetCopy = null;
121
                try {
122
                        insertLineLog(RasterToolsUtil.getText(this, "leyendo_raster"));
123
                        dsetCopy = rasterDataSource.newDataset();
124
                        BufferFactory bufferFactory = new BufferFactory(dsetCopy);
125
                        if (!RasterBuffer.loadInMemory(dsetCopy))
126
                                bufferFactory.setReadOnly(true);
127
                        bufferFactory.setAllDrawableBands();
128

    
129
                        IBuffer buffer = null;
130

    
131
                        writerBufferServer = new WriterBufferServer();
132

    
133
                        bufferFactory.setAreaOfInterest();
134

    
135
                        Grid grid = new Grid(bufferFactory, true);
136

    
137
                        //Obtenemos la lista de filtros
138
                        rasterFilterList = grid.getFilterList();
139
                        
140
                        //Aplicamos los filtros usados en el panel
141
                        addSelectedFilters(rasterFilterList, listFilterUsed);
142
                        
143
                        insertLineLog(RasterToolsUtil.getText(this, "aplicando_filtros"));
144

    
145
                        grid.setNoDataValue(rasterDataSource.getNoDataValue());
146

    
147
                        //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
                        grid.applyFilters();
151
                        
152
                        GridTransparency transparency = (GridTransparency) grid.getFilterList().getEnv().get("Transparency");
153

    
154
                        insertLineLog(RasterToolsUtil.getText(this, "guardando_capa"));
155

    
156
                        buffer = grid.getRasterBuf();
157

    
158
                        writerBufferServer.setBuffer(buffer, -1);
159
                        // TODO: FUNCIONALIDAD: Poner los getWriter con la proyecci?n del fichero fuente
160
                        
161
                        //En el caso de que la imagen no tuviera una banda alpha creamos una y la asignamos en el WriteBufferServer
162
                        if (rasterDataSource.getTransparencyFilesStatus().getAlphaBandNumber() < 0 && transparency.getAlphaBand() != null) {
163
                                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
                        
168
                        //Asignamos la interpretaci?n de color al escritor
169
                        DatasetColorInterpretation colorInterpretation = getColorIntepretation(buffer, grid);
170
                        if (transparency.isTransparencyActive()) {
171
                                DatasetColorInterpretation alphaI;
172
                                alphaI = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.ALPHA_BAND });
173
                                colorInterpretation.addColorInterpretation(alphaI);
174
                        }
175

    
176
                        geoRasterWriter.setColorBandsInterpretation(colorInterpretation.getValues());
177

    
178
                        geoRasterWriter.dataWrite();
179
                        geoRasterWriter.writeClose();
180
                        
181
                        // Guardamos en el RMF del fichero el valor NoData
182
                        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
                                ser = new NoDataRmfSerializer(rasterDataSource.getNoDataValue(), rasterDataSource.isNoDataEnabled()?2:0, rasterDataSource.getDataType()[0]);
187
        
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
                                        RasterToolsUtil.messageBoxError("error_salvando_rmf", this, e);
198
                                } catch (IOException e) {
199
                                        RasterToolsUtil.messageBoxError("error_salvando_rmf", this, e);
200
                                }
201
                                manager.removeClient(ser.getClass());
202
                        }
203
                        
204
                        if (externalActions != null)
205
                                externalActions.end(filename);
206

    
207
                } catch (NotSupportedExtensionException e) {
208
                        RasterToolsUtil.messageBoxError("error_writer_notsupportedextension", this, e);
209
                } catch (RasterDriverException e) {
210
                        RasterToolsUtil.messageBoxError("error_writer", this, e);
211
                } catch (IOException e) {
212
                        RasterToolsUtil.messageBoxError("error_writer", this, e);
213
                } catch (FilterTypeException e) {
214
                        RasterToolsUtil.messageBoxError("error_adding_filters", this, e);
215
                }
216
        }
217

    
218
        /**
219
         * Sustituye la lista de filtros de filterList por la que le pasamos en forma
220
         * de ArrayList
221
         * @param filterList
222
         * @param listFilterUsed
223
         * @throws FilterTypeException 
224
         */
225
        public static void addSelectedFilters(RasterFilterList filterList, ArrayList listFilterUsed) throws FilterTypeException {
226
                filterList.clear();
227
                RasterFilterListManager stackManager = new RasterFilterListManager(filterList);
228

    
229
                for (int i = 0; i < listFilterUsed.size(); i++) {
230
                        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
        /*
239
         * (non-Javadoc)
240
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
241
         */
242
        public int getPercent() {
243
                if (rasterFilterList == null)
244
                        return 0;
245

    
246
                if (rasterFilterList.getPercent() < 100)
247
                        return rasterFilterList.getPercent();
248

    
249
                if (geoRasterWriter == null)
250
                        return 0;
251

    
252
                return geoRasterWriter.getPercent();
253
        }
254

    
255
        /*
256
         * (non-Javadoc)
257
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
258
         */
259
        public String getTitle() {
260
                return PluginServices.getText(this, "aplicando_filtros");
261
        }
262
}