root / trunk / extensions / extRemoteSensing / src / es / idr / teledeteccion / gridmath / GridMathProcess.java @ 12181
History | View | Annotate | Download (11.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 es.idr.teledeteccion.gridmath; |
42 |
|
43 |
import java.awt.Component; |
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.util.HashMap; |
47 |
import java.util.Iterator; |
48 |
|
49 |
import javax.swing.JOptionPane; |
50 |
|
51 |
import org.gvsig.fmap.layers.FLyrRasterSE; |
52 |
import org.gvsig.gui.beans.incrementabletask.IIncrementable; |
53 |
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent; |
54 |
import org.gvsig.gui.beans.incrementabletask.IncrementableListener; |
55 |
import org.gvsig.gui.beans.incrementabletask.IncrementableTask; |
56 |
import org.gvsig.raster.buffer.RasterBuffer; |
57 |
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException; |
58 |
import org.gvsig.raster.buffer.RasterBufferInvalidException; |
59 |
import org.gvsig.raster.dataset.GeoRasterWriter; |
60 |
import org.gvsig.raster.dataset.IBuffer; |
61 |
import org.gvsig.raster.dataset.NotSupportedExtensionException; |
62 |
import org.gvsig.raster.dataset.RasterDriverException; |
63 |
import org.gvsig.raster.grid.Grid; |
64 |
import org.gvsig.raster.grid.GridExtent; |
65 |
import org.gvsig.raster.grid.OutOfGridException; |
66 |
import org.gvsig.rastertools.cutting.WriterBufferServer; |
67 |
import org.nfunk.jep.JEP; |
68 |
import org.nfunk.jep.Variable; |
69 |
|
70 |
import com.iver.andami.PluginServices; |
71 |
import com.iver.andami.Utilities; |
72 |
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException; |
73 |
import com.iver.cit.gvsig.fmap.MapContext; |
74 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
75 |
|
76 |
/**
|
77 |
*
|
78 |
* Funcionalidad para realizar operaciones entre grids.
|
79 |
* @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
|
80 |
*
|
81 |
*/
|
82 |
|
83 |
public class GridMathProcess implements Runnable, IIncrementable, IncrementableListener { |
84 |
|
85 |
HashMap params = null; // Asignacion de variables a grids. |
86 |
JEP parser = null;
|
87 |
String expression = null; |
88 |
GridExtent resultExtent = null;
|
89 |
private IncrementableTask incrementableTask = null; |
90 |
private Thread blinker = null; |
91 |
private WriterBufferServer writerBufferServer = null; |
92 |
private int percent = 0; |
93 |
private boolean cancel = false; |
94 |
private MapContext mapContext = null; |
95 |
private String layerName = null; |
96 |
|
97 |
|
98 |
public GridMathProcess() {
|
99 |
parser = new JEP();
|
100 |
parser.setAllowUndeclared(true);
|
101 |
parser.addStandardFunctions(); |
102 |
} |
103 |
|
104 |
|
105 |
/**@param expresion: Expresion a ser evaluada
|
106 |
* @param params: HashMap con los param
|
107 |
* @param resultExtent Extent de salida
|
108 |
* @param mapContext: Contexto de la vista
|
109 |
* @param layername: nombre capa
|
110 |
* */
|
111 |
public GridMathProcess(String expression, HashMap params,GridExtent resultExtent,MapContext mapContext,String layerName) { |
112 |
|
113 |
parser = new JEP();
|
114 |
parser.setAllowUndeclared(true);
|
115 |
parser.addStandardFunctions(); |
116 |
parser.parseExpression(expression); |
117 |
this.params = params;
|
118 |
this.resultExtent= resultExtent;
|
119 |
this.mapContext= mapContext;
|
120 |
this.layerName= layerName;
|
121 |
|
122 |
} |
123 |
|
124 |
/**
|
125 |
* @return HashMap con las variables asociadas a sus Grid correspondientes
|
126 |
* */
|
127 |
public HashMap getParams() { |
128 |
return params;
|
129 |
} |
130 |
|
131 |
|
132 |
public void setParams(HashMap params) { |
133 |
this.params = params;
|
134 |
} |
135 |
|
136 |
|
137 |
public void setExpression(String expression){ |
138 |
this.expression = expression;
|
139 |
parser.getSymbolTable().clear(); |
140 |
parser.parseExpression(expression); |
141 |
} |
142 |
|
143 |
public String getExpression() { |
144 |
return expression;
|
145 |
} |
146 |
|
147 |
|
148 |
public boolean hasError(){ |
149 |
if (parser!=null) |
150 |
return parser.hasError();
|
151 |
else
|
152 |
return true; |
153 |
} |
154 |
|
155 |
/**
|
156 |
* Lanza el proceso.
|
157 |
*
|
158 |
*/
|
159 |
public void start() { |
160 |
cancel = false;
|
161 |
blinker = new Thread(this); |
162 |
blinker.start(); |
163 |
} |
164 |
|
165 |
/**
|
166 |
* Calculo del grid resultante. Escritura en fichero temporal y volcado a la vista.
|
167 |
* */
|
168 |
public void run() { |
169 |
/*
|
170 |
Comprobar que todas las variables que intervienen en la expresin
|
171 |
estan en el HasMap de parmetros.
|
172 |
*/
|
173 |
|
174 |
for (Iterator iter = parser.getSymbolTable().values().iterator(); iter.hasNext();) { |
175 |
Variable element = (Variable) iter.next(); |
176 |
if (!params.containsKey(element.getName()))
|
177 |
try {
|
178 |
throw new NoAssignedVarsException(); |
179 |
} catch (NoAssignedVarsException e) {
|
180 |
e.printStackTrace(); |
181 |
} |
182 |
} |
183 |
|
184 |
if (resultExtent == null) |
185 |
resultExtent = ((Grid)params.values().iterator().next()).getGridExtent(); |
186 |
|
187 |
// Construccion del Grid para escritura
|
188 |
Grid resultGrid = null;
|
189 |
try {
|
190 |
resultGrid = new Grid (resultExtent,resultExtent,IBuffer.TYPE_DOUBLE,new int[]{0}); |
191 |
} catch (RasterBufferInvalidException e) {
|
192 |
e.printStackTrace(); |
193 |
} |
194 |
|
195 |
int iNX = resultGrid.getNX();
|
196 |
int iNY = resultGrid.getNY();
|
197 |
|
198 |
// Calculo de grid resultante
|
199 |
for (int x=0;x<iNX;x++){ |
200 |
|
201 |
if (cancel) return; //Proceso cancelado |
202 |
for(int y=0;y<iNY;y++) |
203 |
{ |
204 |
int i=0; |
205 |
for (Iterator iter = params.keySet().iterator(); iter.hasNext();) { |
206 |
|
207 |
String varName = (String)iter.next(); |
208 |
Grid inputGrid = (Grid)params.get(varName); |
209 |
double value=0; |
210 |
|
211 |
// Caso que el RasterBufer de la variable asociada se de tipo Byte
|
212 |
if(inputGrid.getRasterBuf().getDataType() == RasterBuffer.TYPE_BYTE){
|
213 |
|
214 |
try {
|
215 |
value = inputGrid.getCellValueAsByte(x, y); |
216 |
} catch (RasterBufferInvalidAccessException e) {
|
217 |
e.printStackTrace(); |
218 |
} |
219 |
if(inputGrid.isInGrid(x, y)){
|
220 |
parser.setVarValue(varName,new Double(value)); |
221 |
i++; |
222 |
} |
223 |
else{
|
224 |
resultGrid.setNoData(x,y); |
225 |
break;
|
226 |
} |
227 |
} |
228 |
// Caso que el RasterBufer de la variable asociada se de tipo SHORT
|
229 |
if(inputGrid.getRasterBuf().getDataType() == RasterBuffer.TYPE_SHORT){
|
230 |
|
231 |
try {
|
232 |
value = inputGrid.getCellValueAsShort(x, y); |
233 |
} catch (RasterBufferInvalidAccessException e) {
|
234 |
e.printStackTrace(); |
235 |
} |
236 |
|
237 |
if(inputGrid.isInGrid(x, y)){
|
238 |
parser.setVarValue(varName,new Double(value)); |
239 |
i++; |
240 |
} |
241 |
else{
|
242 |
resultGrid.setNoData(x,y); |
243 |
break;
|
244 |
} |
245 |
} |
246 |
// Caso que el RasterBufer de la variable asociada se de tipo INT
|
247 |
if(inputGrid.getRasterBuf().getDataType() == RasterBuffer.TYPE_INT){
|
248 |
|
249 |
try {
|
250 |
value = inputGrid.getCellValueAsInt(x, y); |
251 |
} catch (RasterBufferInvalidAccessException e) {
|
252 |
e.printStackTrace(); |
253 |
} |
254 |
|
255 |
if(inputGrid.isInGrid(x, y)){
|
256 |
parser.setVarValue(varName,new Double(value)); |
257 |
i++; |
258 |
} |
259 |
else{
|
260 |
resultGrid.setNoData(x,y); |
261 |
break;
|
262 |
} |
263 |
} |
264 |
// Caso que el RasterBufer de la variable asociada se de tipo float
|
265 |
if(inputGrid.getRasterBuf().getDataType() == RasterBuffer.TYPE_FLOAT){
|
266 |
|
267 |
|
268 |
try {
|
269 |
value = inputGrid.getCellValueAsFloat(x, y); |
270 |
} catch (RasterBufferInvalidAccessException e) {
|
271 |
e.printStackTrace(); |
272 |
} |
273 |
|
274 |
if(inputGrid.isInGrid(x, y)){
|
275 |
parser.setVarValue(varName,new Double(value)); |
276 |
i++; |
277 |
} |
278 |
else{
|
279 |
resultGrid.setNoData(x,y); |
280 |
break;
|
281 |
} |
282 |
} |
283 |
// Caso que el RasterBufer de la variable asociada se de tipo Double
|
284 |
if(inputGrid.getRasterBuf().getDataType() == RasterBuffer.TYPE_DOUBLE){
|
285 |
|
286 |
|
287 |
try {
|
288 |
value = inputGrid.getCellValueAsDouble(x, y); |
289 |
} catch (RasterBufferInvalidAccessException e) {
|
290 |
e.printStackTrace(); |
291 |
} |
292 |
|
293 |
if(inputGrid.isInGrid(x, y)){
|
294 |
parser.setVarValue(varName,new Double(value)); |
295 |
i++; |
296 |
} |
297 |
else{
|
298 |
resultGrid.setNoData(x,y); |
299 |
break;
|
300 |
} |
301 |
} |
302 |
|
303 |
} |
304 |
// Evaluacion de la exprsion en el x,y.
|
305 |
if (i == params.size()){
|
306 |
try {
|
307 |
resultGrid.setCellValue(x, y, parser.getValue()); |
308 |
} catch (OutOfGridException e) {
|
309 |
e.printStackTrace(); |
310 |
} |
311 |
percent = x*100/resultGrid.getNX();
|
312 |
} |
313 |
} |
314 |
} |
315 |
|
316 |
// Escritura de los datos a disco
|
317 |
String fileName=Utilities.createTempDirectory()+"/" + layerName +".tif"; |
318 |
|
319 |
int endIndex = fileName.lastIndexOf("."); |
320 |
if (endIndex < 0) |
321 |
endIndex = fileName.length(); |
322 |
|
323 |
IBuffer buffer = resultGrid.getRasterBuf(); |
324 |
writerBufferServer = new WriterBufferServer(buffer);
|
325 |
|
326 |
try{
|
327 |
|
328 |
GeoRasterWriter grw = GeoRasterWriter.getWriter(writerBufferServer, fileName, buffer.getBandCount(),resultExtent, buffer.getWidth(), buffer.getHeight(),buffer.getDataType(), GeoRasterWriter.getWriter(fileName).getParams()); |
329 |
grw.dataWrite(); |
330 |
grw.writeClose(); |
331 |
|
332 |
|
333 |
}catch(NotSupportedExtensionException e1){
|
334 |
e1.printStackTrace(); |
335 |
} |
336 |
|
337 |
catch(RasterDriverException e2){
|
338 |
e2.printStackTrace(); |
339 |
} |
340 |
|
341 |
catch(IOException e3){ |
342 |
e3.printStackTrace(); |
343 |
} |
344 |
|
345 |
|
346 |
mapContext.beginAtomicEvent(); |
347 |
FLayer lyr = null;
|
348 |
|
349 |
try {
|
350 |
|
351 |
lyr = FLyrRasterSE.createLayer(layerName +".tif",new File(fileName), mapContext.getProjection()); |
352 |
} catch (LoadLayerException e) {
|
353 |
|
354 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
355 |
PluginServices.getText(this, "error_cargar_capa")); |
356 |
} |
357 |
|
358 |
mapContext.getLayers().addLayer(lyr); |
359 |
mapContext.endAtomicEvent(); |
360 |
mapContext.invalidate(); |
361 |
mapContext.endAtomicEvent(); |
362 |
|
363 |
if (incrementableTask != null) |
364 |
incrementableTask.processFinalize(); |
365 |
|
366 |
}// Fin run()
|
367 |
|
368 |
|
369 |
|
370 |
public GridExtent getResultExtent() {
|
371 |
return resultExtent;
|
372 |
} |
373 |
|
374 |
public void setResultExtent(GridExtent resultExtent) { |
375 |
this.resultExtent = resultExtent;
|
376 |
} |
377 |
|
378 |
public String getLabel() { |
379 |
return PluginServices.getText(this,"procesando"); |
380 |
} |
381 |
|
382 |
public String getLog() { |
383 |
if (writerBufferServer==null) |
384 |
return PluginServices.getText(this,"calculando_imagen")+"..."; |
385 |
else
|
386 |
return PluginServices.getText(this,"escribiendo_resultado")+"..."; |
387 |
} |
388 |
|
389 |
public int getPercent() { |
390 |
if (writerBufferServer==null) |
391 |
return percent;
|
392 |
else
|
393 |
return writerBufferServer.getPercent();
|
394 |
} |
395 |
|
396 |
public String getTitle() { |
397 |
return PluginServices.getText(this,"band_math"); |
398 |
} |
399 |
|
400 |
public void actionCanceled(IncrementableEvent e) { |
401 |
if(writerBufferServer != null) |
402 |
writerBufferServer.setCanceled(true, 0); |
403 |
cancel = true;
|
404 |
|
405 |
} |
406 |
|
407 |
public void actionResumed(IncrementableEvent e) { |
408 |
|
409 |
} |
410 |
|
411 |
public void actionSuspended(IncrementableEvent e) { |
412 |
|
413 |
} |
414 |
|
415 |
public void setIncrementableTask(IncrementableTask incrementableTask) { |
416 |
this.incrementableTask = incrementableTask;
|
417 |
this.incrementableTask.addIncrementableListener(this); |
418 |
} |
419 |
|
420 |
} |