root / trunk / libraries / libCq_CMS_praster / src / org / cresques / io / data / GeoRasterMultiFile.java @ 8026
History | View | Annotate | Download (10.4 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.io.File; |
22 |
import java.util.ArrayList; |
23 |
|
24 |
import org.cresques.io.GeoRasterFile; |
25 |
import org.cresques.io.exceptions.BandFoundInListException; |
26 |
import org.cresques.io.exceptions.FileFoundInListException; |
27 |
import org.cresques.io.exceptions.NotSupportedExtensionException; |
28 |
import org.cresques.px.Extent; |
29 |
|
30 |
/**
|
31 |
* Clase que representa una imagen de raster georreferenciada formada por varias
|
32 |
* imagenes de disco que tienen la misma extensi?n. Contiene fincionalidades para
|
33 |
* abrir ficheros, gestionar el extent, pintar el raster sobre un DataImage con
|
34 |
* su gesti?n de bandas correspondiente.
|
35 |
*
|
36 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
37 |
*
|
38 |
*/
|
39 |
public class GeoRasterMultiFile{ |
40 |
//File list
|
41 |
private ArrayList files = new ArrayList(); |
42 |
private String name = null; |
43 |
//Band list
|
44 |
private BandList bandList = new BandList(); |
45 |
|
46 |
public GeoRasterMultiFile(String name){ |
47 |
this.name = name;
|
48 |
} |
49 |
|
50 |
/**
|
51 |
* Add a file to the list.
|
52 |
* @param f file to add.
|
53 |
*/
|
54 |
public void addFile(GeoRasterFile f)throws FileFoundInListException{ |
55 |
if(findFile(f))
|
56 |
throw new FileFoundInListException("The file already is in list."); |
57 |
files.add(f); |
58 |
addBands(f); |
59 |
} |
60 |
|
61 |
/**
|
62 |
* A?ade un fichero a la lista a partir de su nombre
|
63 |
* @param f fichero a a?adir.
|
64 |
*/
|
65 |
public void addFile(String fileName)throws FileFoundInListException, NotSupportedExtensionException{ |
66 |
if(findFile(fileName))
|
67 |
throw new FileFoundInListException("The file already is in list."); |
68 |
GeoRasterFile f = GeoRasterFile.openFile(null, fileName);
|
69 |
files.add(f); |
70 |
addBands(f); |
71 |
} |
72 |
|
73 |
/**
|
74 |
* A?ade el fichero a lista de georrasterfiles y sus bandas a la lista de bandas
|
75 |
* @param grf
|
76 |
*/
|
77 |
private void addBands(GeoRasterFile grf){ |
78 |
if(grf == null) |
79 |
return;
|
80 |
|
81 |
int dataType = grf.getDataType();
|
82 |
for(int i = 0; i < grf.getBandCount();i++){ |
83 |
try{
|
84 |
Band band = new Band(grf.getName(), i, dataType);
|
85 |
bandList.addBand(band, i); |
86 |
}catch(BandFoundInListException ex){
|
87 |
//No a?adimos la banda
|
88 |
} |
89 |
} |
90 |
} |
91 |
|
92 |
/**
|
93 |
* Elimina un fichero a la lista a partir de su nombre
|
94 |
* @param fileName Nombre del fichero a eliminar.
|
95 |
*/
|
96 |
public void removeFile(String fileName){ |
97 |
for(int i=0;i<files.size();i++){ |
98 |
if(((GeoRasterFile)files.get(i)).getName().equals(fileName)){
|
99 |
files.remove(i); |
100 |
bandList.removeBands(fileName); |
101 |
return;
|
102 |
} |
103 |
} |
104 |
} |
105 |
|
106 |
/**
|
107 |
* Elimina un fichero a la lista
|
108 |
* @param file Fichero a eliminar
|
109 |
*/
|
110 |
public void removeFile(GeoRasterFile file){ |
111 |
for(int i=0;i<files.size();i++){ |
112 |
if(((GeoRasterFile)files.get(i)).getName().equals(file.getName())){
|
113 |
files.remove(i); |
114 |
bandList.removeBands(file.getName()); |
115 |
return;
|
116 |
} |
117 |
} |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Obtiene el n?mero de ficheros en la lista
|
122 |
* @return integer.
|
123 |
*/
|
124 |
public int getFileCount(){ |
125 |
return files.size();
|
126 |
} |
127 |
|
128 |
/**
|
129 |
* Encuentra un fichero en la lista.
|
130 |
* @param file Fichero b?scado.
|
131 |
* @return true si se ha hallado el fichero y false si no se
|
132 |
* ha encontrado
|
133 |
*/
|
134 |
public boolean findFile(GeoRasterFile file){ |
135 |
for(int i = 0;i<files.size();i++){ |
136 |
GeoRasterFile grf = (GeoRasterFile)files.get(i); |
137 |
if( grf.getName().equals(file.getName()))
|
138 |
return true; |
139 |
} |
140 |
return false; |
141 |
} |
142 |
|
143 |
/**
|
144 |
* Encuentra un fichero en la lista.
|
145 |
* @param file Fichero b?scado.
|
146 |
* @return true si se ha hallado el fichero y false si no se
|
147 |
* ha encontrado
|
148 |
*/
|
149 |
public boolean findFile(String fileName){ |
150 |
for(int i = 0;i<files.size();i++){ |
151 |
if(((GeoRasterFile)files.get(i)).getName().equals(fileName))
|
152 |
return true; |
153 |
} |
154 |
return false; |
155 |
} |
156 |
|
157 |
/**
|
158 |
* @see org.javaGeoRaster.io.GeoData
|
159 |
*/
|
160 |
public void close(){ |
161 |
for(int i = 0; i < files.size(); i++) |
162 |
((GeoRasterFile)files.get(i)).close(); |
163 |
} |
164 |
|
165 |
/**
|
166 |
* Obtiene en un array de String la lista de nombres de ficheros
|
167 |
* @return lista de nombres de los ficheros del GeoRasterMultiFile
|
168 |
*/
|
169 |
public String[] getNameFileStringList(){ |
170 |
String[] list = new String[files.size()]; |
171 |
for(int i = 0; i < files.size(); i++) |
172 |
list[i] = ((GeoRasterFile)files.get(i)).getName(); |
173 |
return list;
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* Obtiene una ventana de datos de la imagen a partir de coordenadas reales.
|
178 |
* No aplica supersampleo ni subsampleo sino que devuelve una matriz de igual tama?o a los
|
179 |
* pixeles de disco.
|
180 |
* @param x Posici?n X superior izquierda
|
181 |
* @param y Posici?n Y superior izquierda
|
182 |
* @param w Ancho en coordenadas reales
|
183 |
* @param h Alto en coordenadas reales
|
184 |
* @param bandList
|
185 |
* @return Buffer de datos
|
186 |
*/
|
187 |
public RasterBuf getWindowRaster(double x, double y, double w, double h) { |
188 |
|
189 |
Extent selectedExtent = new Extent(x, y, x + w, y + h);
|
190 |
|
191 |
int width = (int)((selectedExtent.width() * ((GeoRasterFile)files.get(0)).getWidth()) / ((GeoRasterFile)files.get(0)).getExtent().width()); |
192 |
int height = (int)((selectedExtent.height() * ((GeoRasterFile)files.get(0)).getHeight()) / ((GeoRasterFile)files.get(0)).getExtent().height()); |
193 |
|
194 |
RasterBuf raster = new RasterBuf(getDataType()[0], width, height, bandList.getDrawableBandsCount(), false); |
195 |
|
196 |
for(int i = 0; i < files.size(); i++) |
197 |
raster = ((GeoRasterFile)files.get(i)).getWindowRaster(x, y, w, h, bandList, raster); |
198 |
|
199 |
return raster;
|
200 |
} |
201 |
|
202 |
/**
|
203 |
* Obtiene una ventana de datos de la imagen a partir de coordenadas reales.
|
204 |
* No aplica supersampleo ni subsampleo sino que devuelve una matriz de igual tama?o a los
|
205 |
* pixeles de disco.
|
206 |
* @param x Posici?n X superior izquierda
|
207 |
* @param y Posici?n Y superior izquierda
|
208 |
* @param w Ancho en coordenadas pixel
|
209 |
* @param h Alto en coordenadas pixel
|
210 |
* @param bandList
|
211 |
* @return Buffer de datos
|
212 |
*/
|
213 |
public RasterBuf getWindowRaster(int x, int y, int w, int h) { |
214 |
|
215 |
RasterBuf raster = new RasterBuf(getDataType()[0], w, h, bandList.getDrawableBandsCount(), false); |
216 |
|
217 |
for(int i = 0; i < files.size(); i++) |
218 |
raster = ((GeoRasterFile)files.get(i)).getWindowRaster(x, y, w, h, bandList, raster); |
219 |
|
220 |
return raster;
|
221 |
} |
222 |
|
223 |
//******************************
|
224 |
//Setters and Getters
|
225 |
//******************************
|
226 |
|
227 |
/**
|
228 |
* Calcula el tama?o de los ficheros en disco
|
229 |
* @return tama?o en bytes de todos los ficheros de la lista
|
230 |
*/
|
231 |
public long getFileSize(){ |
232 |
int len = 0; |
233 |
for(int i=0;i<files.size();i++){ |
234 |
if(((GeoRasterFile)files.get(i)) != null){ |
235 |
File f = new File(((GeoRasterFile)files.get(i)).getName()); |
236 |
len += f.length(); |
237 |
} |
238 |
} |
239 |
return len;
|
240 |
} |
241 |
|
242 |
/**
|
243 |
* Obtiene la altura de la imagen a partir de la primera
|
244 |
* @return altura
|
245 |
*/
|
246 |
public int getHeight() { |
247 |
for(int i=0;i<files.size();i++) |
248 |
if(((GeoRasterFile)files.get(i)) != null) |
249 |
return ((GeoRasterFile)files.get(i)).getHeight();
|
250 |
return 0; |
251 |
} |
252 |
|
253 |
/**
|
254 |
* Obtiene la anchura de la imagen a partir de la primera
|
255 |
* @return anchura
|
256 |
*/
|
257 |
public int getWidth() { |
258 |
for(int i=0;i<files.size();i++) |
259 |
if(((GeoRasterFile)files.get(i)) != null) |
260 |
return ((GeoRasterFile)files.get(i)).getWidth();
|
261 |
return 0; } |
262 |
|
263 |
/**
|
264 |
* Obtiene el n?mero de bandas del fichero
|
265 |
* @return
|
266 |
*/
|
267 |
public int getBandCount(){ |
268 |
return bandList.getBandCount();
|
269 |
} |
270 |
|
271 |
/**
|
272 |
* Obtiene el tipo de dato por banda
|
273 |
* @return tipo de dato por banda
|
274 |
*/
|
275 |
public int[] getDataType() { |
276 |
int[] dt = new int[getFileCount()]; |
277 |
for(int i=0;i<files.size();i++) |
278 |
dt[i] = ((GeoRasterFile)files.get(i)).getDataType(); |
279 |
|
280 |
if(dt.length == 0) |
281 |
return null; |
282 |
else
|
283 |
return dt;
|
284 |
} |
285 |
|
286 |
/**
|
287 |
* Obtiene fichero de la posici?n i.
|
288 |
* @param i Posici?n del fichero a obtener.
|
289 |
* @return GeoRasterFileDataset.
|
290 |
*/
|
291 |
public GeoRasterFile getFile(int i){ |
292 |
return (GeoRasterFile)files.get(i);
|
293 |
} |
294 |
|
295 |
/**
|
296 |
* Obtiene fichero de nombre fileName.
|
297 |
* @param i Posici?n del fichero a obtener.
|
298 |
* @return GeoRasterFile.
|
299 |
*/
|
300 |
public GeoRasterFile getFile(String fileName){ |
301 |
for(int i=0;i<files.size();i++){ |
302 |
if(((GeoRasterFile)files.get(i)).getName().equals(fileName))
|
303 |
return (GeoRasterFile)files.get(i);
|
304 |
} |
305 |
return null; |
306 |
} |
307 |
|
308 |
/**
|
309 |
* Asigna el nombre al GeoRasterMultiFile
|
310 |
* @param name Nombre del GeoRasterMultiFile
|
311 |
*/
|
312 |
public void setName(String name){ |
313 |
this.name = name;
|
314 |
} |
315 |
|
316 |
/**
|
317 |
* Obtiene la lista de bandas
|
318 |
* @return BandList
|
319 |
*/
|
320 |
public BandList getBands(){
|
321 |
return bandList;
|
322 |
} |
323 |
|
324 |
/**
|
325 |
* Obtiene la coordenada X m?nima de toda la lista
|
326 |
* @return Coordenada X m?nima
|
327 |
*/
|
328 |
public double getMinX(){ |
329 |
double minx = Double.MAX_VALUE; |
330 |
for(int i = 0; i < files.size(); i++){ |
331 |
double aux = ((GeoRasterFile)files.get(i)).getExtent().getMin().getX();
|
332 |
if(aux < minx)
|
333 |
minx = aux; |
334 |
} |
335 |
return minx;
|
336 |
} |
337 |
|
338 |
/**
|
339 |
* Obtiene la coordenada Y m?nima de toda la lista
|
340 |
* @return Coordenada Y m?nima
|
341 |
*/
|
342 |
public double getMinY(){ |
343 |
double miny = Double.MAX_VALUE; |
344 |
for(int i = 0; i < files.size(); i++){ |
345 |
double aux = ((GeoRasterFile)files.get(i)).getExtent().getMin().getY();
|
346 |
if(aux < miny)
|
347 |
miny = aux; |
348 |
} |
349 |
return miny;
|
350 |
} |
351 |
|
352 |
/**
|
353 |
* Obtiene la coordenada Y m?xima de toda la lista
|
354 |
* @return Coordenada Y m?xima
|
355 |
*/
|
356 |
public double getMaxX(){ |
357 |
double maxx = Double.MIN_VALUE; |
358 |
for(int i = 0; i < files.size(); i++){ |
359 |
double aux = ((GeoRasterFile)files.get(i)).getExtent().getMin().getY();
|
360 |
if(aux > maxx)
|
361 |
maxx = aux; |
362 |
} |
363 |
return maxx;
|
364 |
} |
365 |
|
366 |
/**
|
367 |
* Obtiene la coordenada Y m?xima de toda la lista
|
368 |
* @return Coordenada Y m?xima
|
369 |
*/
|
370 |
public double getMaxY(){ |
371 |
double maxy = Double.MIN_VALUE; |
372 |
for(int i = 0; i < files.size(); i++){ |
373 |
double aux = ((GeoRasterFile)files.get(i)).getExtent().getMin().getY();
|
374 |
if(aux > maxy)
|
375 |
maxy = aux; |
376 |
} |
377 |
return maxy;
|
378 |
} |
379 |
|
380 |
/**
|
381 |
* Obtiene el extent del multi fichero. Este corresponde al primer
|
382 |
* GeoRasterFile de la lista.
|
383 |
* @return Extent
|
384 |
*/
|
385 |
public Extent getExtent(){
|
386 |
if(files.size() == 0) |
387 |
return null; |
388 |
else
|
389 |
return ((GeoRasterFile)files.get(0)).getExtent(); |
390 |
} |
391 |
} |