Revision 17884

View differences:

trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/RasterFilterList.java
354 354
	 */
355 355
	private void executeFilterByDataType(int dataType) throws InterruptedException {
356 356
		environment.put("FirstUseTransparency", Boolean.TRUE);
357
		environment.put("HasNoDataFilter", Boolean.valueOf(isActive("nodata")));
358
		
359
		environment.put("FirstRaster", rasterBuf);
357 360
		for (int i = 0; i < list.size(); i++) {
358 361
			RasterFilter filter = (RasterFilter) list.get(i);
359 362

  
......
370 373
			if (filter.getResult("raster") != null)
371 374
				this.rasterBuf = (IBuffer) filter.getResult("raster");
372 375
		}
376
		environment.remove("FirstRaster");
373 377
	}
374 378

  
375 379
	/**
trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/RasterFilter.java
18 18
 */
19 19
package org.gvsig.raster.grid.filter;
20 20

  
21
import java.lang.reflect.Constructor;
22
import java.lang.reflect.InvocationTargetException;
21 23
import java.util.Hashtable;
22 24
import java.util.TreeMap;
23 25

  
......
25 27
import org.gvsig.raster.dataset.Params;
26 28
import org.gvsig.raster.datastruct.Extent;
27 29
import org.gvsig.raster.grid.GridTransparency;
30
import org.gvsig.raster.grid.filter.bands.NoDataFilter;
28 31
import org.gvsig.raster.process.RasterTask;
29 32
import org.gvsig.raster.process.RasterTaskQueue;
33
import org.gvsig.raster.util.RasterUtilities;
30 34
/**
31 35
 * Filtro para raster. Ancestro de todos los filtros.
32 36
 *
......
34 38
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
35 39
 */
