Revision 30008 branches/v2_0_0_prep/libraries/libRaster/src/org/gvsig/raster/grid/GridWriter.java

View differences:

GridWriter.java
9 9
import org.gvsig.raster.dataset.IBuffer;
10 10

  
11 11
/**
12
 * Esta clase representa un grid para escritura 
12
 * Esta clase representa un grid para escritura
13 13
 * @author Nacho Brodin (nachobrodin@gmail.com)
14 14
 */
15 15
public class GridWriter{
......
18 18
	private GridExtent		extent = null;
19 19
	private int 			dataType = IBuffer.TYPE_UNDEFINED;
20 20
	private int				bandToOperate = 0;
21
	
21

  
22 22
	/**
23
	 * Asignaci?n del buffer de datos e inicializaci?n de variables. 
23
	 * Asignaci?n del buffer de datos e inicializaci?n de variables.
24 24
	 * @param ge Extent del grid
25 25
	 * @param dataType Tipo de dato del grid
26 26
	 * @param rasterBuf Buffer de datos del grid
27 27
	 */
28 28
	public GridWriter(GridExtent ge, int dataType, RasterBuffer rasterBuf){
29 29
		this.rasterBuf = rasterBuf;
30
		this.extent = ge;	
30
		this.extent = ge;
31 31
	}
32
	
32

  
33 33
	/**
34 34
	 * Creaci?n del escritor de grid a trav?s de los par?metros de su extent. Con
35 35
	 * ellos se crear? el GridExtent y el buffer de datos para escribir.
......
42 42
	 * @param bands Bandas a escribir
43 43
	 */
44 44
	public GridWriter(int iNX, int iNY, double dCellSize,
45
						double dMinX, double dMinY, int dataType, 
45
						double dMinX, double dMinY, int dataType,
46 46
						int[] bands) {
47 47
		extent = new GridExtent();
48 48
		extent.setCellSize(dCellSize);
49 49
		extent.setXRange(dMinX, dMinX + iNX * dCellSize);
50 50
		extent.setYRange(dMinY, dMinY + iNY * dCellSize);
51
	
51

  
52 52
		rasterBuf = RasterBuffer.getBuffer(dataType, iNX, iNY, bands.length, true);
53 53
	}
54
	
54

  
55 55
	public void assign(byte value) throws RasterBufferInvalidAccessException {
56 56
		try{
57 57
			rasterBuf.assign(bandToOperate, value);
......
61 61
			throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
62 62
		}
63 63
	}
64
	
64

  
65 65
	public void assign(short value) throws RasterBufferInvalidAccessException {
66 66
		try{
67 67
			rasterBuf.assign(bandToOperate, value);
......
69 69
			throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
70 70
		}catch(NullPointerException e){
71 71
			throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
72
		}	
72
		}
73 73
	}
74
	
74

  
75 75
	public void assign(int value) throws RasterBufferInvalidAccessException {
76 76
		try{
77 77
			rasterBuf.assign(bandToOperate, value);
......
79 79
			throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
80 80
		}catch(NullPointerException e){
81 81
			throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
82
		}	
82
		}
83 83
	}
84
	
84

  
85 85
	public void assign(float value) throws RasterBufferInvalidAccessException {
86 86
		try{
87 87
			rasterBuf.assign(bandToOperate, value);
......
89 89
			throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
90 90
		}catch(NullPointerException e){
91 91
			throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
92
		}	
92
		}
93 93
	}
94
	
94

  
95 95
	public void assign(double value) throws RasterBufferInvalidAccessException {
96 96
		try{
97 97
			rasterBuf.assign(bandToOperate, value);
......
99 99
			throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
100 100
		}catch(NullPointerException e){
101 101
			throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid");
102
		}	
102
		}
103 103
	}
104
	
