Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / filter / FilterProcess.java @ 18368

History | View | Annotate | Download (9.74 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.rastertools.filter;
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.buffer.BufferFactory;
27
import org.gvsig.raster.buffer.RasterBuffer;
28
import org.gvsig.raster.dataset.GeoRasterWriter;
29
import org.gvsig.raster.dataset.IBuffer;
30
import org.gvsig.raster.dataset.IRasterDataSource;
31
import org.gvsig.raster.dataset.NotSupportedExtensionException;
32
import org.gvsig.raster.dataset.io.RasterDriverException;
33
import org.gvsig.raster.dataset.io.rmf.RmfBlocksManager;
34
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
35
import org.gvsig.raster.datastruct.serializer.NoDataRmfSerializer;
36
import org.gvsig.raster.grid.Grid;
37
import org.gvsig.raster.grid.GridTransparency;
38
import org.gvsig.raster.grid.filter.FilterTypeException;
39
import org.gvsig.raster.grid.filter.IRasterFilterListManager;
40
import org.gvsig.raster.grid.filter.RasterFilterList;
41
import org.gvsig.raster.grid.filter.RasterFilterListManager;
42
import org.gvsig.raster.grid.filter.bands.NoDataFilter;
43
import org.gvsig.raster.hierarchy.IRasterRendering;
44
import org.gvsig.raster.util.RasterToolsUtil;
45
import org.gvsig.raster.util.RasterUtilities;
46
import org.gvsig.rastertools.RasterProcess;
47
import org.gvsig.rastertools.clipping.WriterBufferServer;
48
import org.gvsig.rastertools.filter.FilterListener.ParamStruct;
49

    
50
import com.iver.andami.PluginServices;
51
/**
52
 * Clase donde se hara todo el proceso de aplicar un filtro a una capa. Muestra
53
 * 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
         * 
80
         * @param geoRasterWriter
81
         */
82
        private DatasetColorInterpretation getColorIntepretation(GeoRasterWriter geoRasterWriter, IBuffer buffer, Grid grid) {
83
                
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 (buffer.getBandCount() == rasterDataSource.getBandCount()) {
95
                                colorInterpretation = rendering.getRender().getColorInterpretation();
96
                                break;
97
                        }
98

    
99
                        String[] colorInterp = new String[rasterDataSource.getColorInterpretation(0).getValues().length];
100
                        
101
                        for (int i = 0; i<rasterDataSource.getColorInterpretation(0).getValues().length; i++) {
102
                                if (rasterDataSource.getColorInterpretation(0).getValues()[i].equals(DatasetColorInterpretation.UNDEF_BAND)) {
103
                                        colorInterp[i] = DatasetColorInterpretation.GRAY_BAND;
104
                                        continue;
105
                                }
106
                                colorInterp[i] = rasterDataSource.getColorInterpretation(0).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
                try {
123
                        insertLineLog(RasterToolsUtil.getText(this, "leyendo_raster"));
124
                        dsetCopy = rasterDataSource.newDataset();
125
                        BufferFactory bufferFactory = new BufferFactory(dsetCopy);
126
                        if (!RasterBuffer.loadInMemory(dsetCopy))
127
                                bufferFactory.setReadOnly(true);
128
                        bufferFactory.setAllDrawableBands();
129

    
130
                        IBuffer buffer = null;
131

    
132
                        writerBufferServer = new WriterBufferServer();
133

    
134
                        bufferFactory.setAreaOfInterest();
135

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

    
138
                        // Obtengo la lista de filtros
139
                        rasterFilterList = grid.getFilterList();
140
                        // Le aplico los filtros usados en el panel
141
                        addSelectedFilters(rasterFilterList, listFilterUsed);
142
                        // Quito el filtro NoData porque vamos a guardar su valor en un RMF
143
                        rasterFilterList.remove(NoDataFilter.class);
144

    
145
                        insertLineLog(RasterToolsUtil.getText(this, "aplicando_filtros"));
146

    
147
                        grid.setNoDataValue(rasterDataSource.getNoDataValue(0));
148

    
149
                        grid.applyFilters();
150
                        
151
                        GridTransparency transparency = (GridTransparency) grid.getFilterList().getEnv().get("Transparency");
152

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

    
155
                        buffer = grid.getRasterBuf();
156

    
157
                        writerBufferServer.setBuffer(buffer, -1);
158
                        // TODO: FUNCIONALIDAD: Poner los getWriter con la proyecci?n del fichero fuente
159
                        if (transparency.isTransparencyActive()) {
160
                                // Asigno el buffer alpha
161
                                writerBufferServer.setAlphaBuffer(transparency.getAlphaBand());
162
                                // Creo un GeoRasterWriter con una banda mas para el buffer alpha
163
                                geoRasterWriter = GeoRasterWriter.getWriter(writerBufferServer, filename, buffer.getBandCount() + 1, rasterDataSource.getAffineTransform(), buffer.getWidth(), buffer.getHeight(), buffer.getDataType(), GeoRasterWriter.getWriter(filename).getParams(), null);
164
                        } else {
165
                                // Creo un GeoRasterWriter
166
                                geoRasterWriter = GeoRasterWriter.getWriter(writerBufferServer, filename, buffer.getBandCount(), rasterDataSource.getAffineTransform(), buffer.getWidth(), buffer.getHeight(), buffer.getDataType(), GeoRasterWriter.getWriter(filename).getParams(), null);
167
                        }
168
                        
169
                        DatasetColorInterpretation colorInterpretation = getColorIntepretation(geoRasterWriter, 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(Double.valueOf(rasterDataSource.getNoDataValue(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
                        externalActions.end(filename);
205

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

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

    
228
                for (int i = 0; i < listFilterUsed.size(); i++) {
229
                        ParamStruct aux = (ParamStruct) listFilterUsed.get(i);
230
                        IRasterFilterListManager filterManager = stackManager.getManagerByFilterClass(aux.getFilterClass());
231
                        filterManager.addFilter(aux.getFilterClass(), aux.getFilterParam());
232
                }
233

    
234
                filterList.resetPercent();
235
        }
236

    
237
        /*
238
         * (non-Javadoc)
239
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
240
         */
241
        public int getPercent() {
242
                if (rasterFilterList == null)
243
                        return 0;
244

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

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

    
251
                return geoRasterWriter.getPercent();
252
        }
253

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