Statistics
| Revision:

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

History | View | Annotate | Download (9.23 KB)

1 14486 amunoz
/* 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 13790 dguerrero
package org.gvsig.remotesensing.classification;
42
43 18246 amunoz
import java.awt.Color;
44 13790 dguerrero
import java.io.File;
45
import java.io.IOException;
46 14322 gsdiego
import java.util.ArrayList;
47
import java.util.Iterator;
48 13790 dguerrero
49
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
50 18540 nbrodin
import org.gvsig.raster.RasterProcess;
51 14465 amunoz
import org.gvsig.raster.buffer.BufferFactory;
52 13790 dguerrero
import org.gvsig.raster.buffer.RasterBuffer;
53 14465 amunoz
import org.gvsig.raster.buffer.RasterBufferInvalidException;
54 13790 dguerrero
import org.gvsig.raster.dataset.GeoRasterWriter;
55 14465 amunoz
import org.gvsig.raster.dataset.IRasterDataSource;
56 13790 dguerrero
import org.gvsig.raster.dataset.NotSupportedExtensionException;
57 16598 amunoz
import org.gvsig.raster.dataset.io.RasterDriverException;
58 14322 gsdiego
import org.gvsig.raster.datastruct.ColorItem;
59 13790 dguerrero
import org.gvsig.raster.grid.Grid;
60 17111 nbrodin
import org.gvsig.raster.grid.filter.FilterTypeException;
61 14322 gsdiego
import org.gvsig.raster.grid.roi.ROI;
62 16568 amunoz
import org.gvsig.raster.util.RasterToolsUtil;
63 15592 gsdiego
import org.gvsig.rastertools.clipping.WriterBufferServer;
64 17773 dguerrero
import org.gvsig.remotesensing.RemoteSensingUtils;
65 13790 dguerrero
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 14465 amunoz
import com.iver.cit.gvsig.project.documents.view.gui.View;
71 13790 dguerrero
72
/**
73 14760 amunoz
 * 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 14773 amunoz
 *@version 19/10/2007
83 14760 amunoz
 * */
