gvsig-raster / org.gvsig.raster / tags / 2.0.0 / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / GridWriter.java @ 1708
History | View | Annotate | Download (8.3 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.impl.grid; |
23 |
|
24 |
import org.gvsig.fmap.dal.coverage.RasterLibrary; |
25 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
26 |
import org.gvsig.fmap.dal.coverage.datastruct.GridExtent; |
27 |
import org.gvsig.fmap.dal.coverage.datastruct.NoData; |
28 |
import org.gvsig.fmap.dal.coverage.exception.OutOfGridException; |
29 |
import org.gvsig.fmap.dal.coverage.exception.RasterBufferInvalidAccessException; |
30 |
import org.gvsig.raster.impl.DefaultRasterManager; |
31 |
|
32 |
/**
|
33 |
* Esta clase representa un grid para escritura
|
34 |
*
|
35 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
36 |
*/
|
37 |
public class GridWriter{ |
38 |
|
39 |
private Buffer rasterBuf = null; |
40 |
private GridExtent extent = null; |
41 |
private int dataType = Buffer.TYPE_UNDEFINED; |
42 |
private int bandToOperate = 0; |
43 |
|
44 |
/**
|
45 |
* Asignaci?n del buffer de datos e inicializaci?n de variables.
|
46 |
* @param ge Extent del grid
|
47 |
* @param dataType Tipo de dato del grid
|
48 |
* @param rasterBuf Buffer de datos del grid
|
49 |
*/
|
50 |
public GridWriter(GridExtent ge, int dataType, Buffer rasterBuf){ |
51 |
this.rasterBuf = rasterBuf;
|
52 |
this.extent = ge;
|
53 |
} |
54 |
|
55 |
/**
|
56 |
* Creaci?n del escritor de grid a trav?s de los par?metros de su extent. Con
|
57 |
* ellos se crear? el GridExtent y el buffer de datos para escribir.
|
58 |
* @param iNX Ancho en pixeles
|
59 |
* @param iNY Alto en pixeles
|
60 |
* @param dCellSize Tama?o de celda
|
61 |
* @param dMinX Coordenada m?nima en X
|
62 |
* @param dMinY Coordenada m?nima en Y
|
63 |
* @param dataType Tipo de dato
|
64 |
* @param bands Bandas a escribir
|
65 |
*/
|
66 |
public GridWriter(int iNX, int iNY, double dCellSize, |
67 |
double dMinX, double dMinY, int dataType, |
68 |
int[] bands) { |
69 |
extent = new GridExtentImpl();
|
70 |
extent.setCellSize(dCellSize); |
71 |
extent.setXRange(dMinX, dMinX + iNX * dCellSize); |
72 |
extent.setYRange(dMinY, dMinY + iNY * dCellSize); |
73 |
|
74 |
rasterBuf = DefaultRasterManager.getInstance().createBuffer(dataType, iNX, iNY, bands.length, true);
|
75 |
} |
76 |
|
77 |
public void assign(byte value) throws RasterBufferInvalidAccessException { |
78 |
try{
|
79 |
rasterBuf.assign(bandToOperate, value); |
80 |
}catch(ArrayIndexOutOfBoundsException e){ |
81 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
82 |
}catch(NullPointerException e){ |
83 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
84 |
} |
85 |
} |
86 |
|
87 |
public void assign(short value) throws RasterBufferInvalidAccessException { |
88 |
try{
|
89 |
rasterBuf.assign(bandToOperate, value); |
90 |
}catch(ArrayIndexOutOfBoundsException e){ |
91 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
92 |
}catch(NullPointerException e){ |
93 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
94 |
} |
95 |
} |
96 |
|
97 |
public void assign(int value) throws RasterBufferInvalidAccessException { |
98 |
try{
|
99 |
rasterBuf.assign(bandToOperate, value); |
100 |
}catch(ArrayIndexOutOfBoundsException e){ |
101 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
102 |
}catch(NullPointerException e){ |
103 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
104 |
} |
105 |
} |
106 |
|
107 |
public void assign(float value) throws RasterBufferInvalidAccessException { |
108 |
try{
|
109 |
rasterBuf.assign(bandToOperate, value); |
110 |
}catch(ArrayIndexOutOfBoundsException e){ |
111 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
112 |
}catch(NullPointerException e){ |
113 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
114 |
} |
115 |
} |
116 |
|
117 |
public void assign(double value) throws RasterBufferInvalidAccessException { |
118 |
try{
|
119 |
rasterBuf.assign(bandToOperate, value); |
120 |
}catch(ArrayIndexOutOfBoundsException e){ |
121 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
122 |
}catch(NullPointerException e){ |
123 |
throw new RasterBufferInvalidAccessException("Access to data type " + dataType + "invalid"); |
124 |
} |
125 |
} |
126 |
|
127 |
public GridExtent getGridExtent() {
|
128 |
return extent;
|
129 |
} |
130 |
|
131 |
public void setCellValue( int iX, int iY, byte value) throws OutOfGridException { |
132 |
if (isCellInGrid(iX, iY))
|
133 |
rasterBuf.setElem(iY, iX, bandToOperate, value); |
134 |
else
|
135 |
throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid."); |
136 |
} |
137 |
|
138 |
public void setCellValue( int iX, int iY, short value) throws OutOfGridException { |
139 |
if (isCellInGrid(iX, iY))
|
140 |
rasterBuf.setElem(iY, iX, bandToOperate, value); |
141 |
else
|
142 |
throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid."); |
143 |
} |
144 |
|
145 |
public void setCellValue( int iX, int iY, int value) throws OutOfGridException { |
146 |
if (isCellInGrid(iX, iY))
|
147 |
rasterBuf.setElem(iY, iX, bandToOperate, value); |
148 |
else
|
149 |
throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid."); |
150 |
} |
151 |
|
152 |
public void setCellValue( int iX, int iY, float value) throws OutOfGridException { |
153 |
if (isCellInGrid(iX, iY))
|
154 |
rasterBuf.setElem(iY, iX, bandToOperate, value); |
155 |
else
|
156 |
throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid."); |
157 |
} |
158 |
|
159 |
public void setCellValue( int iX, int iY, double value) throws OutOfGridException { |
160 |
if (isCellInGrid(iX, iY))
|
161 |
rasterBuf.setElem(iY, iX, bandToOperate, value); |
162 |
else
|
163 |
throw new OutOfGridException("Point:(" + iX + "," + iY + ") out of grid."); |
164 |
} |
165 |
|
166 |
public void setNoData( int iX, int iY) { |
167 |
if (isCellInGrid(iX,iY))
|
168 |
switch(rasterBuf.getDataType()) {
|
169 |
case Buffer.TYPE_BYTE: rasterBuf.setElem(iY, iX, bandToOperate, |
170 |
rasterBuf.getNoDataValue().isDefined() ? rasterBuf.getNoDataValue().getValue().byteValue() : RasterLibrary.defaultByteNoDataValue); |
171 |
break;
|
172 |
case Buffer.TYPE_SHORT: rasterBuf.setElem(iY, iX, bandToOperate, |
173 |
rasterBuf.getNoDataValue().isDefined() ? rasterBuf.getNoDataValue().getValue().shortValue() : RasterLibrary.defaultShortNoDataValue); |
174 |
break;
|
175 |
case Buffer.TYPE_INT: rasterBuf.setElem(iY, iX, bandToOperate, |
176 |
rasterBuf.getNoDataValue().isDefined() ? rasterBuf.getNoDataValue().getValue().intValue() : RasterLibrary.defaultIntegerNoDataValue); |
177 |
break;
|
178 |
case Buffer.TYPE_FLOAT: rasterBuf.setElem(iY, iX, bandToOperate, |
179 |
rasterBuf.getNoDataValue().isDefined() ? rasterBuf.getNoDataValue().getValue().floatValue() : RasterLibrary.defaultFloatNoDataValue); |
180 |
break;
|
181 |
case Buffer.TYPE_DOUBLE: rasterBuf.setElem(iY, iX, bandToOperate, |
182 |
rasterBuf.getNoDataValue().isDefined() ? rasterBuf.getNoDataValue().getValue().doubleValue() : RasterLibrary.defaultDoubleNoDataValue); |
183 |
break;
|
184 |
} |
185 |
} |
186 |
|
187 |
public void setNoDataValue(NoData noDataValue) { |
188 |
if(noDataValue.isDefined())
|
189 |
rasterBuf.setNoDataValue(noDataValue); |
190 |
} |
191 |
|
192 |
/**
|
193 |
* Consulta si el grid tiene la posici?n indicada por par?metro o si esta cae
|
194 |
* fuera del ?rea.
|
195 |
* @param iX Posici?n X
|
196 |
* @param iY Posici?n Y
|
197 |
* @return true si est? incluido en el Grid y false si no lo est?
|
198 |
*/
|
199 |
public boolean isCellInGrid(int iX, int iY) { |
200 |
return (iX >= 0 && iX < extent.getNX() && iY >= 0 && iY < extent.getNY()); |
201 |
} |
202 |
|
203 |
/**
|
204 |
* Asigna la banda sobre la que se realizan las operaciones. Por defecto es la banda 0
|
205 |
* con lo que para el uso de MDTs no habr? que modificar este valor.
|
206 |
* @param band Banda sobre la que se realizan las operaciones.
|
207 |
*/
|
208 |
public void setBandToOperate(int band) { |
209 |
this.bandToOperate = band;
|
210 |
} |
211 |
|
212 |
/**
|
213 |
* Releases buffer resources
|
214 |
*/
|
215 |
public void dispose() { |
216 |
if (rasterBuf != null) { |
217 |
rasterBuf.dispose(); |
218 |
} |
219 |
try {
|
220 |
finalize(); |
221 |
} catch (Throwable e) { |
222 |
} |
223 |
} |
224 |
|
225 |
/*
|
226 |
* (non-Javadoc)
|
227 |
* @see java.lang.Object#finalize()
|
228 |
*/
|
229 |
protected void finalize() throws Throwable { |
230 |
rasterBuf = null;
|
231 |
extent = null;
|
232 |
super.finalize();
|
233 |
} |
234 |
} |