svn-gvsig-desktop / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / classification / ClassificationGeneralProcess.java @ 16598
History | View | Annotate | Download (11.8 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 | import java.io.File; |
||
44 | import java.io.IOException; |
||
45 | 14322 | gsdiego | import java.util.ArrayList; |
46 | import java.util.Iterator; |
||
47 | 13790 | dguerrero | |
48 | import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
||
49 | import org.gvsig.gui.beans.incrementabletask.IIncrementable; |
||
50 | import org.gvsig.gui.beans.incrementabletask.IncrementableEvent; |
||
51 | import org.gvsig.gui.beans.incrementabletask.IncrementableListener; |
||
52 | import org.gvsig.gui.beans.incrementabletask.IncrementableTask; |
||
53 | 14465 | amunoz | import org.gvsig.raster.buffer.BufferFactory; |
54 | 13790 | dguerrero | import org.gvsig.raster.buffer.RasterBuffer; |
55 | 14465 | amunoz | import org.gvsig.raster.buffer.RasterBufferInvalidException; |
56 | 13790 | dguerrero | import org.gvsig.raster.dataset.GeoRasterWriter; |
57 | 14465 | amunoz | import org.gvsig.raster.dataset.IRasterDataSource; |
58 | 13790 | dguerrero | import org.gvsig.raster.dataset.NotSupportedExtensionException; |
59 | 16598 | amunoz | import org.gvsig.raster.dataset.io.RasterDriverException; |
60 | |||
61 | 14322 | gsdiego | import org.gvsig.raster.datastruct.ColorItem; |
62 | import org.gvsig.raster.datastruct.ColorTable; |
||
63 | 13790 | dguerrero | import org.gvsig.raster.grid.Grid; |
64 | 14322 | gsdiego | import org.gvsig.raster.grid.GridPalette; |
65 | import org.gvsig.raster.grid.filter.RasterFilterList; |
||
66 | import org.gvsig.raster.grid.filter.RasterFilterListManager; |
||
67 | import org.gvsig.raster.grid.filter.bands.ColorTableFilter; |
||
68 | import org.gvsig.raster.grid.filter.bands.ColorTableListManager; |
||
69 | import org.gvsig.raster.grid.filter.enhancement.LinearEnhancementFilter; |
||
70 | import org.gvsig.raster.grid.filter.statistics.TailTrimFilter; |
||
71 | import org.gvsig.raster.grid.roi.ROI; |
||
72 | import org.gvsig.raster.hierarchy.IRasterRendering; |
||
73 | 13790 | dguerrero | import org.gvsig.raster.process.CancelEvent; |
74 | import org.gvsig.raster.process.RasterTask; |
||
75 | 16568 | amunoz | import org.gvsig.raster.util.RasterToolsUtil; |
76 | 15592 | gsdiego | import org.gvsig.rastertools.clipping.WriterBufferServer; |
77 | 13790 | dguerrero | |
78 | import com.iver.andami.PluginServices; |
||
79 | import com.iver.cit.gvsig.exceptions.layers.LoadLayerException; |
||
80 | import com.iver.cit.gvsig.fmap.MapContext; |
||
81 | import com.iver.cit.gvsig.fmap.layers.FLayer; |
||
82 | 14465 | amunoz | import com.iver.cit.gvsig.project.documents.view.gui.View; |
83 | 13790 | dguerrero | |
84 | /**
|
||
85 | 14760 | amunoz | * ClassificationGeneraProccess es la clase abstracta base que extienden todos
|
86 | * los metodos de clasificaci?n. Recoge procedimientos comunes a todos los metodos:
|
||
87 | * construccion del grid que se va a clasificar, asignacion de la leyenda a la capa
|
||
88 | * resultante o escritura en fichero.
|
||
89 | * Cada procedimiento de clasificaci?n implementar? los m?todos que determinan la
|
||
90 | * clase a la que pertenece un pixel, adem?s del metodo run().
|
||
91 | |||
92 | *@author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)
|
||
93 | *@author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
|
||
94 | 14773 | amunoz | *@version 19/10/2007
|
95 | 14760 | amunoz | * */
|
96 | 14483 | amunoz | public abstract class ClassificationGeneralProcess implements Runnable, IIncrementable, IncrementableListener { |
97 | 13790 | dguerrero | |
98 | 14483 | amunoz | protected Grid inputGrid = null; |
99 | protected IRasterDataSource raster = null; |
||
100 | protected RasterBuffer rasterResult = null; |
||
101 | protected MapContext mapContext = null; |
||
102 | protected int percent = 0; |
||
103 | protected Thread blinker = null; |
||
104 | protected ArrayList rois = null; |
||
105 | protected IncrementableTask incrementableTask = null; |
||
106 | protected WriterBufferServer writerBufferServer = null; |
||
107 | protected String fileNameOutput = null; |
||
108 | protected boolean selectedBands[] = null; |
||
109 | protected int numClases = 0; |
||
110 | 14506 | gsdiego | protected View view = null; |
111 | 14483 | amunoz | protected RasterTask rasterTask = new RasterTask(this); |
112 | 14506 | gsdiego | protected int[] bandList = null; |
113 | 14174 | amunoz | |
114 | 14483 | amunoz | /**
|
115 | 14760 | amunoz | * M?todo que determina la clase de pertenencia del pixel cuyos valores
|
116 | * por banda se pasan en un array de bytes.
|
||
117 | * @param valores del pixel en cada una de las bandas
|
||
118 | * @return clase a la que pertenece el pixel
|
||
119 | */
|
||
120 | 14483 | amunoz | public abstract int getPixelClassForTypeByte(byte pixelBandsValues[]); |
121 | 13790 | dguerrero | |
122 | 14483 | amunoz | /**
|
123 | 14760 | amunoz | * M?todo que determina la clase de pertenencia del pixel cuyos valores
|
124 | * por banda se pasan en un array de shorts.
|
||
125 | * @param valores del pixel en cada una de las bandas
|
||
126 | * @return clase a la que pertenece el pixel
|
||
127 | */
|
||
128 | 14483 | amunoz | public abstract int getPixelClassForTypeShort(short pixelBandsValues[]); |
129 | 14180 | amunoz | |
130 | 14483 | amunoz | /**
|
131 | 14760 | amunoz | * M?todo que determina la clase de pertenencia del pixel cuyos valores
|
132 | * por banda se pasan en un array de int.
|
||
133 | * @param valores del pixel en cada una de las bandas
|
||
134 | * @return clase a la que pertenece el pixel
|
||
135 | */
|
||
136 | 14483 | amunoz | public abstract int getPixelClassForTypeInt(int pixelBandsValues[]); |
137 | 13979 | amunoz | |
138 | 14483 | amunoz | /**
|
139 | 14760 | amunoz | * M?todo que determina la clase de pertenencia del pixel cuyos valores
|
140 | * por banda se pasan en un array de float.
|
||
141 | * @param valores del pixel en cada una de las bandas
|
||
142 | * @return clase a la que pertenece el pixel
|
||
143 | */
|
||
144 | 14483 | amunoz | public abstract int getPixelClassForTypeFloat(float pixelBandsValues[]); |
145 | 14322 | gsdiego | |
146 | 14465 | amunoz | |
147 | 14483 | amunoz | /**
|
148 | 14760 | amunoz | * M?todo que determina la clase de pertenencia del pixel cuyos valores
|
149 | * por banda se pasan en un array de double.
|
||
150 | * @param valores del pixel en cada una de las bandas
|
||
151 | * @return clase a la que pertenece el pixel
|
||
152 | */
|
||
153 | 14483 | amunoz | public abstract int getPixelClassForTypeDouble(double pixelBandsValues[]); |
154 | |||
155 | |||
156 | 14782 | amunoz | /**
|
157 | * Lanzar el Thread del proceso de clasificaci?n.
|
||
158 | */
|
||
159 | 14760 | amunoz | public void start() {} |
160 | 14465 | amunoz | |
161 | 14483 | amunoz | /**
|
162 | * Asigna la leyenda a la capa resultante
|
||
163 | 14760 | amunoz | * @param lyr capa a la que se asigna la leyenda
|
164 | */
|
||
165 | 14483 | amunoz | public void setLeyend(FLayer lyr) { |
166 | 14322 | gsdiego | if(lyr instanceof IRasterRendering) { |
167 | IRasterRendering rendering = (IRasterRendering) lyr; |
||
168 | ArrayList colorItems = new ArrayList(); |
||
169 | ColorItem colorItem = null;
|
||
170 | int classValue = 0; |
||
171 | 14760 | amunoz | |
172 | 14322 | gsdiego | for (Iterator iter = rois.iterator(); iter.hasNext();) { |
173 | ROI roi = (ROI) iter.next(); |
||
174 | colorItem = new ColorItem();
|
||
175 | colorItem.setColor(roi.getColor()); |
||
176 | colorItem.setNameClass(roi.getName()); |
||
177 | colorItem.setValue(classValue); |
||
178 | colorItems.add(colorItem); |
||
179 | classValue++; |
||
180 | } |
||
181 | ColorTable colorTable = new ColorTable();
|
||
182 | colorTable.createPaletteFromColorItems(colorItems, false);
|
||
183 | 14760 | amunoz | |
184 | 14322 | gsdiego | RasterFilterList filterList = rendering.getRenderFilterList(); |
185 | RasterFilterListManager manager = new RasterFilterListManager(filterList);
|
||
186 | ColorTableListManager cManager = (ColorTableListManager) manager.getManagerByClass(ColorTableListManager.class); |
||
187 | 14760 | amunoz | |
188 | 14322 | gsdiego | filterList.remove(ColorTableFilter.class); |
189 | |||
190 | ((FLyrRasterSE)lyr).setLastLegend(null);
|
||
191 | |||
192 | filterList.remove(LinearEnhancementFilter.class); |
||
193 | filterList.remove(TailTrimFilter.class); |
||
194 | GridPalette gridPalette = new GridPalette(colorTable);
|
||
195 | cManager.addColorTableFilter(gridPalette); |
||
196 | ((FLyrRasterSE)lyr).setLastLegend(gridPalette); |
||
197 | |||
198 | rendering.setRenderFilterList(filterList); |
||
199 | } |
||
200 | } |
||
201 | 13956 | amunoz | |
202 | |||
203 | 14174 | amunoz | /**
|
204 | 14760 | amunoz | * Establece el grid con las bandas recogidas en bandList.
|
205 | */
|
||
206 | 14483 | amunoz | public void setGrid(){ |
207 | BufferFactory bufferFactory = new BufferFactory(raster);
|
||
208 | 14506 | gsdiego | try {
|
209 | 14483 | amunoz | bufferFactory.setAreaOfInterest(); |
210 | bufferFactory.setAllDrawableBands(); |
||
211 | 14760 | amunoz | inputGrid = new Grid(bufferFactory,bandList);
|
212 | 16598 | amunoz | |
213 | } catch (RasterDriverException e) {
|
||
214 | RasterToolsUtil.messageBoxError(PluginServices.getText(this, "raster_buffer_invalid_extension"), this, e); |
||
215 | 16568 | amunoz | }catch (RasterBufferInvalidException e) {
|
216 | RasterToolsUtil.messageBoxError(PluginServices.getText(this, "raster_buffer_invalid_extension"), this, e); |
||
217 | 16572 | amunoz | }catch (InterruptedException e) { |
218 | RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_preview_stop"), this, e); |
||
219 | 16568 | amunoz | } |
220 | 13790 | dguerrero | } |
221 | 14483 | amunoz | |
222 | 14760 | amunoz | /**
|
223 | * Escritura del resultado a disco y carga de la capa en la vista.
|
||
224 | */
|
||
225 | 14483 | amunoz | public void writeToFile(){ |
226 | 14322 | gsdiego | |
227 | 14483 | amunoz | try{
|
228 | GeoRasterWriter grw = null;
|
||
229 | writerBufferServer = new WriterBufferServer(rasterResult);
|
||
230 | grw = GeoRasterWriter.getWriter(writerBufferServer, fileNameOutput, rasterResult.getBandCount(),raster.getAffineTransform(), rasterResult.getWidth(), rasterResult.getHeight(), rasterResult.getDataType(), GeoRasterWriter.getWriter(fileNameOutput).getParams(), null);
|
||
231 | grw.dataWrite(); |
||
232 | grw.setWkt(raster.getWktProjection()); |
||
233 | grw.writeClose(); |
||
234 | rasterResult.free(); |
||
235 | mapContext= view.getModel().getMapContext(); |
||
236 | mapContext.beginAtomicEvent(); |
||
237 | FLayer lyr = null;
|
||
238 | int endIndex = fileNameOutput.lastIndexOf("."); |
||
239 | if (endIndex < 0) |
||
240 | endIndex = fileNameOutput.length(); |
||
241 | |||
242 | lyr = FLyrRasterSE.createLayer(fileNameOutput.substring(fileNameOutput.lastIndexOf(File.separator) + 1, endIndex),new File(fileNameOutput), null); |
||
243 | setLeyend(lyr); |
||
244 | mapContext.getLayers().addLayer(lyr); |
||
245 | mapContext.endAtomicEvent(); |
||
246 | mapContext.invalidate(); |
||
247 | |||
248 | } catch (NotSupportedExtensionException e) {
|
||
249 | 16568 | amunoz | RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e); |
250 | 14483 | amunoz | } catch (RasterDriverException e) {
|
251 | 16568 | amunoz | RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e); |
252 | 14483 | amunoz | } catch (IOException e) { |
253 | 16568 | amunoz | RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e); |
254 | 14483 | amunoz | }catch (LoadLayerException e) {
|
255 | 16568 | amunoz | RasterToolsUtil.messageBoxError("error_cargar_capa", this, e); |
256 | 14483 | amunoz | }catch (InterruptedException e) { |
257 | Thread.currentThread().interrupt();
|
||
258 | 16572 | amunoz | }finally{
|
259 | if (incrementableTask != null) |
||
260 | incrementableTask.processFinalize(); |
||
261 | 14760 | amunoz | } |
262 | 13790 | dguerrero | } |
263 | |||
264 | 14760 | amunoz | |
265 | /**
|
||
266 | * @return buffer monobanda con el resultado de la clasificaci?n
|
||
267 | */
|
||
268 | 14483 | amunoz | public RasterBuffer getResult(){
|
269 | 13790 | dguerrero | return rasterResult;
|
270 | } |
||
271 | |||
272 | 14782 | amunoz | /*
|
273 | * (non-Javadoc)
|
||
274 | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
|
||
275 | */
|
||
276 | 13790 | dguerrero | public String getLabel() { |
277 | return PluginServices.getText(this,"procesando"); |
||
278 | } |
||
279 | |||
280 | 14782 | amunoz | /*
|
281 | * (non-Javadoc)
|
||
282 | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
|
||
283 | */
|
||
284 | 13790 | dguerrero | public String getLog() { |
285 | if (writerBufferServer==null) |
||
286 | return PluginServices.getText(this,"obteniendo_imagen")+"..."; |
||
287 | else
|
||
288 | return PluginServices.getText(this,"escribiendo_resultado")+"..."; |
||
289 | } |
||
290 | |||
291 | 14782 | amunoz | |
292 | /*
|
||
293 | * (non-Javadoc)
|
||
294 | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
|
||
295 | */
|
||
296 | 13790 | dguerrero | public int getPercent() { |
297 | if (writerBufferServer==null) |
||
298 | return percent;
|
||
299 | else
|
||
300 | return writerBufferServer.getPercent();
|
||
301 | } |
||
302 | |||
303 | 14782 | amunoz | /*
|
304 | * (non-Javadoc)
|
||
305 | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
||
306 | */
|
||
307 | 13790 | dguerrero | public String getTitle() { |
308 | return PluginServices.getText(this,"clasificacion"); |
||
309 | } |
||
310 | |||
311 | 14782 | amunoz | /*
|
312 | * (non-Javadoc)
|
||
313 | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionResumed()
|
||
314 | */
|
||
315 | 13790 | dguerrero | public void actionResumed(IncrementableEvent e) { |
316 | |||
317 | } |
||
318 | |||
319 | 14782 | amunoz | /*
|
320 | * (non-Javadoc)
|
||
321 | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionSuspend()
|
||
322 | */
|
||
323 | 13790 | dguerrero | public void actionSuspended(IncrementableEvent e) { |
324 | } |
||
325 | |||
326 | 14760 | amunoz | |
327 | 14782 | amunoz | /*
|
328 | * (non-Javadoc)
|
||
329 | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#setIncrementableTask()
|
||
330 | */
|
||
331 | 13790 | dguerrero | public void setIncrementableTask(IncrementableTask incrementableTask) { |
332 | this.incrementableTask = incrementableTask;
|
||
333 | this.incrementableTask.addIncrementableListener(this); |
||
334 | } |
||
335 | |||
336 | 14782 | amunoz | /*
|
337 | * (non-Javadoc)
|
||
338 | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionCanceled()
|
||
339 | */
|
||
340 | 14483 | amunoz | public void actionCanceled(IncrementableEvent e) { |
341 | rasterTask.setEvent(new CancelEvent(this)); |
||
342 | if (incrementableTask != null) |
||
343 | incrementableTask.processFinalize(); |
||
344 | } |
||
345 | |||
346 | |||
347 | 13790 | dguerrero | } |