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