root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / GridWriter.java @ 18355
History | View | Annotate | Download (8.78 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, (double)rasterBuf.getNoDataValue());break; |
152 |
} |
153 |
} |
154 |
} |
155 |
|
156 |
/**
|
157 |
* Exporta a fichero ArcView ASCII pasandole el nombre del fichero a salvar. Esta
|
158 |
* llamada afecta solo a la banda 0 del raster y es especial para MDT's y para conservar
|
159 |
* la compatibilidad con c?digo que la usa de esta forma.
|
160 |
*
|
161 |
* @param sFile Nombre del fichero
|
162 |
* @throws IOException
|
163 |
* @throws NumberFormatException
|
164 |
*/
|
165 |
public void ExportToArcViewASCIIFile(String sFile) |
166 |
throws IOException, NumberFormatException { |
167 |
ExportToArcViewASCIIFile(sFile, bandToOperate); |
168 |
} |
169 |
|
170 |
/**
|
171 |
* Exporta a fichero ArcView ASCII pasandole el nombre del fichero a salvar.
|
172 |
* @param sFile Nombre del fichero
|
173 |
* @param band
|
174 |
* @throws IOException
|
175 |
* @throws NumberFormatException
|
176 |
*/
|
177 |
public void ExportToArcViewASCIIFile(String sFile, int band) |
178 |
throws IOException, NumberFormatException { |
179 |
|
180 |
BufferedWriter fout = new BufferedWriter(new FileWriter(sFile)); |
181 |
|
182 |
fout.write("ncols " + Integer.toString(extent.getNX())); |
183 |
fout.newLine(); |
184 |
fout.write("nrows " + Integer.toString(extent.getNY())); |
185 |
fout.newLine(); |
186 |
fout.write("xllcorner " + Double.toString(extent.minX())); |
187 |
fout.newLine(); |
188 |
fout.write("yllcorner " + Double.toString(extent.minY())); |
189 |
fout.newLine(); |
190 |
fout.write("cellsize " + Double.toString(extent.getCellSize())); |
191 |
fout.newLine(); |
192 |
fout.write("nodata_value " + Float.toString((float)rasterBuf.getNoDataValue())); |
193 |
fout.newLine(); |
194 |
|
195 |
switch(rasterBuf.getDataType()) {
|
196 |
case IBuffer.TYPE_BYTE: for (int i = 0; i < extent.getNY(); i++) { |
197 |
for (int j = 0; j < extent.getNX(); j++) |
198 |
fout.write(Byte.toString(rasterBuf.getElemByte(i, j, band)/*[j][i]*/) + " "); |
199 |
fout.newLine(); |
200 |
} |
201 |
break;
|
202 |
case IBuffer.TYPE_SHORT:for (int i = 0; i < extent.getNY(); i++) { |
203 |
for (int j = 0; j < extent.getNX(); j++) |
204 |
fout.write(Short.toString(rasterBuf.getElemShort(i, j, band)/*[j][i]*/) + " "); |
205 |
fout.newLine(); |
206 |
} |
207 |
break;
|
208 |
case IBuffer.TYPE_INT: for (int i = 0; i < extent.getNY(); i++) { |
209 |
for (int j = 0; j < extent.getNX(); j++) |
210 |
fout.write(Integer.toString(rasterBuf.getElemInt(i, j, band)/*[j][i]*/) + " "); |
211 |
fout.newLine(); |
212 |
} |
213 |
break;
|
214 |
case IBuffer.TYPE_FLOAT:for (int i = 0; i < extent.getNY(); i++) { |
215 |
for (int j = 0; j < extent.getNX(); j++) |
216 |
fout.write(Float.toString(rasterBuf.getElemFloat(i, j, band)/*[j][i]*/) + " "); |
217 |
fout.newLine(); |
218 |
} |
219 |
break;
|
220 |
case IBuffer.TYPE_DOUBLE:for (int i = 0; i < extent.getNY(); i++) { |
221 |
for (int j = 0; j < extent.getNX(); j++) |
222 |
fout.write(Double.toString(rasterBuf.getElemDouble(i, j, band)/*[j][i]*/) + " "); |
223 |
fout.newLine(); |
224 |
} |
225 |
break;
|
226 |
} |
227 |
fout.close(); |
228 |
} |
229 |
|
230 |
public void setNoDataValue(double noDataValue) { |
231 |
rasterBuf.setNoDataValue(noDataValue); |
232 |
} |
233 |
|
234 |
public boolean isCellInGrid(int iX, int iY) { |
235 |
return (iX >= 0 && iX < extent.getNX() && iY >= 0 && iY < extent.getNY()); |
236 |
} |
237 |
|
238 |
/**
|
239 |
* Asigna la banda sobre la que se realizan las operaciones. Por defecto es la banda 0
|
240 |
* con lo que para el uso de MDTs no habr? que modificar este valor.
|
241 |
* @param band Banda sobre la que se realizan las operaciones.
|
242 |
*/
|
243 |
public void setBandToOperate(int band) { |
244 |
this.bandToOperate = band;
|
245 |
} |
246 |
} |