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