84 17839 amunoz
public abstract class ClassificationGeneralProcess extends RasterProcess {
85 13790 dguerrero
86 14483 amunoz
        protected Grid                                         inputGrid                        = null;
87 17839 amunoz
        protected FLyrRasterSE                        rasterSE                        = null;
88 14483 amunoz
        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 17839 amunoz
        protected String                                 filename                        = null;
94 14483 amunoz
        protected boolean                                selectedBands[]                = null;
95
        protected        int                                        numClases                        = 0;
96 14506 gsdiego
        protected View                                         view                                = null;
97
        protected   int[]                                 bandList                        = null;
98 18246 amunoz
        protected boolean                                withdefaultClass                = false;
99 14174 amunoz
100 14483 amunoz
        /**
101 14760 amunoz
        * 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 14483 amunoz
        public abstract int getPixelClassForTypeByte(byte pixelBandsValues[]);
107 13790 dguerrero
108 14483 amunoz
        /**
109 14760 amunoz
        * 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 14483 amunoz
        public abstract int getPixelClassForTypeShort(short pixelBandsValues[]);
115 14180 amunoz
116 14483 amunoz
        /**
117 14760 amunoz
        * 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 14483 amunoz
        public abstract int getPixelClassForTypeInt(int pixelBandsValues[]);
123 13979 amunoz
124 14483 amunoz
        /**
125 14760 amunoz
        * 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 14483 amunoz
        public abstract int getPixelClassForTypeFloat(float pixelBandsValues[]);
131 14322 gsdiego
132 14465 amunoz
133 14483 amunoz
        /**
134 14760 amunoz
        * 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 14483 amunoz
        public abstract int getPixelClassForTypeDouble(double pixelBandsValues[]);
140
141
142 14782 amunoz
        /**
143 14760 amunoz
        * Establece el grid con las bandas recogidas en bandList.
144
        */
145 14483 amunoz
        public  void setGrid(){
146 18067 amunoz
147 17839 amunoz
                IRasterDataSource dsetCopy = null;
148 17887 bsanchez
                dsetCopy = rasterSE.getDataSource().newDataset();
149 17839 amunoz
                BufferFactory bufferFactory = new BufferFactory(dsetCopy);
150 18067 amunoz
                if (!RasterBuffer.loadInMemory(dsetCopy))
151
                        bufferFactory.setReadOnly(true);
152 14506 gsdiego
                try {
153 14483 amunoz
                        bufferFactory.setAllDrawableBands();
154 14760 amunoz
                        inputGrid = new Grid(bufferFactory,bandList);
155 18067 amunoz
                } catch (RasterBufferInvalidException e) {
156
                        RasterToolsUtil.messageBoxError("buffer_incorrecto", this, e);
157 16568 amunoz
                }
158 13790 dguerrero
        }
159 14483 amunoz
160 14760 amunoz
        /**
161
        * Escritura del resultado a disco y carga de la capa en la vista.
162
        */
163 14483 amunoz
        public void writeToFile(){
164 14322 gsdiego
165 14483 amunoz
                try{
166
                        GeoRasterWriter grw = null;
167
                        writerBufferServer = new WriterBufferServer(rasterResult);
168 17839 amunoz
                        grw = GeoRasterWriter.getWriter(writerBufferServer, filename, rasterResult.getBandCount(),rasterSE.getAffineTransform(), rasterResult.getWidth(), rasterResult.getHeight(), rasterResult.getDataType(), GeoRasterWriter.getWriter(filename).getParams(), null);
169 14483 amunoz
                        grw.dataWrite();
170 17839 amunoz
                        grw.setWkt(rasterSE.getWktProjection());
171 14483 amunoz
                        grw.writeClose();
172
                        rasterResult.free();
173
                        mapContext= view.getModel().getMapContext();
174
                        mapContext.beginAtomicEvent();
175
                        FLayer lyr = null;
176 17839 amunoz
                        int endIndex = filename.lastIndexOf(".");
177 14483 amunoz
                        if (endIndex < 0)
178 17839 amunoz
                                endIndex = filename.length();
179 14483 amunoz
180 17998 bsanchez
                        lyr = FLyrRasterSE.createLayer(
181
                                        filename.substring(filename.lastIndexOf(File.separator) + 1, endIndex),
182 18246 amunoz
                                        filename,
183
                                        view.getMapControl().getProjection()
184
                                        );
185 17773 dguerrero
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 18246 amunoz
                        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 17773 dguerrero
                        RemoteSensingUtils.setLeyend(lyr,colorItems);
209 18030 amunoz
210 14483 amunoz
                        mapContext.getLayers().addLayer(lyr);
211
                        mapContext.endAtomicEvent();
212
                        mapContext.invalidate();
213
214
                } catch (NotSupportedExtensionException e) {
215 16568 amunoz
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e);
216 14483 amunoz
                } catch (RasterDriverException e) {
217 16568 amunoz
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);
218 14483 amunoz
                } catch (IOException e) {
219 16568 amunoz
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e);
220 14483 amunoz
                }catch (LoadLayerException e) {
221 16568 amunoz
                        RasterToolsUtil.messageBoxError("error_cargar_capa", this, e);
222 14483 amunoz
                }catch (InterruptedException e) {
223
                        Thread.currentThread().interrupt();
224 17773 dguerrero
                } catch (FilterTypeException e) {
225
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_adding_leyend"), this, e);
226 17850 amunoz
                }
227
228 13790 dguerrero
        }
229
230 14760 amunoz
231
        /**
232
        * @return buffer monobanda con el resultado de la clasificaci?n
233
        */
234 18829 bsanchez
        public  Object getResult(){
235 13790 dguerrero
                return rasterResult;
236
        }
237
238 14782 amunoz
        /*
239
         * (non-Javadoc)
240
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
241
         */
242 13790 dguerrero
        public String getLabel() {
243
                return  PluginServices.getText(this,"procesando");
244
        }
245
246 14782 amunoz
        /*
247
         * (non-Javadoc)
248
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
249
         */
250 13790 dguerrero
        public String getLog() {
251
                if (writerBufferServer==null)
252 18067 amunoz
                        return PluginServices.getText(this,"clasificando_imagen")+"...";
253 13790 dguerrero
                else
254
                        return PluginServices.getText(this,"escribiendo_resultado")+"...";
255
        }
256
257 14782 amunoz
258
        /*
259
         * (non-Javadoc)
260
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
261
         */
262 13790 dguerrero
        public int getPercent() {
263
                if (writerBufferServer==null)
264
                        return percent;
265
                else
266
                        return writerBufferServer.getPercent();
267
        }
268
269 14782 amunoz
        /*
270
         * (non-Javadoc)
271
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
272
         */
273 13790 dguerrero
        public String getTitle() {
274
                return PluginServices.getText(this,"clasificacion");
275
        }
276
277
278
}