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