root / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / tasseledcap / TasseledCapProcess.java @ 14720
History | View | Annotate | Download (11.4 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 |
|
42 |
package org.gvsig.remotesensing.tasseledcap; |
43 |
|
44 |
import java.awt.geom.AffineTransform; |
45 |
import java.io.IOException; |
46 |
|
47 |
import org.gvsig.gui.beans.incrementabletask.IIncrementable; |
48 |
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent; |
49 |
import org.gvsig.gui.beans.incrementabletask.IncrementableListener; |
50 |
import org.gvsig.gui.beans.incrementabletask.IncrementableTask; |
51 |
import org.gvsig.raster.buffer.BufferFactory; |
52 |
import org.gvsig.raster.buffer.RasterBuffer; |
53 |
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException; |
54 |
import org.gvsig.raster.buffer.RasterBufferInvalidException; |
55 |
import org.gvsig.raster.dataset.GeoRasterWriter; |
56 |
import org.gvsig.raster.dataset.IBuffer; |
57 |
import org.gvsig.raster.dataset.IRasterDataSource; |
58 |
import org.gvsig.raster.dataset.NotSupportedExtensionException; |
59 |
import org.gvsig.raster.dataset.RasterDriverException; |
60 |
import org.gvsig.raster.grid.Grid; |
61 |
import org.gvsig.raster.grid.GridExtent; |
62 |
import org.gvsig.raster.process.CancelEvent; |
63 |
import org.gvsig.raster.process.RasterTask; |
64 |
import org.gvsig.raster.process.RasterTaskQueue; |
65 |
import org.gvsig.raster.util.RasterNotLoadException; |
66 |
import org.gvsig.raster.util.RasterToolsUtil; |
67 |
import org.gvsig.rastertools.RasterModule; |
68 |
import org.gvsig.rastertools.cutting.WriterBufferServer; |
69 |
|
70 |
import com.iver.andami.PluginServices; |
71 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
72 |
|
73 |
|
74 |
/**
|
75 |
* Implementaci?n de la transformaci?n Tasseled Cap. La Trasformaci?n es aplicable a imagenes
|
76 |
* LandSat MS, LandSatTM y LandSantETM
|
77 |
*
|
78 |
* @author Alejandro Mu?oz
|
79 |
*
|
80 |
*/
|
81 |
public class TasseledCapProcess implements Runnable, IIncrementable, IncrementableListener{ |
82 |
|
83 |
private Grid grid = null; |
84 |
private IncrementableTask incrementableTask = null; |
85 |
private int percent = 0; |
86 |
private View view = null; |
87 |
private int type = 0; |
88 |
private String layerName = null; |
89 |
private Thread blinker = null; |
90 |
private WriterBufferServer writerBufferServer = null; |
91 |
protected IBuffer rasterResult = null; |
92 |
private AffineTransform atransform = null; |
93 |
private RasterTask rasterTask = new RasterTask(this); |
94 |
private IRasterDataSource raster = null; |
95 |
/**
|
96 |
* Constructor.
|
97 |
*
|
98 |
* @param raster raster al que se aplica la transformacion
|
99 |
* @param view vista actual
|
100 |
* @param type tipo de imagen: <code>1</code> LandSat MS, <code>2</code> LandSat TM, <code>3</code> LandSat ETM+
|
101 |
* @param layerName nombre de la capa generada
|
102 |
*/
|
103 |
public TasseledCapProcess(IRasterDataSource raster,View view,int typeTransformation, String layerName) { |
104 |
this.raster= raster;
|
105 |
this.view = view;
|
106 |
this.type = typeTransformation;
|
107 |
this.layerName = layerName;
|
108 |
this.atransform= raster.getOwnAffineTransform();
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* Lanza el proceso.
|
113 |
*
|
114 |
*/
|
115 |
public void start() { |
116 |
blinker = new Thread(this); |
117 |
blinker.start(); |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Proceso.
|
122 |
*/
|
123 |
public void run() { |
124 |
RasterTaskQueue.register(rasterTask); //Registro de la tarea
|
125 |
double matrixParams[][]= null; |
126 |
|
127 |
switch (type){
|
128 |
|
129 |
case 0: |
130 |
// MATRIZ COEFICIENTES IMAGEN LANDSAT MS
|
131 |
double LandSatMS[][]= |
132 |
{ |
133 |
/* Brillo de suelo */ {0.433, 0.632, 0.586, 0.264}, |
134 |
/* Indice de Verdor */ {-0.290, -0.562, 0.600, 0.491}, |
135 |
/* Indice de Senescencia*/{-0.829, 0.522, -0.039, 0.194}, |
136 |
/* Cuarta */ {0.223, 0.012, -0.543, 0.810} |
137 |
}; |
138 |
|
139 |
matrixParams=LandSatMS; |
140 |
break;
|
141 |
|
142 |
case 1: |
143 |
// MATRIZ COEFICIENTES IMAGEN LANDSAT TM
|
144 |
|
145 |
double LandSatTM[][]={ |
146 |
|
147 |
/* Brillo */ {0.33183, 0.33121, 0.55177, 0.42514, 0.48087, 0.25252 }, |
148 |
/* Verdor */ {-0.24717, -0.16263, -0.40639, 0.85468, 0.05493, -0.11749 }, |
149 |
/* Tercera */ {0.13929, 0.22490, 0.40359, 0.25178, -0.70133, -0.45732 }, |
150 |
|
151 |
}; |
152 |
|
153 |
matrixParams= LandSatTM; |
154 |
break;
|
155 |
|
156 |
|
157 |
case 2: |
158 |
// MATRIZ COEFICIENTES IMAGEN LANDSAT ETM
|
159 |
double LandSatETM[][]={ |
160 |
|
161 |
/* Brightness */ {0.3561, 0.3972, 0.3904, 0.6966, 0.2286, 0.1596 }, |
162 |
/* Greenness */ {-0.3344, -0.3544, -0.4556, 0.6966, -0.0242, -0.2630}, |
163 |
/* Vetness */ {0.2626, 0.2141, 0.0926, 0.0656, -0.7629, -0.5388 }, |
164 |
/* Fourth */ {0.0805, -0.0498, 0.1950, -0.1327, 0.5752, -0.7775 }, |
165 |
/* Fifth */ {-0.7252, -0.0202, 0.6683, 0.0631, -0.1494, -0.0274 }, |
166 |
/* Sixth */ {0.4000, -0.8172, 0.3832, 0.0602, -0.1095, -0.0985 } |
167 |
}; |
168 |
|
169 |
matrixParams= LandSatETM; |
170 |
break;
|
171 |
|
172 |
} |
173 |
percent=0;
|
174 |
setGrid(); |
175 |
GridExtent layerExtent = null;
|
176 |
layerExtent= grid.getGridExtent(); |
177 |
|
178 |
rasterResult = RasterBuffer.getBuffer(IBuffer.TYPE_FLOAT,layerExtent.getNX(), layerExtent.getNY(), matrixParams.length, true);
|
179 |
|
180 |
// Algoritmo TasseledCap. Trasformacion
|
181 |
int valor=0; |
182 |
int iNX=layerExtent.getNY();
|
183 |
int iNY=layerExtent.getNX();
|
184 |
|
185 |
try{
|
186 |
// BUFFER TYPE_BYTE
|
187 |
if (grid.getRasterBuf().getDataType()== IBuffer.TYPE_BYTE){
|
188 |
for(int i=0; i<iNY;i++ ) { |
189 |
for (int j=0; j<iNX;j++){ |
190 |
for (int k=0; k<matrixParams.length; k++){ |
191 |
for(int s=0;s<matrixParams[0].length;s++) |
192 |
{ grid.setBandToOperate(s); |
193 |
valor+=(double)grid.getCellValueAsByte(i,j)*matrixParams[k][s];
|
194 |
} |
195 |
rasterResult.setElem(j, i,k,(float) valor);
|
196 |
valor=0;
|
197 |
} |
198 |
} |
199 |
percent = i*100/grid.getNY();
|
200 |
} |
201 |
} |
202 |
|
203 |
// BUFFER TYPE_SHORT
|
204 |
if (grid.getRasterBuf().getDataType()== IBuffer.TYPE_SHORT){
|
205 |
for(int i=0; i<iNY;i++ ) { |
206 |
for (int j=0; j<iNX;j++){ |
207 |
for (int k=0; k<matrixParams.length ; k++){ |
208 |
for(int s=0;s<matrixParams[0].length;s++) |
209 |
{ grid.setBandToOperate(s); |
210 |
valor+=(double)grid.getCellValueAsShort(i,j)*matrixParams[k][s];
|
211 |
} |
212 |
rasterResult.setElem(j, i,k,(float) valor);
|
213 |
valor=0;
|
214 |
} |
215 |
} |
216 |
percent = i*100/grid.getNY();
|
217 |
} |
218 |
} |
219 |
|
220 |
// BUFFER TYPE INT
|
221 |
if (grid.getRasterBuf().getDataType()== IBuffer.TYPE_INT){
|
222 |
for(int i=0; i<iNY;i++ ) { |
223 |
for (int j=0; j<iNX;j++){ |
224 |
for (int k=0; k<matrixParams.length ; k++){ |
225 |
for(int s=0;s<matrixParams[0].length;s++) |
226 |
{ grid.setBandToOperate(s); |
227 |
valor+=(double)grid.getCellValueAsInt(i,j)*matrixParams[k][s];
|
228 |
} |
229 |
rasterResult.setElem(j, i,k,(float) valor);
|
230 |
valor=0;
|
231 |
} |
232 |
} |
233 |
percent = i*100/grid.getNY();
|
234 |
} |
235 |
} |
236 |
|
237 |
// BUFFER TYPE FLOAT
|
238 |
if (grid.getRasterBuf().getDataType()== IBuffer.TYPE_FLOAT){
|
239 |
for(int i=0; i<iNY;i++ ) { |
240 |
for (int j=0; j<iNX;j++){ |
241 |
for (int k=0; k<matrixParams.length ; k++){ |
242 |
for(int s=0;s<matrixParams[0].length;s++) |
243 |
{ grid.setBandToOperate(s); |
244 |
valor+=(double)grid.getCellValueAsFloat(i,j)*matrixParams[k][s];
|
245 |
} |
246 |
rasterResult.setElem(j, i,k,(float) valor);
|
247 |
valor=0;
|
248 |
} |
249 |
} |
250 |
percent = i*100/grid.getNY();
|
251 |
} |
252 |
} |
253 |
|
254 |
// BUFFER TYPE DOUBLE
|
255 |
if (grid.getRasterBuf().getDataType()== IBuffer.TYPE_DOUBLE){
|
256 |
for(int i=0; i<iNY;i++ ) { |
257 |
for (int j=0; j<iNX;j++){ |
258 |
for (int k=0; k<matrixParams.length ; k++){ |
259 |
for(int s=0;s<matrixParams[0].length;s++) |
260 |
{ grid.setBandToOperate(s); |
261 |
valor+=(double)grid.getCellValueAsDouble(i,j)*matrixParams[k][s];
|
262 |
} |
263 |
rasterResult.setElem(j, i,k, (float)valor);
|
264 |
valor=0;
|
265 |
} |
266 |
} |
267 |
percent = i*100/grid.getNY();
|
268 |
} |
269 |
} |
270 |
|
271 |
|
272 |
// Escritura a disco
|
273 |
GeoRasterWriter grw = null;
|
274 |
writerBufferServer = new WriterBufferServer(rasterResult);
|
275 |
|
276 |
int endIndex = layerName.lastIndexOf("."); |
277 |
if (endIndex < 0) |
278 |
endIndex = layerName.length(); |
279 |
|
280 |
grw = GeoRasterWriter.getWriter(writerBufferServer, layerName, rasterResult.getBandCount(),atransform, rasterResult.getWidth(), rasterResult.getHeight(), rasterResult.getDataType(), GeoRasterWriter.getWriter(layerName).getParams(), null);
|
281 |
grw.dataWrite(); |
282 |
grw.setWkt(raster.getWktProjection()); |
283 |
grw.writeClose(); |
284 |
rasterResult.free(); |
285 |
String viewName = PluginServices.getMDIManager().getWindowInfo(view).getTitle();
|
286 |
try {
|
287 |
RasterToolsUtil.loadLayer(viewName, layerName, null);
|
288 |
} catch (RasterNotLoadException e) {
|
289 |
RasterToolsUtil.messageBoxError("error_cargar_capa", this, e); |
290 |
} |
291 |
|
292 |
RasterModule.layerCount++; |
293 |
|
294 |
} catch (NotSupportedExtensionException e) {
|
295 |
e.printStackTrace(); |
296 |
} catch (RasterDriverException e) {
|
297 |
e.printStackTrace(); |
298 |
} catch (RasterBufferInvalidAccessException e) {
|
299 |
e.printStackTrace(); |
300 |
} catch (IOException e) { |
301 |
e.printStackTrace(); |
302 |
} catch (InterruptedException e) { |
303 |
Thread.currentThread().interrupt();
|
304 |
} finally {
|
305 |
if(incrementableTask!=null) |
306 |
incrementableTask.processFinalize(); |
307 |
RasterTaskQueue.remove(rasterTask); |
308 |
} |
309 |
} // Fin run()
|
310 |
|
311 |
|
312 |
|
313 |
/**
|
314 |
* Establece el grid con las bandas correspondientes dependiendo del tipo de Imagen.
|
315 |
* */
|
316 |
public void setGrid(){ |
317 |
|
318 |
BufferFactory bufferFactory = new BufferFactory(raster);
|
319 |
int bands[]=null; |
320 |
if (raster.getBandCount()==7){ |
321 |
int[] bands7 ={0,1,2,3,5,6}; |
322 |
bands=bands7; |
323 |
} |
324 |
else{
|
325 |
bands = new int [raster.getBandCount()]; |
326 |
for(int i=0; i<raster.getBandCount();i++) |
327 |
bands[i]=i; |
328 |
} |
329 |
try {
|
330 |
bufferFactory.setAreaOfInterest(); |
331 |
bufferFactory.setAllDrawableBands(); |
332 |
grid = new Grid(bufferFactory, bands);
|
333 |
} catch (RasterBufferInvalidException e) {
|
334 |
e.printStackTrace(); |
335 |
} catch (InterruptedException e1) { |
336 |
e1.printStackTrace(); |
337 |
} |
338 |
} |
339 |
|
340 |
public IBuffer getBufferResult(){
|
341 |
return rasterResult;
|
342 |
} |
343 |
|
344 |
|
345 |
public String getLabel() { |
346 |
return PluginServices.getText(this,"procesando"); |
347 |
} |
348 |
|
349 |
public String getLog() { |
350 |
if (writerBufferServer==null) |
351 |
return PluginServices.getText(this,"calculando_tasseled_cap")+"..."; |
352 |
else
|
353 |
return PluginServices.getText(this,"escribiendo_resultado")+"..."; |
354 |
} |
355 |
|
356 |
public int getPercent() { |
357 |
if (writerBufferServer==null) |
358 |
return percent;
|
359 |
else
|
360 |
return writerBufferServer.getPercent();
|
361 |
} |
362 |
|
363 |
public String getTitle() { |
364 |
return PluginServices.getText(this,"tasseled_cap"); |
365 |
} |
366 |
|
367 |
public void actionCanceled(IncrementableEvent e) { |
368 |
rasterTask.setEvent(new CancelEvent(this)); |
369 |
|
370 |
} |
371 |
|
372 |
public void actionResumed(IncrementableEvent e) { |
373 |
|
374 |
} |
375 |
|
376 |
public void actionSuspended(IncrementableEvent e) { |
377 |
} |
378 |
|
379 |
public void setIncrementableTask(IncrementableTask incrementableTask) { |
380 |
this.incrementableTask = incrementableTask;
|
381 |
this.incrementableTask.addIncrementableListener(this); |
382 |
} |
383 |
|
384 |
} |