svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extRasterTools-SE / src / org / gvsig / raster / util / process / FilterProcess.java @ 30008
History | View | Annotate | Download (12 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.raster.util.process; |
20 |
|
21 |
import java.io.IOException; |
22 |
import java.util.ArrayList; |
23 |
|
24 |
import javax.swing.SwingUtilities; |
25 |
|
26 |
import org.gvsig.andami.PluginServices; |
27 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
28 |
import org.gvsig.raster.Configuration; |
29 |
import org.gvsig.raster.RasterProcess; |
30 |
import org.gvsig.raster.beans.previewbase.ParamStruct; |
31 |
import org.gvsig.raster.buffer.BufferFactory; |
32 |
import org.gvsig.raster.buffer.WriterBufferServer; |
33 |
import org.gvsig.raster.buffer.cache.RasterReadOnlyBuffer; |
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.NotSupportedExtensionException; |
38 |
import org.gvsig.raster.dataset.RasterDataset; |
39 |
import org.gvsig.raster.dataset.RasterDriverException; |
40 |
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation; |
41 |
import org.gvsig.raster.dataset.serializer.RmfSerializerException; |
42 |
import org.gvsig.raster.datastruct.NoData; |
43 |
import org.gvsig.raster.datastruct.Transparency; |
44 |
import org.gvsig.raster.grid.Grid; |
45 |
import org.gvsig.raster.grid.filter.FilterTypeException; |
46 |
import org.gvsig.raster.grid.filter.IRasterFilterListManager; |
47 |
import org.gvsig.raster.grid.filter.RasterFilterList; |
48 |
import org.gvsig.raster.grid.filter.RasterFilterListManager; |
49 |
import org.gvsig.raster.hierarchy.IRasterRendering; |
50 |
import org.gvsig.raster.util.RasterToolsUtil; |
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 IRasterDataSource rasterDataSource = null; |
62 |
private ArrayList listFilterUsed = null; |
63 |
|
64 |
private RasterFilterList rasterFilterList = null; |
65 |
private GeoRasterWriter geoRasterWriter = null; |
66 |
private IRasterRendering rendering = null; |
67 |
private FLyrRasterSE lyr = null; |
68 |
|
69 |
/*
|
70 |
* (non-Javadoc)
|
71 |
* @see org.gvsig.rastertools.RasterProcess#init()
|
72 |
*/
|
73 |
public void init() { |
74 |
rendering = (IRasterRendering) getParam("rendering");
|
75 |
filename = getStringParam("filename");
|
76 |
rasterDataSource = (IRasterDataSource) getParam("rasterdatasource");
|
77 |
listFilterUsed = (ArrayList) getParam("listfilterused"); |
78 |
lyr = (FLyrRasterSE)getParam("layer");
|
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 DatasetColorInterpretation getColorIntepretation(IBuffer buffer, Grid grid) {
|
87 |
DatasetColorInterpretation colorInterpretation = null;
|
88 |
|
89 |
do {
|
90 |
// Si tiene una tabla de color asignamos las tres bandas
|
91 |
if (grid.getFilterList().isActive("colortable")) { |
92 |
colorInterpretation = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.RED_BAND, DatasetColorInterpretation.GREEN_BAND, DatasetColorInterpretation.BLUE_BAND }); |
93 |
break;
|
94 |
} |
95 |
|
96 |
// Si el numero de bandas coincide asignamos la misma interpretacion que tenia antes
|
97 |
if ((rendering != null) && (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(DatasetColorInterpretation.UNDEF_BAND)) {
|
106 |
colorInterp[i] = DatasetColorInterpretation.GRAY_BAND; |
107 |
continue;
|
108 |
} |
109 |
colorInterp[i] = rasterDataSource.getColorInterpretation().getValues()[i]; |
110 |
} |
111 |
colorInterpretation = new DatasetColorInterpretation(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 InterruptedException { |
122 |
WriterBufferServer writerBufferServer = null;
|
123 |
if(lyr != null) |
124 |
lyr.setReadingData(Thread.currentThread().toString());
|
125 |
|
126 |
IRasterDataSource dsetCopy = null;
|
127 |
BufferFactory bufferFactory = null;
|
128 |
IBuffer buffer = null;
|
129 |
|
130 |
try {
|
131 |
insertLineLog(RasterToolsUtil.getText(this, "leyendo_raster")); |
132 |
|
133 |
//Creaci?n del BufferFactory
|
134 |
dsetCopy = rasterDataSource.newDataset(); |
135 |
bufferFactory = new BufferFactory(dsetCopy);
|
136 |
//if (!RasterBuffer.loadInMemory(dsetCopy))
|
137 |
bufferFactory.setReadOnly(true);
|
138 |
|
139 |
//Asignaci?n de bandas
|
140 |
int[] renderBands = rendering.getRenderBands(); |
141 |
if (rendering != 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 |
bufferFactory.setDrawableBands(renderBands); |
146 |
} else
|
147 |
bufferFactory.setAllDrawableBands(); |
148 |
|
149 |
bufferFactory.setAreaOfInterest(); |
150 |
|
151 |
IBuffer buff = bufferFactory.getRasterBuf(); |
152 |
if(buff instanceof RasterReadOnlyBuffer) |
153 |
((RasterReadOnlyBuffer) buff).addDrawableBands(renderBands); |
154 |
|
155 |
Grid grid = new Grid(bufferFactory, 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 = new WriterBufferServer();
|
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 |
DatasetColorInterpretation colorInterpretation = null;
|
178 |
if((rendering != null && rendering.existColorTable()) || buffer.getBandCount() > 1) |
179 |
colorInterpretation = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.RED_BAND, DatasetColorInterpretation.GREEN_BAND, DatasetColorInterpretation.BLUE_BAND }); |
180 |
else if(rendering != null && buffer.getBandCount() == 2) { |
181 |
renderBands = rendering.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] = DatasetColorInterpretation.RED_BAND; break; |
186 |
case 1:ci[i] = DatasetColorInterpretation.GREEN_BAND; break; |
187 |
case 2:ci[i] = DatasetColorInterpretation.BLUE_BAND; break; |
188 |
default: ci[i] = DatasetColorInterpretation.UNDEF_BAND;
|
189 |
} |
190 |
} |
191 |
colorInterpretation = new DatasetColorInterpretation(ci);
|
192 |
} else
|
193 |
colorInterpretation = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.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.getTransparencyFilesStatus() != null && |
199 |
rasterDataSource.getTransparencyFilesStatus().getAlphaBandNumber() >= 0) {
|
200 |
bufferFactory.setDrawableBands(new int[]{rasterDataSource.getTransparencyFilesStatus().getAlphaBandNumber()}); |
201 |
bufferFactory.setAreaOfInterest(); |
202 |
IBuffer alpha = bufferFactory.getRasterBuf(); |
203 |
if(grid.getFilterList().getAlphaBand() != null) |
204 |
alpha = Transparency.merge(alpha, grid.getFilterList().getAlphaBand());
|
205 |
writerBufferServer.setAlphaBuffer(alpha); |
206 |
//Asignamos la interpretaci?n de color de la banda alpha
|
207 |
DatasetColorInterpretation alphaI = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.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 |
DatasetColorInterpretation alphaI = new DatasetColorInterpretation(new String[] { DatasetColorInterpretation.ALPHA_BAND }); |
214 |
colorInterpretation.addColorInterpretation(alphaI); |
215 |
nbands ++; |
216 |
} |
217 |
|
218 |
geoRasterWriter = GeoRasterWriter.getWriter(writerBufferServer, filename, nbands, rasterDataSource.getAffineTransform(0), buffer.getWidth(), buffer.getHeight(), buffer.getDataType(), GeoRasterWriter.getWriter(filename).getParams(), null); |
219 |
|
220 |
if (rendering == null) |
221 |
colorInterpretation = getColorIntepretation(buffer, grid); |
222 |
|
223 |
geoRasterWriter.setColorBandsInterpretation(colorInterpretation.getValues()); |
224 |
|
225 |
geoRasterWriter.dataWrite(); |
226 |
geoRasterWriter.writeClose(); |
227 |
geoRasterWriter = null;
|
228 |
|
229 |
// Guardamos en el RMF del fichero el valor NoData
|
230 |
if (Configuration.getValue("nodata_transparency_enabled", Boolean.FALSE).booleanValue()) { |
231 |
try {
|
232 |
RasterDataset.saveObjectToRmfFile(filename, NoData.class, new NoData(rasterDataSource.getNoDataValue(), rasterDataSource.isNoDataEnabled()?2:0, rasterDataSource.getDataType()[0])); |
233 |
} catch (RmfSerializerException e) {
|
234 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this,"error_salvando_rmf"), this, e); |
235 |
} |
236 |
} |
237 |
|
238 |
SwingUtilities.invokeLater(new Runnable() { |
239 |
public void run() { |
240 |
if (externalActions != null) |
241 |
externalActions.end(filename); |
242 |
} |
243 |
}); |
244 |
} catch (NotSupportedExtensionException e) {
|
245 |
RasterToolsUtil.messageBoxError("error_writer_notsupportedextension", this, e); |
246 |
} catch (RasterDriverException e) {
|
247 |
RasterToolsUtil.messageBoxError("error_writer", this, e); |
248 |
} catch (IOException e) { |
249 |
RasterToolsUtil.messageBoxError("error_writer", this, e); |
250 |
} catch (FilterTypeException e) {
|
251 |
RasterToolsUtil.messageBoxError("error_adding_filters", this, e); |
252 |
} finally {
|
253 |
rasterDataSource = null;
|
254 |
if(bufferFactory != null) |
255 |
bufferFactory.free(); |
256 |
if(buffer != null) |
257 |
buffer.free(); |
258 |
if(lyr != null) |
259 |
lyr.setReadingData(null);
|
260 |
} |
261 |
} |
262 |
|
263 |
/**
|
264 |
* Sustituye la lista de filtros de filterList por la que le pasamos en forma
|
265 |
* de ArrayList
|
266 |
* @param filterList
|
267 |
* @param listFilterUsed
|
268 |
* @throws FilterTypeException
|
269 |
*/
|
270 |
public static void addSelectedFilters(RasterFilterList filterList, ArrayList listFilterUsed) throws FilterTypeException { |
271 |
filterList.clear(); |
272 |
RasterFilterListManager stackManager = new RasterFilterListManager(filterList);
|
273 |
|
274 |
for (int i = 0; i < listFilterUsed.size(); i++) { |
275 |
ParamStruct aux = (ParamStruct) listFilterUsed.get(i); |
276 |
IRasterFilterListManager filterManager = stackManager.getManagerByFilterClass(aux.getFilterClass()); |
277 |
filterManager.addFilter(aux.getFilterClass(), aux.getFilterParam()); |
278 |
} |
279 |
|
280 |
filterList.resetPercent(); |
281 |
} |
282 |
|
283 |
/*
|
284 |
* (non-Javadoc)
|
285 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
|
286 |
*/
|
287 |
public int getPercent() { |
288 |
if (rasterFilterList == null) |
289 |
return 0; |
290 |
|
291 |
if (rasterFilterList.getPercent() < 100) |
292 |
return rasterFilterList.getPercent();
|
293 |
|
294 |
if (geoRasterWriter == null) |
295 |
return 0; |
296 |
|
297 |
return geoRasterWriter.getPercent();
|
298 |
} |
299 |
|
300 |
/*
|
301 |
* (non-Javadoc)
|
302 |
* @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
303 |
*/
|
304 |
public String getTitle() { |
305 |
return PluginServices.getText(this, "aplicando_filtros"); |
306 |
} |
307 |
} |