Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extRemoteSensing / src / org / gvsig / remotesensing / mosaic / process / HistogramMatchProcess.java @ 26348

History | View | Annotate | Download (8.91 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
         *
3
         * Copyright (C) 2006 Instituto de Desarrollo Regional 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
         * For more information, contact:
20
         *
21
         *  Generalitat Valenciana
22
         *   Conselleria d'Infraestructures i Transport
23
         *   Av. Blasco Iba?ez, 50
24
         *   46010 VALENCIA
25
         *   SPAIN
26
         *
27
         *      +34 963862235
28
         *   gvsig@gva.es
29
         *      www.gvsig.gva.es
30
         *
31
         *    or
32
         *
33
         *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
         *   Campus Universitario s/n
35
         *   02071 Alabacete
36
         *   Spain
37
         *
38
         *   +34 967 599 200
39
         */
40

    
41
package org.gvsig.remotesensing.mosaic.process;
42

    
43
import java.awt.image.DataBuffer;
44
import java.io.File;
45
import java.io.IOException;
46

    
47
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
48
import org.gvsig.raster.RasterProcess;
49
import org.gvsig.raster.buffer.BufferFactory;
50
import org.gvsig.raster.buffer.RasterBuffer;
51
import org.gvsig.raster.buffer.WriterBufferServer;
52
import org.gvsig.raster.dataset.FileNotOpenException;
53
import org.gvsig.raster.dataset.GeoRasterWriter;
54
import org.gvsig.raster.dataset.IBuffer;
55
import org.gvsig.raster.dataset.IRasterDataSource;
56
import org.gvsig.raster.dataset.NotSupportedExtensionException;
57
import org.gvsig.raster.dataset.io.RasterDriverException;
58
import org.gvsig.raster.datastruct.Histogram;
59
import org.gvsig.raster.grid.filter.RasterFilter;
60
import org.gvsig.raster.grid.filter.enhancement.EnhancementStretchListManager;
61
import org.gvsig.raster.grid.filter.enhancement.LinearStretchParams;
62
import org.gvsig.raster.grid.filter.histogramMatching.HistogramMatchingByteFilter;
63
import org.gvsig.raster.util.RasterToolsUtil;
64

    
65
import com.iver.andami.PluginServices;
66
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
67

    
68

    
69
/** 
70
 * Clase que implementa el proceso de Histogram Matching para un conjuto de  imagenes
71
 * que se quieren hacer corresponder con un histogram de referencia. El proceso tomara como parametros 
72
 * un conjunto de capas y un histograma maestro y ejecutara de forma secuencial un filtro de histogramMatch 
73
 * para cada una de ellas. El proceso se realiza sobre las bandas de visulaizaci?n.
74
 * 
75
 * @params
76
 * <LI>FLyrRasterSE[] "inputRasterLayers": Capas raster de entrada</LI>
77
 * <LI>Histogram "masterHistogram": Histograma con el que se hace la correspondencia</LI>
78
 * <LI>int "numbandas": PROVISIONAL.</LI>
79
 * <LI>String "outputPath": Ruta completa al fichero de salida del proceso</LI>
80
 * 
81
 * @result
82
 * <LI>outputRassterLayers[]: Capas raster resultantes</LI>
83
 *
84
 * par histograma source - histograma referencia.
85
 * 
86
 * @author aMu?oz (alejandro.mu?oz@uclm.es)
87
 * @version 30/4/2008
88
 * */
89
public class HistogramMatchProcess  extends RasterProcess{
90

    
91
        private FLyrRasterSE                         inputRasterLayers[]                                = null;
92
        private FLyrRasterSE                         outputRassterLayers[]                         = null;
93
        private Histogram                                 histogramMaster                                        = null;
94
        private String                                         layerInProcess                                        = null;
95
        private int                                         countInProcess                                        = 0,totalcount= 0;
96
        private int                                         numbands                                                 = 0;
97
        private RasterFilter                         filtro                                                        = new HistogramMatchingByteFilter();
98
        private IBuffer                                 rasterResult                                        = null;
99
        private WriterBufferServer                 writerBufferServer                                = null;
100
        private String                                         path                                                         = null;
101
        
102
        
103
        /** Metodo en el que se recogen los par?metros para el proceso
104
         *  layers conjunto de capas a las que se aplica el proceso
105
         *         histogramMaster histograma de referencia
106
         *  view vista sobre la que se cargan los resultados
107
         * */
108
        
109
        public void init() {
110
                inputRasterLayers =(FLyrRasterSE []) getParam("inputRasterLayers");
111
                outputRassterLayers = new FLyrRasterSE[inputRasterLayers.length];
112
                histogramMaster= (Histogram) getParam("masterHistogram");
113
                numbands = ((Integer)getParam("numbands")).intValue(); //******************************
114
                path = (String) getStringParam("outputPath");
115
        }
116

    
117
        
118
        /** Proceso.
119
         *  Para cada capa de las contenidas en layers, se aplica un filtro de histogramMatching
120
         *  entre la capa y el histograma de referencia pasado como paramero.
121
         * */
122
        public void process() throws InterruptedException {
123
                try{
124
                        int k=0;
125
                        totalcount= inputRasterLayers.length-1;
126
                        IBuffer inputBuffer= null;
127
                        for (int i=0; i< inputRasterLayers.length;i++){
128
                                // Para el resto de los histogramas seleccionados
129
                                if(inputRasterLayers[i]!=null){
130
                                        k++;
131
                                        layerInProcess= inputRasterLayers[i].getName();
132
                                        IRasterDataSource dsetCopy = null; 
133
                                        dsetCopy = inputRasterLayers[i].getDataSource().newDataset();
134
                                        BufferFactory bufferFactory = new BufferFactory(dsetCopy);
135
                                        bufferFactory.setDrawableBands(inputRasterLayers[i].getRenderBands());
136
                                        if (!RasterBuffer.loadInMemory(dsetCopy))
137
                                                bufferFactory.setReadOnly(true);        
138
                                        bufferFactory.setAreaOfInterest();
139
                                        
140
                                        inputBuffer = bufferFactory.getRasterBuf();
141
                                        //Aplicar filtro de realce si es necesario:
142
                                        
143
                                        if(inputBuffer.getDataType()!=DataBuffer.TYPE_BYTE){
144
                                                LinearStretchParams leParams = null;
145
                                                try {
146
                                                        leParams = LinearStretchParams.createStandardParam(inputRasterLayers[i].getRenderBands(), 0.0, bufferFactory.getDataSource().getStatistics(), false);
147
                                                } catch (FileNotOpenException e) {
148
                                                        // TODO Auto-generated catch block
149
                                                        e.printStackTrace();
150
                                                } catch (RasterDriverException e) {
151
                                                        // TODO Auto-generated catch block
152
                                                        e.printStackTrace();
153
                                                }
154
                                        
155
                                                RasterFilter linearStretchEnhancementFilter = EnhancementStretchListManager.createEnhancedFilter(leParams, bufferFactory.getDataSource().getStatistics(),
156
                                                                inputRasterLayers[i].getRenderBands(), false);
157
                                                linearStretchEnhancementFilter.addParam("raster", inputBuffer);
158
                                                linearStretchEnhancementFilter.execute();
159
                                                
160
                                                inputBuffer = (IBuffer)linearStretchEnhancementFilter.getResult("raster");
161
                                        }
162
                                        
163
                                        filtro.addParam("raster",inputBuffer);
164
                                        filtro.addParam("histogramReference",histogramMaster);
165
                                        filtro.addParam("numbands",new Integer(numbands));
166
                                        filtro.addParam("filterName",new String("histogram"));
167
                                        filtro.execute();
168
                                        rasterResult=(RasterBuffer) filtro.getResult("raster");
169
                                        outputRassterLayers[i] = createLayer(path + File.separator + "hist" + i + ".tif",i);        
170
                                } 
171
                        }
172
        
173
                }catch (Exception e2) {                
174
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "filter_error"), this, e2);                
175
                }
176
                finally{
177
                        externalActions.end(outputRassterLayers);
178
                }
179
                
180
        }