105
	public GridExtent getGridExtent() {		
104

  
105
	public GridExtent getGridExtent() {
106 106
		return extent;
107 107
	}
108
	
108

  
109 109
	public void setCellValue( int iX, int iY, byte value) throws OutOfGridException {
110 110
		if (isCellInGrid(iX, iY))
111
			rasterBuf.setElem(iY, iX, bandToOperate, value);	
111
			rasterBuf.setElem(iY, iX, bandToOperate, value);
112 112
		else
113 113
			throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid.");
114 114
	}
115
	
115

  
116 116
	public void setCellValue( int iX, int iY, short value) throws OutOfGridException {
117 117
		if (isCellInGrid(iX, iY))
118
			rasterBuf.setElem(iY, iX, bandToOperate, value);	
118
			rasterBuf.setElem(iY, iX, bandToOperate, value);
119 119
		else
120 120
			throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid.");
121 121
	}
122
	
122

  
123 123
	public void setCellValue( int iX, int iY, int value) throws OutOfGridException {
124 124
		if (isCellInGrid(iX, iY))
125
			rasterBuf.setElem(iY, iX, bandToOperate, value);	
125
			rasterBuf.setElem(iY, iX, bandToOperate, value);
126 126
		else
127 127
			throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid.");
128 128
	}
129
	
129

  
130 130
	public void setCellValue( int iX, int iY, float value) throws OutOfGridException {
131 131
		if (isCellInGrid(iX, iY))
132
			rasterBuf.setElem(iY, iX, bandToOperate, value);	
132
			rasterBuf.setElem(iY, iX, bandToOperate, value);
133 133
		else
134 134
			throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid.");
135 135
	}
136
	
136

  
137 137
	public void setCellValue( int iX, int iY, double value) throws OutOfGridException {
138 138
		if (isCellInGrid(iX, iY))
139
			rasterBuf.setElem(iY, iX, bandToOperate, value);	
139
			rasterBuf.setElem(iY, iX, bandToOperate, value);
140 140
		else
141 141
			throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid.");
142 142
	}
143
	
143

  
144 144
	public void setNoData( int iX, int iY) {
145
		if (isCellInGrid(iX,iY)) {
145
		if (isCellInGrid(iX,iY))
146 146
			switch(rasterBuf.getDataType()) {
147 147
			case IBuffer.TYPE_BYTE: rasterBuf.setElem(iY, iX, bandToOperate, (byte)rasterBuf.getNoDataValue());break;
148 148
			case IBuffer.TYPE_SHORT: rasterBuf.setElem(iY, iX, bandToOperate, (short)rasterBuf.getNoDataValue());break;
149 149
			case IBuffer.TYPE_INT: rasterBuf.setElem(iY, iX, bandToOperate, (int)rasterBuf.getNoDataValue());break;
150 150
			case IBuffer.TYPE_FLOAT: rasterBuf.setElem(iY, iX, bandToOperate, (float)rasterBuf.getNoDataValue());break;
151
			case IBuffer.TYPE_DOUBLE: rasterBuf.setElem(iY, iX, bandToOperate, (double)rasterBuf.getNoDataValue());break;
151
			case IBuffer.TYPE_DOUBLE: rasterBuf.setElem(iY, iX, bandToOperate, rasterBuf.getNoDataValue());break;
152 152
			}
153
		}
154 153
	}
155
	
154

  
156 155
	/**
157 156
	 * Exporta a fichero ArcView ASCII pasandole el nombre del fichero a salvar. Esta
158 157
	 * llamada afecta solo a la banda 0 del raster y es especial para MDT's y para conservar
159 158
	 * la compatibilidad con c?digo que la usa de esta forma.
160
	 * 
159
	 *
161 160
	 * @param sFile Nombre del fichero
162
	 * @throws IOException 
161
	 * @throws IOException
163 162
	 * @throws NumberFormatException
164 163
	 */
165 164
	public void ExportToArcViewASCIIFile(String sFile)
166 165
				throws IOException, NumberFormatException {
167 166
		ExportToArcViewASCIIFile(sFile, bandToOperate);
168 167
	}
169
	
168

  
170 169
	/**
171 170
	 * Exporta a fichero ArcView ASCII pasandole el nombre del fichero a salvar.
172 171
	 * @param sFile Nombre del fichero
173
	 * @param band 
172
	 * @param band
174 173
	 * @throws IOException
175 174
	 * @throws NumberFormatException
176 175
	 */
177 176
	public void ExportToArcViewASCIIFile(String sFile, int band)
178 177
				throws IOException, NumberFormatException {
179
		
178

  
180 179
		BufferedWriter fout = new BufferedWriter(new FileWriter(sFile));
181
	
180

  
182 181
		fout.write("ncols " + Integer.toString(extent.getNX()));
183 182
		fout.newLine();
184 183
		fout.write("nrows " + Integer.toString(extent.getNY()));
185 184
		fout.newLine();
186 185
		fout.write("xllcorner " + Double.toString(extent.minX()));
187
		fout.newLine();	
186
		fout.newLine();
188 187
		fout.write("yllcorner " + Double.toString(extent.minY()));
189 188
		fout.newLine();
190 189
		fout.write("cellsize " + Double.toString(extent.getCellSize()));
191 190
		fout.newLine();
192 191
		fout.write("nodata_value " + Float.toString((float)rasterBuf.getNoDataValue()));
193 192
		fout.newLine();
194
		
193

  
195 194
		switch(rasterBuf.getDataType()) {
196 195
		case IBuffer.TYPE_BYTE: for (int i = 0; i < extent.getNY(); i++) {
197 196
									for (int j = 0; j < extent.getNX(); j++)
......
226 225
		}
227 226
		fout.close();
228 227
	}
229
		
228

  
230 229
	public void setNoDataValue(double noDataValue) {
231 230
		rasterBuf.setNoDataValue(noDataValue);
232 231
	}
......
241 240
	public boolean isCellInGrid(int iX, int iY) {
242 241
		return (iX >= 0 && iX < extent.getNX() && iY >= 0 && iY < extent.getNY());
243 242
	}
244
	
243

  
245 244
	/**
246 245
	 * Asigna la banda sobre la que se realizan las operaciones. Por defecto es la banda 0
247 246
	 * con lo que para el uso de MDTs no habr? que modificar este valor.
......
250 249
	public void setBandToOperate(int band) {
251 250
		this.bandToOperate = band;
252 251
	}
252

  
253
	/**
254
	 * Releases buffer resources
255
	 */
256
	public void free() {
257
		if (rasterBuf != null) {
258
			rasterBuf.free();
259
			rasterBuf = null;
260
		}
261
	}
253 262
}

Also available in: Unified diff