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