root / branches / v2_0_0_prep / extensions / extRemoteSensing / src / org / gvsig / remotesensing / gridmath / GridMathProcess.java @ 28315
History | View | Annotate | Download (12.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 |
package org.gvsig.remotesensing.gridmath; |
42 |
|
43 |
import java.awt.geom.AffineTransform; |
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.util.HashMap; |
47 |
import java.util.Iterator; |
48 |
|
49 |
import org.gvsig.fmap.mapcontext.MapContext; |
50 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
51 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
52 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
53 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
54 |
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent; |
55 |
import org.gvsig.raster.RasterProcess; |
56 |
import org.gvsig.raster.buffer.BufferFactory; |
57 |
import org.gvsig.raster.buffer.RasterBuffer; |
58 |
import org.gvsig.raster.buffer.WriterBufferServer; |
59 |
import org.gvsig.raster.dataset.*; |
60 |
import org.gvsig.raster.dataset.io.RasterDriverException; |
61 |
import org.gvsig.raster.grid.GridExtent; |
62 |
import org.gvsig.raster.process.CancelEvent; |
63 |
import org.gvsig.raster.util.RasterToolsUtil; |
64 |
import org.gvsig.remotesensing.RemoteSensingUtils; |
65 |
import org.gvsig.remotesensing.gridmath.gui.GridMathPanel; |
66 |
import org.nfunk.jep.JEP; |
67 |
import org.nfunk.jep.Variable; |
68 |
|
69 |
import com.iver.andami.PluginServices; |
70 |
|
71 |
/**
|
72 |
* GridMathProcess implenta la funcionalidad para el c�lculo de operaciones entre grids.
|
73 |
* El proceso permite hacer operaciones matem�ticas entre los valores de las bandas de la misma imagen o entre
|
74 |
* diferentes im�genes bajo ciertas restricciones espaciales (siempre sobre los datos originales).
|
75 |
*
|
76 |
* @author Alejandro Mu�oz Sanchez (alejandro.munoz@uclm.es)
|
77 |
* @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
|
78 |
* @version 19/10/2007
|
79 |
*
|
80 |
*/
|
81 |
|
82 |
public class GridMathProcess extends RasterProcess{ |
83 |
|
84 |
HashMap params = null; // Asignacion de variables a grids. |
85 |
JEP parser = null;
|
86 |
String expression = null; |
87 |
GridExtent resultExtent = null;
|
88 |
private WriterBufferServer writerBufferServer = null; |
89 |
private int percent = 0; |
90 |
private boolean cancel = false; |
91 |
private MapContext mapContext = null; |
92 |
private String filePath = null; |
93 |
private AffineTransform aTransform = null; |
94 |
private RasterBuffer rasterResult = null; |
95 |
private GridMathPanel gridMathPanel = null; |
96 |
private boolean loadEnd= false; |
97 |
int loadBuffers =0; |
98 |
int numVar =1; |
99 |
/**
|
100 |
* Constructor
|
101 |
*/
|
102 |
public GridMathProcess(){
|
103 |
parser = new JEP();
|
104 |
parser.setAllowUndeclared(true);
|
105 |
parser.addStandardFunctions(); |
106 |
} |
107 |
|
108 |
|
109 |
/**
|
110 |
* @return HashMap con las variables asociadas a sus grid correspondientes
|
111 |
* */
|
112 |
public HashMap getParams() { |
113 |
return params;
|
114 |
} |
115 |
|
116 |
|
117 |
/**
|
118 |
* Asigna el params con las variables
|
119 |
* @param params
|
120 |
*/
|
121 |
public void setParams(HashMap params) { |
122 |
this.params = params;
|
123 |
} |
124 |
|
125 |
|
126 |
/**
|
127 |
* Establece la expresi�n en el parser
|
128 |
* @param expression expresion
|
129 |
*/
|
130 |
public void setExpression(String expression){ |
131 |
this.expression = expression;
|
132 |
parser.getSymbolTable().clear(); |
133 |
parser.parseExpression(expression); |
134 |
} |
135 |
|
136 |
/**
|
137 |
* @return expresi�n a evaluar
|
138 |
*/
|
139 |
public String getExpression() { |
140 |
return expression;
|
141 |
} |
142 |
|
143 |
|
144 |
/**
|
145 |
* @return true si en el parseo de la expresion hay error
|
146 |
*/
|
147 |
public boolean hasError(){ |
148 |
if (parser!=null) |
149 |
return parser.hasError();
|
150 |
else
|
151 |
return true; |
152 |
} |
153 |
|
154 |
|
155 |
|
156 |
/**
|
157 |
* Escritura del resultado en disco.
|
158 |
*/
|
159 |
private void writeToFile(){ |
160 |
|
161 |
if(filePath==null) |
162 |
return;
|
163 |
try {
|
164 |
writerBufferServer = new WriterBufferServer(rasterResult);
|
165 |
aTransform = new AffineTransform(resultExtent.getCellSizeX(), 0.0, 0.0, -resultExtent.getCellSizeY(), resultExtent.getMin().getX(), resultExtent.getMax().getY()); |
166 |
GeoRasterWriter grw = GeoRasterWriter.getWriter(writerBufferServer, filePath, rasterResult.getBandCount(), aTransform, resultExtent.getNX(), resultExtent.getNY(), rasterResult.getDataType(), GeoRasterWriter.getWriter(filePath).getParams(), mapContext.getProjection()); |
167 |
grw.dataWrite(); |
168 |
grw.writeClose(); |
169 |
} catch (NotSupportedExtensionException e) {
|
170 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer_notsupportedextension"), this, e); |
171 |
} catch (IOException e) { |
172 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e); |
173 |
} catch (InterruptedException e) { |
174 |
Thread.currentThread().interrupt();
|
175 |
} catch (RasterDriverException e) {
|
176 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "raster_buffer_invalid_extension"), this, e); |
177 |
} |
178 |
|
179 |
mapContext.beginAtomicEvent(); |
180 |
FLayer lyr = null;
|
181 |
|
182 |
int endIndex = filePath.lastIndexOf("."); |
183 |
if (endIndex < 0) |
184 |
endIndex = filePath.length(); |
185 |
|
186 |
try {
|
187 |
lyr = FLyrRasterSE.createLayer( |
188 |
filePath.substring(filePath.lastIndexOf(File.separator) + 1, endIndex), |
189 |
new File(filePath), |
190 |
mapContext.getProjection()); |
191 |
} catch (LoadLayerException e) {
|
192 |
RasterToolsUtil.messageBoxError("error_cargar_capa", this, e); |
193 |
} |
194 |
|
195 |
mapContext.getLayers().addLayer(lyr); |
196 |
mapContext.endAtomicEvent(); |
197 |
mapContext.invalidate(); |
198 |
mapContext.endAtomicEvent(); |
199 |
} |
200 |
|
201 |
/**
|
202 |
* @return extent del resultadoroot
|
203 |
*/
|
204 |
public GridExtent getResultExtent() {
|
205 |
return resultExtent;
|
206 |
} |
207 |
|
208 |
/**
|
209 |
* Asignaci�n del extent
|
210 |
* @param resultExtent
|
211 |
*/
|
212 |
public void setResultExtent(GridExtent resultExtent) { |
213 |
this.resultExtent = resultExtent;
|
214 |
} |
215 |
|
216 |
/*
|
217 |
* (non-Javadoc)
|
218 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
|
219 |
*/
|
220 |
public String getLabel() { |
221 |
return PluginServices.getText(this,"procesando"); |
222 |
} |
223 |
|
224 |
|
225 |
/*
|
226 |
* (non-Javadoc)
|
227 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
228 |
*/
|
229 |
public String getTitle() { |
230 |
return PluginServices.getText(this,"band_math"); |
231 |
} |
232 |
|
233 |
/*
|
234 |
* (non-Javadoc)
|
235 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#actionCanceled
|
236 |
*/
|
237 |
public void actionCanceled(IncrementableEvent e) { |
238 |
if(writerBufferServer != null) |
239 |
rasterTask.setEvent(new CancelEvent(this)); |
240 |
cancel = true;
|
241 |
} |
242 |
|
243 |
|
244 |
public void init() { |
245 |
gridMathPanel= (GridMathPanel)getParam("panel");
|
246 |
expression= getStringParam("expresion");
|
247 |
setExpression(expression); |
248 |
params= (HashMap )getParam("params"); |
249 |
resultExtent= (GridExtent) getParam("extent");
|
250 |
mapContext= (MapContext)getParam("mapcontext");
|
251 |
filePath= getStringParam("filepath");
|
252 |
loadBuffers(); |
253 |
} |
254 |
|
255 |
|
256 |
public void process() throws InterruptedException { |
257 |
RasterBuffer inputBuffer=null;
|
258 |
|
259 |
// Construccion del rasterbuffer que recoge el resultado del calculo
|
260 |
rasterResult = RasterBuffer.getBuffer(RasterBuffer.TYPE_DOUBLE, resultExtent.getNX() ,resultExtent.getNY(), 1, true); |
261 |
|
262 |
|
263 |
// Calculo de grid resultante
|
264 |
int iNX = resultExtent.getNX();
|
265 |
int iNY = resultExtent.getNY();
|
266 |
|
267 |
// Calculo de grid resultante
|
268 |
for (int x=0;x<iNX;x++){ |
269 |
if (cancel) return; //Proceso cancelado |
270 |
for(int y=0;y<iNY;y++){ |
271 |
int i=0; |
272 |
for (Iterator iter = params.keySet().iterator(); iter.hasNext();) { |
273 |
String varName = (String)iter.next(); |
274 |
Object data[]= (Object[])params.get(varName); |
275 |
inputBuffer= (RasterBuffer)data[0];
|
276 |
|
277 |
int dataType= ((Integer)data[1]).intValue(); |
278 |
double value=0; |
279 |
|
280 |
if(dataType==IBuffer.TYPE_BYTE){
|
281 |
value =(byte) RemoteSensingUtils.getCellValueInLayerCoords(inputBuffer, x, y, 0); |
282 |
if(value!=inputBuffer.getNoDataValue()){
|
283 |
parser.setVarValue(varName,new Double(value)); |
284 |
i++; |
285 |
}else{
|
286 |
rasterResult.setElem(y, x, 0, rasterResult.noDataValue);
|
287 |
break;
|
288 |
} |
289 |
}else{
|
290 |
value =RemoteSensingUtils.getCellValueInLayerCoords(inputBuffer, x, y, 0);
|
291 |
if(value!=inputBuffer.getNoDataValue()){
|
292 |
parser.setVarValue(varName,new Double(value)); |
293 |
i++; |
294 |
}else{
|
295 |
rasterResult.setElem(y, x, 0, rasterResult.noDataValue);
|
296 |
break;
|
297 |
} |
298 |
} |
299 |
} |
300 |
// Evaluacion de la expresion en el x,y.
|
301 |
if (i == params.size()){
|
302 |
rasterResult.setElem(y, x, 0, (double)parser.getValue()); |
303 |
percent = x*100/rasterResult.getWidth();
|
304 |
} |
305 |
} |
306 |
|
307 |
} |
308 |
// Escritura de los datos a disco
|
309 |
writeToFile(); |
310 |
|
311 |
} |
312 |
|
313 |
void loadBuffers(){
|
314 |
|
315 |
int nBand;
|
316 |
String layerBand;
|
317 |
String layerName;
|
318 |
FLyrRasterSE rasterLayer; |
319 |
FLayers layers = gridMathPanel.getView().getModel().getMapContext().getLayers(); |
320 |
|
321 |
RasterBuffer valor=null;
|
322 |
int numVar= gridMathPanel.getCalculatorPanel().getJTableVariables().getTableFormat().getRowCount();
|
323 |
for (int i=0;i<numVar;i++){ |
324 |
|
325 |
layerBand= gridMathPanel.getCalculatorPanel().getJTableVariables().getTableFormat().getValueAt(i,1).toString();
|
326 |
layerName = layerBand.substring(0,layerBand.indexOf("[")); |
327 |
nBand = Integer.valueOf(layerBand.substring(layerBand.lastIndexOf("Band")+4,layerBand.lastIndexOf("]"))).intValue(); |
328 |
rasterLayer = (FLyrRasterSE)layers.getLayer(layerName); |
329 |
|
330 |
double minX=0,minY=0,maxX=0,maxY=0; |
331 |
minX= gridMathPanel.getOutputExtent().getMin().getX(); |
332 |
minY= gridMathPanel.getOutputExtent().getMin().getY(); |
333 |
maxX= gridMathPanel.getOutputExtent().getMax().getX(); |
334 |
maxY =gridMathPanel.getOutputExtent().getMax().getY(); |
335 |
|
336 |
try {
|
337 |
|
338 |
IRasterDataSource dsetCopy = null;
|
339 |
dsetCopy = rasterLayer.getDataSource().newDataset(); |
340 |
BufferFactory bufferFactory = new BufferFactory(dsetCopy);
|
341 |
if (!RasterBuffer.loadInMemory(dsetCopy))
|
342 |
bufferFactory.setReadOnly(true);
|
343 |
bufferFactory.setAdjustToExtent(false);
|
344 |
bufferFactory.setDrawableBands(new int[]{nBand-1}); |
345 |
bufferFactory.setAreaOfInterest(minX,minY,maxX,maxY,gridMathPanel.getOutputExtent().getNX(),gridMathPanel.getOutputExtent().getNY()); |
346 |
valor=(RasterBuffer) bufferFactory.getRasterBuf(); |
347 |
loadBuffers+=1;
|
348 |
|
349 |
} catch (ArrayIndexOutOfBoundsException e) { |
350 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_writer"), this, e); |
351 |
} catch (InvalidSetViewException e) {
|
352 |
e.printStackTrace(); |
353 |
} catch (InterruptedException e) { |
354 |
Thread.currentThread().interrupt();
|
355 |
} catch (RasterDriverException e) {
|
356 |
e.printStackTrace(); |
357 |
} |
358 |
|
359 |
gridMathPanel.getCalculatorPanel().getQWindowsHash().put(gridMathPanel.getCalculatorPanel().getJTableVariables().getTableFormat().getValueAt(i,0).toString(),new Object[]{valor,new Integer(valor.getDataType())}); |
360 |
|
361 |
} |
362 |
gridMathPanel.getGridMath().setParams(gridMathPanel.getCalculatorPanel().getQWindowsHash()); |
363 |
|
364 |
|
365 |
JEP parser = new JEP();
|
366 |
parser.setAllowUndeclared(true);
|
367 |
parser.addStandardFunctions(); |
368 |
parser.parseExpression(gridMathPanel.getCalculatorPanel().getJTextExpression().getText()); |
369 |
|
370 |
for (Iterator iter = parser.getSymbolTable().values().iterator(); iter.hasNext();) { |
371 |
Variable variable = (Variable) iter.next(); |
372 |
if (!gridMathPanel.getGridMath().getParams().containsKey(variable.getName())){
|
373 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this,"variables_sin_asignar"),this); |
374 |
return;
|
375 |
} |
376 |
} |
377 |
params= gridMathPanel.getGridMath().getParams(); |
378 |
loadEnd= true;
|
379 |
} |
380 |
|
381 |
|
382 |
public int getPercent(){ |
383 |
|
384 |
/*if(!loadEnd)
|
385 |
return (int)((loadBuffers-1)/numVar)*100;
|
386 |
else */if(writerBufferServer!=null) |
387 |
return writerBufferServer.getPercent();
|
388 |
else
|
389 |
return percent;
|
390 |
} |
391 |
|
392 |
|
393 |
/*
|
394 |
* (non-Javadoc)
|
395 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
|
396 |
*/
|
397 |
public String getLog() { |
398 |
|
399 |
/* if(!loadEnd)
|
400 |
return PluginServices.getText(this,"cargando datos")+"...";*/
|
401 |
/*else*/ if (writerBufferServer!=null) |
402 |
return PluginServices.getText(this,"escribiendo_resultado")+"..."; |
403 |
else
|
404 |
return PluginServices.getText(this,"calculando_imagen")+"..."; |
405 |
} |
406 |
|
407 |
|
408 |
public Object getResult(){ |
409 |
return rasterResult;
|
410 |
} |
411 |
} |