root / branches / v10 / libraries / libCq_CMS_praster / src / org / cresques / io / data / Grid.java @ 10938
History | View | Annotate | Download (12.7 KB)
1 |
/* 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.cresques.io.data; |
20 |
|
21 |
import java.awt.geom.Point2D; |
22 |
|
23 |
import org.cresques.io.GeoRasterFile; |
24 |
import org.cresques.io.datastruct.Histogram; |
25 |
import org.cresques.io.datastruct.Statistic; |
26 |
import org.cresques.io.exceptions.FileFoundInListException; |
27 |
import org.cresques.px.Extent; |
28 |
import org.cresques.px.PxRaster; |
29 |
import org.cresques.util.Utilities; |
30 |
|
31 |
public class Grid implements IQueryableRaster, IWritableRaster { |
32 |
|
33 |
private GeoRasterMultiFile grmf = new GeoRasterMultiFile(null); |
34 |
private RasterBuf rasterBuf = null; |
35 |
private Histogram histogram = null; |
36 |
private PxRaster pxRaster = null; |
37 |
private Statistic stats = null; |
38 |
protected int width = 0; |
39 |
protected int height = 0; |
40 |
protected Extent dataExtent = null; |
41 |
|
42 |
private double m_dNoDataValue = -99999; //TODO:temporal |
43 |
|
44 |
/**
|
45 |
* Constructor
|
46 |
*/
|
47 |
public Grid(){}
|
48 |
|
49 |
/**
|
50 |
* Constructor
|
51 |
* @param grf Lista de geoRasterFile
|
52 |
*/
|
53 |
public Grid(GeoRasterFile[] grf){ |
54 |
for(int i = 0; i< grf.length; i++) |
55 |
addFile(grf[i]); |
56 |
} |
57 |
|
58 |
/**
|
59 |
* Constructor
|
60 |
* @param grf GeoRasterFile
|
61 |
*/
|
62 |
public Grid(GeoRasterFile grf){
|
63 |
addFile(grf); |
64 |
} |
65 |
|
66 |
/**
|
67 |
* Asigna el renderizador para el calculo de histogramas a partir
|
68 |
* de la visualizaci?n.
|
69 |
* @param pxR Renderizador.
|
70 |
*/
|
71 |
public void addRenderizer(PxRaster pxR){ |
72 |
this.pxRaster = pxR;
|
73 |
this.stats = pxR.stats;
|
74 |
this.getHistogram().addRenderizer(pxR);
|
75 |
} |
76 |
|
77 |
/**
|
78 |
* A?ade un GeoRasterFile al Grid
|
79 |
* @param grf GeoRasterFile a a?adir
|
80 |
*/
|
81 |
public void addFile(GeoRasterFile grf){ |
82 |
try{
|
83 |
grmf.addFile(grf); |
84 |
width = grf.getWidth(); |
85 |
height = grf.getHeight(); |
86 |
}catch(FileFoundInListException e){
|
87 |
//El fichero est? en la lista por lo que no lo a?adimos
|
88 |
} |
89 |
} |
90 |
|
91 |
/**
|
92 |
* Elimina un GeoRasterFile del Grid
|
93 |
* @param grf GeoRasterFile a eliminar
|
94 |
*/
|
95 |
public void removeFile(GeoRasterFile grf){ |
96 |
grmf.removeFile(grf); |
97 |
} |
98 |
|
99 |
/**
|
100 |
* Obtiene la estructura que contiene la lista de ficheros del Grid
|
101 |
* @return GeoRasterMultiFile
|
102 |
*/
|
103 |
public GeoRasterMultiFile getGeoRasterMultiFile(){
|
104 |
return grmf;
|
105 |
} |
106 |
|
107 |
/**
|
108 |
* Libera el buffer de memoria
|
109 |
*/
|
110 |
public void free(){ |
111 |
rasterBuf = null;
|
112 |
System.gc();
|
113 |
} |
114 |
|
115 |
/**
|
116 |
* Resetea la asignaci?n de dibujado de las bandas de la imagen
|
117 |
* sobre el DataImage cuando se hace un update para esta banda.
|
118 |
*/
|
119 |
public void clearDrawableBand(){ |
120 |
for(int i = 0; i< grmf.getFileCount(); i++) |
121 |
grmf.getBands().clearDrawableBand(); |
122 |
} |
123 |
|
124 |
/**
|
125 |
* Para este GeoRasterFile asigna que bandas se pintaran
|
126 |
* sobre el RasterBuf cuando se haga un update. Cada posici?n del vector es una banda
|
127 |
* del rasterBuf y el contenido de esa posici?n es la banda de la imagen que se dibujar?
|
128 |
* sobre ese RasterBuf.
|
129 |
* @param drawableBands .
|
130 |
*/
|
131 |
public void addDrawableBands(int[] drawableBands){ |
132 |
for(int i = 0; i< drawableBands.length; i++) |
133 |
grmf.getBands().addDrawableBand(i, drawableBands[i]); |
134 |
} |
135 |
|
136 |
/**
|
137 |
* Asigna el ?rea de interes en coordenadas del mundo real. Si las coordenadas exceden del tama?o de la imagen
|
138 |
* estas coordenadas son ajustadas el extent.
|
139 |
* @param x Coordenada X, esquina superior izquierda
|
140 |
* @param y Coordenada Y, esquina superior izquierda
|
141 |
* @param w Ancho del ?rea
|
142 |
* @param h Alto del ?rea
|
143 |
* @throws ArrayIndexOutOfBoundsException
|
144 |
*/
|
145 |
public void setAreaOfInterest(double x, double y, double w, double h) throws ArrayIndexOutOfBoundsException{ |
146 |
dataExtent = new Extent(x, y, x + w, y - h);
|
147 |
Extent fitExtent = Utilities.calculateAdjustedView(dataExtent, grmf.getExtent());
|
148 |
|
149 |
rasterBuf = grmf.getWindowRaster(fitExtent.minX(), fitExtent.maxY(), fitExtent.width(), fitExtent.height()); |
150 |
|
151 |
if(!Utilities.isInside(dataExtent, fitExtent)){ |
152 |
double distWcX = Math.abs(fitExtent.getMin().getX() - dataExtent.getMin().getX()); |
153 |
double distWcY = Math.abs(fitExtent.getMax().getY() - dataExtent.getMax().getY()); |
154 |
double mtsPixelX = grmf.getExtent().width() / grmf.getWidth();
|
155 |
double mtsPixelY = grmf.getExtent().width() / grmf.getWidth();
|
156 |
double pxX = distWcX / mtsPixelX;
|
157 |
double pxY = distWcY / mtsPixelY;
|
158 |
|
159 |
double wPx = dataExtent.width() / mtsPixelX;
|
160 |
double hPx = dataExtent.height() / mtsPixelY;
|
161 |
|
162 |
RasterBuf buf = new RasterBuf(rasterBuf.getDataType(), (int)wPx, (int)hPx, rasterBuf.getBandCount(), true); |
163 |
buf.setToNoData(); |
164 |
|
165 |
for (int iBand = 0; iBand < rasterBuf.getBandCount(); iBand++) |
166 |
for (int row = 0; row < rasterBuf.getHeight(); row++) |
167 |
for (int col = 0; col < rasterBuf.getWidth(); col++) |
168 |
buf.setElemByte((int)(row + pxY), (int)(col + pxX), |
169 |
iBand, |
170 |
rasterBuf.getElemByte(row, col, iBand)); |
171 |
rasterBuf = buf; |
172 |
} |
173 |
} |
174 |
|
175 |
/**
|
176 |
* Asigna el ?rea de interes en coordenadas pixel. Si las coordenadas exceden del tama?o de la imagen
|
177 |
* lanza una excepci?n.
|
178 |
* @param x Coordenada X, esquina superior izquierda
|
179 |
* @param y Coordenada Y, esquina superior izquierda
|
180 |
* @param w Ancho del ?rea
|
181 |
* @param h Alto del ?rea
|
182 |
* @throws ArrayIndexOutOfBoundsException
|
183 |
*/
|
184 |
public void setAreaOfInterest(int x, int y, int w, int h) throws ArrayIndexOutOfBoundsException{ |
185 |
if((x + w) > getWidth() || (y + h) > getHeight())
|
186 |
throw new ArrayIndexOutOfBoundsException("Out of Image"); |
187 |
|
188 |
Point2D begin = grmf.rasterToWorld(new Point2D.Double(x, y)); |
189 |
Point2D end = grmf.rasterToWorld(new Point2D.Double(x + w, y + h)); |
190 |
if(begin != null && end != null) |
191 |
dataExtent = new Extent(begin, end);
|
192 |
|
193 |
rasterBuf = grmf.getWindowRaster(x, y, w, h); |
194 |
//width = rasterBuf.getWidth();
|
195 |
//height = rasterBuf.getHeight();
|
196 |
} |
197 |
|
198 |
/*
|
199 |
* (non-Javadoc)
|
200 |
* @see org.gvsig.fmap.dataaccess.IQueryableRaster#setAreaOfInterest(double, double, double, double, int, int)
|
201 |
*/
|
202 |
public int[] setAreaOfInterest(double minX, double minY, double maxX, double maxY, int bufWidth, int bufHeight) throws ArrayIndexOutOfBoundsException{ |
203 |
//TODO: VALIDACI?N: Comprobaci?n de exceso de memoria reservada
|
204 |
dataExtent = new Extent(minX, minY, maxX, maxY);
|
205 |
Extent fitExtent = Utilities.calculateAdjustedView(dataExtent, grmf.getExtent());
|
206 |
|
207 |
//Caso en que la extensi?n pedida SI se sale fuera del ?rea del raster
|
208 |
//El pixel a medias entre NoData y datos se pone NoData
|
209 |
if(!Utilities.isInside(dataExtent, fitExtent)){ |
210 |
//Upper Left
|
211 |
double distWcX = Math.abs(fitExtent.getMin().getX() - dataExtent.getMin().getX()); |
212 |
double distWcY = Math.abs(fitExtent.getMax().getY() - dataExtent.getMax().getY()); |
213 |
//Pixel inicial del buffer donde se empieza a dibujar. Redondeamos por arriba pq lo que sobra se pone NoData
|
214 |
double initPxX = Math.ceil((distWcX * bufWidth) / (Math.abs(maxX - minX))); |
215 |
double initPxY = Math.ceil((distWcY * bufHeight) / (Math.abs(maxY - minY))); |
216 |
//Obtenemos la coordenada real para el pixel inicial
|
217 |
double wcXInit = (initPxX * (Math.abs(maxX - minX))) / bufWidth; |
218 |
double wcYInit = (initPxY * (Math.abs(maxY - minY))) / bufHeight; |
219 |
|
220 |
//Lower Right
|
221 |
distWcX = Math.abs(fitExtent.getMax().getX() - dataExtent.getMin().getX());
|
222 |
distWcY = Math.abs(fitExtent.getMin().getY() - dataExtent.getMax().getY());
|
223 |
//Pixel final del buffer donde se dibuja. Redondeamos por abajo pq lo que sobra se pone NoData
|
224 |
double endPxX = Math.floor((distWcX * bufWidth) / (Math.abs(maxX - minX))); |
225 |
double endPxY = Math.floor((distWcY * bufHeight) / (Math.abs(maxY - minY))); |
226 |
//Obtenemos la coordenada real para el pixel
|
227 |
double wcXEnd = (endPxX * (Math.abs(maxX - minX))) / bufWidth; |
228 |
double wcYEnd = (endPxY * (Math.abs(maxY - minY))) / bufHeight; |
229 |
|
230 |
rasterBuf = grmf.getWindowRaster(wcXInit, wcYInit, wcXEnd, wcYEnd, (int)Math.abs(endPxX - initPxX), (int)Math.abs(endPxY - initPxY)); |
231 |
|
232 |
RasterBuf buf = new RasterBuf(rasterBuf.getDataType(), bufWidth, bufHeight, rasterBuf.getBandCount(), true); |
233 |
buf.setToNoData(); |
234 |
for (int iBand = 0; iBand < rasterBuf.getBandCount(); iBand++) |
235 |
for (int row = 0; row < rasterBuf.getHeight(); row++) |
236 |
for (int col = 0; col < rasterBuf.getWidth(); col++) |
237 |
buf.setElemByte((int)(row + initPxY), (int)(col + initPxX), |
238 |
iBand, |
239 |
rasterBuf.getElemByte(row, col, iBand)); |
240 |
rasterBuf = buf; |
241 |
return null; |
242 |
|
243 |
} |
244 |
|
245 |
//Caso en que la extensi?n pedida NO se sale fuera del ?rea del raster
|
246 |
rasterBuf = grmf.getWindowRaster(fitExtent.minX(), fitExtent.maxY(), fitExtent.width(), fitExtent.height(), bufWidth, bufHeight); |
247 |
return null; |
248 |
} |
249 |
|
250 |
/*
|
251 |
* (non-Javadoc)
|
252 |
* @see org.gvsig.fmap.dataaccess.IQueryableRaster#setAreaOfInterest(double, double, double, double)
|
253 |
*/
|
254 |
public void setAreaOfInterestWithNoData(double x, double y, double w, double h) throws ArrayIndexOutOfBoundsException{ |
255 |
dataExtent = new Extent(x, y, x + w, y - h);
|
256 |
rasterBuf = grmf.getWindowRasterWithNoData(dataExtent.getMin().getX(), dataExtent.getMax().getY(), w, h); |
257 |
} |
258 |
|
259 |
/**
|
260 |
* Ajusta el ?rea del grid a un ancho y un alto dado en pixeles. Este ajuste se har?
|
261 |
* en relaci?n a un m?todo de interpolaci?n definido en el par?metro.
|
262 |
* @param w Ancho de la nueva imagen.
|
263 |
* @param h Alto de la nueva imagen.
|
264 |
* @param interpolation M?todo de interpolaci?n que se usar? en el ajuste.
|
265 |
* @param bands Bandas que se desea en el nuevo RasterBuf ajustado. Si este par?metro es null
|
266 |
* se usar?n todas las bandas de la imagen.
|
267 |
*/
|
268 |
public void setAdjustedWindow(int w, int h, int interpolationMethod, int[] bands){ |
269 |
if(w == width && h == height)
|
270 |
return;
|
271 |
switch(interpolationMethod){
|
272 |
case IQueryableRaster.INTERPOLATION_NearestNeighbour:
|
273 |
rasterBuf = rasterBuf.adjustRasterNearestNeighbour(w, h, bands); |
274 |
break;
|
275 |
case IQueryableRaster.INTERPOLATION_Bilinear:
|
276 |
System.out.println("Method not implemented yet"); |
277 |
break;
|
278 |
case IQueryableRaster.INTERPOLATION_InverseDistance:
|
279 |
System.out.println("Method not implemented yet"); |
280 |
break;
|
281 |
case IQueryableRaster.INTERPOLATION_BicubicSpline:
|
282 |
System.out.println("Method not implemented yet"); |
283 |
break;
|
284 |
case IQueryableRaster.INTERPOLATION_BSpline:
|
285 |
System.out.println("Method not implemented yet"); |
286 |
break;
|
287 |
} |
288 |
//width = rasterBuf.getWidth();
|
289 |
//height = rasterBuf.getHeight();
|
290 |
} |
291 |
|
292 |
public RasterBuf getData(int x, int y, int w, int h) { |
293 |
|
294 |
return null; |
295 |
} |
296 |
|
297 |
/**
|
298 |
* Obtiene el tipo de datos del grid.
|
299 |
* @return entero que representa el tipo de dato de las celdas del grid.
|
300 |
*/
|
301 |
public int getDataType() { |
302 |
return grmf.getDataType()[0]; |
303 |
} |
304 |
|
305 |
/**
|
306 |
* Obtiene la altura del grid.
|
307 |
* @return altura en celdas del grid.
|
308 |
*/
|
309 |
public int getHeight() { |
310 |
return height;
|
311 |
} |
312 |
|
313 |
/**
|
314 |
* Obtiene la anchura del grid.
|
315 |
* @return anchura en celdas del grid.
|
316 |
*/
|
317 |
public int getWidth() { |
318 |
return width;
|
319 |
} |
320 |
|
321 |
public double getXCellSize() { |
322 |
|
323 |
//TODO:remove this
|
324 |
Extent e = grmf.getExtent(); |
325 |
double dCellsize = (e.getMax().getX() - e.getMin().getX() ) / (double) width; |
326 |
|
327 |
return dCellsize;
|
328 |
|
329 |
} |
330 |
|
331 |
public double getYCellSize() { |
332 |
|
333 |
return 0; |
334 |
} |
335 |
|
336 |
public boolean isNoData(int x, int y, int band) { |
337 |
|
338 |
return false; |
339 |
|
340 |
} |
341 |
|
342 |
public void setNoDataValue(double dNoDataValue){ |
343 |
|
344 |
//TODO:temporal
|
345 |
m_dNoDataValue = dNoDataValue; |
346 |
} |
347 |
|
348 |
public double getNoDataValue(){ |
349 |
|
350 |
return m_dNoDataValue;
|
351 |
|
352 |
} |
353 |
|
354 |
public void setDataType(int dt) { |
355 |
|
356 |
} |
357 |
|
358 |
public int getBandCount(){ |
359 |
if(rasterBuf != null) |
360 |
return rasterBuf.getBandCount();
|
361 |
else
|
362 |
return this.getGeoRasterMultiFile().getBandCount(); |
363 |
} |
364 |
|
365 |
/**
|
366 |
* Obtiene el buffer de datos del grid
|
367 |
* @return RasterBuf
|
368 |
*/
|
369 |
public RasterBuf getRasterBuf() {
|
370 |
return rasterBuf;
|
371 |
} |
372 |
|
373 |
/**
|
374 |
* Obtiene el objeto histograma asociado al grid
|
375 |
* @return
|
376 |
*/
|
377 |
public Histogram getHistogram() {
|
378 |
if(histogram == null) |
379 |
histogram = new Histogram(this); |
380 |
|
381 |
return histogram;
|
382 |
} |
383 |
|
384 |
/**
|
385 |
* Obtiene la clase para estadisticas
|
386 |
* @return
|
387 |
*/
|
388 |
public Statistic getStatistic() {
|
389 |
return stats;
|
390 |
} |
391 |
|
392 |
/**
|
393 |
* Evalua si una coordenada pixel cae dentro de la imagen.
|
394 |
* @param x coordenada pixel X
|
395 |
* @param y coordenada pixel Y
|
396 |
*/
|
397 |
public boolean isPixelInGrid(int x, int y){ |
398 |
return (x >= 0 && y >= 0 && x <= getWidth() && y <= getHeight()); |
399 |
} |
400 |
|
401 |
/**
|
402 |
* Extent de la fuente de datos
|
403 |
*/
|
404 |
public Extent getExtent(){
|
405 |
return grmf.getExtent();
|
406 |
} |
407 |
|
408 |
/**
|
409 |
* Extent del grid
|
410 |
*/
|
411 |
public Extent getDataExtent(){
|
412 |
return dataExtent;
|
413 |
} |
414 |
} |
415 |
|