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