181
        
182
        
183
        
184
        public String getTitle() {
185
                return PluginServices.getText(this,"histohramMatchProcess");
186
        }
187

    
188
        public int getPercent() {
189
                if(writerBufferServer!=null)
190
                        return writerBufferServer.getPercent();
191
                else
192
                        return filtro.getPercent();
193
        }
194

    
195
        /** log con la informaci?n del proceso */
196
        public String getLog(){
197
                return PluginServices.getText(this,"hiatogramMatchLog")+layerInProcess+"  ("+countInProcess+"/"+totalcount+")";
198
                
199
        }
200
        
201
        
202
        public FLyrRasterSE createLayer(String path, int i){
203

    
204
                FLyrRasterSE lyr = null;  
205
                try{
206
                        if(path==null)
207
                                return null;
208
                        GeoRasterWriter grw = null;
209
                        writerBufferServer = new WriterBufferServer(rasterResult);
210
                        grw = GeoRasterWriter.getWriter(writerBufferServer, path, rasterResult.getBandCount(),inputRasterLayers[i].getAffineTransform(), rasterResult.getWidth(), rasterResult.getHeight(), rasterResult.getDataType(), GeoRasterWriter.getWriter(path).getParams(), inputRasterLayers[i].getProjection());
211
                        
212
                        grw.dataWrite();
213
                        grw.setWkt(inputRasterLayers[i].getWktProjection());
214
                        grw.writeClose();
215
                        rasterResult.free();
216
                        
217
                        int endIndex = path.lastIndexOf(".");
218
                        if (endIndex < 0)
219
                                endIndex = path.length();
220
                        lyr = FLyrRasterSE.createLayer(
221
                                        path.substring(path.lastIndexOf(File.separator) + 1, endIndex),
222
                                        path,
223
                                        null
224
                                        );
225
                        
226
                } catch (NotSupportedExtensionException e) {
227
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e);                
228
                } catch (RasterDriverException e) {
229
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);        
230
                } catch (IOException e) {
231
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);        
232
                } catch (InterruptedException e) {
233
                        Thread.currentThread().interrupt();
234
                } catch (LoadLayerException e) {
235
                        RasterToolsUtil.messageBoxError("error_cargar_capa", this, e);
236
                }
237
                return lyr;                
238
        }
239

    
240

    
241
        public Object getResult() {
242
                return outputRassterLayers;
243
        }
244

    
245
}