Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / filter / FilterProcess.java @ 17882

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

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

    
58
        private RasterFilterList  rasterFilterList = null;
59
        private GeoRasterWriter   geoRasterWriter  = null;
60

    
61
        /*
62
         * (non-Javadoc)
63
         * @see org.gvsig.rastertools.RasterProcess#init()
64
         */
65
        public void init() {
66
                filename = getStringParam("filename");
67
                rasterDataSource = (IRasterDataSource) getParam("rasterdatasource");
68
                listFilterUsed = (ArrayList) getParam("listfilterused");
69
        }
70
        
71
        /*
72
         * (non-Javadoc)
73
         * @see org.gvsig.rastertools.RasterProcess#process()
74
         */
75
        public void process() throws InterruptedException {
76
                WriterBufferServer writerBufferServer = null;
77

    
78
                IRasterDataSource dsetCopy = null;
79
                try {
80
                        insertLineLog(PluginServices.getText(this, "leyendo_raster"));
81
                        dsetCopy = rasterDataSource.newDataset();
82
                        BufferFactory bufferFactory = new BufferFactory(dsetCopy);
83
                        if (!RasterBuffer.loadInMemory(dsetCopy))
84
                                bufferFactory.setReadOnly(true);
85
                        bufferFactory.setAllDrawableBands();
86

    
87
                        IBuffer buffer = null;
88

    
89
                        writerBufferServer = new WriterBufferServer();
90

    
91
                        bufferFactory.setAreaOfInterest();
92

    
93
                        Grid grid = new Grid(bufferFactory, true);
94
                        rasterFilterList = grid.getFilterList();
95
                        addSelectedFilters(rasterFilterList, listFilterUsed);
96

    
97
                        insertLineLog(PluginServices.getText(this, "aplicando_filtros"));
98

    
99
                        grid.setNoDataValue(rasterDataSource.getNoDataValue(0));
100

    
101
                        grid.applyFilters();
102

    
103
                        insertLineLog(PluginServices.getText(this, "guardando_capa"));
104

    
105
                        buffer = grid.getRasterBuf();
106

    
107
                        writerBufferServer.setBuffer(buffer, -1);
108

    
109
                        // TODO: FUNCIONALIDAD: Poner los getWriter con la proyecci?n del fichero fuente
110
                        geoRasterWriter = GeoRasterWriter.getWriter(writerBufferServer, filename, buffer.getBandCount(), rasterDataSource.getAffineTransform(), buffer.getWidth(), buffer.getHeight(), buffer.getDataType(), GeoRasterWriter.getWriter(filename).getParams(), null);
111

    
112
                        geoRasterWriter.dataWrite();
113
                        geoRasterWriter.writeClose();
114
                        
115
                        // Guardamos en el RMF del fichero el valor NoData
116
                        String filenameRMF = RasterUtilities.getNameWithoutExtension(filename) + ".rmf";
117
                        RmfBlocksManager manager = dsetCopy.getDataset(0)[0].getRmfBlocksManager();
118
                        NoDataRmfSerializer ser;
119
                        ser = new NoDataRmfSerializer(Double.valueOf(rasterDataSource.getNoDataValue(0)));
120

    
121
                        manager.setPath(filenameRMF);
122

    
123
                        if (!manager.checkRmf())
124
                                return;
125

    
126
                        manager.addClient(ser);
127
                        try {
128
                                RasterUtilities.copyFile(manager.getPath(), manager.getPath() + "~");
129
                                manager.write(true);
130
                        } catch (FileNotFoundException e) {
131
                        } catch (IOException e) {
132

    
133
                        }
134
                        manager.removeClient(ser.getClass());
135
                        
136
                        externalActions.end(filename);
137

    
138
                } catch (NotSupportedExtensionException e) {
139
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e);
140
                } catch (RasterDriverException e) {
141
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);
142
                } catch (IOException e) {
143
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);
144
                } catch (FilterTypeException e) {
145
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_adding_filters"), this, e);
146
                }
147
        }
148

    
149
        /**
150
         * Sustituye la lista de filtros de filterList por la que le pasamos en forma
151
         * de ArrayList
152
         * @param filterList
153
         * @param listFilterUsed
154
         * @throws FilterTypeException 
155
         */
156
        public static void addSelectedFilters(RasterFilterList filterList, ArrayList listFilterUsed) throws FilterTypeException {
157
                filterList.clear();
158
                RasterFilterListManager stackManager = new RasterFilterListManager(filterList);
159

    
160
                for (int i = 0; i < listFilterUsed.size(); i++) {
161
                        ParamStruct aux = (ParamStruct) listFilterUsed.get(i);
162
                        IRasterFilterListManager filterManager = stackManager.getManagerByFilterClass(aux.getFilterClass());
163
                        filterManager.addFilter(aux.getFilterClass(), aux.getFilterParam());
164
                }
165

    
166
                filterList.resetPercent();
167
        }
168

    
169
        /*
170
         * (non-Javadoc)
171
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
172
         */
173
        public int getPercent() {
174
                if (rasterFilterList == null)
175
                        return 0;
176

    
177
                if (rasterFilterList.getPercent() < 100)
178
                        return rasterFilterList.getPercent();
179

    
180
                if (geoRasterWriter == null)
181
                        return 0;
182

    
183
                return geoRasterWriter.getPercent();
184
        }
185

    
186
        /*
187
         * (non-Javadoc)
188
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
189
         */
190
        public String getTitle() {
191
                return PluginServices.getText(this, "aplicando_filtros");
192
        }
193
}