gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / raster / process / FilterProcess.java @ 1419
History | View | Annotate | Download (13 KB)
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.tools.app.basic.raster.process; |
23 |
|
24 |
import java.io.IOException; |
25 |
import java.util.ArrayList; |
26 |
|
27 |
import javax.swing.SwingUtilities; |
28 |
|
29 |
import org.gvsig.andami.PluginServices; |
30 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
31 |
import org.gvsig.fmap.dal.coverage.RasterManager; |
32 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
33 |
import org.gvsig.fmap.dal.coverage.datastruct.NoData; |
34 |
import org.gvsig.fmap.dal.coverage.exception.FilterManagerException; |
35 |
import org.gvsig.fmap.dal.coverage.exception.FilterTypeException; |
36 |
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException; |
37 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
38 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
39 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
40 |
import org.gvsig.fmap.dal.coverage.grid.Grid; |
41 |
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList; |
42 |
import org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager; |
43 |
import org.gvsig.fmap.dal.coverage.store.DataServerWriter; |
44 |
import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
45 |
import org.gvsig.fmap.dal.coverage.store.RasterQuery; |
46 |
import org.gvsig.fmap.dal.coverage.store.RasterWriter; |
47 |
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation; |
48 |
import org.gvsig.fmap.dal.exception.CloseException; |
49 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
50 |
import org.gvsig.raster.tools.app.basic.RasterToolsUtil; |
51 |
import org.gvsig.raster.tools.app.basic.raster.bean.previewbase.ParamStruct; |
52 |
|
53 |
/**
|
54 |
* Clase donde se hara todo el proceso de aplicar una lista de filtros a una
|
55 |
* capa. Muestra una ventana de dialogo de incremento informativa.
|
56 |
*
|
57 |
* @version 24/05/2007
|
58 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
59 |
*/
|
60 |
public class FilterProcess extends RasterProcess { |
61 |
private String filename = ""; |
62 |
private RasterDataStore rasterDataSource = null; |
63 |
private ArrayList<ParamStruct> listFilterUsed = null; |
64 |
|
65 |
private RasterFilterList rasterFilterList = null; |
66 |
private RasterWriter geoRasterWriter = null; |
67 |
private FLyrRaster lyr = null; |
68 |
private RasterManager rManager = RasterLocator.getManager();
|
69 |
|
70 |
/*
|
71 |
* (non-Javadoc)
|
72 |
* @see org.gvsig.rastertools.RasterProcess#init()
|
73 |
*/
|
74 |
@SuppressWarnings("unchecked") |
75 |
public void init() { |
76 |
filename = getStringParam("filename");
|
77 |
listFilterUsed = (ArrayList) getParam("listfilterused"); |
78 |
lyr = (FLyrRaster)getParam("layer");
|
79 |
rasterDataSource = lyr.getDataStore(); |
80 |
} |
81 |
|
82 |
/**
|
83 |
* Devuelve una interpretacion de color. Es usado cuando no conseguimos un
|
84 |
* rendering y no podemos saber que interpretacion l?gica ten?a antes.
|
85 |
* @param geoRasterWriter
|
86 |
*/
|
87 |
private ColorInterpretation getColorIntepretation(Buffer buffer, Grid grid) { |
88 |
ColorInterpretation colorInterpretation = null;
|
89 |
|
90 |
do {
|
91 |
// Si tiene una tabla de color asignamos las tres bandas
|
92 |
if (grid.getFilterList().isActive("colortable")) { |
93 |
colorInterpretation = rManager.createColorInterpretation(new String[] { ColorInterpretation.RED_BAND, ColorInterpretation.GREEN_BAND, ColorInterpretation.BLUE_BAND }); |
94 |
break;
|
95 |
} |
96 |
|
97 |
// Si el numero de bandas coincide asignamos la misma interpretacion que tenia antes
|
98 |
if (buffer.getBandCount() == rasterDataSource.getBandCount()) {
|
99 |
colorInterpretation = rasterDataSource.getColorInterpretation(); |
100 |
break;
|
101 |
} |
102 |
|
103 |
String[] colorInterp = new String[rasterDataSource.getColorInterpretation().getValues().length]; |
104 |
|
105 |
for (int i = 0; i < rasterDataSource.getColorInterpretation().getValues().length; i++) { |
106 |
if (rasterDataSource.getColorInterpretation().getValues()[i].equals(ColorInterpretation.UNDEF_BAND)) {
|
107 |
colorInterp[i] = ColorInterpretation.GRAY_BAND; |
108 |
continue;
|
109 |
} |
110 |
colorInterp[i] = rasterDataSource.getColorInterpretation().getValues()[i]; |
111 |
} |
112 |
colorInterpretation = rManager.createColorInterpretation(colorInterp); |
113 |
} while (false); |
114 |
|
115 |
return colorInterpretation;
|
116 |
} |
117 |
|
118 |
/*
|
119 |
* (non-Javadoc)
|
120 |
* @see org.gvsig.rastertools.RasterProcess#process()
|
121 |
*/
|
122 |
public void process() throws ProcessInterruptedException, ProcessException { |
123 |
DataServerWriter writerBufferServer = null;
|
124 |
if(lyr != null) |
125 |
lyr.setReadingData(Thread.currentThread().getId() + ""); |
126 |
|
127 |
RasterDataStore dstoreCopy = null;
|
128 |
RasterQuery query = null;
|
129 |
Buffer buffer = null; |
130 |
Buffer buff = null; |
131 |
Grid grid = null;
|
132 |
Buffer alpha = null; |
133 |
try {
|
134 |
insertLineLog(RasterToolsUtil.getText(this, "leyendo_raster")); |
135 |
|
136 |
//Creaci?n del BufferFactory
|
137 |
dstoreCopy = rasterDataSource.newDataStore(); |
138 |
query = rManager.createQuery(); |
139 |
//if (!RasterBuffer.loadInMemory(dsetCopy))
|
140 |
query.setReadOnly(true);
|
141 |
|
142 |
//Asignaci?n de bandas
|
143 |
int[] renderBands = lyr.getRender().getRenderBands(); |
144 |
if (renderBands != null) { |
145 |
// Si es gris, se reduce a una sola banda
|
146 |
if ((renderBands.length == 3) && (renderBands[0] == renderBands[1]) && (renderBands[1] == renderBands[2])) |
147 |
renderBands = new int[] { renderBands[0] }; |
148 |
query.setDrawableBands(renderBands); |
149 |
} else
|
150 |
query.setAllDrawableBands(); |
151 |
|
152 |
query.setAreaOfInterest(); |
153 |
|
154 |
buff = dstoreCopy.query(query); |
155 |
if(buff.isReadOnlyBuffer())
|
156 |
buff.addDrawableBands(renderBands); |
157 |
|
158 |
grid = rManager.createGrid(buff, dstoreCopy, true);
|
159 |
|
160 |
//Obtenemos la lista de filtros
|
161 |
rasterFilterList = grid.getFilterList(); |
162 |
|
163 |
//Aplicamos los filtros usados en el panel
|
164 |
addSelectedFilters(rasterFilterList, listFilterUsed); |
165 |
|
166 |
insertLineLog(RasterToolsUtil.getText(this, "aplicando_filtros")); |
167 |
|
168 |
grid.setNoDataValue(rasterDataSource.getNoDataValue()); |
169 |
grid.applyFilters(); |
170 |
|
171 |
insertLineLog(RasterToolsUtil.getText(this, "guardando_capa")); |
172 |
|
173 |
buffer = grid.getRasterBuf(); |
174 |
|
175 |
writerBufferServer = rManager.createDataServerWriter(); |
176 |
writerBufferServer.setBuffer(buffer, -1);
|
177 |
// TODO: FUNCIONALIDAD: Poner los getWriter con la proyecci?n del fichero fuente
|
178 |
|
179 |
//Calculo de la interpretaci?n de color
|
180 |
ColorInterpretation colorInterpretation = null;
|
181 |
if(lyr.existColorTable() || buffer.getBandCount() > 1) |
182 |
colorInterpretation = rManager.createColorInterpretation(new String[] { ColorInterpretation.RED_BAND, ColorInterpretation.GREEN_BAND, ColorInterpretation.BLUE_BAND }); |
183 |
else if(buffer.getBandCount() == 2) { |
184 |
renderBands = lyr.getRender().getRenderBands(); |
185 |
String[] ci = new String[renderBands.length]; |
186 |
for (int i = 0; i < renderBands.length; i++) { |
187 |
switch (renderBands[i]) {
|
188 |
case 0:ci[i] = ColorInterpretation.RED_BAND; break; |
189 |
case 1:ci[i] = ColorInterpretation.GREEN_BAND; break; |
190 |
case 2:ci[i] = ColorInterpretation.BLUE_BAND; break; |
191 |
default: ci[i] = ColorInterpretation.UNDEF_BAND;
|
192 |
} |
193 |
} |
194 |
colorInterpretation = rManager.createColorInterpretation(ci); |
195 |
} else
|
196 |
colorInterpretation = rManager.createColorInterpretation(new String[] { ColorInterpretation.GRAY_BAND }); |
197 |
|
198 |
//Si la imagen original ten?a una banda de transparencia se asignar? esta. Si los filtros generados
|
199 |
//crean una banda se mezclar? con la original.
|
200 |
int nbands = buffer.getBandCount();
|
201 |
if( rasterDataSource.getTransparency() != null && |
202 |
rasterDataSource.getTransparency().getAlphaBandNumber() >= 0) {
|
203 |
query.setDrawableBands(new int[]{rasterDataSource.getTransparency().getAlphaBandNumber()}); |
204 |
query.setAreaOfInterest(); |
205 |
alpha = dstoreCopy.query(query); |
206 |
if(grid.getFilterList().getAlphaBand() != null) |
207 |
alpha = rManager.getColorConversion().mergeTransparencyBuffers(alpha, grid.getFilterList().getAlphaBand()); |
208 |
writerBufferServer.setAlphaBuffer(alpha); |
209 |
//Asignamos la interpretaci?n de color de la banda alpha
|
210 |
ColorInterpretation alphaI = rManager.createColorInterpretation(new String[] { ColorInterpretation.ALPHA_BAND }); |
211 |
colorInterpretation.addColorInterpretation(alphaI); |
212 |
nbands ++; |
213 |
} else if(grid.getFilterList().getAlphaBand() != null) { |
214 |
writerBufferServer.setAlphaBuffer(grid.getFilterList().getAlphaBand()); |
215 |
//Asignamos la interpretaci?n de color de la banda alpha
|
216 |
ColorInterpretation alphaI = rManager.createColorInterpretation(new String[] { ColorInterpretation.ALPHA_BAND }); |
217 |
colorInterpretation.addColorInterpretation(alphaI); |
218 |
nbands ++; |
219 |
} |
220 |
geoRasterWriter = rManager.createWriter(writerBufferServer, |
221 |
filename, |
222 |
nbands, |
223 |
rasterDataSource.getAffineTransform(), |
224 |
buffer.getWidth(), |
225 |
buffer.getHeight(), |
226 |
buffer.getDataType(), |
227 |
rManager.createWriter(filename).getParams(), |
228 |
null);
|
229 |
|
230 |
if (lyr.getRender() == null) |
231 |
colorInterpretation = getColorIntepretation(buffer, grid); |
232 |
|
233 |
geoRasterWriter.setColorBandsInterpretation(colorInterpretation.getValues()); |
234 |
|
235 |
geoRasterWriter.dataWrite(); |
236 |
geoRasterWriter.writeClose(); |
237 |
geoRasterWriter = null;
|
238 |
|
239 |
// Guardamos en el RMF el valor NoData
|
240 |
//Si el del buffer tiene valor es porque alg?n filtro le ha salvado valor nodata por lo que se pone ese
|
241 |
//sino se pone el de la imagen original
|
242 |
NoData nodataOrigin = (NoData)rasterDataSource.getNoDataValue(); |
243 |
NoData nodataBuffer = (NoData)buffer.getNoDataValue(); |
244 |
if(nodataBuffer != null && nodataBuffer.isDefined()) { |
245 |
nodataBuffer.setFileName(filename); |
246 |
nodataBuffer.save(); |
247 |
} else if(nodataOrigin != null && nodataOrigin.isDefined()) { |
248 |
nodataOrigin.setFileName(filename); |
249 |
nodataOrigin.save(); |
250 |
} |
251 |
|
252 |
SwingUtilities.invokeLater(new Runnable() { |
253 |
public void run() { |
254 |
if (externalActions != null) |
255 |
externalActions.end(filename); |
256 |
} |
257 |
}); |
258 |
} catch (NotSupportedExtensionException e) {
|
259 |
throw new ProcessException("error_writer_notsupportedextension", e); |
260 |
} catch (RasterDriverException e) {
|
261 |
throw new ProcessException("error_writer", e); |
262 |
} catch (IOException e) { |
263 |
throw new ProcessException("error_writer", e); |
264 |
} catch (FilterTypeException e) {
|
265 |
throw new ProcessException("error_adding_filters", e); |
266 |
} catch (InvalidSetViewException e) {
|
267 |
throw new ProcessException("error_loading_data", e); |
268 |
} catch (FilterManagerException e) {
|
269 |
throw new ProcessException("error_adding_filters", e); |
270 |
} finally {
|
271 |
if(writerBufferServer != null) |
272 |
writerBufferServer.dispose(); |
273 |
if(buff != null) |
274 |
buff.dispose(); |
275 |
if(rasterFilterList != null) |
276 |
rasterFilterList.dispose(); |
277 |
if (grid != null) |
278 |
grid.dispose(); |
279 |
if(alpha != null) |
280 |
alpha.dispose(); |
281 |
if(dstoreCopy != null) |
282 |
try {
|
283 |
dstoreCopy.close(); |
284 |
} catch (CloseException e) {
|
285 |
} |
286 |
if(lyr != null) |
287 |
lyr.setReadingData(null);
|
288 |
dispose(); |
289 |
} |
290 |
} |
291 |
|
292 |
/**
|
293 |
* Sustituye la lista de filtros de filterList por la que le pasamos en forma
|
294 |
* de ArrayList
|
295 |
* @param filterList
|
296 |
* @param listFilterUsed
|
297 |
* @throws FilterTypeException
|
298 |
* @throws FilterTypeException
|
299 |
*/
|
300 |
public void addSelectedFilters(RasterFilterList filterList, ArrayList<ParamStruct> listFilterUsed) throws FilterManagerException, FilterTypeException { |
301 |
filterList.clear(); |
302 |
|
303 |
for (int i = 0; i < listFilterUsed.size(); i++) { |
304 |
ParamStruct aux = (ParamStruct) listFilterUsed.get(i); |
305 |
RasterFilterListManager filterManager = filterList.getManagerByFilterClass(aux.getFilterClass()); |
306 |
filterManager.addFilter(aux.getFilterClass(), aux.getFilterParam()); |
307 |
} |
308 |
|
309 |
filterList.resetPercent(); |
310 |
} |
311 |
|
312 |
/*
|
313 |
* (non-Javadoc)
|
314 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
|
315 |
*/
|
316 |
public int getPercent() { |
317 |
if (rasterFilterList == null) |
318 |
return 0; |
319 |
|
320 |
if (rasterFilterList.getPercent() < 100) |
321 |
return rasterFilterList.getPercent();
|
322 |
|
323 |
if (geoRasterWriter == null) |
324 |
return 0; |
325 |
|
326 |
return geoRasterWriter.getPercent();
|
327 |
} |
328 |
|
329 |
/*
|
330 |
* (non-Javadoc)
|
331 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
332 |
*/
|
333 |
public String getTitle() { |
334 |
return PluginServices.getText(this, "aplicando_filtros"); |
335 |
} |
336 |
|
337 |
/*
|
338 |
* (non-Javadoc)
|
339 |
* @see org.gvsig.raster.tools.app.basic.raster.process.RasterProcess#dispose()
|
340 |
*/
|
341 |
public void dispose() { |
342 |
rasterDataSource = null;
|
343 |
if(listFilterUsed != null) { |
344 |
listFilterUsed.clear(); |
345 |
listFilterUsed = null;
|
346 |
} |
347 |
rasterFilterList = null;
|
348 |
geoRasterWriter = null;
|
349 |
lyr = null;
|
350 |
} |
351 |
|
352 |
/*
|
353 |
* (non-Javadoc)
|
354 |
* @see java.lang.Object#finalize()
|
355 |
*/
|
356 |
@Override
|
357 |
protected void finalize() throws Throwable { |
358 |
dispose(); |
359 |
super.finalize();
|
360 |
} |
361 |
} |