Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / classification / ClassificationGeneralProcess.java @ 20872

History | View | Annotate | Download (9.22 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.classification;
42

    
43
import java.awt.Color;
44
import java.io.File;
45
import java.io.IOException;
46
import java.util.ArrayList;
47
import java.util.Iterator;
48

    
49
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
50
import org.gvsig.raster.RasterProcess;
51
import org.gvsig.raster.buffer.BufferFactory;
52
import org.gvsig.raster.buffer.RasterBuffer;
53
import org.gvsig.raster.buffer.RasterBufferInvalidException;
54
import org.gvsig.raster.buffer.WriterBufferServer;
55
import org.gvsig.raster.dataset.GeoRasterWriter;
56
import org.gvsig.raster.dataset.IRasterDataSource;
57
import org.gvsig.raster.dataset.NotSupportedExtensionException;
58
import org.gvsig.raster.dataset.io.RasterDriverException;
59
import org.gvsig.raster.datastruct.ColorItem;
60
import org.gvsig.raster.grid.Grid;
61
import org.gvsig.raster.grid.filter.FilterTypeException;
62
import org.gvsig.raster.grid.roi.ROI;
63
import org.gvsig.raster.util.RasterToolsUtil;
64
import org.gvsig.remotesensing.RemoteSensingUtils;
65

    
66
import com.iver.andami.PluginServices;
67
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
68
import com.iver.cit.gvsig.fmap.MapContext;
69
import com.iver.cit.gvsig.fmap.layers.FLayer;
70
import com.iver.cit.gvsig.project.documents.view.gui.View;
71

    
72
/**
73
 * ClassificationGeneraProccess es la clase abstracta base que extienden todos 
74
 * los metodos de clasificaci?n. Recoge procedimientos comunes a todos los metodos:
75
 * construccion del grid que se va a clasificar, asignacion de la leyenda a la capa
76
 * resultante o escritura en fichero. 
77
 * Cada procedimiento de clasificaci?n implementar? los m?todos que determinan la 
78
 * clase a la que pertenece un pixel, adem?s del metodo run(). 
79
 
80
 *@author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)
81
 *@author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
82
 *@version 19/10/2007 
83
 * */
84
public abstract class ClassificationGeneralProcess extends RasterProcess {
85
        
86
        protected Grid                                         inputGrid                        = null;
87
        protected FLyrRasterSE                        rasterSE                        = null;
88
        protected RasterBuffer                         rasterResult                = null;
89
        protected MapContext                         mapContext                         = null;
90
        protected int                                         percent                           = 0;
91
        protected ArrayList                                rois                                = null;
92
        protected WriterBufferServer         writerBufferServer        = null;
93
        protected String                                 filename                        = null;
94
        protected boolean                                selectedBands[]                = null;
95
        protected        int                                        numClases                        = 0;
96
        protected View                                         view                                = null;
97
        protected   int[]                                 bandList                        = null;
98
        protected boolean                                withdefaultClass                = false;
99
        
100
        /**
101
        * M?todo que determina la clase de pertenencia del pixel cuyos valores 
102
        * por banda se pasan en un array de bytes.
103
        * @param  valores del pixel en cada una de las bandas 
104
        * @return clase a la que pertenece el pixel
105
        */
106
        public abstract int getPixelClassForTypeByte(byte pixelBandsValues[]);
107
        
108
        /**
109
        * M?todo que determina la clase de pertenencia del pixel cuyos valores 
110
        * por banda se pasan en un array de shorts.
111
        * @param  valores del pixel en cada una de las bandas 
112
        * @return clase a la que pertenece el pixel
113
        */
114
        public abstract int getPixelClassForTypeShort(short pixelBandsValues[]);
115
                
116
        /**
117
        * M?todo que determina la clase de pertenencia del pixel cuyos valores 
118
        * por banda se pasan en un array de int.
119
        * @param  valores del pixel en cada una de las bandas 
120
        * @return clase a la que pertenece el pixel
121
        */
122
        public abstract int getPixelClassForTypeInt(int pixelBandsValues[]);
123
                
124
        /**
125
        * M?todo que determina la clase de pertenencia del pixel cuyos valores 
126
        * por banda se pasan en un array de float.
127
        * @param  valores del pixel en cada una de las bandas 
128
        * @return clase a la que pertenece el pixel
129
        */
130
        public abstract int getPixelClassForTypeFloat(float pixelBandsValues[]);
131
        
132
        
133
        /**
134
        * M?todo que determina la clase de pertenencia del pixel cuyos valores 
135
        * por banda se pasan en un array de double.
136
        * @param  valores del pixel en cada una de las bandas 
137
        * @return clase a la que pertenece el pixel
138
        */
139
        public abstract int getPixelClassForTypeDouble(double pixelBandsValues[]);
140
        
141
        
142
        /**
143
        * Establece el grid con las bandas recogidas en bandList. 
144
        */
145
        public  void setGrid(){
146
                
147
                IRasterDataSource dsetCopy = null; 
148
                dsetCopy = rasterSE.getDataSource().newDataset();
149
                BufferFactory bufferFactory = new BufferFactory(dsetCopy);
150
                if (!RasterBuffer.loadInMemory(dsetCopy))
151
                        bufferFactory.setReadOnly(true);
152
                try {        
153
                        bufferFactory.setAllDrawableBands();
154
                        inputGrid = new Grid(bufferFactory,bandList);        
155
                } catch (RasterBufferInvalidException e) {
156
                        RasterToolsUtil.messageBoxError("buffer_incorrecto", this, e);
157
                }
158
        }
159
                
160
        /**
161
        * Escritura del resultado a disco y carga de la capa en la vista.
162
        */
163
        public void writeToFile(){
164

    
165
                try{
166
                        GeoRasterWriter grw = null;
167
                        writerBufferServer = new WriterBufferServer(rasterResult);
168
                        grw = GeoRasterWriter.getWriter(writerBufferServer, filename, rasterResult.getBandCount(),rasterSE.getAffineTransform(), rasterResult.getWidth(), rasterResult.getHeight(), rasterResult.getDataType(), GeoRasterWriter.getWriter(filename).getParams(), null);
169
                        grw.dataWrite();
170
                        grw.setWkt(rasterSE.getWktProjection());
171
                        grw.writeClose();
172
                        rasterResult.free();
173
                        mapContext= view.getModel().getMapContext();
174
                        mapContext.beginAtomicEvent();
175
                        FLayer lyr = null;
176
                        int endIndex = filename.lastIndexOf(".");
177
                        if (endIndex < 0)
178
                                endIndex = filename.length();
179
                
180
                        lyr = FLyrRasterSE.createLayer(
181
                                        filename.substring(filename.lastIndexOf(File.separator) + 1, endIndex),
182
                                        filename,
183
                                        view.getMapControl().getProjection()
184
                                        );
185
                        
186
                        /*
187
                         * A?adir la leyenda a la nueva capa.
188
                         */
189
                        ArrayList colorItems = new ArrayList();
190
                        ColorItem colorItem = null;
191
                        int classValue = 0;
192
                        for (Iterator iter = rois.iterator(); iter.hasNext();) {
193
                                ROI roi = (ROI) iter.next();
194
                                colorItem = new ColorItem();
195
                                colorItem.setColor(roi.getColor());
196
                                colorItem.setNameClass(roi.getName());
197
                                colorItem.setValue(classValue);
198
                                colorItems.add(colorItem);
199
                                classValue++;
200
                        }
201
                        if(withdefaultClass){
202
                                ColorItem defaultColor = new ColorItem();
203
                                defaultColor.setColor(Color.BLACK);
204
                                defaultColor.setNameClass("no_class_assigned");
205
                                defaultColor.setValue(classValue);
206
                                colorItems.add(defaultColor);
207
                        }
208
                        RemoteSensingUtils.setLeyend(lyr,colorItems);
209
        
210
                        mapContext.getLayers().addLayer(lyr);
211
                        mapContext.endAtomicEvent();
212
                        mapContext.invalidate();
213
                
214
                } catch (NotSupportedExtensionException e) {
215
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e);
216
                } catch (RasterDriverException e) {
217
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);        
218
                } catch (IOException e) {
219
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);
220
                }catch (LoadLayerException e) {
221
                        RasterToolsUtil.messageBoxError("error_cargar_capa", this, e);
222
                }catch (InterruptedException e) {
223
                        Thread.currentThread().interrupt();
224
                } catch (FilterTypeException e) {
225
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_adding_leyend"), this, e);
226
                }
227
                        
228
        }
229
        
230
        
231
        /**
232
        * @return buffer monobanda con el resultado de la clasificaci?n 
233
        */
234
        public  Object getResult(){
235
                return rasterResult;
236
        }
237
        
238
        /*
239
         * (non-Javadoc)
240
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
241
         */
242
        public String getLabel() {
243
                return  PluginServices.getText(this,"procesando");
244
        }
245

    
246
        /*
247
         * (non-Javadoc)
248
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
249
         */
250
        public String getLog() {
251
                if (writerBufferServer==null)
252
                        return PluginServices.getText(this,"clasificando_imagen")+"...";
253
                else
254
                        return PluginServices.getText(this,"escribiendo_resultado")+"...";
255
        }
256

    
257
        
258
        /*
259
         * (non-Javadoc)
260
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
261
         */
262
        public int getPercent() {
263
                if (writerBufferServer==null)
264
                        return percent;
265
                else
266
                        return writerBufferServer.getPercent();
267
        }
268

    
269
        /*
270
         * (non-Javadoc)
271
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
272
         */
273
        public String getTitle() {
274
                return PluginServices.getText(this,"clasificacion");
275
        }
276

    
277

    
278
}