Revision 15048
org.gvsig.raster/tags/org.gvsig.raster-2.2.166/org.gvsig.raster.algorithm/src/main/java/org/gvsig/raster/algorithm/RasterBaseAlgorithmManager.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.raster.algorithm; |
|
23 |
|
|
24 |
import org.gvsig.raster.algorithm.process.DataProcess; |
|
25 |
import org.gvsig.raster.algorithm.process.ProcessException; |
|
26 |
import org.gvsig.tools.ToolsLocator; |
|
27 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
|
28 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
|
29 |
|
|
30 |
/** |
|
31 |
* Default {@link RasterBaseAlgorithmManager} implementation. |
|
32 |
* |
|
33 |
* @author Nacho Brodin (nachobrodin@gmail.com) |
|
34 |
* @version $Id$ |
|
35 |
*/ |
|
36 |
public class RasterBaseAlgorithmManager { |
|
37 |
private static RasterBaseAlgorithmManager internalInstance = new RasterBaseAlgorithmManager(); |
|
38 |
|
|
39 |
/** |
|
40 |
* Gets an instance of this object for internal use. |
|
41 |
* @return RasterBaseAlgorithmManager |
|
42 |
*/ |
|
43 |
public static RasterBaseAlgorithmManager getInstance() { |
|
44 |
return internalInstance; |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* Returns a new instance of a process. |
|
49 |
* @param processLabel |
|
50 |
* Label to identify the process |
|
51 |
* @return DataProcess |
|
52 |
* @throws ProcessException |
|
53 |
*/ |
|
54 |
public DataProcess createRasterTask(String processLabel) throws ProcessException { |
|
55 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
|
56 |
ExtensionPoint point = extensionPoints.add(RasterBaseAlgorithmLibrary.REGISTER_PROCESS_LABEL); |
|
57 |
try { |
|
58 |
DataProcess process = (DataProcess)point.create(processLabel); |
|
59 |
process.setName(processLabel); |
|
60 |
return process; |
|
61 |
} catch (InstantiationException e) { |
|
62 |
throw new ProcessException("", e); |
|
63 |
} catch (IllegalAccessException e) { |
|
64 |
throw new ProcessException("", e); |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
} |
|
0 | 69 |
org.gvsig.raster/tags/org.gvsig.raster-2.2.166/org.gvsig.raster.algorithm/src/main/java/org/gvsig/raster/algorithm/util/Operation.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.raster.algorithm.util; |
|
23 |
|
|
24 |
/** |
|
25 |
* Interface for operations |
|
26 |
* @author Nacho Brodin nachobrodin@gmail.com |
|
27 |
*/ |
|
28 |
public interface Operation { |
|
29 |
public boolean compare(double a, double b); |
|
30 |
|
|
31 |
public void invoke(); |
|
32 |
} |
|
0 | 33 |
org.gvsig.raster/tags/org.gvsig.raster-2.2.166/org.gvsig.raster.algorithm/src/main/java/org/gvsig/raster/algorithm/util/AbstractOperation.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.raster.algorithm.util; |
|
23 |
|
|
24 |
/** |
|
25 |
* Interface for operations |
|
26 |
* @author Nacho Brodin nachobrodin@gmail.com |
|
27 |
*/ |
|
28 |
public abstract class AbstractOperation implements Operation { |
|
29 |
public boolean compare(double a, double b) { |
|
30 |
return false; |
|
31 |
} |
|
32 |
|
|
33 |
public void invoke() { |
|
34 |
//do nothing |
|
35 |
} |
|
36 |
} |
|
0 | 37 |
org.gvsig.raster/tags/org.gvsig.raster-2.2.166/org.gvsig.raster.algorithm/src/main/java/org/gvsig/raster/algorithm/util/Interpolation.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.raster.algorithm.util; |
|
23 |
|
|
24 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
|
25 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
|
26 |
import org.gvsig.fmap.dal.coverage.datastruct.NoData; |
|
27 |
|
|
28 |
/** |
|
29 |
* Calculates a pixel value using a interpolation method |
|
30 |
* @author Nacho Brodin nachobrodin@gmail.com |
|
31 |
* @author Victor Olaya |
|
32 |
*/ |
|
33 |
public class Interpolation { |
|
34 |
private Buffer buffer = null; |
|
35 |
private double nodata = 0; |
|
36 |
|
|
37 |
public Interpolation(Buffer buf) { |
|
38 |
this.buffer = buf; |
|
39 |
NoData nodata = RasterLocator.getManager().getDataStructFactory().createDefaultNoData( |
|
40 |
1, Buffer.TYPE_DOUBLE); |
|
41 |
this.nodata = nodata.getValue().doubleValue(); |
|
42 |
} |
|
43 |
|
|
44 |
private double[] getKernel(int x, int y, int band) { |
|
45 |
if(buffer.getDataType() == Buffer.TYPE_BYTE) { |
|
46 |
return getKernelByte(x, y, band); |
|
47 |
} |
|
48 |
if(buffer.getDataType() == Buffer.TYPE_DOUBLE) { |
|
49 |
return getKernelByte(x, y, band); |
|
50 |
} |
|
51 |
if(buffer.getDataType() == Buffer.TYPE_FLOAT) { |
|
52 |
return getKernelByte(x, y, band); |
|
53 |
} |
|
54 |
if(buffer.getDataType() == Buffer.TYPE_SHORT) { |
|
55 |
return getKernelByte(x, y, band); |
|
56 |
} |
|
57 |
if(buffer.getDataType() == Buffer.TYPE_INT) { |
|
58 |
return getKernelByte(x, y, band); |
|
59 |
} |
|
60 |
return null; |
|
61 |
} |
|
62 |
|
|
63 |
public double getNearestNeighbour(double x, double y, int band) { |
|
64 |
int dy = (int)Math.round(y); |
|
65 |
int dx = (int)Math.round(x); |
|
66 |
dy = dy < buffer.getHeight() ? dy : buffer.getHeight() - 1; |
|
67 |
dx = dx < buffer.getWidth() ? dx : buffer.getWidth() - 1; |
|
68 |
if(buffer.getDataType() == Buffer.TYPE_BYTE) { |
|
69 |
return (double)buffer.getElemByte(dy, dx, band); |
|
70 |
} |
|
71 |
if(buffer.getDataType() == Buffer.TYPE_DOUBLE) { |
|
72 |
return (double)buffer.getElemDouble(dy, dx, band); |
|
73 |
} |
|
74 |
if(buffer.getDataType() == Buffer.TYPE_FLOAT) { |
|
75 |
return (double)buffer.getElemFloat(dy, dx, band); |
|
76 |
} |
|
77 |
if(buffer.getDataType() == Buffer.TYPE_SHORT) { |
|
78 |
return (double)buffer.getElemShort(dy, dx, band); |
|
79 |
} |
|
80 |
if(buffer.getDataType() == Buffer.TYPE_INT) { |
|
81 |
return (double)buffer.getElemInt(dy, dx, band); |
|
82 |
} |
|
83 |
return nodata; |
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* Calcula los valores N y Z para el m?todo bilinear y obtiene el valor del pixel como |
|
88 |
* Z / N |
|
89 |
* @param dx distancia en X desde el centro del pixel hasta el punto. Es un valor entre 0 y 1 |
|
90 |
* @param dy distancia en Y desde el centro del pixel hasta el punto. Es un valor entre 0 y 1 |
|
91 |
* @param kernel valor del pixel y alrededor |
|
92 |
* @return valor del pixel |
|
93 |
*/ |
|
94 |
public double getBilinearValue(double x, double y, int band) { |
|
95 |
double[] kernel = getKernel((int)x, (int)y, band); |
|
96 |
double dx = x - ((int) x); |
|
97 |
double dy = y - ((int) y); |
|
98 |
|
|
99 |
double z = 0.0, n = 0.0, d; |
|
100 |
d = (1.0 - dx) * (1.0 - dy); |
|
101 |
z += d * kernel[0]; |
|
102 |
n += d; |
|
103 |
|
|
104 |
d = dx * (1.0 - dy); |
|
105 |
z += d * kernel[1]; |
|
106 |
n += d; |
|
107 |
|
|
108 |
d = (1.0 - dx) * dy; |
|
109 |
z += d * kernel[2]; |
|
110 |
n += d; |
|
111 |
|
|
112 |
d = dx * dy; |
|
113 |
z += d * kernel[3]; |
|
114 |
n += d; |
|
115 |
|
|
116 |
double b = 0; |
|
117 |
if(n > 0.0) |
|
118 |
b = (z / n); |
|
119 |
return b; |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* Calcula los valores N y Z para el m?todo de distancia inversa y calcula el valor del |
|
124 |
* pixel como Z / N. |
|
125 |
* @param dx distancia en X desde el centro del pixel hasta el punto. Es un valor entre 0 y 1 |
|
126 |
* @param dy distancia en Y desde el centro del pixel hasta el punto. Es un valor entre 0 y 1 |
|
127 |
* @param kernel valor del pixel y alrededor |
|
128 |
* @return valor del pixel |
|
129 |
*/ |
|
130 |
public double getInverseDistance(double x, double y, int band) { |
|
131 |
double[] kernel = getKernel((int)x, (int)y, band); |
|
132 |
double dx = x - ((int) x); |
|
133 |
double dy = y - ((int) y); |
|
134 |
|
|
135 |
double z = 0.0, n = 0.0, d; |
|
136 |
d = 1.0 / Math.sqrt(dx * dx + dy * dy); |
|
137 |
z += d * kernel[0]; |
|
138 |
n += d; |
|
139 |
|
|
140 |
d = 1.0 / Math.sqrt((1.0 - dx) * ( 1.0 - dx) + dy * dy); |
|
141 |
z += d * kernel[1]; |
|
142 |
n += d; |
|
143 |
|
|
144 |
d = 1.0 / Math.sqrt(dx*dx + (1.0-dy)*(1.0-dy)); |
|
145 |
z += d * kernel[2]; |
|
146 |
n += d; |
|
147 |
|
|
148 |
d = 1.0 / Math.sqrt((1.0 - dx) *( 1.0 - dx) + (1.0 - dy) * (1.0 - dy)); |
|
149 |
z += d * kernel[3]; |
|
150 |
n += d; |
|
151 |
|
|
152 |
double b = 0; |
|
153 |
if(n > 0.0) |
|
154 |
b = (z / n); |
|
155 |
return b; |
|
156 |
} |
|
157 |
|
|
158 |
/** |
|
159 |
* Obtiene un kernel de cuatro elemento que corresponden a los pixeles (x, y), (x + 1, y), |
|
160 |
* (x, y + 1), (x + 1, y + 1). Si los pixeles x + 1 o y + 1 se salen del raster de origen |
|
161 |
* se tomar? x e y. |
|
162 |
* @param x Coordenada X del pixel inicial |
|
163 |
* @param y Coordenada Y del pixel inicial |
|
164 |
* @param band N?mero de banda. |
|
165 |
* @return Kernel solicitado en forma de array. |
|
166 |
*/ |
|
167 |
private double[] getKernelByte(int x, int y, int band) { |
|
168 |
double[] d = new double[4]; |
|
169 |
d[0] = (buffer.getElemByte(y, x, band) & 0xff); |
|
170 |
int nextX = ((x + 1) >= buffer.getWidth()) ? x : (x + 1); |
|
171 |
int nextY = ((y + 1) >= buffer.getHeight()) ? y : (y + 1); |
|
172 |
d[1] = (buffer.getElemByte(y, nextX, band) & 0xff); |
|
173 |
d[2] = (buffer.getElemByte(nextY, x, band) & 0xff); |
|
174 |
d[3] = (buffer.getElemByte(nextY, nextX, band) & 0xff); |
|
175 |
return d; |
|
176 |
} |
|
177 |
|
|
178 |
/** |
|
179 |
* Obtiene un kernel de cuatro elemento que corresponden a los pixeles (x, y), (x + 1, y), |
|
180 |
* (x, y + 1), (x + 1, y + 1). Si los pixeles x + 1 o y + 1 se salen del raster de origen |
|
181 |
* se tomar? x e y. |
|
182 |
* @param x Coordenada X del pixel inicial |
|
183 |
* @param y Coordenada Y del pixel inicial |
|
184 |
* @param band N?mero de banda. |
|
185 |
* @return Kernel solicitado en forma de array. |
|
186 |
*/ |
|
187 |
@SuppressWarnings("unused") |
|
188 |
private double[] getKernelShort(int x, int y, int band) { |
|
189 |
double[] d = new double[4]; |
|
190 |
d[0] = (buffer.getElemShort(y, x, band) & 0xffff); |
|
191 |
int nextX = ((x + 1) >= buffer.getWidth()) ? x : (x + 1); |
|
192 |
int nextY = ((y + 1) >= buffer.getHeight()) ? y : (y + 1); |
|
193 |
d[1] = (buffer.getElemShort(y, nextX, band) & 0xffff); |
|
194 |
d[2] = (buffer.getElemShort(nextY, x, band) & 0xffff); |
|
195 |
d[3] = (buffer.getElemShort(nextY, nextX, band) & 0xffff); |
|
196 |
return d; |
|
197 |
} |
|
198 |
|
|
199 |
/** |
|
200 |
* Obtiene un kernel de cuatro elemento que corresponden a los pixeles (x, y), (x + 1, y), |
|
201 |
* (x, y + 1), (x + 1, y + 1). Si los pixeles x + 1 o y + 1 se salen del raster de origen |
|
202 |
* se tomar? x e y. |
|
203 |
* @param x Coordenada X del pixel inicial |
|
204 |
* @param y Coordenada Y del pixel inicial |
|
205 |
* @param band N?mero de banda. |
|
206 |
* @return Kernel solicitado en forma de array. |
|
207 |
*/ |
|
208 |
@SuppressWarnings("unused") |
|
209 |
private double[] getKernelInt(int x, int y, int band) { |
|
210 |
double[] d = new double[4]; |
|
211 |
d[0] = (buffer.getElemInt(y, x, band) & 0xffffffff); |
|
212 |
int nextX = ((x + 1) >= buffer.getWidth()) ? x : (x + 1); |
|
213 |
int nextY = ((y + 1) >= buffer.getHeight()) ? y : (y + 1); |
|
214 |
d[1] = (buffer.getElemInt(y, nextX, band) & 0xffffffff); |
|
215 |
d[2] = (buffer.getElemInt(nextY, x, band) & 0xffffffff); |
|
216 |
d[3] = (buffer.getElemInt(nextY, nextX, band) & 0xffffffff); |
|
217 |
return d; |
|
218 |
} |
|
219 |
|
|
220 |
/** |
|
221 |
* Obtiene un kernel de cuatro elemento que corresponden a los pixeles (x, y), (x + 1, y), |
|
222 |
* (x, y + 1), (x + 1, y + 1). Si los pixeles x + 1 o y + 1 se salen del raster de origen |
|
223 |
* se tomar? x e y. |
|
224 |
* @param x Coordenada X del pixel inicial |
|
225 |
* @param y Coordenada Y del pixel inicial |
|
226 |
* @param band N?mero de banda. |
|
227 |
* @return Kernel solicitado en forma de array. |
|
228 |
*/ |
|
229 |
@SuppressWarnings("unused") |
|
230 |
private double[] getKernelFloat(int x, int y, int band) { |
|
231 |
double[] d = new double[4]; |
|
232 |
d[0] = buffer.getElemFloat(y, x, band); |
|
233 |
int nextX = ((x + 1) >= buffer.getWidth()) ? x : (x + 1); |
|
234 |
int nextY = ((y + 1) >= buffer.getHeight()) ? y : (y + 1); |
|
235 |
d[1] = buffer.getElemFloat(y, nextX, band); |
|
236 |
d[2] = buffer.getElemFloat(nextY, x, band); |
|
237 |
d[3] = buffer.getElemFloat(nextY, nextX, band); |
|
238 |
return d; |
|
239 |
} |
|
240 |
|
|
241 |
/** |
|
242 |
* Obtiene un kernel de cuatro elemento que corresponden a los pixeles (x, y), (x + 1, y), |
|
243 |
* (x, y + 1), (x + 1, y + 1). Si los pixeles x + 1 o y + 1 se salen del raster de origen |
|
244 |
* se tomar? x e y. |
|
245 |
* @param x Coordenada X del pixel inicial |
|
246 |
* @param y Coordenada Y del pixel inicial |
|
247 |
* @param band N?mero de banda. |
|
248 |
* @return Kernel solicitado en forma de array. |
|
249 |
*/ |
|
250 |
@SuppressWarnings("unused") |
|
251 |
private double[] getKernelDouble(int x, int y, int band) { |
|
252 |
double[] d = new double[4]; |
|
253 |
d[0] = buffer.getElemDouble(y, x, band); |
|
254 |
int nextX = ((x + 1) >= buffer.getWidth()) ? x : (x + 1); |
|
255 |
int nextY = ((y + 1) >= buffer.getHeight()) ? y : (y + 1); |
|
256 |
d[1] = buffer.getElemDouble(y, nextX, band); |
|
257 |
d[2] = buffer.getElemDouble(nextY, x, band); |
|
258 |
d[3] = buffer.getElemDouble(nextY, nextX, band); |
|
259 |
return d; |
|
260 |
} |
|
261 |
} |
|
0 | 262 |
org.gvsig.raster/tags/org.gvsig.raster-2.2.166/org.gvsig.raster.algorithm/src/main/java/org/gvsig/raster/algorithm/process/DataProcess.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.raster.algorithm.process; |
|
23 |
|
|
24 |
import java.util.HashMap; |
|
25 |
import java.util.Hashtable; |
|
26 |
|
|
27 |
import javax.swing.SwingUtilities; |
|
28 |
|
|
29 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
|
30 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
|
31 |
import org.gvsig.fmap.dal.coverage.process.CancelEvent; |
|
32 |
import org.gvsig.fmap.dal.coverage.process.TaskEventManager; |
|
33 |
import org.gvsig.raster.algorithm.gui.IIncrementable; |
|
34 |
import org.gvsig.raster.algorithm.gui.IncrementableEvent; |
|
35 |
import org.gvsig.raster.algorithm.gui.IncrementableListener; |
|
36 |
import org.gvsig.raster.algorithm.gui.IncrementableTask; |
|
37 |
import org.gvsig.raster.swing.basepanel.IButtonsPanel; |
|
38 |
import org.gvsig.tools.dispose.Disposable; |
|
39 |
import org.slf4j.Logger; |
|
40 |
import org.slf4j.LoggerFactory; |
|
41 |
/** |
|
42 |
* Clase base de todos los procesos raster. En ella se genstionan todas las |
|
43 |
* funciones comunes como incremento de la tarea, gesti๏ฟฝn de eventos a la tarea, |
|
44 |
* par๏ฟฝmetros de la tarea, etc ... |
|
45 |
* |
|
46 |
* 18/12/2007 |
|
47 |
* @author Nacho Brodin nachobrodin@gmail.com |
|
48 |
*/ |
|
49 |
public abstract class DataProcess extends ProcessParamsManagement implements IIncrementable, IncrementableListener, Runnable, Disposable { |
|
50 |
protected IncrementableTask incrementableTask = null; |
|
51 |
protected volatile Thread blinker = null; |
|
52 |
protected TaskEventManager taskEventManager = null; |
|
53 |
protected IProcessActions externalActions = null; |
|
54 |
protected Hashtable<String, Object> |
|
55 |
taskParams = new Hashtable<String, Object>(); |
|
56 |
|
|
57 |
|
|
58 |
private String log = ""; |
|
59 |
private String lastLine = ""; |
|
60 |
private long time = 0; |
|
61 |
private boolean progressActive = true; |
|
62 |
private IProcessActions queueActions = null; |
|
63 |
protected Logger logger = LoggerFactory.getLogger(DataProcess.class.toString()); |
|
64 |
private int percent = 0; |
|
65 |
private String processName = null; |
|
66 |
|
|
67 |
//*************************************************** |
|
68 |
//Task control |
|
69 |
//*************************************************** |
|
70 |
|
|
71 |
/** |
|
72 |
* Builds the window <code>IncrementableTask</code> to show |
|
73 |
* the increment of the task. |
|
74 |
*/ |
|
75 |
public IncrementableTask getIncrementableTask() { |
|
76 |
if (incrementableTask == null) { |
|
77 |
incrementableTask = new IncrementableTask(this); |
|
78 |
incrementableTask.addIncrementableListener(this); |
|
79 |
} |
|
80 |
return incrementableTask; |
|
81 |
} |
|
82 |
|
|
83 |
/** |
|
84 |
* Sets the button to cancel the task |
|
85 |
*/ |
|
86 |
public void setCancelable(boolean enabled) { |
|
87 |
getIncrementableTask().getButtonsPanel().setEnabled(IButtonsPanel.BUTTON_CANCEL, enabled); |
|
88 |
} |
|
89 |
|
|
90 |
/** |
|
91 |
* Show the increment of this task <code>IncrementableTask</code> |
|
92 |
*/ |
|
93 |
public void showIncrementableWindow() { |
|
94 |
if (progressActive) { |
|
95 |
getIncrementableTask().showWindow(); |
|
96 |
getIncrementableTask().start(); |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* Starts the process. It will call the run method |
|
102 |
*/ |
|
103 |
public void start() { |
|
104 |
showIncrementableWindow(); |
|
105 |
if(blinker == null) |
|
106 |
blinker = new Thread(this); |
|
107 |
blinker.start(); |
|
108 |
} |
|
109 |
|
|
110 |
/** |
|
111 |
* A specific task will load parameters using this initialization. |
|
112 |
*/ |
|
113 |
public abstract void init(); |
|
114 |
|
|
115 |
/** |
|
116 |
* This call will be implemented by a specific process. It will do the actions to carry out |
|
117 |
* the task |
|
118 |
* @throws InterruptedException |
|
119 |
*/ |
|
120 |
public abstract void process() throws ProcessInterruptedException, ProcessException; |
|
121 |
|
|
122 |
/** |
|
123 |
* Gets the result of the process. All processes will have a parameter PROCESS that it is |
|
124 |
* the own process. It is useful to get the process name or other data. |
|
125 |
* |
|
126 |
* This result of the process is sent to the method "end" in <code>IProcessActions</code> when the task |
|
127 |
* is thrown in a thread or is recovered by the user when the task is thrown sequentially. |
|
128 |
* |
|
129 |
* @return Result of the task |
|
130 |
*/ |
|
131 |
public HashMap<String, Object> getResult() { |
|
132 |
outputParameters.put("PROCESS", this); |
|
133 |
outputParameters.put(TIME, getTime()); |
|
134 |
outputParameters.put(PROCESS_NAME, getName()); |
|
135 |
return outputParameters; |
|
136 |
} |
|
137 |
|
|
138 |
/** |
|
139 |
* Adds a output from the process in runtime |
|
140 |
* @param key |
|
141 |
* @param value |
|
142 |
*/ |
|
143 |
protected void addOutputValue(String key, Object value) { |
|
144 |
outputParameters.put(key, value); |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* Sequential execution of this task |
|
149 |
* @throws ProcessException |
|
150 |
*/ |
|
151 |
public void execute() throws ProcessInterruptedException, ProcessException { |
|
152 |
taskEventManager = RasterLocator.getManager().createRasterTask(this); |
|
153 |
loadGlobalParameters(); |
|
154 |
init(); |
|
155 |
process(); |
|
156 |
|
|
157 |
//Sets in the output the result of the process |
|
158 |
SwingUtilities.invokeLater(new Runnable() { |
|
159 |
public void run() { |
|
160 |
if (externalActions != null) { |
|
161 |
externalActions.end(getResult()); |
|
162 |
} |
|
163 |
} |
|
164 |
}); |
|
165 |
} |
|
166 |
|
|
167 |
/** |
|
168 |
* This method will be executed by the Thread. It will have the global actions for |
|
169 |
* any task and it will call to the <code>execute</code> function. |
|
170 |
*/ |
|
171 |
public void run() { |
|
172 |
long t1 = new java.util.Date().getTime(); |
|
173 |
|
|
174 |
try { |
|
175 |
execute(); |
|
176 |
} catch (ProcessInterruptedException e) { |
|
177 |
if (externalActions != null) |
|
178 |
externalActions.interrupted(); |
|
179 |
Thread.currentThread().interrupt(); |
|
180 |
} catch (ProcessException e) { |
|
181 |
if (progressActive) { |
|
182 |
if (incrementableTask != null) { |
|
183 |
getIncrementableTask().processFinalize(); |
|
184 |
incrementableTask = null; |
|
185 |
} |
|
186 |
} |
|
187 |
logger.warn(RasterLocator.getManager().getRasterUtils().getTrace(e)); |
|
188 |
if(e.getMessage() != null && e.getMessage().compareTo("") != 0) |
|
189 |
messageBoxError(e.getMessage(), null); |
|
190 |
else |
|
191 |
messageBoxError("error_processing", null); |
|
192 |
queueActions = null; |
|
193 |
|
|
194 |
} catch (Exception e) { |
|
195 |
if (progressActive) { |
|
196 |
if (incrementableTask != null) { |
|
197 |
getIncrementableTask().processFinalize(); |
|
198 |
incrementableTask = null; |
|
199 |
} |
|
200 |
} |
|
201 |
logger.warn(RasterLocator.getManager().getRasterUtils().getTrace(e)); |
|
202 |
if(e.getMessage() != null && e.getMessage().compareTo("") != 0) |
|
203 |
messageBoxError(e.getMessage(), null); |
|
204 |
else |
|
205 |
messageBoxError("error_processing", null); |
|
206 |
queueActions = null; |
|
207 |
} finally { |
|
208 |
try { |
|
209 |
//Evita un NullPointerException sun.awt.X11.XWindowPeer.restoreTransientFor(XWindowPeer.java:1500) |
|
210 |
Thread.sleep(500); |
|
211 |
} catch(Exception e) {} |
|
212 |
|
|
213 |
taskEventManager.removeTask(); |
|
214 |
if (progressActive) { |
|
215 |
if (incrementableTask != null) |
|
216 |
getIncrementableTask().processFinalize(); |
|
217 |
} |
|
218 |
if (queueActions != null) |
|
219 |
queueActions.end(this); |
|
220 |
time = new java.util.Date().getTime() - t1; |
|
221 |
blinker = null; |
|
222 |
} |
|
223 |
} |
|
224 |
|
|
225 |
/** |
|
226 |
* When the process is launched in a thread, this method enables or disables |
|
227 |
* the GUI to show the progress. |
|
228 |
*/ |
|
229 |
public void setProgressActive(boolean active) { |
|
230 |
this.progressActive = active; |
|
231 |
} |
|
232 |
|
|
233 |
/** |
|
234 |
* Gets the time that a task lasts |
|
235 |
*/ |
|
236 |
public long getTime() { |
|
237 |
return time; |
|
238 |
} |
|
239 |
|
|
240 |
/** |
|
241 |
* Gets the object to execute external actions. |
|
242 |
* A user should be register this object to receive the events |
|
243 |
*/ |
|
244 |
public IProcessActions getActions() { |
|
245 |
return externalActions; |
|
246 |
} |
|
247 |
|
|
248 |
/** |
|
249 |
* Sets the object to execute external actions. |
|
250 |
* A user should be register this object to receive the events |
|
251 |
*/ |
|
252 |
public void setActions(IProcessActions actions) { |
|
253 |
this.externalActions = actions; |
|
254 |
} |
|
255 |
|
|
256 |
/** |
|
257 |
* Gets the object to execute actions in the queue of processes |
|
258 |
*/ |
|
259 |
public IProcessActions getUniqueProcessActions() { |
|
260 |
return queueActions; |
|
261 |
} |
|
262 |
|
|
263 |
/** |
|
264 |
* Asigna el objeto para ejecutar acciones externar. |
|
265 |
* @param IProcessActions |
|
266 |
*/ |
|
267 |
public void setUniqueProcessActions(IProcessActions actions) { |
|
268 |
this.queueActions = actions; |
|
269 |
} |
|
270 |
|
|
271 |
/** |
|
272 |
* Inserts a new line in the increment window |
|
273 |
*/ |
|
274 |
protected void insertLineLog(String line) { |
|
275 |
lastLine = line; |
|
276 |
log = log + line + "\n"; |
|
277 |
} |
|
278 |
|
|
279 |
/** |
|
280 |
* Gets the last line in the increment window |
|
281 |
*/ |
|
282 |
public String getLabel() { |
|
283 |
return lastLine; |
|
284 |
} |
|
285 |
|
|
286 |
/** |
|
287 |
* Gets the log of the increment window |
|
288 |
*/ |
|
289 |
public String getLog() { |
|
290 |
return log; |
|
291 |
} |
|
292 |
|
|
293 |
/** |
|
294 |
* Un evento de cancelado es enviado a la tarea cuando actionCanceled es activado. Para |
|
295 |
* ello se crear๏ฟฝ un objeto CancelEvent y se asignar๏ฟฝ a la tarea en ejecuci๏ฟฝn. Esta lo |
|
296 |
* procesar๏ฟฝ cuando pueda e interrumpir๏ฟฝ el proceso. |
|
297 |
*/ |
|
298 |
public void actionCanceled(IncrementableEvent e) { |
|
299 |
taskEventManager.setEvent(new CancelEvent(this)); |
|
300 |
} |
|
301 |
|
|
302 |
/** |
|
303 |
* Updates the percentaje of progress and manages the cancelation |
|
304 |
* @param parcial |
|
305 |
* @param total |
|
306 |
* @throws ProcessInterruptedException |
|
307 |
*/ |
|
308 |
public void updatePercent(int parcial, int total) throws ProcessInterruptedException { |
|
309 |
if (taskEventManager != null && taskEventManager.getEvent() != null) |
|
310 |
taskEventManager.manageEvent(taskEventManager.getEvent()); |
|
311 |
if(externalActions != null) |
|
312 |
externalActions.updateProgress(parcial, total); |
|
313 |
percent = (int)((parcial * 100) / total); |
|
314 |
} |
|
315 |
|
|
316 |
public int getPercent() { |
|
317 |
return percent; |
|
318 |
} |
|
319 |
|
|
320 |
public void actionResumed(IncrementableEvent e) { |
|
321 |
} |
|
322 |
|
|
323 |
public void actionSuspended(IncrementableEvent e) { |
|
324 |
} |
|
325 |
|
|
326 |
public boolean isCancelable() { |
|
327 |
return true; |
|
328 |
} |
|
329 |
|
|
330 |
public boolean isPausable() { |
|
331 |
return false; |
|
332 |
} |
|
333 |
|
|
334 |
public String getName() { |
|
335 |
return processName; |
|
336 |
} |
|
337 |
|
|
338 |
public void setName(String name) { |
|
339 |
this.processName = name; |
|
340 |
} |
|
341 |
|
|
342 |
public void dispose() { |
|
343 |
try { |
|
344 |
finalize(); |
|
345 |
} catch (Throwable e) { |
|
346 |
} |
|
347 |
} |
|
348 |
|
|
349 |
@Override |
|
350 |
protected void finalize() throws Throwable { |
|
351 |
incrementableTask = null; |
|
352 |
blinker = null; |
|
353 |
externalActions = null; |
|
354 |
super.finalize(); |
|
355 |
} |
|
356 |
|
|
357 |
} |
|
0 | 358 |
org.gvsig.raster/tags/org.gvsig.raster-2.2.166/org.gvsig.raster.algorithm/src/main/java/org/gvsig/raster/algorithm/process/IProcessActions.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.raster.algorithm.process; |
|
23 |
|
|
24 |
/** |
|
25 |
* Acciones que puede ejecutar un proceso sobre un objeto externo. Un proceso |
|
26 |
* debe ser independiente de objetos que sean dependientes de su funcionalidad. Es |
|
27 |
* decir, cualquier extensi?n debe poder usar un proceso y este ejecutar metodos |
|
28 |
* de la funcionalidad. Esto se hace ha trav?s de este interfaz. |
|
29 |
* |
|
30 |
* @version 26/09/2007 |
|
31 |
* @author Nacho Brodin (nachobrodin@gmail.com) |
|
32 |
* |
|
33 |
*/ |
|
34 |
public interface IProcessActions { |
|
35 |
|
|
36 |
/** |
|
37 |
* El proceso comunica que ha sido interrumpido |
|
38 |
* |
|
39 |
*/ |
|
40 |
public void interrupted(); |
|
41 |
|
|
42 |
/** |
|
43 |
* Acciones de finalizaci?n del proceso |
|
44 |
*/ |
|
45 |
public void end(Object param); |
|
46 |
|
|
47 |
/** |
|
48 |
* A process can report the progress of the task |
|
49 |
* @param current |
|
50 |
* @param total |
|
51 |
*/ |
|
52 |
public void updateProgress(int current, int total); |
|
53 |
} |
|
0 | 54 |
org.gvsig.raster/tags/org.gvsig.raster-2.2.166/org.gvsig.raster.algorithm/src/main/java/org/gvsig/raster/algorithm/process/ProcessException.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.raster.algorithm.process; |
|
23 |
/** |
|
24 |
* This exception is thrown when a problem inside a raster process |
|
25 |
* is detected. |
|
26 |
* |
|
27 |
* @author Nacho Brodin (nachobrodin@gmail.com) |
|
28 |
*/ |
|
29 |
public class ProcessException extends Exception { |
|
30 |
private static final long serialVersionUID = 666908550965442025L; |
|
31 |
|
|
32 |
public ProcessException(String msg, Throwable e) { |
|
33 |
super(msg, e); |
|
34 |
} |
|
35 |
|
|
36 |
public ProcessException(String msg){ |
|
37 |
super(msg); |
|
38 |
} |
|
39 |
} |
|
0 | 40 |
org.gvsig.raster/tags/org.gvsig.raster-2.2.166/org.gvsig.raster.algorithm/src/main/java/org/gvsig/raster/algorithm/process/ProcessParamsManagement.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.raster.algorithm.process; |
|
23 |
|
|
24 |
import java.util.ArrayList; |
|
25 |
import java.util.HashMap; |
|
26 |
import java.util.Hashtable; |
|
27 |
import java.util.Iterator; |
|
28 |
import java.util.List; |
|
29 |
|
|
30 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
|
31 |
import org.gvsig.raster.algorithm.gui.IIncrementable; |
|
32 |
import org.gvsig.raster.algorithm.gui.IncrementableListener; |
|
33 |
import org.gvsig.tools.ToolsLocator; |
|
34 |
import org.gvsig.tools.dispose.Disposable; |
|
35 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
|
36 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
|
37 |
|
|
38 |
/** |
|
39 |
* Base class for all geoprocesses. This class contains all utilities to register |
|
40 |
* and get parameters in a geoprocess instance |
|
41 |
* |
|
42 |
* @author Nacho Brodin nachobrodin@gmail.com |
|
43 |
*/ |
|
44 |
public abstract class ProcessParamsManagement extends ProcessUtils implements IIncrementable, IncrementableListener, Runnable, Disposable { |
|
45 |
//*********************** |
|
46 |
//Global input parameters |
|
47 |
//*********************** |
|
48 |
|
|
49 |
/** |
|
50 |
* Input parameter: If is defined then the ROIs will be used |
|
51 |
*/ |
|
52 |
public static String ROI_EPSG = "ROI_EPSG"; |
|
53 |
/** |
|
54 |
* Input parameter: Force to this bounding box. If is defined, then ROI_EPSG is not taken into account |
|
55 |
*/ |
|
56 |
public static String WINDOW = "WINDOW"; |
|
57 |
/** |
|
58 |
* Input parameter: Force to this width in pixels in the output buffer |
|
59 |
*/ |
|
60 |
public static String OUTPUT_WIDTH = "OUTPUT_WIDTH"; |
|
61 |
/** |
|
62 |
* Input parameter: Force to this height in pixels in the output buffer |
|
63 |
*/ |
|
64 |
public static String OUTPUT_HEIGHT = "OUTPUT_HEIGHT"; |
|
65 |
/** |
|
66 |
* Input parameter: The output of this algorithm is for previews. It usually implies that the buffer will be |
|
67 |
* send as output parameter, the buffer will be RGB and the input buffer will be to read and write |
|
68 |
*/ |
|
69 |
public static String PREVIEW = "PREVIEW"; |
|
70 |
|
|
71 |
//************************ |
|
72 |
//Global output parameters |
|
73 |
//*********************** |
|
74 |
|
|
75 |
/** |
|
76 |
* Output parameter: The getResult sets the correct value in the output. |
|
77 |
*/ |
|
78 |
public static String TIME = "TIME"; |
|
79 |
/** |
|
80 |
* Output parameter: The getResult sets the correct value in the output. |
|
81 |
*/ |
|
82 |
public static String PROCESS = "PROCESS"; |
|
83 |
/** |
|
84 |
* Output parameter: The getResult sets the correct value in the output. |
|
85 |
*/ |
|
86 |
public static String PROCESS_NAME = "PROCESS_NAME"; |
|
87 |
|
|
88 |
public static final String REGISTER_INPUT_PARAMETERS_LABEL = "RasterProcessInputParam"; |
|
89 |
public static final String REGISTER_OUTPUT_PARAMETERS_LABEL = "RasterProcessOutputParam"; |
|
90 |
|
|
91 |
private Extent outputWindow = null; |
|
92 |
private int outputWidth = 0; |
|
93 |
private int outputHeight = 0; |
|
94 |
private String roiEPSG = null; |
|
95 |
private boolean preview = false; |
|
96 |
private boolean globalParametersLoaded = false; |
|
97 |
|
|
98 |
protected Hashtable<String, Object> inputParameters = new Hashtable<String, Object>(); |
|
99 |
protected HashMap<String, Object> outputParameters = new HashMap<String, Object>(); |
|
100 |
|
|
101 |
/** |
|
102 |
* Registers input parameters of a raster process |
|
103 |
*/ |
|
104 |
public static void registerInputParameter(String parameterLabel, Class<?> parameterClass, String processLabel) { |
|
105 |
registerParameter(processLabel, parameterLabel, parameterClass, REGISTER_INPUT_PARAMETERS_LABEL); |
|
106 |
} |
|
107 |
|
|
108 |
/** |
|
109 |
* Registers output parameters of a raster process |
|
110 |
*/ |
|
111 |
public static void registerOutputParameter(String parameterLabel, Class<?> parameterClass, String processLabel) { |
|
112 |
registerParameter(processLabel, parameterLabel, parameterClass, REGISTER_OUTPUT_PARAMETERS_LABEL); |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* Registers a parameter for a raster process |
|
117 |
*/ |
|
118 |
private static void registerParameter(String processLabel, String parameterLabel, Class<?> parameterClass, String type) { |
|
119 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
|
120 |
ExtensionPoint point = extensionPoints.add(type + "_" + processLabel); |
|
121 |
point.append(parameterLabel, "", parameterClass); |
|
122 |
registerGlobalInputParameters(parameterLabel, parameterClass, processLabel); |
|
123 |
registerGlobalOutputParameters(parameterLabel, parameterClass, processLabel); |
|
124 |
} |
|
125 |
|
|
126 |
public static void registerGlobalInputParameters(String parameterLabel, Class<?> parameterClass, String processLabel) { |
|
127 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
|
128 |
ExtensionPoint point = extensionPoints.add(REGISTER_INPUT_PARAMETERS_LABEL + "_" + processLabel); |
|
129 |
if(!point.has(ROI_EPSG)) |
|
130 |
point.append(ROI_EPSG, "", String.class); |
|
131 |
if(!point.has(WINDOW)) |
|
132 |
point.append(WINDOW, "", Extent.class); |
|
133 |
if(!point.has(OUTPUT_HEIGHT)) |
|
134 |
point.append(OUTPUT_HEIGHT, "", Integer.class); |
|
135 |
if(!point.has(OUTPUT_WIDTH)) |
|
136 |
point.append(OUTPUT_WIDTH, "", Integer.class); |
|
137 |
if(!point.has(PREVIEW)) |
|
138 |
point.append(PREVIEW, "", Boolean.class); |
|
139 |
} |
|
140 |
|
|
141 |
public static void registerGlobalOutputParameters(String parameterLabel, Class<?> parameterClass, String processLabel) { |
|
142 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
|
143 |
ExtensionPoint point = extensionPoints.add(REGISTER_OUTPUT_PARAMETERS_LABEL + "_" + processLabel); |
|
144 |
if(!point.has(TIME)) |
|
145 |
point.append(TIME, "", Long.class); |
|
146 |
if(!point.has(PROCESS)) |
|
147 |
point.append(PROCESS, "", DataProcess.class); |
|
148 |
if(!point.has(PROCESS_NAME)) |
|
149 |
point.append(PROCESS_NAME, "", String.class); |
|
150 |
} |
|
151 |
|
|
152 |
/** |
|
153 |
* Gets the key list of the input parameters |
|
154 |
* @param processLabel |
|
155 |
* @return |
|
156 |
*/ |
|
157 |
@SuppressWarnings("unchecked") |
|
158 |
public List<String> getRasterTaskInputParameters(String processLabel) { |
|
159 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
|
160 |
ExtensionPoint point = extensionPoints.add(REGISTER_INPUT_PARAMETERS_LABEL + "_" + processLabel); |
|
161 |
return point.getNames(); |
|
162 |
} |
|
163 |
|
|
164 |
/** |
|
165 |
* Gets the key list of the output parameters |
|
166 |
* @param processLabel |
|
167 |
* @return |
|
168 |
*/ |
|
169 |
@SuppressWarnings("unchecked") |
|
170 |
public List<String> getRasterTaskOutputParameters(String processLabel) { |
|
171 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
|
172 |
ExtensionPoint point = extensionPoints.add(REGISTER_OUTPUT_PARAMETERS_LABEL + "_" + processLabel); |
|
173 |
return point.getNames(); |
|
174 |
} |
|
175 |
|
|
176 |
/** |
|
177 |
* Gets the class of a parameter in a process. |
|
178 |
* @param processLabel |
|
179 |
* @param parameterName |
|
180 |
* @return |
|
181 |
*/ |
|
182 |
public Class<?> getParameterTypeByProcess(String processLabel, String parameterName) { |
|
183 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
|
184 |
ExtensionPoint point = extensionPoints.get(REGISTER_INPUT_PARAMETERS_LABEL + "_" + processLabel); |
|
185 |
return point.get(parameterName).getExtension(); |
|
186 |
} |
|
187 |
|
|
188 |
/** |
|
189 |
* Gets a list with the class of all parameters. |
|
190 |
* @param processLabel |
|
191 |
* @param parameterName |
|
192 |
* @return |
|
193 |
*/ |
|
194 |
public List<Class<?>> getParameterClassList(String processLabel, String parameterName) { |
|
195 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
|
196 |
ExtensionPoint point = extensionPoints.get(REGISTER_INPUT_PARAMETERS_LABEL + "_" + processLabel); |
|
197 |
List<Class<?>> classList = new ArrayList<Class<?>>(); |
|
198 |
Iterator<?> it = point.iterator(); |
|
199 |
while(it.hasNext()) { |
|
200 |
Object obj = it.next(); |
|
201 |
classList.add((Class<?>)obj); |
|
202 |
} |
|
203 |
return classList; |
|
204 |
} |
|
205 |
|
|
206 |
@Override |
|
207 |
protected void finalize() throws Throwable { |
|
208 |
if(inputParameters != null) { |
|
209 |
inputParameters.clear(); |
|
210 |
inputParameters = null; |
|
211 |
} |
|
212 |
super.finalize(); |
|
213 |
} |
|
214 |
|
|
215 |
/** |
|
216 |
* Add a parameter to this task at runtime. The |
|
217 |
* parameter should have been registered before add |
|
218 |
* @param name key |
|
219 |
* @param param object to this task |
|
220 |
*/ |
|
221 |
public void addParam(String key, Object param) { |
|
222 |
if (param != null) |
|
223 |
inputParameters.put(key, param); |
|
224 |
else |
|
225 |
inputParameters.remove(key); |
|
226 |
} |
|
227 |
|
|
228 |
/** |
|
229 |
* Remove a parameter to this task at runtime. |
|
230 |
* @param name key |
|
231 |
*/ |
|
232 |
public void removeParam(String key) { |
|
233 |
inputParameters.remove(key); |
|
234 |
} |
|
235 |
|
|
236 |
/** |
|
237 |
* Gets a parameter from its key |
|
238 |
* @param name key |
|
239 |
* @return parameter |
|
240 |
*/ |
|
241 |
public Object getParam(String key) { |
|
242 |
loadGlobalParameters(); |
|
243 |
return inputParameters.get(key); |
|
244 |
} |
|
245 |
|
|
246 |
/** |
|
247 |
* Gets a <code>String</code> parameter from its key |
|
248 |
* @param key |
|
249 |
* @return parameter |
|
250 |
*/ |
|
251 |
public String getStringParam(String key) { |
|
252 |
loadGlobalParameters(); |
|
253 |
Object value = inputParameters.get(key); |
|
254 |
return (value != null && value instanceof String) ? (String)value : null; |
|
255 |
} |
|
256 |
|
|
257 |
/** |
|
258 |
* Gets a <code>Byte</code> parameter from its key |
|
259 |
* @param key |
|
260 |
* @return parameter |
|
261 |
*/ |
|
262 |
public byte getByteParam(String name) { |
|
263 |
loadGlobalParameters(); |
|
264 |
Object value = inputParameters.get(name); |
|
265 |
return (value != null && value instanceof Byte) ? ((Byte)value).byteValue() : 0; |
|
266 |
} |
|
267 |
|
|
268 |
/** |
|
269 |
* Gets a <code>Float</code> parameter from its key |
|
270 |
* @param key |
|
271 |
* @return parameter |
|
272 |
*/ |
|
273 |
public float getFloatParam(String name) { |
|
274 |
loadGlobalParameters(); |
|
275 |
Object value = inputParameters.get(name); |
|
276 |
return (value != null && value instanceof Float) ? ((Float)value).floatValue() : 0F; |
|
277 |
} |
|
278 |
|
|
279 |
/** |
|
280 |
* Gets a <code>double</code> parameter from its key |
|
281 |
* @param key |
|
282 |
* @return parameter |
|
283 |
*/ |
|
284 |
public double getDoubleParam(String name) { |
|
285 |
loadGlobalParameters(); |
|
286 |
Object value = inputParameters.get(name); |
|
287 |
return (value != null && value instanceof Double) ? ((Double)value).doubleValue() : 0D; |
|
288 |
} |
|
289 |
|
|
290 |
/** |
|
291 |
* Gets a <code>int</code> parameter from its key |
|
292 |
* @param key |
|
293 |
* @return parameter |
|
294 |
*/ |
|
295 |
public int getIntParam(String name) { |
|
296 |
loadGlobalParameters(); |
|
297 |
Object value = inputParameters.get(name); |
|
298 |
return (value != null && value instanceof Integer) ? ((Integer)value).intValue() : 0; |
|
299 |
} |
|
300 |
|
|
301 |
/** |
|
302 |
* Gets a <code>Boolean</code> parameter from its key |
|
303 |
* @param key |
|
304 |
* @return parameter |
|
305 |
*/ |
|
306 |
public boolean getBooleanParam(String name) { |
|
307 |
loadGlobalParameters(); |
|
308 |
Object value = inputParameters.get(name); |
|
309 |
return (value != null && value instanceof Boolean) ? ((Boolean)value).booleanValue() : false; |
|
310 |
} |
|
311 |
|
|
312 |
/** |
|
313 |
* Gets a <code>int[]</code> parameter from its key |
|
314 |
* @param key |
|
315 |
* @return parameter |
|
316 |
*/ |
|
317 |
public int[] getIntArrayParam(String name) { |
|
318 |
loadGlobalParameters(); |
|
319 |
Object value = inputParameters.get(name); |
|
320 |
return (value != null && value instanceof int[]) ? ((int[])value) : null; |
|
321 |
} |
|
322 |
|
|
323 |
/** |
|
324 |
* Gets a <code>double[]</code> parameter from its key |
|
325 |
* @param key |
|
326 |
* @return parameter |
|
327 |
*/ |
|
328 |
public double[] getDoubleArrayParam(String name) { |
|
329 |
loadGlobalParameters(); |
|
330 |
Object value = inputParameters.get(name); |
|
331 |
return (value != null && value instanceof double[]) ? ((double[])value) : null; |
|
332 |
} |
|
333 |
|
|
334 |
/** |
|
335 |
* Gets a <code>Extent</code> parameter from its key |
|
336 |
* @param key |
|
337 |
* @return parameter |
|
338 |
*/ |
|
339 |
public Extent getExtentParam(String name) { |
|
340 |
loadGlobalParameters(); |
|
341 |
Object value = inputParameters.get(name); |
|
342 |
return (value != null && value instanceof Extent) ? ((Extent)value) : null; |
|
343 |
} |
|
344 |
|
|
345 |
/** |
|
346 |
* Gets the default EPSG for the regions of interest. If this parameter is null |
|
347 |
* @return |
|
348 |
*/ |
|
349 |
protected String getROIEPSG() { |
|
350 |
return roiEPSG; |
|
351 |
} |
|
352 |
|
|
353 |
/** |
|
354 |
* Gets the bounding box defined by the user to the process |
|
355 |
* @return |
|
356 |
*/ |
|
357 |
protected Extent getOutputWindow() { |
|
358 |
return outputWindow; |
|
359 |
} |
|
360 |
|
|
361 |
/** |
|
362 |
* Gets the width in pixels of the output |
|
363 |
* @return |
|
364 |
*/ |
|
365 |
protected int getOutputWidth() { |
|
366 |
return outputWidth; |
|
367 |
} |
|
368 |
|
|
369 |
/** |
|
370 |
* Gets the height in pixels of the output |
|
371 |
* @return |
|
372 |
*/ |
|
373 |
protected int getOutputHeight() { |
|
374 |
return outputHeight; |
|
375 |
} |
|
376 |
|
|
377 |
/** |
|
378 |
* Returns true if the output will be rescaled |
|
379 |
* @return |
|
380 |
*/ |
|
381 |
protected boolean isOutputRescaled() { |
|
382 |
return (outputWidth != 0 || outputHeight != 0); |
|
383 |
} |
|
384 |
|
|
385 |
/** |
|
386 |
* Returns true if the output of this algorithm is for previews. It usually implies that the buffer will be |
|
387 |
* send as output parameter, the buffer will be RGB and the input buffer will be to read and write. |
|
388 |
* @return |
|
389 |
*/ |
|
390 |
protected boolean isForPreviews() { |
|
391 |
return preview; |
|
392 |
} |
|
393 |
|
|
394 |
/** |
|
395 |
* Loads global parameters defined in this class |
|
396 |
*/ |
|
397 |
protected void loadGlobalParameters() { |
|
398 |
if(globalParametersLoaded) |
|
399 |
return; |
|
400 |
globalParametersLoaded = true; |
|
401 |
roiEPSG = getStringParam(ROI_EPSG); |
|
402 |
outputWindow = getParam(WINDOW) != null ? (Extent)getParam(WINDOW) : null; |
|
403 |
outputWidth = getIntParam(OUTPUT_WIDTH); |
|
404 |
outputHeight = getIntParam(OUTPUT_HEIGHT); |
|
405 |
preview = getBooleanParam(PREVIEW); |
|
406 |
} |
|
407 |
} |
|
0 | 408 |
org.gvsig.raster/tags/org.gvsig.raster-2.2.166/org.gvsig.raster.algorithm/src/main/java/org/gvsig/raster/algorithm/process/ProcessUtils.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.raster.algorithm.process; |
|
23 |
|
|
24 |
import java.awt.Component; |
|
25 |
import java.awt.Rectangle; |
|
26 |
import java.awt.geom.AffineTransform; |
|
27 |
import java.awt.geom.Point2D; |
|
28 |
import java.awt.geom.Rectangle2D; |
|
29 |
import java.util.ArrayList; |
|
30 |
import java.util.List; |
|
31 |
|
|
32 |
import javax.swing.JOptionPane; |
|
33 |
|
|
34 |
import org.cresques.cts.IProjection; |
|
35 |
import org.gvsig.fmap.dal.DALLocator; |
|
36 |
import org.gvsig.fmap.dal.DataManager; |
|
37 |
import org.gvsig.fmap.dal.DataServerExplorer; |
|
38 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
|
39 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
|
40 |
import org.gvsig.fmap.dal.coverage.RasterManager; |
|
41 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
|
42 |
import org.gvsig.fmap.dal.coverage.dataset.BufferParam; |
|
43 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
|
44 |
import org.gvsig.fmap.dal.coverage.datastruct.NoData; |
|
45 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
|
46 |
import org.gvsig.fmap.dal.coverage.exception.BufferCreationException; |
|
47 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
|
48 |
import org.gvsig.fmap.dal.coverage.exception.QueryException; |
|
49 |
import org.gvsig.fmap.dal.coverage.store.DataServerWriter; |
|
50 |
import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
|
51 |
import org.gvsig.fmap.dal.coverage.store.RasterQuery; |
|
52 |
import org.gvsig.fmap.dal.coverage.store.RasterWriter; |
|
53 |
import org.gvsig.fmap.dal.coverage.store.parameter.NewRasterStoreParameters; |
|
54 |
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation; |
|
55 |
import org.gvsig.fmap.dal.exception.DataException; |
|
56 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
57 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
|
58 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
59 |
import org.gvsig.i18n.Messages; |
|
60 |
import org.gvsig.raster.roi.ROI; |
|
61 |
import org.slf4j.Logger; |
|
62 |
import org.slf4j.LoggerFactory; |
|
63 |
/** |
|
64 |
* Clase base de todos los procesos raster. En ella se genstionan todas las |
|
65 |
* funciones comunes como incremento de la tarea, gesti?n de eventos a la tarea, |
|
66 |
* par?metros de la tarea, etc ... |
|
67 |
* |
|
68 |
* @author Nacho Brodin nachobrodin@gmail.com |
|
69 |
*/ |
|
70 |
public abstract class ProcessUtils { |
|
71 |
private Logger log = LoggerFactory.getLogger(ProcessUtils.class); |
|
72 |
protected NoData doubleNODATA = RasterLocator.getManager().getDataStructFactory().createDefaultNoData(1, Buffer.TYPE_DOUBLE); |
|
73 |
|
|
74 |
/** |
|
75 |
* Registra un mensaje de error en el log de gvSIG |
|
76 |
* @param msg Mensaje a guardar en el log |
|
77 |
* @param parent Objeto que hizo disparar el mensaje |
|
78 |
* @param exception Excepcion que ha sido recogida |
|
79 |
*/ |
|
80 |
public void debug(String msg, Object parent, Exception exception) { |
|
81 |
if(parent != null) |
|
82 |
LoggerFactory |
|
83 |
.getLogger(parent.getClass()).debug(Messages.getText(msg), exception); |
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* Shows a error dialog with a text and a accept button |
|
88 |
* @param msg Message to show in the dialog |
|
89 |
* @param parentWindow Parent window |
|
90 |
*/ |
|
91 |
public void messageBoxError(String msg, Object parentWindow){ |
|
92 |
String string = Messages.getText("accept"); |
|
93 |
Object[] options = {string}; |
|
94 |
JOptionPane.showOptionDialog((Component)parentWindow, |
|
95 |
"<html>" + Messages.getText(msg).replaceAll("\n", "<br>") + "</html>", |
|
96 |
Messages.getText("confirmacion"), |
|
97 |
JOptionPane.OK_OPTION, |
|
98 |
JOptionPane.ERROR_MESSAGE, |
|
99 |
null, |
|
100 |
options, |
|
101 |
string); |
|
102 |
} |
|
103 |
|
|
104 |
/** |
|
105 |
* Muestra un dialogo de error con un texto y un bot?n de aceptar. El error es |
|
106 |
* registrado en el log de gvSIG con la excepcion que se le pase por parametro |
|
107 |
* @param msg Mensaje a mostrar en el dialogo. |
|
108 |
* @param parentWindow Ventana desde la que se lanza el dialogo |
|
109 |
* @param exception Excepcion que ha sido recogida |
|
110 |
*/ |
|
111 |
public void messageBoxError(String msg, Object parentWindow, Exception exception) { |
|
112 |
debug(msg, parentWindow, exception); |
|
113 |
messageBoxError(msg, parentWindow); |
|
114 |
} |
|
115 |
|
|
116 |
/** |
|
117 |
* Muestra un dialogo de error con un texto y un bot?n de aceptar. Se le pasa como ?ltimo par?metros |
|
118 |
* una lista de excepciones que ser?n guardadas en el log |
|
119 |
* @param msg Mensaje a mostrar en el dialogo. |
|
120 |
* @param parentWindow Ventana desde la que se lanza el dialogo |
|
121 |
* @param exception Excepcion que ha sido recogida |
|
122 |
*/ |
|
123 |
public void messageBoxError(String msg, Object parentWindow, ArrayList<Exception> exception) { |
|
124 |
for (int i = 0; i < exception.size(); i++) |
|
125 |
debug(msg, parentWindow, exception.get(i)); |
|
126 |
messageBoxError(msg, parentWindow); |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* Exports a raster buffer to disk |
|
131 |
* @param sFilename |
|
132 |
* @param buf |
|
133 |
* @param cellsize |
|
134 |
* @param minX |
|
135 |
* @param minY |
|
136 |
* @return |
|
137 |
* @throws ProviderNotRegisteredException |
|
138 |
* @throws InitializeException |
|
139 |
*/ |
|
140 |
public void exportRaster(final String sFilename, |
|
141 |
Buffer buf, |
|
142 |
ColorInterpretation ci, |
|
143 |
Extent windowExtent, |
|
144 |
NoData nodata, |
|
145 |
IProjection proj) { |
|
146 |
nodata.setFileName(sFilename); |
|
147 |
nodata.save(); |
|
148 |
|
|
149 |
buf.setDataExtent(windowExtent.toRectangle2D()); |
|
150 |
|
|
151 |
DataManager manager = DALLocator.getDataManager(); |
|
152 |
String provider = "Gdal Store"; |
Also available in: Unified diff