36 40
public abstract class RasterFilter implements IRasterFilter, Cloneable {
37
	protected IBuffer	 	raster = null;
38
	protected int 			height = 0;
39
	protected int 			width = 0;
40
	protected Hashtable     params = new Hashtable();
41
	protected TreeMap       environment = new TreeMap();
42
	protected Extent		extent = null;
43
	private int 				percent = 0;
44
	private String			fName = "";
41
	protected IBuffer   raster       = null;
42
	protected IBuffer   rasterResult = null;
43
	protected int       height       = 0;
44
	protected int       width        = 0;
45
	protected Hashtable params       = new Hashtable();
46
	protected TreeMap   environment  = new TreeMap();
47
	protected Extent    extent       = null;
48
	private int         percent      = 0;
49
	private String      fName        = "";
45 50
	/**
46 51
	 * Variable que control la aplicaci?n o no del filtro. Si est? a false aunque est? en
47 52
	 * la pila el filtro no se ejecutar?.
......
135 140
	 */
136 141
	public RasterFilter() {
137 142
	}
143
	
144
	/**
145
	 * Instancia un filtro a partir de su nombre
146
	 * @param strPackage Paquete 
147
	 * @return Filtro instanciado
148
	 * @throws FilterTypeException
149
	 */
150
	static public RasterFilter createFilter(String strPackage) throws FilterTypeException {
151
		Class filterClass = null;
152
		try {
153
			filterClass = Class.forName(strPackage.trim());
154
		} catch (ClassNotFoundException e) {
155
			throw new FilterTypeException("No puedo instanciar " + strPackage.trim());
156
		}
157
		
158
		Constructor con = null;
159
		try {
160
			con = filterClass.getConstructor(null);
161
		} catch (SecurityException e) {
162
			throw new FilterTypeException("");
163
		} catch (NoSuchMethodException e) {
164
			throw new FilterTypeException("");
165
		}
166
		
167
		RasterFilter newFilter = null;
168
		try {
169
			newFilter = (RasterFilter) con.newInstance(null);
170
		} catch (IllegalArgumentException e) {
171
			throw new FilterTypeException("");
172
		} catch (InstantiationException e) {
173
			throw new FilterTypeException("");
174
		} catch (IllegalAccessException e) {
175
			throw new FilterTypeException("");
176
		} catch (InvocationTargetException e) {
177
			throw new FilterTypeException("");
178
		}
179
		return newFilter;
180
	}
138 181

  
139 182
	/**
140 183
	 * Aplica el filtro sobre el raster pasado pixel a pixel
......
143 186
	public void execute() throws InterruptedException {
144 187
		RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
145 188
		pre();
146
		if(raster != null && raster.getDataType() != this.getInRasterDataType())
189
		boolean doNoData = false;
190
		if (raster != null && raster.getDataType() != this.getInRasterDataType())
147 191
			exec = false;
148 192
		percent = 0;
149 193
		if (exec) {
150
			for (int row = 0; row < height; row ++) {
151
				for (int col = 0; col < width; col ++)
152
					process(col, row);
194
			Boolean hasNoData = (Boolean) environment.get("HasNoDataFilter");
195
			// Comprobamos si existe el filtro noData para conservar sus valores en el
196
			// Raster
197
			if ((hasNoData != null) && (hasNoData.booleanValue())) {
198
				try {
199
					IBuffer firstRaster = (IBuffer) getEnv().get("FirstRaster");
200
					String classFilter = NoDataFilter.class.toString();
201
					String packageFilter;
202
					packageFilter = classFilter.substring(classFilter.indexOf(" ") + 1, classFilter.lastIndexOf("Filter"));
203
					packageFilter += RasterUtilities.typesToString(firstRaster.getDataType());
204
					packageFilter += "Filter";
205
					NoDataFilter noDataFilterIn = (NoDataFilter) createFilter(packageFilter);
206
					noDataFilterIn.addParam("raster", firstRaster);
207
					noDataFilterIn.addParam("noDataValue", Double.valueOf(firstRaster.getNoDataValue()));
208
					noDataFilterIn.pre();
153 209

  
154
				if (task.getEvent() != null)
155
					task.manageEvent(task.getEvent());
210
					classFilter = NoDataFilter.class.toString();
211
					packageFilter = classFilter.substring(classFilter.indexOf(" ") + 1, classFilter.lastIndexOf("Filter"));
212
					packageFilter += RasterUtilities.typesToString(this.getOutRasterDataType());
213
					packageFilter += "Filter";
214
					NoDataFilter noDataFilterOut = (NoDataFilter) createFilter(packageFilter);
215
					noDataFilterOut.addParam("raster", firstRaster);
216
					noDataFilterOut.addParam("rasterResult", rasterResult);
217
					noDataFilterOut.addParam("noDataValue", Double.valueOf(firstRaster.getNoDataValue()));
218
					noDataFilterOut.pre();
156 219

  
157
				percent = (row * 100) / height;
220
					if ((firstRaster != null) && (rasterResult != null)) {
221
						for (int row = 0; row < height; row ++) {
222
							for (int col = 0; col < width; col ++) {
223
								if (noDataFilterIn.hasNoData(col, row)) {
224
									noDataFilterOut.writeNoData(col, row);
225
								} else {
226
									process(col, row);
227
								}
228
							}
229
			
230
							if (task.getEvent() != null)
231
								task.manageEvent(task.getEvent());
232
			
233
							percent = (row * 100) / height;
234
						}
235
						doNoData = true;
236
					}
237
				} catch (FilterTypeException e) {
238
					e.printStackTrace();
239
				}
158 240
			}
241
			// Si no se pudo hacer el proceso de valores NoData, hacemos el proceso
242
			// tradicional
243
			if (!doNoData) {
244
				for (int row = 0; row < height; row ++) {
245
					for (int col = 0; col < width; col ++)
246
						process(col, row);
247
	
248
					if (task.getEvent() != null)
249
						task.manageEvent(task.getEvent());
250
	
251
					percent = (row * 100) / height;
252
				}
253
			}
159 254
		}
160 255
		percent = 100;
161 256
		post();
trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/bands/NoDataByteFilter.java
26 26
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
27 27
 */
28 28
public class NoDataByteFilter extends NoDataFilter {
29
	
29 30
	/*
30 31
	 * (non-Javadoc)
31
	 * @see org.gvsig.raster.grid.filter.bands.ColorTableFilter#process(int, int)
32
	 * @see org.gvsig.raster.grid.filter.bands.NoDataFilter#hasNoData(int, int)
32 33
	 */
33
	public void process(int col, int line) {
34
		byte alpha = (byte) 255;
35
		for (int band = 0; band < raster.getBandCount(); band++) {
36
			double value = (double) raster.getElemByte(line, col, band);
37
			if (noDataValue == value) {
38
				alpha = 0;
39
				break;
40
			}
41
		}
42
		rasterAlpha.setElem(line, col, 0, alpha);
34
	public boolean hasNoData(int col, int line) {
35
		byte data[] = new byte[raster.getBandCount()];
36
		raster.getElemByte(line, col, data);
37
		for (int band = 0; band < data.length; band++)
38
			if (noDataValue == (double) data[band])
39
				return true;
40
		return false;
43 41
	}
44 42

  
45 43
	/*
46 44
	 * (non-Javadoc)
45
	 * @see org.gvsig.raster.grid.filter.bands.NoDataFilter#writeNoData(int, int)
46
	 */
47
	public void writeNoData(int col, int line) {
48
		for (int band = 0; band < rasterResult.getBandCount(); band++)
49
			rasterResult.setElem(line, col, band, (byte) noDataValue);
50
	}
51

  
52
	/*
53
	 * (non-Javadoc)
47 54
	 * @see org.gvsig.raster.grid.filter.bands.ColorTableFilter#getInRasterDataType()
48 55
	 */
49 56
	public int getInRasterDataType() {
trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/bands/NoDataDoubleFilter.java
26 26
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
27 27
 */
28 28
public class NoDataDoubleFilter extends NoDataFilter {
29

  
29 30
	/*
30 31
	 * (non-Javadoc)
31
	 * @see org.gvsig.raster.grid.filter.bands.ColorTableFilter#process(int, int)
32
	 * @see org.gvsig.raster.grid.filter.bands.NoDataFilter#hasNoData(int, int)
32 33
	 */
33
	public void process(int col, int line) {
34
		byte alpha = (byte) 255;
35
		for (int band = 0; band < raster.getBandCount(); band++) {
36
			double value = raster.getElemDouble(line, col, band);
37
			if (noDataValue == value) {
38
				alpha = 0;
39
				break;
40
			}
41
		}
42
		rasterAlpha.setElem(line, col, 0, alpha);
34
	public boolean hasNoData(int col, int line) {
35
		double data[] = new double[raster.getBandCount()];
36
		raster.getElemDouble(line, col, data);
37
		for (int band = 0; band < data.length; band++)
38
			if (noDataValue == data[band])
39
				return true;
40
		return false;
43 41
	}
44 42

  
45 43
	/*
46 44
	 * (non-Javadoc)
45
	 * @see org.gvsig.raster.grid.filter.bands.NoDataFilter#writeNoData(int, int)
46
	 */
47
	public void writeNoData(int col, int line) {
48
		for (int band = 0; band < raster.getBandCount(); band++)
49
			raster.setElem(line, col, band, (double) noDataValue);
50
	}
51

  
52
	
53
	/*
54
	 * (non-Javadoc)
47 55
	 * @see org.gvsig.raster.grid.filter.bands.ColorTableFilter#getInRasterDataType()
48 56
	 */
49 57
	public int getInRasterDataType() {
trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/bands/NoDataIntegerFilter.java
26 26
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
27 27
 */
28 28
public class NoDataIntegerFilter extends NoDataFilter {
29

  
29 30
	/*
30 31
	 * (non-Javadoc)
31
	 * @see org.gvsig.raster.grid.filter.bands.ColorTableFilter#process(int, int)
32
	 * @see org.gvsig.raster.grid.filter.bands.NoDataFilter#hasNoData(int, int)
32 33
	 */
33
	public void process(int col, int line) {
34
		byte alpha = (byte) 255;
35
		for (int band = 0; band < raster.getBandCount(); band++) {
36
			double value = (double) raster.getElemInt(line, col, band);
37
			if (noDataValue == value) {
38
				alpha = 0;
39
				break;
40
			}
41
		}
42
		rasterAlpha.setElem(line, col, 0, alpha);
34
	public boolean hasNoData(int col, int line) {
35
		int data[] = new int[raster.getBandCount()];
36
		raster.getElemInt(line, col, data);
37
		for (int band = 0; band < data.length; band++)
38
			if (noDataValue == (double) data[band])
39
				return true;
40
		return false;
43 41
	}
44 42

  
45 43
	/*
46 44
	 * (non-Javadoc)
45
	 * @see org.gvsig.raster.grid.filter.bands.NoDataFilter#writeNoData(int, int)
46
	 */
47
	public void writeNoData(int col, int line) {
48
		for (int band = 0; band < raster.getBandCount(); band++)
49
			raster.setElem(line, col, band, (int) noDataValue);
50
	}
51

  
52
	/*
53
	 * (non-Javadoc)
47 54
	 * @see org.gvsig.raster.grid.filter.bands.ColorTableFilter#getInRasterDataType()
48 55
	 */
49 56
	public int getInRasterDataType() {
trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/bands/NoDataShortFilter.java
26 26
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
27 27
 */
28 28
public class NoDataShortFilter extends NoDataFilter {
29

  
29 30
	/*
30 31
	 * (non-Javadoc)
31
	 * @see org.gvsig.raster.grid.filter.bands.ColorTableFilter#process(int, int)
32
	 * @see org.gvsig.raster.grid.filter.bands.NoDataFilter#hasNoData(int, int)
32 33
	 */
33
	public void process(int col, int line) {
34
		byte alpha = (byte) 255;
35
		for (int band = 0; band < raster.getBandCount(); band++) {
36
			double value = (double) raster.getElemShort(line, col, band);
37
			if (noDataValue == value) {
38
				alpha = 0;
39
				break;
40
			}
41
		}
42
		rasterAlpha.setElem(line, col, 0, alpha);
34
	public boolean hasNoData(int col, int line) {
35
		short data[] = new short[raster.getBandCount()];
36
		raster.getElemShort(line, col, data);
37
		for (int band = 0; band < data.length; band++)
38
			if (noDataValue == (double) data[band])
39
				return true;
40
		return false;
43 41
	}
44 42

  
45 43
	/*
46 44
	 * (non-Javadoc)
45
	 * @see org.gvsig.raster.grid.filter.bands.NoDataFilter#writeNoData(int, int)
46
	 */
47
	public void writeNoData(int col, int line) {
48
		for (int band = 0; band < rasterResult.getBandCount(); band++)
49
			rasterResult.setElem(line, col, band, (short) noDataValue);
50
	}
51
	
52
	/*
53
	 * (non-Javadoc)
47 54
	 * @see org.gvsig.raster.grid.filter.bands.ColorTableFilter#getInRasterDataType()
48 55
	 */
49 56
	public int getInRasterDataType() {
trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/bands/NoDataFloatFilter.java
26 26
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
27 27
 */
28 28
public class NoDataFloatFilter extends NoDataFilter {
29
	
29 30
	/*
30 31
	 * (non-Javadoc)
31
	 * @see org.gvsig.raster.grid.filter.bands.ColorTableFilter#process(int, int)
32
	 * @see org.gvsig.raster.grid.filter.bands.NoDataFilter#hasNoData(int, int)
32 33
	 */
33
	public void process(int col, int line) {
34
		byte alpha = (byte) 255;
35
		for (int band = 0; band < raster.getBandCount(); band++) {
36
			double value = (double) raster.getElemFloat(line, col, band);
37
			if (noDataValue == value) {
38
				alpha = 0;
39
				break;
40
			}
41
		}
42
		rasterAlpha.setElem(line, col, 0, alpha);
34
	public boolean hasNoData(int col, int line) {
35
		float data[] = new float[raster.getBandCount()];
36
		raster.getElemFloat(line, col, data);
37
		for (int band = 0; band < data.length; band++)
38
			if (noDataValue == (double) data[band])
39
				return true;
40
		return false;
43 41
	}
44 42

  
45 43
	/*
46 44
	 * (non-Javadoc)
45
	 * @see org.gvsig.raster.grid.filter.bands.NoDataFilter#writeNoData(int, int)
46
	 */
47
	public void writeNoData(int col, int line) {
48
		for (int band = 0; band < raster.getBandCount(); band++)
49
			raster.setElem(line, col, band, (float) noDataValue);
50
	}
51
	
52
	/*
53
	 * (non-Javadoc)
47 54
	 * @see org.gvsig.raster.grid.filter.bands.ColorTableFilter#getInRasterDataType()
48 55
	 */
49 56
	public int getInRasterDataType() {
trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/bands/NoDataFilter.java
56 56
	public void pre() {
57 57
		exec = true;
58 58
		raster = (RasterBuffer) params.get("raster");
59
		if(raster != null) {
59
		rasterResult = (RasterBuffer) params.get("rasterResult");
60
		if (raster != null) {
60 61
			height = raster.getHeight();
61 62
			width = raster.getWidth();
62 63
			noDataValue = ((Double) params.get("noDataValue")).doubleValue();
......
111 112
	}
112 113

  
113 114
	public void process(int x, int y) {
115
		if (hasNoData(x, y))
116
			rasterAlpha.setElem(y, x, 0, (byte) 0);
117
		else
118
			rasterAlpha.setElem(y, x, 0, (byte) 255);
114 119
	}
115 120

  
121
	/**
122
	 * Devuelve si en esa posicion hay un valor NoData
123
	 * @param x
124
	 * @param y
125
	 * @return
126
	 */
127
	public boolean hasNoData(int x, int y) {
128
		return false;
129
	}
130
	
131
	/**
132
	 * Escribe el valor NoData en RasterResult en dicha posicion
133
	 * @param x
134
	 * @param y
135
	 */
136
	public void writeNoData(int x, int y) {
137
	}
138

  
116 139
	/*
117 140
	 * (non-Javadoc)
118 141
	 * @see org.gvsig.raster.grid.filter.RasterFilter#getInRasterDataType()
trunk/libraries/libRaster/src/org/gvsig/raster/grid/filter/RasterFilterListManager.java
110 110
					Matcher m = p.matcher(oldClass);
111 111
					String newClass = m.replaceAll(RasterUtilities.typesToString(rasterFilterList.getInitDataType()));
112 112
					String strPackage = packageFilter + "." + newClass;
113
						
114
						RasterFilter newFilter = null;
115
						try {
116
							newFilter = instanceFilter(strPackage);
117
							newFilter.params = rasterFilterList.get(i).params;
118
							if (newFilter.params.get("filterName") != null)
119
								newFilter.setName((String) newFilter.params.get("filterName"));
120
							else
121
								newFilter.setName(rasterFilterList.get(i).getName());
122
							rasterFilterList.replace(newFilter, i);
123
						} catch (FilterTypeException e) {
124
							exceptions.add(e);
125
						}
113

  
114
					renewFilterFromControlTypes(strPackage, i, exceptions);
126 115
				}
127 116

  
128
				// Desde el filtro 2 en adelante se compara la salida de uno con la
129
				// entrada del siguiente
117
				// Desde el filtro 2 en adelante se compara la salida de uno con la entrada del siguiente
130 118
			} else {
131
				if (rasterFilterList.get(i-1).getOutRasterDataType() != rasterFilterList.get(i).getInRasterDataType()) {
119
				if (rasterFilterList.get(i - 1).getOutRasterDataType() != rasterFilterList.get(i).getInRasterDataType()) {
132 120
					Pattern p = Pattern.compile(RasterUtilities.typesToString(rasterFilterList.get(i).getInRasterDataType()));
133 121
					Matcher m = p.matcher(oldClass);
134 122
					String newClass = m.replaceAll(RasterUtilities.typesToString(rasterFilterList.get(i - 1).getOutRasterDataType()));
135 123
					String strPackage = packageFilter + "." + newClass;
136 124
					
137
					RasterFilter newFilter = null;
138
					try {
139
						newFilter = instanceFilter(strPackage.trim());
140
						newFilter.params = rasterFilterList.get(i).params;
141
						if (newFilter.params.get("filterName") != null)
142
							newFilter.setName((String) newFilter.params.get("filterName"));
143
						else
144
							newFilter.setName(rasterFilterList.get(i).getName());
145
						rasterFilterList.replace(newFilter, i);
146
					} catch (FilterTypeException e) {
147
						exceptions.add(e);
148
					}
125
					renewFilterFromControlTypes(strPackage.trim(), i, exceptions);
149 126
				}
150 127
			}
151 128
		}
152
		
153
		if(exceptions.size() > 0)
129

  
130
		if (exceptions.size() > 0)
154 131
			throw new FilterTypeException("");
155 132

  
156 133
		if (debug)
......
158 135
	}
159 136
	
160 137
	/**
161
	 * Instancia un filtro a partir de su nombre
162
	 * @param strPackage Paquete 
163
	 * @return Filtro instanciado
164
	 * @throws FilterTypeException
138
	 * Reemplaza un filtro segun su nombre a la lista de filtros, util para cambiar
139
	 * el tipo de datos de un filtro en la pila de filtros.
140
	 * @param nameFilter
141
	 * @param pos
142
	 * @param exceptions
165 143
	 */
166
	private RasterFilter instanceFilter(String strPackage) throws FilterTypeException {
167
		Class filterClass = null;
144
	private void renewFilterFromControlTypes(String nameFilter, int pos, ArrayList exceptions) {
168 145
		try {
169
			filterClass = Class.forName(strPackage.trim());
170
		} catch (ClassNotFoundException e) {
171
			throw new FilterTypeException("No puedo instanciar " + strPackage.trim());
146
			RasterFilter newFilter = RasterFilter.createFilter(nameFilter);
147
			newFilter.params = rasterFilterList.get(pos).params;
148
			if (newFilter.params.get("filterName") != null)
149
				newFilter.setName((String) newFilter.params.get("filterName"));
150
			else
151
				newFilter.setName(rasterFilterList.get(pos).getName());
152
			rasterFilterList.replace(newFilter, pos);
153
		} catch (FilterTypeException e) {
154
			exceptions.add(e);
172 155
		}
173
		
174
		Constructor con = null;
175
		try {
176
			con = filterClass.getConstructor(null);
177
		} catch (SecurityException e) {
178
			throw new FilterTypeException("");
179
		} catch (NoSuchMethodException e) {
180
			throw new FilterTypeException("");
181
		}
182
		
183
		RasterFilter newFilter = null;
184
		try {
185
			newFilter = (RasterFilter) con.newInstance(null);
186
		} catch (IllegalArgumentException e) {
187
			throw new FilterTypeException("");
188
		} catch (InstantiationException e) {
189
			throw new FilterTypeException("");
190
		} catch (IllegalAccessException e) {
191
			throw new FilterTypeException("");
192
		} catch (InvocationTargetException e) {
193
			throw new FilterTypeException("");
194
		}
195
		return newFilter;
196 156
	}
197

  
157
	
198 158
	/**
199 159
	 * M?todo que devuelve true si el tipo de filtro pasado por par?metro est? en
200 160
	 * la pila y false si no lo est?.
201
	 *
202 161
	 * @param filter Tipo de filtro a comprobar
203 162
	 * @return true si est? en la pila y false si no lo est?
204 163
	 */

Also available in: Unified diff