root / branches / v2_0_0_prep / libraries / libRaster / src / org / gvsig / raster / grid / GridWriter.java @ 30008
History | View | Annotate | Download (9.08 KB)
1 |
package org.gvsig.raster.grid; |
---|---|
2 |
|
3 |
import java.io.BufferedWriter; |
4 |
import java.io.FileWriter; |
5 |
import java.io.IOException; |
6 |
|
7 |
import org.gvsig.raster.buffer.RasterBuffer; |
8 |
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException; |
9 |
import org.gvsig.raster.dataset.IBuffer; |
10 |
|
11 |
/**
|
12 |
* Esta clase representa un grid para escritura
|
13 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
14 |
*/
|
15 |
public class GridWriter{ |
16 |
|
17 |
private RasterBuffer rasterBuf = null; |
18 |
private GridExtent extent = null; |
19 |
private int dataType = IBuffer.TYPE_UNDEFINED; |
20 |
private int bandToOperate = 0; |
21 |
|
22 |
/**
|
23 |
* Asignaci?n del buffer de datos e inicializaci?n de variables.
|
24 |
* @param ge Extent del grid
|
25 |
* @param dataType Tipo de dato del grid
|
26 |
* @param rasterBuf Buffer de datos del grid
|
27 |
*/
|
28 |
public GridWriter(GridExtent ge, int dataType, RasterBuffer rasterBuf){ |
29 |
this.rasterBuf = rasterBuf;
|
30 |
this.extent = ge;
|
31 |
} |
32 |
|
33 |
/**
|
34 |
* Creaci?n del escritor de grid a trav?s de los par?metros de su extent. Con
|
35 |
* ellos se crear? el GridExtent y el buffer de datos para escribir.
|
36 |
* @param iNX Ancho en pixeles
|
37 |
* @param iNY Alto en pixeles
|
38 |
* @param dCellSize Tama?o de celda
|
39 |
* @param dMinX Coordenada m?nima en X
|
40 |
* @param dMinY Coordenada m?nima en Y
|
41 |
* @param dataType Tipo de dato
|
42 |
* @param bands Bandas a escribir
|
43 |
*/
|
44 |
public GridWriter(int iNX, int iNY, double dCellSize, |
45 |
double dMinX, double dMinY, int dataType, |
46 |
int[] bands) { |
47 |
extent = new GridExtent();
|
48 |
extent.setCellSize(dCellSize); |
49 |
extent.setXRange(dMinX, dMinX + iNX * dCellSize); |
50 |
extent.setYRange(dMinY, dMinY + iNY * dCellSize); |
51 |
|
52 |
rasterBuf = RasterBuffer.getBuffer(dataType, iNX, iNY, bands.length, true);
|
53 |
} |
54 |
|
55 |
public void assign(byte value) throws RasterBufferInvalidAccessException { |
56 |
try{
|
57 |
rasterBuf.assign(bandToOperate, value); |
58 |
}catch(ArrayIndexOutOfBoundsException e){ |
59 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
60 |
}catch(NullPointerException e){ |
61 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
62 |
} |
63 |
} |
64 |
|
65 |
public void assign(short value) throws RasterBufferInvalidAccessException { |
66 |
try{
|
67 |
rasterBuf.assign(bandToOperate, value); |
68 |
}catch(ArrayIndexOutOfBoundsException e){ |
69 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
70 |
}catch(NullPointerException e){ |
71 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
72 |
} |
73 |
} |
74 |
|
75 |
public void assign(int value) throws RasterBufferInvalidAccessException { |
76 |
try{
|
77 |
rasterBuf.assign(bandToOperate, value); |
78 |
}catch(ArrayIndexOutOfBoundsException e){ |
79 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
80 |
}catch(NullPointerException e){ |
81 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
82 |
} |
83 |
} |
84 |
|
85 |
public void assign(float value) throws RasterBufferInvalidAccessException { |
86 |
try{
|
87 |
rasterBuf.assign(bandToOperate, value); |
88 |
}catch(ArrayIndexOutOfBoundsException e){ |
89 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
90 |
}catch(NullPointerException e){ |
91 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
92 |
} |
93 |
} |
94 |
|
95 |
public void assign(double value) throws RasterBufferInvalidAccessException { |
96 |
try{
|
97 |
rasterBuf.assign(bandToOperate, value); |
98 |
}catch(ArrayIndexOutOfBoundsException e){ |
99 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
100 |
}catch(NullPointerException e){ |
101 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
102 |
} |
103 |
} |
104 |
|
105 |
public GridExtent getGridExtent() {
|
106 |
return extent;
|
107 |
} |
108 |
|
109 |
public void setCellValue( int iX, int iY, byte value) throws OutOfGridException { |
110 |
if (isCellInGrid(iX, iY))
|
111 |
rasterBuf.setElem(iY, iX, bandToOperate, value); |
112 |
else
|
113 |
throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid."); |
114 |
} |
115 |
|
116 |
public void setCellValue( int iX, int iY, short value) throws OutOfGridException { |
117 |
if (isCellInGrid(iX, iY))
|
118 |
rasterBuf.setElem(iY, iX, bandToOperate, value); |
119 |
else
|
120 |
throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid."); |
121 |
} |
122 |
|
123 |
public void setCellValue( int iX, int iY, int value) throws OutOfGridException { |
124 |
if (isCellInGrid(iX, iY))
|
125 |
rasterBuf.setElem(iY, iX, bandToOperate, value); |
126 |
else
|
127 |
throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid."); |
128 |
} |
129 |
|
130 |
public void setCellValue( int iX, int iY, float value) throws OutOfGridException { |
131 |
if (isCellInGrid(iX, iY))
|
132 |
rasterBuf.setElem(iY, iX, bandToOperate, value); |
133 |
else
|
134 |
throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid."); |
135 |
} |
136 |
|
137 |
public void setCellValue( int iX, int iY, double value) throws OutOfGridException { |
138 |
if (isCellInGrid(iX, iY))
|
139 |
rasterBuf.setElem(iY, iX, bandToOperate, value); |
140 |
else
|
141 |
throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid."); |
142 |
} |
143 |
|
144 |
public void setNoData( int iX, int iY) { |
145 |
if (isCellInGrid(iX,iY))
|
146 |
switch(rasterBuf.getDataType()) {
|
147 |
case IBuffer.TYPE_BYTE: rasterBuf.setElem(iY, iX, bandToOperate, (byte)rasterBuf.getNoDataValue());break; |
148 |
case IBuffer.TYPE_SHORT: rasterBuf.setElem(iY, iX, bandToOperate, (short)rasterBuf.getNoDataValue());break; |
149 |
case IBuffer.TYPE_INT: rasterBuf.setElem(iY, iX, bandToOperate, (int)rasterBuf.getNoDataValue());break; |
150 |
case IBuffer.TYPE_FLOAT: rasterBuf.setElem(iY, iX, bandToOperate, (float)rasterBuf.getNoDataValue());break; |
151 |
case IBuffer.TYPE_DOUBLE: rasterBuf.setElem(iY, iX, bandToOperate, rasterBuf.getNoDataValue());break; |
152 |
} |
153 |
} |
154 |
|
155 |
/**
|
156 |
* Exporta a fichero ArcView ASCII pasandole el nombre del fichero a salvar. Esta
|
157 |
* llamada afecta solo a la banda 0 del raster y es especial para MDT's y para conservar
|
158 |
* la compatibilidad con c?digo que la usa de esta forma.
|
159 |
*
|
160 |
* @param sFile Nombre del fichero
|
161 |
* @throws IOException
|
162 |
* @throws NumberFormatException
|
163 |
*/
|
164 |
public void ExportToArcViewASCIIFile(String sFile) |
165 |
throws IOException, NumberFormatException { |
166 |
ExportToArcViewASCIIFile(sFile, bandToOperate); |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Exporta a fichero ArcView ASCII pasandole el nombre del fichero a salvar.
|
171 |
* @param sFile Nombre del fichero
|
172 |
* @param band
|
173 |
* @throws IOException
|
174 |
* @throws NumberFormatException
|
175 |
*/
|
176 |
public void ExportToArcViewASCIIFile(String sFile, int band) |
177 |
throws IOException, NumberFormatException { |
178 |
|
179 |
BufferedWriter fout = new BufferedWriter(new FileWriter(sFile)); |
180 |
|
181 |
fout.write("ncols " + Integer.toString(extent.getNX())); |
182 |
fout.newLine(); |
183 |
fout.write("nrows " + Integer.toString(extent.getNY())); |
184 |
fout.newLine(); |
185 |
fout.write("xllcorner " + Double.toString(extent.minX())); |
186 |
fout.newLine(); |
187 |
fout.write("yllcorner " + Double.toString(extent.minY())); |
188 |
fout.newLine(); |
189 |
fout.write("cellsize " + Double.toString(extent.getCellSize())); |
190 |
fout.newLine(); |
191 |
fout.write("nodata_value " + Float.toString((float)rasterBuf.getNoDataValue())); |
192 |
fout.newLine(); |
193 |
|
194 |
switch(rasterBuf.getDataType()) {
|
195 |
case IBuffer.TYPE_BYTE: for (int i = 0; i < extent.getNY(); i++) { |
196 |
for (int j = 0; j < extent.getNX(); j++) |
197 |
fout.write(Byte.toString(rasterBuf.getElemByte(i, j, band)/*[j][i]*/) + " "); |
198 |
fout.newLine(); |
199 |
} |
200 |
break;
|
201 |
case IBuffer.TYPE_SHORT:for (int i = 0; i < extent.getNY(); i++) { |
202 |
for (int j = 0; j < extent.getNX(); j++) |
203 |
fout.write(Short.toString(rasterBuf.getElemShort(i, j, band)/*[j][i]*/) + " "); |
204 |
fout.newLine(); |
205 |
} |
206 |
break;
|
207 |
case IBuffer.TYPE_INT: for (int i = 0; i < extent.getNY(); i++) { |
208 |
for (int j = 0; j < extent.getNX(); j++) |
209 |
fout.write(Integer.toString(rasterBuf.getElemInt(i, j, band)/*[j][i]*/) + " "); |
210 |
fout.newLine(); |
211 |
} |
212 |
break;
|
213 |
case IBuffer.TYPE_FLOAT:for (int i = 0; i < extent.getNY(); i++) { |
214 |
for (int j = 0; j < extent.getNX(); j++) |
215 |
fout.write(Float.toString(rasterBuf.getElemFloat(i, j, band)/*[j][i]*/) + " "); |
216 |
fout.newLine(); |
217 |
} |
218 |
break;
|
219 |
case IBuffer.TYPE_DOUBLE:for (int i = 0; i < extent.getNY(); i++) { |
220 |
for (int j = 0; j < extent.getNX(); j++) |
221 |
fout.write(Double.toString(rasterBuf.getElemDouble(i, j, band)/*[j][i]*/) + " "); |
222 |
fout.newLine(); |
223 |
} |
224 |
break;
|
225 |
} |
226 |
fout.close(); |
227 |
} |
228 |
|
229 |
public void setNoDataValue(double noDataValue) { |
230 |
rasterBuf.setNoDataValue(noDataValue); |
231 |
} |
232 |
|
233 |
/**
|
234 |
* Consulta si el grid tiene la posici?n indicada por par?metro o si esta cae
|
235 |
* fuera del ?rea.
|
236 |
* @param iX Posici?n X
|
237 |
* @param iY Posici?n Y
|
238 |
* @return true si est? incluido en el Grid y false si no lo est?
|
239 |
*/
|
240 |
public boolean isCellInGrid(int iX, int iY) { |
241 |
return (iX >= 0 && iX < extent.getNX() && iY >= 0 && iY < extent.getNY()); |
242 |
} |
243 |
|
244 |
/**
|
245 |
* Asigna la banda sobre la que se realizan las operaciones. Por defecto es la banda 0
|
246 |
* con lo que para el uso de MDTs no habr? que modificar este valor.
|
247 |
* @param band Banda sobre la que se realizan las operaciones.
|
248 |
*/
|
249 |
public void setBandToOperate(int band) { |
250 |
this.bandToOperate = band;
|
251 |
} |
252 |
|
253 |
/**
|
254 |
* Releases buffer resources
|
255 |
*/
|
256 |
public void free() { |
257 |
if (rasterBuf != null) { |
258 |
rasterBuf.free(); |
259 |
rasterBuf = null;
|
260 |
} |
261 |
} |
262 |
} |