root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / Grid.java @ 20871
History | View | Annotate | Download (36.6 KB)
1 | 10740 | nacho | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | */
|
||
19 | package org.gvsig.raster.grid; |
||
20 | |||
21 | import java.io.IOException; |
||
22 | import java.util.Arrays; |
||
23 | |||
24 | 11076 | nacho | import org.gvsig.raster.buffer.BufferFactory; |
25 | import org.gvsig.raster.buffer.RasterBuffer; |
||
26 | import org.gvsig.raster.buffer.RasterBufferInvalidAccessException; |
||
27 | import org.gvsig.raster.buffer.RasterBufferInvalidException; |
||
28 | 10939 | nacho | import org.gvsig.raster.dataset.IBuffer; |
29 | 13328 | nacho | import org.gvsig.raster.dataset.IRasterDataSource; |
30 | 17686 | nbrodin | import org.gvsig.raster.datastruct.Extent; |
31 | 12383 | nacho | import org.gvsig.raster.datastruct.Transparency; |
32 | 10740 | nacho | import org.gvsig.raster.grid.filter.RasterFilterList; |
33 | |||
34 | /**
|
||
35 | * Clase que representa una rejilla de datos raster. Este tipo de grid tiene el interfaz necesario
|
||
36 | * de acceso a la informaci?n raster para aplicaciones de analisis raster. Contiene m?todos de acceso al
|
||
37 | * dato tanto en lectura como en escritura y encapsula operaciones entre grids. Adem?s contiene
|
||
38 | * objetos que contienen las caracter?sticas asociadas a ese grid. Sobre un grid pueden
|
||
39 | * aplicarse tambi?n operaciones de filtrado.
|
||
40 | 12166 | bsanchez | *
|
41 | 10740 | nacho | * Podemos crear un Grid de cinco formas distitas:
|
42 | * <UL>
|
||
43 | * <LI>A partir de una fuente de datos (cargada en el constructor). Se har? una petici?n del extent pasado por par?metro con las bandas seleccionadas por par?metro.</LI>
|
||
44 | * <LI>A partir de un extensi?n de capa, una extensi?n de vista, un tipo de dato y un n?mero de datos
|
||
45 | * crea un Grid vacio util para escritura.</LI>
|
||
46 | * <LI>A partir de una fuente de datos (cargados). Datasource se usa como buffer de datos cargados y se selecciona si queremos el reader interpolado.</LI>
|
||
47 | * <LI>A partir de una fuente de datos (cargada en el constructor). Se har? una petici?n del extent completo de la fuente con las bandas seleccionadas por par?metro.</LI>
|
||
48 | * <LI>A partir de una fuente de datos (cargada en el constructor). Se har? una petici?n del extent completo de la fuente con todas las bandas disponibles.</LI>
|
||
49 | * </UL>
|
||
50 | 12166 | bsanchez | *
|
51 | 10740 | nacho | * @author Victor Olaya (volaya@ya.com)
|
52 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
53 | */
|
||
54 | public class Grid implements IQueryableGrid, IWritableGrid{ |
||
55 | |||
56 | 18322 | nbrodin | public final static double DEG_45_IN_RAD = Math.PI / 180. * 45.; |
57 | public final static double DEG_90_IN_RAD = Math.PI / 180. * 90.; |
||
58 | public final static double DEG_180_IN_RAD = Math.PI ; |
||
59 | public final static double DEG_270_IN_RAD = Math.PI / 180. * 270.; |
||
60 | public final static double DEG_360_IN_RAD = Math.PI * 2.; |
||
61 | 12166 | bsanchez | |
62 | 18651 | nbrodin | /* neighbor's address*/ /* N NE E SE S SW W NW */ |
63 | 10740 | nacho | private final static int m_iOffsetX []= { 0, 1, 1, 1, 0, -1, -1, -1}; |
64 | private final static int m_iOffsetY []= { 1, 1, 0, -1, -1, -1, 0, 1}; |
||
65 | 12166 | bsanchez | |
66 | 10740 | nacho | private double m_dDist[]; |
67 | public double _2DX, _6DX, _DX_2, _4DX_2; |
||
68 | 12166 | bsanchez | |
69 | 10740 | nacho | private int[] bands = null; |
70 | private int dataType = IBuffer.TYPE_UNDEFINED; |
||
71 | |||
72 | private RasterBuffer rasterBuf = null; |
||
73 | private GridReader reader = null; |
||
74 | private GridWriter writer = null; |
||
75 | private GridExtent windowExtent = null; |
||
76 | private GridExtent layerExtent = null; |
||
77 | private GridStatistic statistic = null; |
||
78 | private GridPalette[] palette = null; |
||
79 | private RasterFilterList filterList = null; |
||
80 | |||
81 | |||
82 | 12166 | bsanchez | |
83 | 10740 | nacho | /**
|
84 | 11579 | nacho | * Crea un grid a partir de un MultiRasterDataset. Usa el extent de la fuente de datos como
|
85 | * extent completo y el extent pasado como par?metro como extensi?n de ventana.
|
||
86 | 12166 | bsanchez | *
|
87 | 11579 | nacho | * Cuando se construye el reader se carga el buffer con la extensi?n definida en windowExtent
|
88 | * y las bandas especificadas en bands.
|
||
89 | 12166 | bsanchez | *
|
90 | 13328 | nacho | * @param IRasterDataSource datasets que proporcionan los datos
|
91 | 10740 | nacho | * @param bands n?mero de bandas requeridas
|
92 | 12166 | bsanchez | * @param windowExtent Extensi?n de la ventana. Si este par?metro es null se usar? el
|
93 | 10740 | nacho | * mismo que el de la capa.
|
94 | */
|
||
95 | 13328 | nacho | public Grid(IRasterDataSource datasets, int[] bands, GridExtent windowExtent) |
96 | 10740 | nacho | throws RasterBufferInvalidException{
|
97 | 17686 | nbrodin | BufferFactory bufferFactory = new BufferFactory(datasets);
|
98 | 20871 | dguerrero | double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(), |
99 | bufferFactory.getSourceHeight()); |
||
100 | 17686 | nbrodin | layerExtent = new GridExtent( bufferFactory.getDataSource().getExtent(),
|
101 | 20871 | dguerrero | cellSize[0],
|
102 | cellSize[1]);
|
||
103 | 17686 | nbrodin | if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null) |
104 | dataType = bufferFactory.getDataSource().getDataType()[0];
|
||
105 | 10740 | nacho | this.bands = bands;
|
106 | 12166 | bsanchez | |
107 | 10740 | nacho | if(windowExtent == null) |
108 | this.windowExtent = layerExtent;
|
||
109 | else
|
||
110 | this.windowExtent = windowExtent;
|
||
111 | 12166 | bsanchez | |
112 | 17686 | nbrodin | rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf(); |
113 | 12166 | bsanchez | |
114 | 10740 | nacho | if (windowExtent.fitsIn(layerExtent))
|
115 | 17686 | nbrodin | reader = new GridNotInterpolated(bufferFactory, layerExtent, windowExtent, bands);
|
116 | 10740 | nacho | else
|
117 | 17686 | nbrodin | reader = new GridInterpolated(bufferFactory, layerExtent, windowExtent, bands);
|
118 | 10740 | nacho | writer = new GridWriter(layerExtent, dataType, rasterBuf);
|
119 | 12166 | bsanchez | |
120 | 17686 | nbrodin | init(bufferFactory); |
121 | 10740 | nacho | } |
122 | 12166 | bsanchez | |
123 | 10740 | nacho | /**
|
124 | 12166 | bsanchez | * Crea un grid vacio a partir de un extent y un tipo de datos. Se crea un buffer vacio en el
|
125 | 10740 | nacho | * que puede escribirse a trav?s del writer. Los datos escritos pueden ser consultados con el
|
126 | * reader. No tiene una fuente de datos asociada ya que es un grid vacio basicamente para
|
||
127 | 12166 | bsanchez | * escritura.
|
128 | 10740 | nacho | * @param layerExtent Tama?o completo de la capa
|
129 | * @param windowExtent Ventana de datos.
|
||
130 | * @param dataType Tipo de datos del buffer
|
||
131 | 12166 | bsanchez | * @param bands n?mero de bandas requeridas y orden de dibujado en el buffer
|
132 | 10740 | nacho | */
|
133 | public Grid(GridExtent layerExtent,
|
||
134 | GridExtent windowExtent, |
||
135 | 12166 | bsanchez | int dataType,
|
136 | 10740 | nacho | int[] bands) throws RasterBufferInvalidException{ |
137 | this.windowExtent = windowExtent;
|
||
138 | this.layerExtent = layerExtent;
|
||
139 | this.dataType = dataType;
|
||
140 | 12166 | bsanchez | |
141 | 11396 | nacho | rasterBuf = RasterBuffer.getBuffer(dataType, layerExtent.getNX(), layerExtent.getNY(), bands.length, true);
|
142 | 12166 | bsanchez | |
143 | 10740 | nacho | if (windowExtent.fitsIn(layerExtent))
|
144 | 12166 | bsanchez | reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
|
145 | 10740 | nacho | else
|
146 | reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
|
||
147 | writer = new GridWriter(layerExtent, dataType, rasterBuf);
|
||
148 | init(null);
|
||
149 | } |
||
150 | 12166 | bsanchez | |
151 | 10740 | nacho | /**
|
152 | 11076 | nacho | * Crea un grid a partir de un BufferFactory. El buffer debe estar cargado de datos y el extent
|
153 | 10740 | nacho | * de este viene definido en el par?metro windowExtent.
|
154 | 13328 | nacho | * @param bufferFactory Fuente de datos
|
155 | * @param windowExtent Extent de los datos cargados en bufferFactory
|
||
156 | 10740 | nacho | * @param notInterp Si es true fuerza a que el reader sea sin interpolaci?n. Si es false decide si
|
157 | * el reader es interpolado o no a partir de los extents de capa y la ventana seleccionada.
|
||
158 | */
|
||
159 | 13328 | nacho | public Grid(BufferFactory bufferFactory, boolean notInterp) { |
160 | 20871 | dguerrero | double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(), |
161 | bufferFactory.getSourceHeight()); |
||
162 | 17686 | nbrodin | this.layerExtent = new GridExtent( bufferFactory.getDataSource().getExtent(), |
163 | 20871 | dguerrero | cellSize[0],
|
164 | cellSize[1]);
|
||
165 | 17686 | nbrodin | if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null) |
166 | dataType = bufferFactory.getDataSource().getDataType()[0];
|
||
167 | 13328 | nacho | bands = bufferFactory.getDrawableBands(); |
168 | 12166 | bsanchez | |
169 | 17686 | nbrodin | this.windowExtent = new GridExtent( bufferFactory.getDataExtent(), |
170 | 20871 | dguerrero | cellSize[0],
|
171 | cellSize[1]);
|
||
172 | 12166 | bsanchez | |
173 | 13328 | nacho | rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf(); |
174 | 12166 | bsanchez | |
175 | 11579 | nacho | if(notInterp) {
|
176 | 10740 | nacho | reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
|
177 | 11579 | nacho | } else {
|
178 | 10740 | nacho | if (windowExtent.fitsIn(layerExtent))
|
179 | 12166 | bsanchez | reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
|
180 | 10740 | nacho | else
|
181 | reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
|
||
182 | } |
||
183 | writer = new GridWriter(layerExtent, dataType, rasterBuf);
|
||
184 | 13328 | nacho | init(bufferFactory); |
185 | 10740 | nacho | } |
186 | 12166 | bsanchez | |
187 | 10740 | nacho | /**
|
188 | 11076 | nacho | * Crea un grid a partir de un BufferFactory. Se har? una petici?n del extent completo de la fuente.
|
189 | 13328 | nacho | * bufferFactory tiene asociada una fuente de datos pero se ignorar? si tiene el buffer lleno. La instanciaci?n
|
190 | 12166 | bsanchez | * de GridNotInterpolated har? que se haga la petici?n para cargar raster completo.
|
191 | 13328 | nacho | * @param bufferFactory Fuente de datos
|
192 | 10740 | nacho | * @param bands n?mero de bandas requeridas y orden de dibujado en el buffer
|
193 | */
|
||
194 | 13328 | nacho | public Grid(BufferFactory bufferFactory, int[] bands) |
195 | 10740 | nacho | throws RasterBufferInvalidException{
|
196 | //La petici?n es del raster completo
|
||
197 | 20871 | dguerrero | double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(), |
198 | bufferFactory.getSourceHeight()); |
||
199 | 17686 | nbrodin | windowExtent = layerExtent = new GridExtent(bufferFactory.getDataSource().getExtent(),
|
200 | 20871 | dguerrero | cellSize[0],
|
201 | cellSize[1]);
|
||
202 | 17686 | nbrodin | if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null) |
203 | dataType = bufferFactory.getDataSource().getDataType()[0];
|
||
204 | 10740 | nacho | this.bands = bands;
|
205 | 12166 | bsanchez | |
206 | 13328 | nacho | reader = new GridNotInterpolated(bufferFactory, layerExtent, windowExtent, bands);
|
207 | rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf(); |
||
208 | 10740 | nacho | writer = new GridWriter(windowExtent, dataType, rasterBuf);
|
209 | 12166 | bsanchez | |
210 | 13328 | nacho | init(bufferFactory); |
211 | 10740 | nacho | } |
212 | 12166 | bsanchez | |
213 | 10740 | nacho | /**
|
214 | 11076 | nacho | * Crea un grid a partir de un BufferFactory. Se har? una petici?n del extent completo de la fuente.
|
215 | 13328 | nacho | * bufferFactory tiene asociada una fuente de datos pero se ignorar? si tiene el buffer lleno. La instanciaci?n
|
216 | 12166 | bsanchez | * de GridNotInterpolated har? que se haga la petici?n para cargar raster completo. El buffer se cargar?
|
217 | * con todas las bandas disponibles.
|
||
218 | 13328 | nacho | * @param bufferFactory Fuente de datos
|
219 | 10740 | nacho | */
|
220 | 13328 | nacho | public Grid(BufferFactory bufferFactory) throws RasterBufferInvalidException { |
221 | 10740 | nacho | //La petici?n es del raster completo
|
222 | 20871 | dguerrero | double cellSize[] = calcCellSize(bufferFactory.getDataSource(), bufferFactory.getSourceWidth(), |
223 | bufferFactory.getSourceHeight()); |
||
224 | 17686 | nbrodin | windowExtent = layerExtent = new GridExtent(bufferFactory.getDataSource().getExtent(),
|
225 | 20871 | dguerrero | cellSize[0],
|
226 | cellSize[1]);
|
||
227 | 17686 | nbrodin | if(bufferFactory.getDataSource() != null && bufferFactory.getDataSource().getDataType() != null) |
228 | dataType = bufferFactory.getDataSource().getDataType()[0];
|
||
229 | 12166 | bsanchez | |
230 | 13328 | nacho | bands = new int[bufferFactory.getBandCount()]; |
231 | for(int i = 0; i < bufferFactory.getBandCount(); i ++) |
||
232 | 10740 | nacho | bands[i] = i; |
233 | 13328 | nacho | reader = new GridNotInterpolated(bufferFactory, layerExtent, windowExtent, bands);
|
234 | rasterBuf = (RasterBuffer)bufferFactory.getRasterBuf(); |
||
235 | 10740 | nacho | writer = new GridWriter(windowExtent, dataType, rasterBuf);
|
236 | 12166 | bsanchez | |
237 | 13328 | nacho | init(bufferFactory); |
238 | 10740 | nacho | } |
239 | 17686 | nbrodin | |
240 | /**
|
||
241 | 20871 | dguerrero | * Calcula el tama?o de celda a partir de un dataset, un ancho y un alto en pixeles
|
242 | 17686 | nbrodin | * @return
|
243 | */
|
||
244 | 20871 | dguerrero | private double[] calcCellSize(IRasterDataSource mDataset, double w, double h) { |
245 | double dCellsize[] = new double[2]; |
||
246 | 17686 | nbrodin | try {
|
247 | Extent e = mDataset.getExtent(); |
||
248 | 20871 | dguerrero | dCellsize[0] = (e.getMax().getX() - e.getMin().getX() ) / w;
|
249 | dCellsize[1] = (e.getMax().getY() - e.getMin().getY() ) / h;
|
||
250 | |||
251 | 17686 | nbrodin | return dCellsize;
|
252 | } catch (NullPointerException e) { |
||
253 | 20871 | dguerrero | dCellsize[0] = 1; |
254 | dCellsize[1] = 1; |
||
255 | return dCellsize;
|
||
256 | 17686 | nbrodin | } |
257 | } |
||
258 | 12166 | bsanchez | |
259 | 10740 | nacho | /**
|
260 | * Selecciona la forma de obtener datos con o sin interpolaci?n. Si pasamos el par?metro
|
||
261 | 12166 | bsanchez | * false al aplicar un m?todo de consulta de datos este ser? aplicado sin interpolaci?n.
|
262 | 10740 | nacho | * @param interpolation
|
263 | */
|
||
264 | public void switchToInterpolationMethod(boolean interpolation){ |
||
265 | //GridExtent layer = new GridExtent(0, 0, rasterBuf.getWidth(), rasterBuf.getHeight(), 1);
|
||
266 | if(interpolation)
|
||
267 | reader = new GridInterpolated(rasterBuf, layerExtent, windowExtent, bands);
|
||
268 | else
|
||
269 | reader = new GridNotInterpolated(rasterBuf, layerExtent, windowExtent, bands);
|
||
270 | init(null);
|
||
271 | } |
||
272 | 12166 | bsanchez | |
273 | 10740 | nacho | /**
|
274 | * Inicializaci?n de constantes
|
||
275 | */
|
||
276 | 12066 | nacho | private void init(BufferFactory ds) { |
277 | 10740 | nacho | int i;
|
278 | 16327 | bsanchez | GridTransparency transparency = null;
|
279 | |||
280 | 10740 | nacho | double dCellSize = getCellSize();
|
281 | 12166 | bsanchez | |
282 | 10740 | nacho | statistic = new GridStatistic(this); |
283 | 16327 | bsanchez | if (ds == null) |
284 | 10740 | nacho | transparency = new GridTransparency();
|
285 | 12066 | nacho | else {
|
286 | 13328 | nacho | Transparency transp = ds.getDataSource().getTransparencyFilesStatus();
|
287 | 16327 | bsanchez | if (transp != null) |
288 | 10740 | nacho | transparency = new GridTransparency(transp);
|
289 | 12218 | nacho | this.palette = new GridPalette[ds.getColorTables().length]; |
290 | 16327 | bsanchez | for (int iPal = 0; iPal < ds.getColorTables().length; iPal++) { |
291 | 12567 | bsanchez | if (ds.getColorTables()[iPal] != null) |
292 | 12218 | nacho | this.palette[iPal] = new GridPalette(ds.getColorTables()[iPal]); |
293 | 10740 | nacho | } |
294 | } |
||
295 | filterList = new RasterFilterList();
|
||
296 | 16327 | bsanchez | if (ds != null) { |
297 | 15778 | nbrodin | filterList.addEnvParam("IStatistics", ds.getDataSource().getStatistics());
|
298 | filterList.addEnvParam("MultiRasterDataset", ds.getDataSource());
|
||
299 | 11766 | nacho | } |
300 | 15778 | nbrodin | filterList.addEnvParam("Transparency", transparency);
|
301 | 10740 | nacho | filterList.setInitRasterBuf(rasterBuf); |
302 | 12166 | bsanchez | |
303 | 10740 | nacho | m_dDist = new double[8]; |
304 | 12166 | bsanchez | |
305 | 16327 | bsanchez | for (i = 0; i < 8; i++) { |
306 | m_dDist[i] = Math.sqrt ( m_iOffsetX[i] * dCellSize * m_iOffsetX[i] * dCellSize
|
||
307 | + m_iOffsetY[i] * dCellSize * m_iOffsetY[i] * dCellSize ); |
||
308 | } |
||
309 | 12166 | bsanchez | |
310 | 16327 | bsanchez | _2DX = dCellSize * 2.0;
|
311 | _6DX = dCellSize * 6.0;
|
||
312 | 10740 | nacho | _DX_2 = dCellSize * dCellSize; |
313 | _4DX_2 = 4.0 * _DX_2;
|
||
314 | } |
||
315 | 12166 | bsanchez | |
316 | 10740 | nacho | //************* Write Services *********************
|
317 | 12166 | bsanchez | |
318 | 10740 | nacho | /*
|
319 | * (non-Javadoc)
|
||
320 | * @see org.gvsig.fmap.grid.IWritableGrid#assign(byte)
|
||
321 | */
|
||
322 | 16590 | nbrodin | public void assign(byte value)throws GridException { |
323 | try {
|
||
324 | writer.assign(value); |
||
325 | } catch (RasterBufferInvalidAccessException e) {
|
||
326 | throw new GridException("Error wrinting buffer"); |
||
327 | } |
||
328 | 10740 | nacho | } |
329 | 12166 | bsanchez | |
330 | 10740 | nacho | /*
|
331 | * (non-Javadoc)
|
||
332 | * @see org.gvsig.fmap.grid.IWritableGrid#assign(short)
|
||
333 | */
|
||
334 | 16590 | nbrodin | public void assign(short value)throws GridException { |
335 | try {
|
||
336 | writer.assign(value); |
||
337 | } catch (RasterBufferInvalidAccessException e) {
|
||
338 | throw new GridException("Error wrinting buffer"); |
||
339 | } |
||
340 | 10740 | nacho | } |
341 | 12166 | bsanchez | |
342 | 10740 | nacho | /*
|
343 | * (non-Javadoc)
|
||
344 | * @see org.gvsig.fmap.grid.IWritableGrid#assign(int)
|
||
345 | 16590 | nbrodin | */
|
346 | public void assign(int value)throws GridException { |
||
347 | try {
|
||
348 | writer.assign(value); |
||
349 | } catch (RasterBufferInvalidAccessException e) {
|
||
350 | throw new GridException("Error wrinting buffer"); |
||
351 | } |
||
352 | 10740 | nacho | } |
353 | 12166 | bsanchez | |
354 | 10740 | nacho | /*
|
355 | * (non-Javadoc)
|
||
356 | * @see org.gvsig.fmap.grid.IWritableGrid#assign(float)
|
||
357 | */
|
||
358 | 16590 | nbrodin | public void assign(float value)throws GridException { |
359 | try {
|
||
360 | writer.assign(value); |
||
361 | } catch (RasterBufferInvalidAccessException e) {
|
||
362 | throw new GridException("Error wrinting buffer"); |
||
363 | } |
||
364 | 10740 | nacho | } |
365 | 12166 | bsanchez | |
366 | 10740 | nacho | /*
|
367 | * (non-Javadoc)
|
||
368 | * @see org.gvsig.fmap.grid.IWritableGrid#assign(double)
|
||
369 | */
|
||
370 | 16590 | nbrodin | public void assign(double value)throws GridException { |
371 | try {
|
||
372 | writer.assign(value); |
||
373 | } catch (RasterBufferInvalidAccessException e) {
|
||
374 | throw new GridException("Error wrinting buffer"); |
||
375 | } |
||
376 | 10740 | nacho | } |
377 | 12166 | bsanchez | |
378 | 10740 | nacho | /*
|
379 | * (non-Javadoc)
|
||
380 | 11076 | nacho | * @see org.gvsig.fmap.grid.IWritableGrid#assign(org.gvsig.fmap.dataaccess.BufferFactory)
|
381 | 10740 | nacho | */
|
382 | 16590 | nbrodin | public void assign(BufferFactory bufferFactory) throws GridException { |
383 | Grid window; |
||
384 | try {
|
||
385 | window = new Grid(bufferFactory.getDataSource(), bands, windowExtent);
|
||
386 | write(window); |
||
387 | writer.setNoDataValue(window.getNoDataValue()); |
||
388 | } catch (RasterBufferInvalidException e) {
|
||
389 | throw new GridException("Error writing buffer"); |
||
390 | } |
||
391 | 10740 | nacho | } |
392 | 12166 | bsanchez | |
393 | 10740 | nacho | /*
|
394 | * (non-Javadoc)
|
||
395 | * @see org.gvsig.fmap.grid.IWritableGrid#assign(org.gvsig.fmap.grid.Grid)
|
||
396 | */
|
||
397 | 16590 | nbrodin | public void assign(Grid driver) throws GridException { |
398 | 10740 | nacho | if (driver.getGridExtent().equals(layerExtent)){
|
399 | write(driver); |
||
400 | writer.setNoDataValue(driver.getNoDataValue()); |
||
401 | 12166 | bsanchez | } |
402 | } |
||
403 | |||
404 | 10740 | nacho | /**
|
405 | * Sobreescribe los datos del grid actual con los datos del grid pasado por par?metro
|
||
406 | * @param g Grid desde donde se obtienen los datos
|
||
407 | 16327 | bsanchez | */
|
408 | 16590 | nbrodin | private void write(Grid g) throws GridException { |
409 | 10740 | nacho | try{
|
410 | switch(rasterBuf.getDataType()){
|
||
411 | 13328 | nacho | case IBuffer.TYPE_BYTE: for (int x = 0; x < g.getNX(); x++) { |
412 | for (int y = 0; y < g.getNY(); y++) { |
||
413 | 10740 | nacho | writer.setCellValue(x, y, g.getCellValueAsByte(x, y)); |
414 | } |
||
415 | 12166 | bsanchez | } |
416 | 10740 | nacho | break;
|
417 | 13328 | nacho | case IBuffer.TYPE_SHORT:for (int x = 0; x < g.getNX(); x++) { |
418 | for (int y = 0; y < g.getNY(); y++) { |
||
419 | 10740 | nacho | writer.setCellValue(x, y, g.getCellValueAsShort(x, y)); |
420 | } |
||
421 | 12166 | bsanchez | } |
422 | 10740 | nacho | break;
|
423 | 13328 | nacho | case IBuffer.TYPE_INT: for (int x = 0; x < g.getNX(); x++) { |
424 | for (int y = 0; y < g.getNY(); y++) { |
||
425 | 10740 | nacho | writer.setCellValue(x, y, g.getCellValueAsInt(x, y)); |
426 | } |
||
427 | 12166 | bsanchez | } |
428 | 10740 | nacho | break;
|
429 | 13328 | nacho | case IBuffer.TYPE_FLOAT:for (int x = 0; x < g.getNX(); x++) { |
430 | for (int y = 0; y < g.getNY(); y++) { |
||
431 | 10740 | nacho | writer.setCellValue(x, y, g.getCellValueAsFloat(x, y)); |
432 | } |
||
433 | 12166 | bsanchez | } |
434 | 10740 | nacho | break;
|
435 | 13328 | nacho | case IBuffer.TYPE_DOUBLE:for (int x = 0; x < g.getNX(); x++) { |
436 | for (int y = 0; y < g.getNY(); y++) { |
||
437 | 10740 | nacho | writer.setCellValue(x, y, g.getCellValueAsDouble(x, y)); |
438 | } |
||
439 | 12166 | bsanchez | } |
440 | 10740 | nacho | break;
|
441 | } |
||
442 | } catch (OutOfGridException e) {
|
||
443 | 16590 | nbrodin | throw new GridException(""); |
444 | } |
||
445 | 10740 | nacho | } |
446 | 12166 | bsanchez | |
447 | 10740 | nacho | /*
|
448 | * (non-Javadoc)
|
||
449 | * @see org.gvsig.fmap.grid.IWritableGrid#assignNoData()
|
||
450 | */
|
||
451 | 12166 | bsanchez | public void assignNoData(){ |
452 | 10740 | nacho | try {
|
453 | switch(rasterBuf.getDataType()){
|
||
454 | case IBuffer.TYPE_BYTE: writer.assign((byte)rasterBuf.getNoDataValue());break; |
||
455 | case IBuffer.TYPE_SHORT: writer.assign((short)rasterBuf.getNoDataValue());break; |
||
456 | case IBuffer.TYPE_INT: writer.assign((int)rasterBuf.getNoDataValue());break; |
||
457 | case IBuffer.TYPE_FLOAT: writer.assign((float)rasterBuf.getNoDataValue());break; |
||
458 | case IBuffer.TYPE_DOUBLE: writer.assign((double)rasterBuf.getNoDataValue());break; |
||
459 | } |
||
460 | } catch (RasterBufferInvalidAccessException e) {
|
||
461 | //No hacemos nada. El tipo de dato al que se accede no es controlado por el usuario por lo
|
||
462 | //que no le mandamos la excepci?n hacia arriba.
|
||
463 | e.printStackTrace(); |
||
464 | } |
||
465 | } |
||
466 | 12166 | bsanchez | |
467 | 10740 | nacho | /*
|
468 | * (non-Javadoc)
|
||
469 | * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, byte)
|
||
470 | */
|
||
471 | public void setCellValue(int x, int y, byte value)throws OutOfGridException{ |
||
472 | writer.setCellValue(x, y, value); |
||
473 | } |
||
474 | 12166 | bsanchez | |
475 | 10740 | nacho | /*
|
476 | * (non-Javadoc)
|
||
477 | * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, short)
|
||
478 | */
|
||
479 | public void setCellValue(int x, int y, short value)throws OutOfGridException{ |
||
480 | writer.setCellValue(x, y, value); |
||
481 | } |
||
482 | 12166 | bsanchez | |
483 | 10740 | nacho | /*
|
484 | * (non-Javadoc)
|
||
485 | * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, int)
|
||
486 | */
|
||
487 | public void setCellValue(int x, int y, int value)throws OutOfGridException{ |
||
488 | writer.setCellValue(x, y, value); |
||
489 | } |
||
490 | 12166 | bsanchez | |
491 | 10740 | nacho | /*
|
492 | * (non-Javadoc)
|
||
493 | * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, float)
|
||
494 | */
|
||
495 | public void setCellValue(int x, int y, float value)throws OutOfGridException{ |
||
496 | writer.setCellValue(x, y, value); |
||
497 | } |
||
498 | 12166 | bsanchez | |
499 | 10740 | nacho | /*
|
500 | * (non-Javadoc)
|
||
501 | * @see org.gvsig.fmap.grid.IWritableGrid#setCellValue(int, int, double)
|
||
502 | */
|
||
503 | public void setCellValue(int x, int y, double value)throws OutOfGridException{ |
||
504 | writer.setCellValue(x, y, value); |
||
505 | } |
||
506 | 12166 | bsanchez | |
507 | 10740 | nacho | /*
|
508 | * (non-Javadoc)
|
||
509 | * @see org.gvsig.fmap.grid.IWritableGrid#add(org.gvsig.fmap.grid.Grid)
|
||
510 | */
|
||
511 | 16590 | nbrodin | public void add(Grid g) throws GridException { |
512 | if (g.getGridExtent().equals(getGridExtent())) {
|
||
513 | 10740 | nacho | boolean interp = (reader instanceof GridInterpolated); |
514 | switchToInterpolationMethod(false);
|
||
515 | try{
|
||
516 | switch(rasterBuf.getDataType()){
|
||
517 | 16590 | nbrodin | case IBuffer.TYPE_BYTE: for (int x = 0; x < g.getNX(); x++) { |
518 | for (int y = 0; y < g.getNY(); y++) { |
||
519 | 10740 | nacho | writer.setCellValue(x, y, reader.getCellValueAsByte(x, y) + g.getCellValueAsByte(x,y)); |
520 | } |
||
521 | 12166 | bsanchez | } |
522 | 10740 | nacho | break;
|
523 | 16590 | nbrodin | case IBuffer.TYPE_SHORT:for (int x = 0; x < g.getNX(); x++) { |
524 | for (int y = 0; y < g.getNY(); y++) { |
||
525 | 10740 | nacho | writer.setCellValue(x, y, reader.getCellValueAsShort(x, y) + g.getCellValueAsShort(x,y)); |
526 | } |
||
527 | 12166 | bsanchez | } |
528 | 10740 | nacho | break;
|
529 | 16590 | nbrodin | case IBuffer.TYPE_INT: for (int x = 0; x < g.getNX(); x++) { |
530 | for (int y = 0; y < g.getNY(); y++) { |
||
531 | 10740 | nacho | writer.setCellValue(x, y, reader.getCellValueAsInt(x, y) + g.getCellValueAsInt(x,y)); |
532 | } |
||
533 | 12166 | bsanchez | } |
534 | 10740 | nacho | break;
|
535 | 16590 | nbrodin | case IBuffer.TYPE_FLOAT:for (int x = 0; x < g.getNX(); x++) { |
536 | for (int y = 0; y < g.getNY(); y++) { |
||
537 | 10740 | nacho | writer.setCellValue(x, y, reader.getCellValueAsFloat(x, y) + g.getCellValueAsFloat(x,y)); |
538 | } |
||
539 | 12166 | bsanchez | } |
540 | 10740 | nacho | break;
|
541 | 16590 | nbrodin | case IBuffer.TYPE_DOUBLE:for (int x = 0; x < g.getNX(); x++) { |
542 | for (int y = 0; y < g.getNY(); y++) { |
||
543 | 10740 | nacho | writer.setCellValue(x, y, reader.getCellValueAsDouble(x, y) + g.getCellValueAsDouble(x,y)); |
544 | } |
||
545 | 12166 | bsanchez | } |
546 | 10740 | nacho | break;
|
547 | } |
||
548 | } catch (OutOfGridException e) {
|
||
549 | 16590 | nbrodin | throw new GridException(""); |
550 | 10740 | nacho | } catch (RasterBufferInvalidAccessException e1) {
|
551 | 16590 | nbrodin | throw new GridException(""); |
552 | } catch (RasterBufferInvalidException e) {
|
||
553 | throw new GridException(""); |
||
554 | 10740 | nacho | } |
555 | switchToInterpolationMethod(interp); |
||
556 | } |
||
557 | } |
||
558 | 12166 | bsanchez | |
559 | 10740 | nacho | /*
|
560 | * (non-Javadoc)
|
||
561 | * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, byte)
|
||
562 | */
|
||
563 | public void addToCellValue(int x, int y, byte value) |
||
564 | 16590 | nbrodin | throws GridException {
|
565 | try {
|
||
566 | writer.setCellValue(x, y, (byte)(reader.getCellValueAsByte(x, y) + value));
|
||
567 | } catch (OutOfGridException e) {
|
||
568 | throw new GridException(""); |
||
569 | } catch (RasterBufferInvalidAccessException e) {
|
||
570 | throw new GridException(""); |
||
571 | } catch (RasterBufferInvalidException e) {
|
||
572 | throw new GridException(""); |
||
573 | } |
||
574 | 10740 | nacho | } |
575 | 12166 | bsanchez | |
576 | 10740 | nacho | /*
|
577 | * (non-Javadoc)
|
||
578 | * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, short)
|
||
579 | */
|
||
580 | public void addToCellValue(int x, int y, short value) |
||
581 | 16590 | nbrodin | throws GridException {
|
582 | try {
|
||
583 | writer.setCellValue(x, y, (short)(reader.getCellValueAsShort(x, y) + value));
|
||
584 | } catch (OutOfGridException e) {
|
||
585 | throw new GridException(""); |
||
586 | } catch (RasterBufferInvalidAccessException e) {
|
||
587 | throw new GridException(""); |
||
588 | } catch (RasterBufferInvalidException e) {
|
||
589 | throw new GridException(""); |
||
590 | } |
||
591 | 10740 | nacho | } |
592 | 12166 | bsanchez | |
593 | 10740 | nacho | /*
|
594 | * (non-Javadoc)
|
||
595 | * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, int)
|
||
596 | */
|
||
597 | public void addToCellValue(int x, int y, int value) |
||
598 | 16590 | nbrodin | throws GridException {
|
599 | try {
|
||
600 | writer.setCellValue(x, y, (int)(reader.getCellValueAsInt(x, y) + value));
|
||
601 | } catch (OutOfGridException e) {
|
||
602 | throw new GridException(""); |
||
603 | } catch (RasterBufferInvalidAccessException e) {
|
||
604 | throw new GridException(""); |
||
605 | } catch (RasterBufferInvalidException e) {
|
||
606 | throw new GridException(""); |
||
607 | } |
||
608 | 10740 | nacho | } |
609 | 12166 | bsanchez | |
610 | 10740 | nacho | /*
|
611 | * (non-Javadoc)
|
||
612 | * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, float)
|
||
613 | */
|
||
614 | public void addToCellValue(int x, int y, float value) |
||
615 | 16590 | nbrodin | throws GridException {
|
616 | try {
|
||
617 | writer.setCellValue(x, y, (float)(reader.getCellValueAsFloat(x, y) + value));
|
||
618 | } catch (OutOfGridException e) {
|
||
619 | throw new GridException(""); |
||
620 | } catch (RasterBufferInvalidAccessException e) {
|
||
621 | throw new GridException(""); |
||
622 | } catch (RasterBufferInvalidException e) {
|
||
623 | throw new GridException(""); |
||
624 | } |
||
625 | 10740 | nacho | } |
626 | 12166 | bsanchez | |
627 | 10740 | nacho | /*
|
628 | * (non-Javadoc)
|
||
629 | * @see org.gvsig.fmap.grid.IWritableGrid#addToCellValue(int, int, double)
|
||
630 | */
|
||
631 | public void addToCellValue(int x, int y, double value) |
||
632 | 16590 | nbrodin | throws GridException {
|
633 | try {
|
||
634 | writer.setCellValue(x, y, (double)(reader.getCellValueAsDouble(x, y) + value));
|
||
635 | } catch (OutOfGridException e) {
|
||
636 | throw new GridException(""); |
||
637 | } catch (RasterBufferInvalidAccessException e) {
|
||
638 | throw new GridException(""); |
||
639 | } catch (RasterBufferInvalidException e) {
|
||
640 | throw new GridException(""); |
||
641 | } |
||
642 | 12166 | bsanchez | } |
643 | |||
644 | 10740 | nacho | /*
|
645 | * (non-Javadoc)
|
||
646 | * @see org.gvsig.fmap.grid.IWritableGrid#multiply(double)
|
||
647 | */
|
||
648 | 16590 | nbrodin | public void multiply(double value) throws GridException { |
649 | 10740 | nacho | boolean interp = (reader instanceof GridInterpolated); |
650 | switchToInterpolationMethod(false);
|
||
651 | try{
|
||
652 | switch(rasterBuf.getDataType()){
|
||
653 | case IBuffer.TYPE_BYTE: for (int x = 0; x < getNX(); x++){ |
||
654 | for (int y = 0; y < getNY(); y++) |
||
655 | writer.setCellValue(x, y, (byte)(reader.getCellValueAsByte(x, y) * value));
|
||
656 | 12166 | bsanchez | } |
657 | 10740 | nacho | break;
|
658 | case IBuffer.TYPE_SHORT:for (int x = 0; x < getNX(); x++){ |
||
659 | for (int y = 0; y < getNY(); y++) |
||
660 | writer.setCellValue(x, y, (short)(reader.getCellValueAsShort(x, y) * value));
|
||
661 | 12166 | bsanchez | } |
662 | 10740 | nacho | break;
|
663 | case IBuffer.TYPE_INT: for (int x = 0; x < getNX(); x++){ |
||
664 | for (int y = 0; y < getNY(); y++) |
||
665 | writer.setCellValue(x, y, (int)(reader.getCellValueAsInt(x, y) * value));
|
||
666 | 12166 | bsanchez | } |
667 | 10740 | nacho | break;
|
668 | case IBuffer.TYPE_FLOAT:for (int x = 0; x < getNX(); x++){ |
||
669 | for (int y = 0; y < getNY(); y++) |
||
670 | writer.setCellValue(x, y, (float)(reader.getCellValueAsFloat(x, y) * value));
|
||
671 | 12166 | bsanchez | } |
672 | 10740 | nacho | break;
|
673 | case IBuffer.TYPE_DOUBLE:for (int x = 0; x < getNX(); x++){ |
||
674 | for (int y = 0; y < getNY(); y++) |
||
675 | writer.setCellValue(x, y, (double)(reader.getCellValueAsDouble(x, y) * value));
|
||
676 | 12166 | bsanchez | } |
677 | 10740 | nacho | break;
|
678 | } |
||
679 | 16590 | nbrodin | } catch (RasterBufferInvalidException e) {
|
680 | throw new GridException(""); |
||
681 | } catch (OutOfGridException e1) {
|
||
682 | throw new GridException(""); |
||
683 | } catch (RasterBufferInvalidAccessException e) {
|
||
684 | throw new GridException(""); |
||
685 | 10740 | nacho | } |
686 | //Restauramos el reader que hab?a
|
||
687 | switchToInterpolationMethod(interp); |
||
688 | } |
||
689 | 12166 | bsanchez | |
690 | 10740 | nacho | /*
|
691 | * (non-Javadoc)
|
||
692 | * @see org.gvsig.fmap.grid.IWritableGrid#setNoDataValue(double)
|
||
693 | */
|
||
694 | public void setNoDataValue(double dNoDataValue){ |
||
695 | writer.setNoDataValue((float) dNoDataValue);
|
||
696 | } |
||
697 | 12166 | bsanchez | |
698 | 10740 | nacho | /*
|
699 | * (non-Javadoc)
|
||
700 | * @see org.gvsig.fmap.grid.IWritableGrid#setNoData(int, int)
|
||
701 | */
|
||
702 | public void setNoData(int x, int y){ |
||
703 | writer.setNoData(x,y); |
||
704 | } |
||
705 | 12166 | bsanchez | |
706 | public void ExportToArcViewASCIIFile(String sFilename){ |
||
707 | 10740 | nacho | try {
|
708 | writer.ExportToArcViewASCIIFile(sFilename); |
||
709 | } catch (NumberFormatException e) { |
||
710 | e.printStackTrace(); |
||
711 | } catch (IOException e) { |
||
712 | e.printStackTrace(); |
||
713 | } |
||
714 | } |
||
715 | 12166 | bsanchez | |
716 | 10740 | nacho | //************* Query Services *********************
|
717 | 12166 | bsanchez | |
718 | 10740 | nacho | /*
|
719 | * (non-Javadoc)
|
||
720 | * @see org.gvsig.fmap.grid.IQueryableGrid#isNoDataValue(double)
|
||
721 | */
|
||
722 | public boolean isNoDataValue(double noDataValue){ |
||
723 | return (reader.getNoDataValue() == noDataValue);
|
||
724 | } |
||
725 | 12166 | bsanchez | |
726 | 10740 | nacho | /*
|
727 | * (non-Javadoc)
|
||
728 | * @see org.gvsig.fmap.grid.IQueryableGrid#isInGrid(int, int)
|
||
729 | */
|
||
730 | public boolean isInGrid(int x, int y){ |
||
731 | return reader.isCellInGrid(x, y);
|
||
732 | } |
||
733 | 12166 | bsanchez | |
734 | 10740 | nacho | /*
|
735 | * (non-Javadoc)
|
||
736 | * @see org.gvsig.fmap.grid.IQueryableGrid#getCellSize()
|
||
737 | */
|
||
738 | public double getCellSize() { |
||
739 | return reader.getCellSize();
|
||
740 | } |
||
741 | 12166 | bsanchez | |
742 | 10740 | nacho | /*
|
743 | * (non-Javadoc)
|
||
744 | * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsByte(int, int)
|
||
745 | */
|
||
746 | 16590 | nbrodin | public byte getCellValueAsByte(int x, int y)throws GridException { |
747 | try {
|
||
748 | return reader.getCellValueAsByte(x, y);
|
||
749 | } catch (RasterBufferInvalidAccessException e) {
|
||
750 | throw new GridException("Position: (" + x + "," + y + ") not valid"); |
||
751 | } catch (RasterBufferInvalidException e) {
|
||
752 | throw new GridException("Buffer not valid"); |
||
753 | } |
||
754 | 10740 | nacho | } |
755 | 12166 | bsanchez | |
756 | 10740 | nacho | /*
|
757 | * (non-Javadoc)
|
||
758 | * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsShort(int, int)
|
||
759 | */
|
||
760 | 16590 | nbrodin | public short getCellValueAsShort(int x, int y)throws GridException { |
761 | try {
|
||
762 | return (short) reader.getCellValueAsShort(x, y); |
||
763 | } catch (RasterBufferInvalidAccessException e) {
|
||
764 | throw new GridException("Position: (" + x + "," + y + ") not valid"); |
||
765 | } catch (RasterBufferInvalidException e) {
|
||
766 | throw new GridException("Buffer not valid"); |
||
767 | } |
||
768 | 10740 | nacho | } |
769 | 12166 | bsanchez | |
770 | 10740 | nacho | /*
|
771 | * (non-Javadoc)
|
||
772 | * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsInt(int, int)
|
||
773 | */
|
||
774 | 16590 | nbrodin | public int getCellValueAsInt(int x, int y)throws GridException { |
775 | try {
|
||
776 | return (int) reader.getCellValueAsInt(x, y); |
||
777 | } catch (RasterBufferInvalidAccessException e) {
|
||
778 | throw new GridException("Position: (" + x + "," + y + ") not valid"); |
||
779 | } catch (RasterBufferInvalidException e) {
|
||
780 | throw new GridException("Buffer not valid"); |
||
781 | } |
||
782 | 10740 | nacho | } |
783 | |||
784 | /*
|
||
785 | * (non-Javadoc)
|
||
786 | * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsFloat(int, int)
|
||
787 | */
|
||
788 | 16590 | nbrodin | public float getCellValueAsFloat(int x, int y)throws GridException { |
789 | try {
|
||
790 | return reader.getCellValueAsFloat(x, y);
|
||
791 | } catch (RasterBufferInvalidAccessException e) {
|
||
792 | throw new GridException("Position: (" + x + "," + y + ") not valid"); |
||
793 | } catch (RasterBufferInvalidException e) {
|
||
794 | throw new GridException("Buffer not valid"); |
||
795 | } |
||
796 | 10740 | nacho | } |
797 | 12166 | bsanchez | |
798 | 10740 | nacho | /*
|
799 | * (non-Javadoc)
|
||
800 | * @see org.gvsig.fmap.grid.IQueryableGrid#getCellValueAsDouble(int, int)
|
||
801 | */
|
||
802 | 16590 | nbrodin | public double getCellValueAsDouble(int x, int y)throws GridException { |
803 | try {
|
||
804 | return (double) reader.getCellValueAsDouble(x, y); |
||
805 | } catch (RasterBufferInvalidAccessException e) {
|
||
806 | throw new GridException("Position: (" + x + "," + y + ") not valid"); |
||
807 | } catch (RasterBufferInvalidException e) {
|
||
808 | throw new GridException("Buffer not valid"); |
||
809 | } |
||
810 | 10740 | nacho | } |
811 | 12166 | bsanchez | |
812 | 10740 | nacho | /*
|
813 | * (non-Javadoc)
|
||
814 | * @see org.gvsig.fmap.grid.IQueryableGrid#getNoDataValue()
|
||
815 | */
|
||
816 | 16590 | nbrodin | public double getNoDataValue() { |
817 | 10740 | nacho | return (double) rasterBuf.getNoDataValue(); |
818 | } |
||
819 | 12166 | bsanchez | |
820 | 10740 | nacho | /*
|
821 | * (non-Javadoc)
|
||
822 | * @see org.gvsig.fmap.grid.IQueryableGrid#getMinValue()
|
||
823 | */
|
||
824 | 16590 | nbrodin | public double getMinValue() throws GridException { |
825 | 10740 | nacho | if (!statistic.isStatisticsCalculated())
|
826 | 16590 | nbrodin | statistic.calculateStatistics(); |
827 | 10740 | nacho | return statistic.getMin();
|
828 | } |
||
829 | 12166 | bsanchez | |
830 | 10740 | nacho | /*
|
831 | * (non-Javadoc)
|
||
832 | * @see org.gvsig.fmap.grid.IQueryableGrid#getMaxValue()
|
||
833 | */
|
||
834 | 16590 | nbrodin | public double getMaxValue() throws GridException { |
835 | 10740 | nacho | if (!statistic.isStatisticsCalculated())
|
836 | 16590 | nbrodin | statistic.calculateStatistics(); |
837 | 11837 | nacho | return statistic.getMax();
|
838 | 10740 | nacho | } |
839 | 12166 | bsanchez | |
840 | 10740 | nacho | /*
|
841 | * (non-Javadoc)
|
||
842 | * @see org.gvsig.fmap.grid.IQueryableGrid#getMeanValue()
|
||
843 | */
|
||
844 | 16590 | nbrodin | public double getMeanValue() throws GridException { |
845 | 10740 | nacho | if (!statistic.isStatisticsCalculated())
|
846 | 16590 | nbrodin | statistic.calculateStatistics(); |
847 | 11837 | nacho | return statistic.getMean();
|
848 | 10740 | nacho | } |
849 | 12166 | bsanchez | |
850 | 10740 | nacho | /*
|
851 | * (non-Javadoc)
|
||
852 | * @see org.gvsig.fmap.grid.IQueryableGrid#getVariance()
|
||
853 | */
|
||
854 | 16590 | nbrodin | public double getVariance() throws GridException { |
855 | 10740 | nacho | if (!statistic.isStatisticsCalculated())
|
856 | 16590 | nbrodin | statistic.calculateStatistics(); |
857 | 10740 | nacho | return statistic.getVariance();
|
858 | } |
||
859 | 12166 | bsanchez | |
860 | 10740 | nacho | /*
|
861 | * (non-Javadoc)
|
||
862 | * @see org.gvsig.fmap.grid.IQueryableGrid#setInterpolationMethod(int)
|
||
863 | */
|
||
864 | public void setInterpolationMethod(int iMethod){ |
||
865 | if (reader instanceof GridInterpolated) |
||
866 | ((GridInterpolated) reader).setInterpolationMethod(iMethod); |
||
867 | else{
|
||
868 | this.switchToInterpolationMethod(true); |
||
869 | ((GridInterpolated) reader).setInterpolationMethod(iMethod); |
||
870 | } |
||
871 | } |
||
872 | 12166 | bsanchez | |
873 | 10740 | nacho | /*
|
874 | * (non-Javadoc)
|
||
875 | * @see org.gvsig.fmap.grid.IQueryableGrid#getNX()
|
||
876 | */
|
||
877 | public int getNX(){ |
||
878 | return reader.getNX();
|
||
879 | } |
||
880 | 12166 | bsanchez | |
881 | 10740 | nacho | /*
|
882 | * (non-Javadoc)
|
||
883 | * @see org.gvsig.fmap.grid.IQueryableGrid#getNY()
|
||
884 | */
|
||
885 | public int getNY(){ |
||
886 | return reader.getNY();
|
||
887 | } |
||
888 | 12166 | bsanchez | |
889 | 10740 | nacho | /*
|
890 | * (non-Javadoc)
|
||
891 | * @see org.gvsig.fmap.grid.IQueryableGrid#getLayerNX()
|
||
892 | */
|
||
893 | public int getLayerNX(){ |
||
894 | return layerExtent.getNX();
|
||
895 | } |
||
896 | 12166 | bsanchez | |
897 | 10740 | nacho | /*
|
898 | * (non-Javadoc)
|
||
899 | * @see org.gvsig.fmap.grid.IQueryableGrid#getLayerNY()
|
||
900 | */
|
||
901 | public int getLayerNY(){ |
||
902 | return layerExtent.getNY();
|
||
903 | } |
||
904 | 12166 | bsanchez | |
905 | 10740 | nacho | public int getDataType(){ |
906 | return dataType;
|
||
907 | } |
||
908 | 12166 | bsanchez | |
909 | 18651 | nbrodin | private boolean getSubMatrix3x3(int x, int y, double subMatrix[]) |
910 | 16590 | nbrodin | throws GridException {
|
911 | 10740 | nacho | int i;
|
912 | int iDir;
|
||
913 | double z, z2;
|
||
914 | |||
915 | z = getCellValueAsDouble(x, y); |
||
916 | |||
917 | 18651 | nbrodin | if(isNoDataValue(z)) {
|
918 | 10740 | nacho | return false; |
919 | 18651 | nbrodin | } else {
|
920 | 10740 | nacho | //SubMatrix[4] = 0.0;
|
921 | 18651 | nbrodin | for(i = 0; i < 4; i++) { |
922 | 10740 | nacho | iDir = 2 * i;
|
923 | z2 = getCellValueAsDouble(x + m_iOffsetX[iDir], y + m_iOffsetY[iDir]); |
||
924 | if( !isNoDataValue(z2)){
|
||
925 | 18651 | nbrodin | subMatrix[i] = z2 - z; |
926 | } else {
|
||
927 | 10740 | nacho | z2 = getCellValueAsDouble(x + m_iOffsetX[(iDir + 4) % 8], y + m_iOffsetY[(iDir + 4) % 8]); |
928 | 18651 | nbrodin | if( !isNoDataValue(z2)) {
|
929 | subMatrix[i] = z - z2; |
||
930 | } else {
|
||
931 | subMatrix[i] = 0.0;
|
||
932 | 10740 | nacho | } |
933 | } |
||
934 | } |
||
935 | |||
936 | return true; |
||
937 | } |
||
938 | } |
||
939 | 12166 | bsanchez | |
940 | 18651 | nbrodin | /*
|
941 | * (non-Javadoc)
|
||
942 | * @see org.gvsig.raster.grid.IQueryableGrid#getSlope(int, int)
|
||
943 | */
|
||
944 | 16590 | nbrodin | public double getSlope(int x, int y)throws GridException { |
945 | 10740 | nacho | double zm[], G, H; |
946 | |||
947 | zm = new double[4]; |
||
948 | 12166 | bsanchez | |
949 | 16590 | nbrodin | try {
|
950 | 18651 | nbrodin | if( getSubMatrix3x3(x, y, zm) ) {
|
951 | G = (zm[0] - zm[2]) / _2DX; |
||
952 | H = (zm[1] - zm[3]) / _2DX; |
||
953 | return Math.atan(Math.sqrt(G * G + H * H)); |
||
954 | 16590 | nbrodin | } |
955 | 18651 | nbrodin | else {
|
956 | 16590 | nbrodin | return rasterBuf.getNoDataValue();
|
957 | } |
||
958 | } catch (GridException e) {
|
||
959 | throw new GridException("Problems accesing 3x3 submatrix"); |
||
960 | 10740 | nacho | } |
961 | } |
||
962 | 12166 | bsanchez | |
963 | 18651 | nbrodin | /*
|
964 | * (non-Javadoc)
|
||
965 | * @see org.gvsig.raster.grid.IQueryableGrid#getAspect(int, int)
|
||
966 | */
|
||
967 | 16590 | nbrodin | public double getAspect(int x, int y)throws GridException { |
968 | 10740 | nacho | double zm[], G, H, dAspect; |
969 | zm = new double[4]; |
||
970 | 12166 | bsanchez | |
971 | 16590 | nbrodin | try {
|
972 | 18651 | nbrodin | if( getSubMatrix3x3(x, y, zm) ) {
|
973 | G = (zm[0] - zm[2]) / _2DX; |
||
974 | H = (zm[1] - zm[3]) / _2DX; |
||
975 | if(G != 0.0) |
||
976 | 16590 | nbrodin | dAspect = DEG_180_IN_RAD + Math.atan2(H, G);
|
977 | else
|
||
978 | dAspect = H > 0.0 ? DEG_270_IN_RAD : (H < 0.0 ? DEG_90_IN_RAD : -1.0); |
||
979 | return dAspect;
|
||
980 | } |
||
981 | 10740 | nacho | else
|
982 | 16590 | nbrodin | return rasterBuf.getNoDataValue();
|
983 | } catch (GridException e) {
|
||
984 | throw new GridException("Problems accesing 3x3 submatrix"); |
||
985 | 10740 | nacho | } |
986 | } |
||
987 | 12166 | bsanchez | |
988 | 10740 | nacho | public static int getXOffsetInDir(int iDir){ |
989 | return m_iOffsetX[iDir];
|
||
990 | } |
||
991 | 12166 | bsanchez | |
992 | 10740 | nacho | public static int getYOffsetInDir(int iDir){ |
993 | return m_iOffsetY[iDir];
|
||
994 | } |
||
995 | 12166 | bsanchez | |
996 | 10740 | nacho | public double getDistToNeighborInDir(int iDir){ |
997 | return m_dDist[iDir];
|
||
998 | } |
||
999 | 12166 | bsanchez | |
1000 | 10740 | nacho | public static double getUnitDistToNeighborInDir(int iDir){ |
1001 | return( (iDir % 2 != 0) ? Math.sqrt(2.0) : 1.0 ); |
||
1002 | } |
||
1003 | 12166 | bsanchez | |
1004 | 10740 | nacho | /*
|
1005 | * (non-Javadoc)
|
||
1006 | * @see org.gvsig.fmap.grid.IQueryableGrid#getDirToNextDownslopeCell(int, int)
|
||
1007 | */
|
||
1008 | 16590 | nbrodin | public int getDirToNextDownslopeCell(int x, int y)throws GridException{ |
1009 | 10740 | nacho | return getDirToNextDownslopeCell(x, y, true); |
1010 | } |
||
1011 | 12166 | bsanchez | |
1012 | 10740 | nacho | /*
|
1013 | * (non-Javadoc)
|
||
1014 | * @see org.gvsig.fmap.grid.IQueryableGrid#getDirToNextDownslopeCell(int, int, boolean)
|
||
1015 | */
|
||
1016 | public int getDirToNextDownslopeCell(int x, int y, boolean bForceDirToNoDataCell) |
||
1017 | 16590 | nbrodin | throws GridException{
|
1018 | 12166 | bsanchez | |
1019 | 10740 | nacho | int i, iDir;
|
1020 | double z, z2, dSlope, dMaxSlope;
|
||
1021 | |||
1022 | z = getCellValueAsDouble(x, y); |
||
1023 | |||
1024 | if(isNoDataValue(z)){
|
||
1025 | return -1; |
||
1026 | } |
||
1027 | |||
1028 | dMaxSlope = 0.0;
|
||
1029 | 16590 | nbrodin | for(iDir=-1, i=0; i<8; i++) { |
1030 | 10740 | nacho | z2 = getCellValueAsDouble(x + m_iOffsetX[i], y + m_iOffsetY[i]); |
1031 | 16590 | nbrodin | if(isNoDataValue(z2)) {
|
1032 | if (bForceDirToNoDataCell) {
|
||
1033 | 10740 | nacho | return i;
|
1034 | } |
||
1035 | 16590 | nbrodin | else {
|
1036 | 10740 | nacho | return -1; |
1037 | } |
||
1038 | } |
||
1039 | 16590 | nbrodin | else {
|
1040 | 10740 | nacho | dSlope = (z - z2) / getDistToNeighborInDir(i); |
1041 | 16590 | nbrodin | if( dSlope > dMaxSlope ) {
|
1042 | 10740 | nacho | iDir = i; |
1043 | dMaxSlope = dSlope; |
||
1044 | } |
||
1045 | } |
||
1046 | } |
||
1047 | return iDir;
|
||
1048 | } |
||
1049 | 12166 | bsanchez | |
1050 | 16590 | nbrodin | public GridCell[] getSortedArrayOfCells()throws GridException{ |
1051 | 10740 | nacho | int i;
|
1052 | int iX,iY;
|
||
1053 | int iNX = getNX();
|
||
1054 | int iCells = getNX() * getNY();
|
||
1055 | GridCell [] cells = null; |
||
1056 | GridCell cell = null;
|
||
1057 | 12166 | bsanchez | |
1058 | 10740 | nacho | cells = new GridCell[iCells];
|
1059 | 12166 | bsanchez | |
1060 | 16590 | nbrodin | for (i = 0; i < iCells; i++) { |
1061 | 10740 | nacho | iX = i % iNX; |
1062 | iY = i / iNX; |
||
1063 | switch(getDataType()){
|
||
1064 | case IBuffer.TYPE_BYTE: cell = new GridCell(iX, iY, getCellValueAsByte(iX, iY)); break; |
||
1065 | case IBuffer.TYPE_SHORT: cell = new GridCell(iX, iY, getCellValueAsShort(iX, iY)); break; |
||
1066 | case IBuffer.TYPE_INT: cell = new GridCell(iX, iY, getCellValueAsInt(iX, iY)); break; |
||
1067 | case IBuffer.TYPE_FLOAT: cell = new GridCell(iX, iY, getCellValueAsFloat(iX, iY)); break; |
||
1068 | case IBuffer.TYPE_DOUBLE: cell = new GridCell(iX, iY, getCellValueAsDouble(iX, iY)); break; |
||
1069 | } |
||
1070 | 12166 | bsanchez | |
1071 | 10740 | nacho | cells[i] = cell; |
1072 | } |
||
1073 | |||
1074 | Arrays.sort(cells);
|
||
1075 | 12166 | bsanchez | |
1076 | 10740 | nacho | return cells;
|
1077 | } |
||
1078 | |||
1079 | /**
|
||
1080 | * Obtiene la paleta para el caso de un MDT. Esta funci?n evita el tener que obtener
|
||
1081 | * un array de paletas y buscar en el cuando se trata de un caso simple de MDT de una
|
||
1082 | * sola banda. Comprueba que se trata de un raster monobanda con paleta asociada antes
|
||
1083 | * de devolverla.
|
||
1084 | * @return GridPalette asociada al Grid
|
||
1085 | */
|
||
1086 | 16590 | nbrodin | public GridPalette getMDTPalette() {
|
1087 | 12166 | bsanchez | if( rasterBuf != null && |
1088 | 10740 | nacho | rasterBuf.getBandCount() == 1 &&
|
1089 | palette != null)
|
||
1090 | return palette[0]; |
||
1091 | return null; |
||
1092 | } |
||
1093 | 12166 | bsanchez | |
1094 | 10740 | nacho | /**
|
1095 | * Obtiene la lista de paletas asociadas al grid
|
||
1096 | * @return Lista de objetos GridPalette
|
||
1097 | */
|
||
1098 | public GridPalette[] getPalettes() { |
||
1099 | return palette;
|
||
1100 | } |
||
1101 | |||
1102 | /**
|
||
1103 | 12166 | bsanchez | * Obtiene el n?mero de bandas del grid o 0 si no tiene buffer de
|
1104 | 11961 | nacho | * datos asociado.
|
1105 | * @return N?mero de bandas
|
||
1106 | */
|
||
1107 | public int getBandCount() { |
||
1108 | if(rasterBuf != null) |
||
1109 | return rasterBuf.getBandCount();
|
||
1110 | return 0; |
||
1111 | } |
||
1112 | 12166 | bsanchez | |
1113 | 11961 | nacho | /**
|
1114 | 10740 | nacho | * Asigna la lista de paletas asociadas al grid
|
1115 | * @param palette Lista de objetos GridPalette
|
||
1116 | */
|
||
1117 | public void setPalettes(GridPalette[] palette) { |
||
1118 | this.palette = palette;
|
||
1119 | } |
||
1120 | 12166 | bsanchez | |
1121 | 10740 | nacho | /**
|
1122 | * Obtiene la lista de filtros.
|
||
1123 | * @return Lista de filtros.
|
||
1124 | */
|
||
1125 | public RasterFilterList getFilterList(){
|
||
1126 | return filterList;
|
||
1127 | } |
||
1128 | 12166 | bsanchez | |
1129 | 10740 | nacho | /**
|
1130 | 12166 | bsanchez | * Asigna la lista de filtros
|
1131 | 10740 | nacho | * @param filterList
|
1132 | */
|
||
1133 | 16590 | nbrodin | public void setFilterList(RasterFilterList filterList) { |
1134 | 10740 | nacho | this.filterList = filterList;
|
1135 | } |
||
1136 | 12166 | bsanchez | |
1137 | 10740 | nacho | /**
|
1138 | *Aplica la lista de filtros sobre el buffer
|
||
1139 | 12325 | bsanchez | * @throws InterruptedException
|
1140 | 10740 | nacho | */
|
1141 | 13409 | nacho | public void applyFilters() throws InterruptedException { |
1142 | 12180 | bsanchez | if (filterList == null) |
1143 | 10740 | nacho | return;
|
1144 | 12180 | bsanchez | |
1145 | 10740 | nacho | filterList.setInitRasterBuf(rasterBuf); |
1146 | 18358 | bsanchez | |
1147 | filterList.getEnv().put("GridExtent", getGridExtent());
|
||
1148 | filterList.getEnv().put("WindowExtent", getWindowExtent());
|
||
1149 | |||
1150 | 10740 | nacho | filterList.execute(); |
1151 | 12180 | bsanchez | rasterBuf = (RasterBuffer) filterList.getResult(); |
1152 | 10740 | nacho | dataType = rasterBuf.getDataType(); |
1153 | } |
||
1154 | 12166 | bsanchez | |
1155 | 10740 | nacho | /**
|
1156 | * Obtiene el buffer de datos del grid
|
||
1157 | * @return RasterBuf
|
||
1158 | */
|
||
1159 | public IBuffer getRasterBuf() {
|
||
1160 | return rasterBuf;
|
||
1161 | } |
||
1162 | 12166 | bsanchez | |
1163 | 10740 | nacho | /**
|
1164 | * Asigna la banda sobre la que se realizan las operaciones. Por defecto es la banda 0
|
||
1165 | * con lo que para el uso de MDTs no habr? que modificar este valor.
|
||
1166 | * @param band Banda sobre la que se realizan las operaciones.
|
||
1167 | */
|
||
1168 | 16590 | nbrodin | public void setBandToOperate(int band) { |
1169 | 10740 | nacho | if(writer != null) |
1170 | writer.setBandToOperate(band); |
||
1171 | if(reader != null) |
||
1172 | reader.setBandToOperate(band); |
||
1173 | 11961 | nacho | if(statistic != null) |
1174 | statistic.setBandToOperate(band); |
||
1175 | 10740 | nacho | } |
1176 | 12166 | bsanchez | |
1177 | 10740 | nacho | /**
|
1178 | * Obtiene el extent de la ventana seleccionada para petici?n de datos
|
||
1179 | * @return GridExtent
|
||
1180 | */
|
||
1181 | 16590 | nbrodin | public GridExtent getWindowExtent() {
|
1182 | 10740 | nacho | return windowExtent;
|
1183 | } |
||
1184 | 12166 | bsanchez | |
1185 | 10740 | nacho | /**
|
1186 | * Obtiene el extent del grid para petici?n de datos
|
||
1187 | * @return GridExtent
|
||
1188 | */
|
||
1189 | 16590 | nbrodin | public GridExtent getGridExtent() {
|
1190 | 10740 | nacho | return layerExtent;
|
1191 | } |
||
1192 | } |