root / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / principalcomponents / PCImageProcess.java @ 22800
History | View | Annotate | Download (13.1 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.principalcomponents; |
42 |
|
43 |
import java.io.IOException; |
44 |
|
45 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
46 |
import org.gvsig.raster.RasterProcess; |
47 |
import org.gvsig.raster.buffer.BufferFactory; |
48 |
import org.gvsig.raster.buffer.RasterBuffer; |
49 |
import org.gvsig.raster.buffer.RasterBufferInvalidException; |
50 |
import org.gvsig.raster.buffer.WriterBufferServer; |
51 |
import org.gvsig.raster.dataset.GeoRasterWriter; |
52 |
import org.gvsig.raster.dataset.IBuffer; |
53 |
import org.gvsig.raster.dataset.IRasterDataSource; |
54 |
import org.gvsig.raster.dataset.NotSupportedExtensionException; |
55 |
import org.gvsig.raster.dataset.io.RasterDriverException; |
56 |
import org.gvsig.raster.grid.Grid; |
57 |
import org.gvsig.raster.grid.GridException; |
58 |
import org.gvsig.raster.grid.GridExtent; |
59 |
import org.gvsig.raster.util.RasterToolsUtil; |
60 |
import org.gvsig.raster.util.RasterUtilities; |
61 |
|
62 |
import com.iver.andami.PluginServices; |
63 |
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException; |
64 |
|
65 |
/**
|
66 |
*Proceso de contrucci?n de la imagen resultante del analisis de componentes principales a partir de los
|
67 |
*componentes seleccionados.
|
68 |
*
|
69 |
*@params
|
70 |
* <LI>FLyrRasterSE "inputRasterLayer": Capa raster de entrada</LI>
|
71 |
* <LI>PCStatistics "statistics": Estad?sticas del An?lisis de C.P. (ej.: las generadas por PCStatisticsProcess)</LI>
|
72 |
* <LI>boolean[] "selectedBands": Bandas del raster original que se tienen en cuenta para la transformaci?n</LI>
|
73 |
* <LI>boolean[] "selectedComponents": Componentes que se usar?n para consturir la imagen transformada</LI>
|
74 |
* <LI>String "outputPath": Ruta completa al fichero de salida del proceso</LI>
|
75 |
*
|
76 |
* @result
|
77 |
* <LI>FLyrRasterSE: Raster transformado</LI>
|
78 |
*
|
79 |
*@author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)
|
80 |
*@author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
|
81 |
*@version 19/10/2007
|
82 |
*/
|
83 |
public class PCImageProcess extends RasterProcess{ |
84 |
|
85 |
private boolean[] selectedPCs = null; |
86 |
private String layerName = null; |
87 |
private Grid inputGrid = null; |
88 |
private RasterBuffer rasterResult = null; |
89 |
private int percent = 0; |
90 |
private WriterBufferServer writerBufferServer = null;; |
91 |
private FLyrRasterSE inputRasterLayer = null; |
92 |
private FLyrRasterSE outputRasterLayer = null; |
93 |
private boolean[] selectedBands = null; |
94 |
private PCStatistics pcStatistics = null; |
95 |
|
96 |
|
97 |
/**
|
98 |
* Connstructor
|
99 |
*/
|
100 |
public PCImageProcess() {
|
101 |
} |
102 |
|
103 |
|
104 |
/**
|
105 |
* @return buffer resultante tras la transformacion
|
106 |
*/
|
107 |
public RasterBuffer getBufferResult(){
|
108 |
return rasterResult;
|
109 |
} |
110 |
|
111 |
public Object getResult() { |
112 |
return outputRasterLayer;
|
113 |
} |
114 |
|
115 |
/*
|
116 |
* (non-Javadoc)
|
117 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
|
118 |
*/
|
119 |
public String getLabel() { |
120 |
return PluginServices.getText(this,"procesando"); |
121 |
} |
122 |
|
123 |
/*
|
124 |
* (non-Javadoc)
|
125 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
|
126 |
*/
|
127 |
public String getLog() { |
128 |
if (writerBufferServer==null) |
129 |
return PluginServices.getText(this,"generando_pcs")+"..."; |
130 |
else
|
131 |
return PluginServices.getText(this,"escribiendo_resultado")+"..."; |
132 |
} |
133 |
|
134 |
/*
|
135 |
* (non-Javadoc)
|
136 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
|
137 |
*/
|
138 |
public int getPercent() { |
139 |
if(writerBufferServer==null) |
140 |
return percent;
|
141 |
return writerBufferServer.getPercent();
|
142 |
} |
143 |
|
144 |
|
145 |
/*
|
146 |
* (non-Javadoc)
|
147 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
148 |
*/
|
149 |
public String getTitle() { |
150 |
return PluginServices.getText(this,"principal_components"); |
151 |
} |
152 |
|
153 |
public void init() { |
154 |
inputRasterLayer = (FLyrRasterSE)getLayerParam("inputRasterLayer");
|
155 |
pcStatistics = (PCStatistics) getParam("statistics");
|
156 |
selectedBands = (boolean []) getParam("selectedBands"); |
157 |
selectedPCs = (boolean[]) getParam("selectedComponents"); |
158 |
layerName = getStringParam("outputPath");
|
159 |
|
160 |
buildGrid(); |
161 |
} |
162 |
|
163 |
public void process() throws InterruptedException { |
164 |
|
165 |
// ResultExtent de la primera banda del array
|
166 |
GridExtent layerExtent = null;
|
167 |
layerExtent= inputGrid.getGridExtent(); |
168 |
|
169 |
// Se determina el numero de bandas de la imagen resultante, en funcion de la seleccion.
|
170 |
int numbandas=0; |
171 |
for (int i=0;i<selectedPCs.length;i++) |
172 |
if (selectedPCs[i])numbandas++;
|
173 |
|
174 |
// Correspondencia entre entre componete seleccionado y banda
|
175 |
int banda[] = new int[selectedPCs.length]; |
176 |
int band=0; |
177 |
for (int i=0;i<selectedPCs.length;i++) |
178 |
{ |
179 |
if (selectedPCs[i]){ banda[i]=band; band++;}
|
180 |
else banda[i]=-1; |
181 |
} |
182 |
|
183 |
// ResultOrden: Orden correcto de autovalores (de mayor a menor)
|
184 |
int resultOrden[]= new int[pcStatistics.getAutovalues().length]; |
185 |
int cont = pcStatistics.getAutovalues().length-1; |
186 |
int noseleccionados=0; |
187 |
for(int i=0;i<pcStatistics.getAutovalues().length;i++){ |
188 |
if (banda[i]!=-1){ |
189 |
resultOrden[i]=cont; |
190 |
cont--; |
191 |
} |
192 |
else noseleccionados++;
|
193 |
} |
194 |
|
195 |
for(int i=0;i<pcStatistics.getAutovalues().length;i++){ |
196 |
resultOrden[i]=resultOrden[i]- noseleccionados; |
197 |
} |
198 |
|
199 |
int bandas[]=new int [resultOrden.length -noseleccionados]; |
200 |
for(int i=0; i< bandas.length;i++) |
201 |
bandas[i]=i; |
202 |
|
203 |
// Construccion del buffer para escritura.
|
204 |
rasterResult=RasterBuffer.getBuffer(IBuffer.TYPE_FLOAT, inputGrid.getLayerNX(), inputGrid.getLayerNY(), bandas.length, true);
|
205 |
|
206 |
double valor=0; |
207 |
int bandCount= inputGrid.getRasterBuf().getBandCount();
|
208 |
|
209 |
// BUFFER TYPE BYTE
|
210 |
if (inputGrid.getRasterBuf().getDataType()== RasterBuffer.TYPE_BYTE){
|
211 |
// Construccion de la imagen en funcion de los componentes seleccionados.
|
212 |
for(int i=0; i<layerExtent.getNY();i++ ) { |
213 |
for (int j=0; j<layerExtent.getNX();j++){ |
214 |
for (int k=0; k<bandCount; k++){ |
215 |
if (selectedPCs[k]){
|
216 |
for(int s=0;s<bandCount;s++) |
217 |
{ |
218 |
if (selectedPCs[s]){
|
219 |
inputGrid.setBandToOperate(s); |
220 |
try {
|
221 |
valor+=(double)inputGrid.getCellValueAsByte(j, i)*pcStatistics.getAutoVectorsMatrix().get(s,resultOrden[k]);
|
222 |
} catch (GridException e) {
|
223 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "grid_error"), this, e); |
224 |
} |
225 |
} |
226 |
} |
227 |
rasterResult.setElem(i, j, banda[k],(float) valor);
|
228 |
valor=0;
|
229 |
} |
230 |
} |
231 |
} |
232 |
percent = i*100/layerExtent.getNX();
|
233 |
} |
234 |
|
235 |
} // Fin caso RasterBuffer TypeByte
|
236 |
|
237 |
|
238 |
// BUFFER TYPE SHORT
|
239 |
if (inputGrid.getRasterBuf().getDataType()== RasterBuffer.TYPE_SHORT){
|
240 |
for(int i=0; i<layerExtent.getNY();i++ ) { |
241 |
for (int j=0; j<layerExtent.getNX();j++){ |
242 |
for (int k=0; k<bandCount; k++){ |
243 |
if (selectedPCs[k]){
|
244 |
for(int s=0;s<bandCount;s++) |
245 |
{ |
246 |
if (selectedPCs[s]){
|
247 |
inputGrid.setBandToOperate(s); |
248 |
try {
|
249 |
valor+=(double)inputGrid.getCellValueAsShort(j, i)*pcStatistics.getAutoVectorsMatrix().get(s,resultOrden[k]);
|
250 |
} catch (GridException e) {
|
251 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "grid_error"), this, e); |
252 |
} |
253 |
} |
254 |
} |
255 |
rasterResult.setElem(i, j, banda[k],(float) valor);
|
256 |
valor=0;
|
257 |
} |
258 |
} |
259 |
} |
260 |
percent = i*100/layerExtent.getNX();
|
261 |
} |
262 |
} // Fin caso RasterBuffer TypeShort
|
263 |
|
264 |
// BUFFER TYPE INT
|
265 |
if (inputGrid.getRasterBuf().getDataType()== RasterBuffer.TYPE_INT){
|
266 |
for(int i=0; i<layerExtent.getNY();i++ ) { |
267 |
for (int j=0; j<layerExtent.getNX();j++){ |
268 |
for (int k=0; k<bandCount; k++){ |
269 |
if (selectedPCs[k]){
|
270 |
for(int s=0;s<bandCount;s++) |
271 |
{ |
272 |
if (selectedPCs[s]){
|
273 |
inputGrid.setBandToOperate(s); |
274 |
try {
|
275 |
valor+=(double)inputGrid.getCellValueAsInt(j, i)*pcStatistics.getAutoVectorsMatrix().get(s,resultOrden[k]);
|
276 |
} catch (GridException e) {
|
277 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "grid_error"), this, e); |
278 |
} |
279 |
} |
280 |
} |
281 |
rasterResult.setElem(i, j, banda[k],(float) valor);
|
282 |
valor=0;
|
283 |
} |
284 |
} |
285 |
} |
286 |
percent = i*100/layerExtent.getNX();
|
287 |
} |
288 |
} // Fin caso RasterBuffer TypeInt
|
289 |
|
290 |
|
291 |
// BUFFER TYPE FLOAT
|
292 |
if (inputGrid.getRasterBuf().getDataType()== RasterBuffer.TYPE_FLOAT){
|
293 |
for(int i=0; i<layerExtent.getNY();i++ ) { |
294 |
for (int j=0; j<layerExtent.getNX();j++){ |
295 |
for (int k=0; k<bandCount; k++){ |
296 |
if (selectedPCs[k]){
|
297 |
for(int s=0;s<bandCount;s++) |
298 |
{ |
299 |
if (selectedPCs[s]){
|
300 |
inputGrid.setBandToOperate(s); |
301 |
try {
|
302 |
valor+=(double)inputGrid.getCellValueAsFloat(j, i)*pcStatistics.getAutoVectorsMatrix().get(s,resultOrden[k]);
|
303 |
} catch (GridException e) {
|
304 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "grid_error"), this, e); |
305 |
} |
306 |
} |
307 |
} |
308 |
rasterResult.setElem(i, j, banda[k],(float) valor);
|
309 |
valor=0;
|
310 |
} |
311 |
} |
312 |
} |
313 |
percent = i*100/layerExtent.getNX();
|
314 |
} |
315 |
|
316 |
} // Fin caso RasterBuffer TypeFloat
|
317 |
|
318 |
|
319 |
// BUFFER TYPE DOUBLE
|
320 |
if (inputGrid.getRasterBuf().getDataType()== RasterBuffer.TYPE_DOUBLE){
|
321 |
for(int i=0; i<layerExtent.getNY();i++ ) { |
322 |
for (int j=0; j<layerExtent.getNX();j++){ |
323 |
for (int k=0; k<bandCount; k++){ |
324 |
if (selectedPCs[k]){
|
325 |
for(int s=0;s<bandCount;s++) |
326 |
{ |
327 |
if (selectedPCs[s]){
|
328 |
inputGrid.setBandToOperate(s); |
329 |
try {
|
330 |
valor+=(double)inputGrid.getCellValueAsDouble(j, i)*pcStatistics.getAutoVectorsMatrix().get(s,resultOrden[k]);
|
331 |
} catch (GridException e) {
|
332 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "grid_error"), this, e); |
333 |
} |
334 |
} |
335 |
} |
336 |
rasterResult.setElem(i, j, banda[k],(float) valor);
|
337 |
valor=0;
|
338 |
} |
339 |
} |
340 |
} |
341 |
percent = i*100/layerExtent.getNX();
|
342 |
} |
343 |
} // Fin caso RasterBuffer TypeDouble
|
344 |
|
345 |
// escritutra a fichero
|
346 |
createLayer(); |
347 |
|
348 |
if (externalActions != null) |
349 |
externalActions.end(outputRasterLayer); |
350 |
} |
351 |
|
352 |
/**
|
353 |
* Escritura del resultado en disco.
|
354 |
*/
|
355 |
private void createLayer(){ |
356 |
try{
|
357 |
// Escritura de los datos a fichero temporal
|
358 |
if(layerName==null) |
359 |
return;
|
360 |
String fileName=layerName;
|
361 |
int endIndex = fileName.lastIndexOf("."); |
362 |
if (endIndex < 0) |
363 |
endIndex = fileName.length(); |
364 |
GeoRasterWriter grw = null;
|
365 |
writerBufferServer = new WriterBufferServer(rasterResult);
|
366 |
grw = GeoRasterWriter.getWriter(writerBufferServer, layerName, rasterResult.getBandCount(),inputRasterLayer.getAffineTransform(), rasterResult.getWidth(), rasterResult.getHeight(), rasterResult.getDataType(), GeoRasterWriter.getWriter(layerName).getParams(), null);
|
367 |
grw.dataWrite(); |
368 |
grw.setWkt(inputRasterLayer.getWktProjection()); |
369 |
grw.writeClose(); |
370 |
|
371 |
outputRasterLayer = FLyrRasterSE.createLayer(RasterUtilities.getFileNameFromCanonical(layerName), |
372 |
layerName, null);
|
373 |
|
374 |
} catch (NotSupportedExtensionException e) {
|
375 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e); |
376 |
} catch (IOException e) { |
377 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e); |
378 |
} catch (InterruptedException e) { |
379 |
Thread.currentThread().interrupt();
|
380 |
} catch (RasterDriverException e) {
|
381 |
e.printStackTrace(); |
382 |
} catch (LoadLayerException e) {
|
383 |
RasterToolsUtil.messageBoxError("error_cargar_capa", this, e); |
384 |
} |
385 |
} |
386 |
|
387 |
/**
|
388 |
* Construye el grid con las bandas seleccionadas
|
389 |
*/
|
390 |
private void buildGrid(){ |
391 |
|
392 |
IRasterDataSource dsetCopy = null;
|
393 |
dsetCopy = inputRasterLayer.getDataSource().newDataset(); |
394 |
BufferFactory bufferFactory = new BufferFactory(dsetCopy);
|
395 |
if (!RasterBuffer.loadInMemory(dsetCopy))
|
396 |
bufferFactory.setReadOnly(true);
|
397 |
|
398 |
int longitud=0; |
399 |
for (int i=0; i<selectedBands.length;i++) |
400 |
if (selectedBands[i]) longitud++;
|
401 |
|
402 |
int bands[]= new int[longitud]; |
403 |
int j=0; |
404 |
for (int i=0; i<selectedBands.length; i++) |
405 |
if (selectedBands[i])
|
406 |
{ bands[j]=i; |
407 |
j++; |
408 |
} |
409 |
try {
|
410 |
inputGrid = new Grid(bufferFactory, bands);
|
411 |
} catch (RasterBufferInvalidException e) {
|
412 |
e.printStackTrace(); |
413 |
} |
414 |
} |
415 |
} |