svn-gvsig-desktop / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / classification / ClassificationGeneralProcess.java @ 16598
History | View | Annotate | Download (11.8 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.io.File; |
44 |
import java.io.IOException; |
45 |
import java.util.ArrayList; |
46 |
import java.util.Iterator; |
47 |
|
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 |
import org.gvsig.raster.buffer.BufferFactory; |
54 |
import org.gvsig.raster.buffer.RasterBuffer; |
55 |
import org.gvsig.raster.buffer.RasterBufferInvalidException; |
56 |
import org.gvsig.raster.dataset.GeoRasterWriter; |
57 |
import org.gvsig.raster.dataset.IRasterDataSource; |
58 |
import org.gvsig.raster.dataset.NotSupportedExtensionException; |
59 |
import org.gvsig.raster.dataset.io.RasterDriverException; |
60 |
|
61 |
import org.gvsig.raster.datastruct.ColorItem; |
62 |
import org.gvsig.raster.datastruct.ColorTable; |
63 |
import org.gvsig.raster.grid.Grid; |
64 |
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 |
import org.gvsig.raster.process.CancelEvent; |
74 |
import org.gvsig.raster.process.RasterTask; |
75 |
import org.gvsig.raster.util.RasterToolsUtil; |
76 |
import org.gvsig.rastertools.clipping.WriterBufferServer; |
77 |
|
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 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
83 |
|
84 |
/**
|
85 |
* 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 |
*@version 19/10/2007
|
95 |
* */
|
96 |
public abstract class ClassificationGeneralProcess implements Runnable, IIncrementable, IncrementableListener { |
97 |
|
98 |
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 |
protected View view = null; |
111 |
protected RasterTask rasterTask = new RasterTask(this); |
112 |
protected int[] bandList = null; |
113 |
|
114 |
/**
|
115 |
* 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 |
public abstract int getPixelClassForTypeByte(byte pixelBandsValues[]); |
121 |
|
122 |
/**
|
123 |
* 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 |
public abstract int getPixelClassForTypeShort(short pixelBandsValues[]); |
129 |
|
130 |
/**
|
131 |
* 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 |
public abstract int getPixelClassForTypeInt(int pixelBandsValues[]); |
137 |
|
138 |
/**
|
139 |
* 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 |
public abstract int getPixelClassForTypeFloat(float pixelBandsValues[]); |
145 |
|
146 |
|
147 |
/**
|
148 |
* 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 |
public abstract int getPixelClassForTypeDouble(double pixelBandsValues[]); |
154 |
|
155 |
|
156 |
/**
|
157 |
* Lanzar el Thread del proceso de clasificaci?n.
|
158 |
*/
|
159 |
public void start() {} |
160 |
|
161 |
/**
|
162 |
* Asigna la leyenda a la capa resultante
|
163 |
* @param lyr capa a la que se asigna la leyenda
|
164 |
*/
|
165 |
public void setLeyend(FLayer lyr) { |
166 |
if(lyr instanceof IRasterRendering) { |
167 |
IRasterRendering rendering = (IRasterRendering) lyr; |
168 |
ArrayList colorItems = new ArrayList(); |
169 |
ColorItem colorItem = null;
|
170 |
int classValue = 0; |
171 |
|
172 |
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 |
|
184 |
RasterFilterList filterList = rendering.getRenderFilterList(); |
185 |
RasterFilterListManager manager = new RasterFilterListManager(filterList);
|
186 |
ColorTableListManager cManager = (ColorTableListManager) manager.getManagerByClass(ColorTableListManager.class); |
187 |
|
188 |
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 |
|
202 |
|
203 |
/**
|
204 |
* Establece el grid con las bandas recogidas en bandList.
|
205 |
*/
|
206 |
public void setGrid(){ |
207 |
BufferFactory bufferFactory = new BufferFactory(raster);
|
208 |
try {
|
209 |
bufferFactory.setAreaOfInterest(); |
210 |
bufferFactory.setAllDrawableBands(); |
211 |
inputGrid = new Grid(bufferFactory,bandList);
|
212 |
|
213 |
} catch (RasterDriverException e) {
|
214 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "raster_buffer_invalid_extension"), this, e); |
215 |
}catch (RasterBufferInvalidException e) {
|
216 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "raster_buffer_invalid_extension"), this, e); |
217 |
}catch (InterruptedException e) { |
218 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_preview_stop"), this, e); |
219 |
} |
220 |
} |
221 |
|
222 |
/**
|
223 |
* Escritura del resultado a disco y carga de la capa en la vista.
|
224 |
*/
|
225 |
public void writeToFile(){ |
226 |
|
227 |
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 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e); |
250 |
} catch (RasterDriverException e) {
|
251 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e); |
252 |
} catch (IOException e) { |
253 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e); |
254 |
}catch (LoadLayerException e) {
|
255 |
RasterToolsUtil.messageBoxError("error_cargar_capa", this, e); |
256 |
}catch (InterruptedException e) { |
257 |
Thread.currentThread().interrupt();
|
258 |
}finally{
|
259 |
if (incrementableTask != null) |
260 |
incrementableTask.processFinalize(); |
261 |
} |
262 |
} |
263 |
|
264 |
|
265 |
/**
|
266 |
* @return buffer monobanda con el resultado de la clasificaci?n
|
267 |
*/
|
268 |
public RasterBuffer getResult(){
|
269 |
return rasterResult;
|
270 |
} |
271 |
|
272 |
/*
|
273 |
* (non-Javadoc)
|
274 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
|
275 |
*/
|
276 |
public String getLabel() { |
277 |
return PluginServices.getText(this,"procesando"); |
278 |
} |
279 |
|
280 |
/*
|
281 |
* (non-Javadoc)
|
282 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
|
283 |
*/
|
284 |
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 |
|
292 |
/*
|
293 |
* (non-Javadoc)
|
294 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
|
295 |
*/
|
296 |
public int getPercent() { |
297 |
if (writerBufferServer==null) |
298 |
return percent;
|
299 |
else
|
300 |
return writerBufferServer.getPercent();
|
301 |
} |
302 |
|
303 |
/*
|
304 |
* (non-Javadoc)
|
305 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
306 |
*/
|
307 |
public String getTitle() { |
308 |
return PluginServices.getText(this,"clasificacion"); |
309 |
} |
310 |
|
311 |
/*
|
312 |
* (non-Javadoc)
|
313 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionResumed()
|
314 |
*/
|
315 |
public void actionResumed(IncrementableEvent e) { |
316 |
|
317 |
} |
318 |
|
319 |
/*
|
320 |
* (non-Javadoc)
|
321 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionSuspend()
|
322 |
*/
|
323 |
public void actionSuspended(IncrementableEvent e) { |
324 |
} |
325 |
|
326 |
|
327 |
/*
|
328 |
* (non-Javadoc)
|
329 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#setIncrementableTask()
|
330 |
*/
|
331 |
public void setIncrementableTask(IncrementableTask incrementableTask) { |
332 |
this.incrementableTask = incrementableTask;
|
333 |
this.incrementableTask.addIncrementableListener(this); |
334 |
} |
335 |
|
336 |
/*
|
337 |
* (non-Javadoc)
|
338 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionCanceled()
|
339 |
*/
|
340 |
public void actionCanceled(IncrementableEvent e) { |
341 |
rasterTask.setEvent(new CancelEvent(this)); |
342 |
if (incrementableTask != null) |
343 |
incrementableTask.processFinalize(); |
344 |
} |
345 |
|
346 |
|
347 |
} |