svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / clipping / ClippingProcess.java @ 16167
History | View | Annotate | Download (13.7 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. 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 |
package org.gvsig.rastertools.clipping; |
20 |
|
21 |
import java.awt.geom.AffineTransform; |
22 |
import java.io.File; |
23 |
import java.io.FileNotFoundException; |
24 |
import java.io.IOException; |
25 |
|
26 |
import org.apache.log4j.Logger; |
27 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
28 |
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent; |
29 |
import org.gvsig.gui.beans.incrementabletask.IncrementableListener; |
30 |
import org.gvsig.gui.beans.incrementabletask.IncrementableTask; |
31 |
import org.gvsig.raster.buffer.BufferFactory; |
32 |
import org.gvsig.raster.buffer.BufferInterpolation; |
33 |
import org.gvsig.raster.buffer.RasterBuffer; |
34 |
import org.gvsig.raster.dataset.GeoRasterWriter; |
35 |
import org.gvsig.raster.dataset.IBuffer; |
36 |
import org.gvsig.raster.dataset.IRasterDataSource; |
37 |
import org.gvsig.raster.dataset.InvalidSetViewException; |
38 |
import org.gvsig.raster.dataset.NotSupportedExtensionException; |
39 |
import org.gvsig.raster.dataset.Params; |
40 |
import org.gvsig.raster.dataset.RasterDataset; |
41 |
import org.gvsig.raster.dataset.RasterDriverException; |
42 |
import org.gvsig.raster.dataset.io.rmf.RmfBlocksManager; |
43 |
import org.gvsig.raster.datastruct.ColorTable; |
44 |
import org.gvsig.raster.datastruct.serializer.ColorTableRmfSerializer; |
45 |
import org.gvsig.raster.grid.GridPalette; |
46 |
import org.gvsig.raster.grid.filter.RasterFilterList; |
47 |
import org.gvsig.raster.grid.filter.bands.ColorTableFilter; |
48 |
import org.gvsig.raster.process.CancelEvent; |
49 |
import org.gvsig.raster.process.RasterTask; |
50 |
import org.gvsig.raster.process.RasterTaskQueue; |
51 |
import org.gvsig.raster.util.RasterNotLoadException; |
52 |
import org.gvsig.raster.util.RasterToolsUtil; |
53 |
import org.gvsig.raster.util.RasterUtilities; |
54 |
import org.gvsig.rastertools.IProcessActions; |
55 |
import org.gvsig.rastertools.RasterModule; |
56 |
/**
|
57 |
* <code>ClippingProcess</code> es un proceso que usa un <code>Thread</code>
|
58 |
* para calcular el recorte de una capa.
|
59 |
*
|
60 |
* @version 24/04/2007
|
61 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
62 |
*/
|
63 |
public class ClippingProcess implements IncrementableListener, Runnable { |
64 |
private String fileName = ""; |
65 |
private IncrementableTask incrementableTask = null; |
66 |
private volatile Thread blinker = null; |
67 |
private WriterBufferServer writerBufferServer = null; |
68 |
private FLyrRasterSE rasterSE = null; |
69 |
private AffineTransform affineTransform = new AffineTransform(); |
70 |
private boolean oneLayerPerBand = false; |
71 |
private int[] drawableBands = { 0, 1, 2 }; |
72 |
private int[] dValues = null; |
73 |
private GeoRasterWriter grw = null; |
74 |
private int interpolationMethod = BufferInterpolation.INTERPOLATION_Undefined; |
75 |
private boolean loadedInToc = true; |
76 |
private String viewName = ""; |
77 |
private Params params = null; |
78 |
private RasterTask rasterTask = new RasterTask(this); |
79 |
private IProcessActions externalActions = null; |
80 |
|
81 |
/**
|
82 |
* Variables de la resoluci?n de salida
|
83 |
*/
|
84 |
private int resolutionWidth = 0; |
85 |
private int resolutionHeight = 0; |
86 |
|
87 |
/**
|
88 |
* Crea un <code>ClippingProcess</code> para generar un recorte
|
89 |
* @param dValues
|
90 |
* @param fileName
|
91 |
* @param writerBufferServer
|
92 |
* @param rasterMultiDataset
|
93 |
* @param extent
|
94 |
* @param drawableBands
|
95 |
* @param oneLayerPerBand
|
96 |
*/
|
97 |
public ClippingProcess(String viewName, int[] dValues, String fileName, |
98 |
WriterBufferServer writerBufferServer, FLyrRasterSE rasterSE) { |
99 |
this.fileName = fileName;
|
100 |
this.writerBufferServer = writerBufferServer;
|
101 |
this.rasterSE = rasterSE;
|
102 |
this.dValues = dValues;
|
103 |
this.viewName = viewName;
|
104 |
} |
105 |
|
106 |
/**
|
107 |
* Arranca el proceso de recorte de un layer
|
108 |
*/
|
109 |
public void start() { |
110 |
blinker = new Thread(this); |
111 |
blinker.start(); |
112 |
} |
113 |
|
114 |
/**
|
115 |
* Establecer la resolucion de salida al crear el recorte
|
116 |
* @param width
|
117 |
* @param height
|
118 |
*/
|
119 |
public void setResolution(int width, int height) { |
120 |
resolutionWidth = width; |
121 |
resolutionHeight = height; |
122 |
} |
123 |
|
124 |
/**
|
125 |
* Salva la tabla de color al fichero rmf.
|
126 |
* @param fName
|
127 |
* @throws IOException
|
128 |
*/
|
129 |
private void saveToRmf(String fileName) { |
130 |
fileName = RasterUtilities.getNameWithoutExtension(fileName) + ".rmf";
|
131 |
|
132 |
RasterDataset rds = null;
|
133 |
int limitNumberOfRequests = 20; |
134 |
while(rds == null && limitNumberOfRequests > 0) { |
135 |
try {
|
136 |
rds = rasterSE.getDataSource().getDataset(0)[0]; |
137 |
} catch(IndexOutOfBoundsException e) { |
138 |
//En ocasiones, sobre todo con servicios remotos al pedir un datasource da una excepci?n de este tipo
|
139 |
//se supone que es porque hay un refresco en el mismo momento de la petici?n por lo que como es m?s lento de
|
140 |
//gestionar pilla a la capa sin datasources asociados ya que est? reasignandolo. Si volvemos a pedirlo debe
|
141 |
//haberlo cargado ya.
|
142 |
try {
|
143 |
Thread.sleep(200); |
144 |
} catch (InterruptedException e1) { |
145 |
} |
146 |
} |
147 |
limitNumberOfRequests --; |
148 |
} |
149 |
|
150 |
if(rds == null) { |
151 |
//RasterToolsUtil.messageBoxError("error_load_layer", this, new Exception("Error writing RMF. limitNumberOfRequests=" + limitNumberOfRequests));
|
152 |
return;
|
153 |
} |
154 |
|
155 |
RmfBlocksManager manager = rds.getRmfBlocksManager(); |
156 |
|
157 |
RasterFilterList rasterFilterList = rasterSE.getRenderFilterList(); |
158 |
|
159 |
ColorTableFilter colorTableFilter = (ColorTableFilter) rasterFilterList.getByName(ColorTableFilter.names[0]);
|
160 |
if (colorTableFilter != null) { |
161 |
GridPalette gridPalette = new GridPalette((ColorTable) colorTableFilter.getColorTable().clone());
|
162 |
|
163 |
ColorTableRmfSerializer ser = new ColorTableRmfSerializer(gridPalette);
|
164 |
manager.setPath(fileName); |
165 |
|
166 |
if (!manager.checkRmf())
|
167 |
return;
|
168 |
manager.addClient(ser); |
169 |
try {
|
170 |
RasterUtilities.copyFile(manager.getPath(), manager.getPath() + "~");
|
171 |
manager.write(); |
172 |
} catch (FileNotFoundException e) { |
173 |
e.printStackTrace(); |
174 |
} catch (IOException e) { |
175 |
e.printStackTrace(); |
176 |
} |
177 |
manager.removeClient(ser.getClass()); |
178 |
} |
179 |
} |
180 |
|
181 |
/**
|
182 |
* M?todo donde se ejecutar? el Thread, aqu? se genera el recorte
|
183 |
*/
|
184 |
public void run() { |
185 |
try {
|
186 |
RasterTaskQueue.register(rasterTask); |
187 |
|
188 |
long t2;
|
189 |
long t1 = new java.util.Date().getTime(); |
190 |
|
191 |
IRasterDataSource d = rasterSE.getDataSource().copy(); |
192 |
BufferFactory bufferFactory = new BufferFactory(d);
|
193 |
bufferFactory.setDrawableBands(getDrawableBands()); |
194 |
|
195 |
IBuffer buffer = null;
|
196 |
|
197 |
if( getInterpolationMethod() != BufferInterpolation.INTERPOLATION_Undefined &&
|
198 |
getInterpolationMethod() != BufferInterpolation.INTERPOLATION_NearestNeighbour) { |
199 |
try {
|
200 |
bufferFactory.setAreaOfInterest(dValues[0], dValues[3], dValues[2] - dValues[0], dValues[1] - dValues[3]); |
201 |
} catch (InvalidSetViewException e) {
|
202 |
Logger.getLogger(this.getClass().getName()).debug("No se ha podido asignar la vista al inicial el proceso de recorte.", e); |
203 |
} |
204 |
buffer = bufferFactory.getRasterBuf(); |
205 |
buffer = ((RasterBuffer)buffer).getAdjustedWindow(resolutionWidth, resolutionHeight, getInterpolationMethod()); |
206 |
} else {
|
207 |
try {
|
208 |
bufferFactory.setAreaOfInterest(dValues[0], dValues[3], Math.abs(dValues[2] - dValues[0]), Math.abs(dValues[1] - dValues[3]), resolutionWidth, resolutionHeight); |
209 |
buffer = bufferFactory.getRasterBuf(); |
210 |
} catch (InvalidSetViewException e) {
|
211 |
Logger.getLogger(this.getClass().getName()).debug("No se ha podido asignar la vista al inicial el proceso de recorte.", e); |
212 |
} |
213 |
} |
214 |
//TODO: FUNCIONALIDAD: Poner los getWriter con la proyecci?n del fichero fuente
|
215 |
String finalFileName = ""; |
216 |
if (isOneLayerPerBand()) {
|
217 |
for (int i = 0; i < drawableBands.length; i++) { |
218 |
finalFileName = fileName + "_B" + drawableBands[i] + ".tif"; |
219 |
writerBufferServer.setBuffer(buffer, i); |
220 |
Params p = null;
|
221 |
if (params == null) |
222 |
p = GeoRasterWriter.getWriter(finalFileName).getParams(); |
223 |
else
|
224 |
p = params; |
225 |
grw = GeoRasterWriter.getWriter(writerBufferServer, finalFileName, 1,
|
226 |
getAffineTransform(), buffer.getWidth(), buffer.getHeight(), |
227 |
buffer.getDataType(), p, null);
|
228 |
grw.setWkt(d.getWktProjection()); |
229 |
grw.dataWrite(); |
230 |
grw.writeClose(); |
231 |
saveToRmf(finalFileName); |
232 |
t2 = new java.util.Date().getTime();
|
233 |
cutFinalize(finalFileName, (t2 - t1)); |
234 |
t1 = new java.util.Date().getTime();
|
235 |
} |
236 |
} else {
|
237 |
writerBufferServer.setBuffer(buffer, -1);
|
238 |
if (params == null) { |
239 |
finalFileName = fileName + ".tif";
|
240 |
params = GeoRasterWriter.getWriter(finalFileName).getParams(); |
241 |
} else
|
242 |
finalFileName = fileName; |
243 |
grw = GeoRasterWriter.getWriter(writerBufferServer, finalFileName, |
244 |
buffer.getBandCount(), getAffineTransform(), buffer.getWidth(), |
245 |
buffer.getHeight(), buffer.getDataType(), params, null);
|
246 |
grw.setWkt(d.getWktProjection()); |
247 |
grw.dataWrite(); |
248 |
grw.writeClose(); |
249 |
saveToRmf(finalFileName); |
250 |
t2 = new java.util.Date().getTime();
|
251 |
if (incrementableTask != null) { |
252 |
incrementableTask.processFinalize(); |
253 |
incrementableTask = null;
|
254 |
} |
255 |
//Damos tiempo a parar el Thread del incrementable para que no se cuelgue la ventana
|
256 |
//El tiempo es como m?nimo el de un bucle de del run de la tarea incrementable
|
257 |
Thread.sleep(600); |
258 |
cutFinalize(finalFileName, (t2 - t1)); |
259 |
} |
260 |
|
261 |
d.close(); |
262 |
} catch (NotSupportedExtensionException e) {
|
263 |
Logger.getLogger(this.getClass().getName()).debug("No se ha podido obtener el writer. Extensi?n no soportada", e); |
264 |
} catch (RasterDriverException e) {
|
265 |
Logger.getLogger(this.getClass().getName()).debug("No se ha podido obtener el writer.", e); |
266 |
} catch (IOException e) { |
267 |
Logger.getLogger(this.getClass().getName()).debug("Error en la escritura en GeoRasterWriter.", e); |
268 |
} catch (InterruptedException e) { |
269 |
if (externalActions != null) |
270 |
externalActions.interrupted(); |
271 |
Thread.currentThread().interrupt();
|
272 |
} finally {
|
273 |
RasterTaskQueue.remove(rasterTask); |
274 |
if (incrementableTask != null) |
275 |
incrementableTask.processFinalize(); |
276 |
} |
277 |
} |
278 |
|
279 |
/**
|
280 |
* Establece el <code>IncrementableTask</code>
|
281 |
* @param value
|
282 |
*/
|
283 |
public void setIncrementableTask(IncrementableTask value) { |
284 |
incrementableTask = value; |
285 |
incrementableTask.addIncrementableListener(this);
|
286 |
} |
287 |
|
288 |
/*
|
289 |
* (non-Javadoc)
|
290 |
* @see org.gvsig.gui.beans.incrementabletask.IncrementableListener#actionCanceled(org.gvsig.gui.beans.incrementabletask.IncrementableEvent)
|
291 |
*/
|
292 |
public void actionCanceled(IncrementableEvent e) { |
293 |
rasterTask.setEvent(new CancelEvent(this)); |
294 |
} |
295 |
|
296 |
/**
|
297 |
* Acciones que se realizan al finalizar de crear los recortes de imagen.
|
298 |
* Este m?todo es llamado por el thread TailRasterProcess al finalizar.
|
299 |
*/
|
300 |
private void cutFinalize(String fileName, long milis) { |
301 |
/*if (!isLoadedInToc()) {
|
302 |
externalActions.end(new Object[]{fileName, new Long(milis)});
|
303 |
return;
|
304 |
}*/
|
305 |
|
306 |
if (!new File(fileName).exists()) |
307 |
return;
|
308 |
|
309 |
if(RasterToolsUtil.messageBoxYesOrNot("cargar_toc", this)) { |
310 |
try {
|
311 |
RasterToolsUtil.loadLayer(viewName, fileName, null);
|
312 |
} catch (RasterNotLoadException e) {
|
313 |
RasterToolsUtil.messageBoxError("error_load_layer", this, e); |
314 |
} |
315 |
} |
316 |
|
317 |
if (externalActions != null) { |
318 |
RasterModule.layerCount++; |
319 |
externalActions.end(new Object[]{fileName, new Long(milis)}); |
320 |
} |
321 |
} |
322 |
|
323 |
/**
|
324 |
* Asigna los par?metros si estos han de ser cambiados. En caso de
|
325 |
* que no se asignen se usaran los que tiene el driver por defecto.
|
326 |
* @param params Par?metros del driver de escritura.
|
327 |
*/
|
328 |
public void setParams(Params params) { |
329 |
this.params = params;
|
330 |
} |
331 |
|
332 |
public boolean isLoadedInToc() { |
333 |
return loadedInToc;
|
334 |
} |
335 |
|
336 |
public void setLoadedInToc(boolean loadedInToc) { |
337 |
this.loadedInToc = loadedInToc;
|
338 |
} |
339 |
|
340 |
public int[] getDrawableBands() { |
341 |
return drawableBands;
|
342 |
} |
343 |
|
344 |
public void setDrawableBands(int[] drawableBands) { |
345 |
this.drawableBands = drawableBands;
|
346 |
} |
347 |
|
348 |
public boolean isOneLayerPerBand() { |
349 |
return oneLayerPerBand;
|
350 |
} |
351 |
|
352 |
public void setOneLayerPerBand(boolean oneLayerPerBand) { |
353 |
this.oneLayerPerBand = oneLayerPerBand;
|
354 |
} |
355 |
|
356 |
public int getInterpolationMethod() { |
357 |
return interpolationMethod;
|
358 |
} |
359 |
|
360 |
public void setInterpolationMethod(int interpolationMethod) { |
361 |
this.interpolationMethod = interpolationMethod;
|
362 |
} |
363 |
|
364 |
public AffineTransform getAffineTransform() { |
365 |
return affineTransform;
|
366 |
} |
367 |
|
368 |
public void setAffineTransform(AffineTransform affineTransform) { |
369 |
this.affineTransform = affineTransform;
|
370 |
} |
371 |
|
372 |
/**
|
373 |
* Obtiene el objeto para ejecutar acciones externar.
|
374 |
* @param IProcessActions
|
375 |
*/
|
376 |
public IProcessActions getActions() {
|
377 |
return externalActions;
|
378 |
} |
379 |
|
380 |
/**
|
381 |
* Asigna el objeto para ejecutar acciones externar.
|
382 |
* @param IProcessActions
|
383 |
*/
|
384 |
public void setActions(IProcessActions actions) { |
385 |
this.externalActions = actions;
|
386 |
} |
387 |
|
388 |
public void actionResumed(IncrementableEvent e) {} |
389 |
public void actionSuspended(IncrementableEvent e) {} |
390 |
} |