Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRaster / src / org / gvsig / raster / dataset / CompositeDataset.java @ 30008

History | View | Annotate | Download (41.3 KB)

1 13377 nacho
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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.dataset;
20
21
import java.awt.geom.AffineTransform;
22 13393 nacho
import java.awt.geom.NoninvertibleTransformException;
23 13377 nacho
import java.awt.geom.Point2D;
24
import java.io.IOException;
25
26 13393 nacho
import org.cresques.cts.IProjection;
27 17661 bsanchez
import org.gvsig.raster.RasterLibrary;
28 13540 nacho
import org.gvsig.raster.buffer.RasterBuffer;
29 13377 nacho
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
30 13590 nacho
import org.gvsig.raster.dataset.properties.DatasetListHistogram;
31 13377 nacho
import org.gvsig.raster.dataset.properties.DatasetListStatistics;
32 22473 bsanchez
import org.gvsig.raster.dataset.serializer.RmfSerializerException;
33 13377 nacho
import org.gvsig.raster.datastruct.ColorTable;
34
import org.gvsig.raster.datastruct.Extent;
35 22337 bsanchez
import org.gvsig.raster.datastruct.GeoPointList;
36 13377 nacho
import org.gvsig.raster.datastruct.Histogram;
37
import org.gvsig.raster.datastruct.HistogramException;
38
import org.gvsig.raster.datastruct.Transparency;
39
import org.gvsig.raster.util.MathUtils;
40
import org.gvsig.raster.util.RasterUtilities;
41
/**
42 20998 bsanchez
 * Esta clase est? compuestas de multiples datasets formando una rejilla de NxM
43
 * rasters. Un cliente de esta clase debe tener una visi?n de la rejilla como si
44
 * fuese un solo raster, gestionando esta el acceso la imagen que corresponda en
45
 * cada petici?n de usuario.
46 30008 nbrodin
 *
47 13377 nacho
 * @version 29/08/2007
48
 * @author Nacho Brodin (nachobrodin@gmail.com)
49
 */
50
public class CompositeDataset implements IRasterDataSource {
51 20998 bsanchez
        private MultiRasterDataset[][] mosaic        = null;
52
        private DatasetListStatistics  stats         = null;
53 30008 nbrodin
        private BandList bandList = null;
54 20998 bsanchez
        private boolean                readOnly      = false;
55
56 16539 nbrodin
        /**
57
         * Flag que fuerza al buffer en memoria
58
         */
59 20998 bsanchez
        private boolean                forceToMemory = false;
60 30008 nbrodin
61 13377 nacho
        /**
62 13540 nacho
         * Constructor. Genera la estructura de n filas por n columnas de rasters.
63 13377 nacho
         * @param n N?mero de filas
64
         * @param m N?mero de columnas
65
         */
66 13540 nacho
        public CompositeDataset(int n, int m) {
67 30008 nbrodin
                bandList = new BandList();
68 13540 nacho
                mosaic = new MultiRasterDataset[n][m];
69 13377 nacho
        }
70
71
        /**
72
         * Constructor. Genera la estructura de n filas por n columnas de rasters y
73
         * las asigna a los raster que se le pasan por par?metro.
74
         * @param n N?mero de filas
75
         * @param m N?mero de columnas
76
         */
77
        public CompositeDataset(MultiRasterDataset[][] mos) throws MosaicNotValidException {
78 30008 nbrodin
                bandList = new BandList();
79 16343 nbrodin
                this.mosaic = deleteNullValues(mos);
80
                //this.mosaic = mos;
81 30008 nbrodin
82 13540 nacho
                /*if(!validateMosaic()) {
83 13377 nacho
                        this.mosaic = null;
84
                        throw new MosaicNotValidException("Extends no validos para montar un mosaico");
85 13540 nacho
                }*/
86
                init();
87 13377 nacho
        }
88 30008 nbrodin
89 16343 nbrodin
        /**
90
         * M?todo que elimina crea un array bidimensional eliminando los nulos del que tiene
91
         * el que se le pasa por par?metro
92
         * @param values
93
         * @return MultiRasterDataset
94
         */
95
        private MultiRasterDataset[][] deleteNullValues(MultiRasterDataset[][] values) {
96
                int n = values.length;
97
                int m = values[0].length;
98
                int posInitX = 0;
99
                int posInitY = 0;
100 30008 nbrodin
101 16343 nbrodin
                int nRows = n, nCols = m;
102
                //Contador de filas
103
                boolean first = true;
104
                for (int row = 0; row < n; row++) {
105
                        boolean isNull = true;
106 30008 nbrodin
                        for (int col = 0; col < m; col++)
107 16343 nbrodin
                                if(values[row][col] != null) {
108
                                        isNull = false;
109
                                        if(first) {
110
                                                posInitX = col;
111
                                                first = false;
112
                                        }
113
                                }
114
                        if(isNull)
115
                                nRows --;
116
                }
117 30008 nbrodin
118 16343 nbrodin
                //Contador de columnas
119
                first = true;
120
                for (int col = 0; col < m; col++) {
121
                        boolean isNull = true;
122 30008 nbrodin
                        for (int row = 0; row < n; row++)
123 16343 nbrodin
                                if(values[row][col] != null) {
124
                                        isNull = false;
125
                                        if(first) {
126
                                                posInitY = row;
127
                                                first = false;
128
                                        }
129
                                }
130
                        if(isNull)
131
                                nCols --;
132
                }
133
                //Copia de datos
134
                MultiRasterDataset[][] result = new MultiRasterDataset[nRows][nCols];
135 30008 nbrodin
136
                for (int row = 0; row < result.length; row++)
137
                        for (int col = 0; col < result[row].length; col++)
138 16343 nbrodin
                                result[row][col] = values[row + posInitY][col + posInitX];
139
                return result;
140
        }
141 30008 nbrodin
142 13393 nacho
        /**
143
         * Abre un dataset pasando como par?metros la proyecci?n y un objeto identificador del dataset. Este
144 30008 nbrodin
         * objeto puede ser una ruta a un fichero en disco. En este caso la extensi?n del fichero servir? para
145 13393 nacho
         * buscar el driver que lo gestiona. Si proporcionamos un array de cadenas se tratar?n como la ruta a N ficheros
146
         * de disco. Tambi?n puede ser un buffer de datos en memoria o cualquier otro objeto
147 30008 nbrodin
         * que pueda aceptar un driver.
148 13393 nacho
         * @param proj PRoyecci?n
149
         * @param datasetOpenParam Par?metros al driver
150
         * @return RasterMultiDatset
151
         * @throws NotSupportedExtensionException
152
         * @throws RasterDriverException
153
         */
154
        public static CompositeDataset open(IProjection proj, Object datasetOpenParam) throws NotSupportedExtensionException, RasterDriverException {
155
                if(datasetOpenParam instanceof String[][]) {
156
                        String[][] param = (String[][])datasetOpenParam;
157 13540 nacho
                        MultiRasterDataset[][] mosaic = new MultiRasterDataset[param.length][param[0].length];
158 30008 nbrodin
                        for (int i = 0; i < param.length; i++)
159
                                for (int j = 0; j < param[i].length; j++)
160 13540 nacho
                                        mosaic[i][j] = MultiRasterDataset.open(proj, param[i][j]);
161 13393 nacho
                        CompositeDataset cd;
162
                        try {
163
                                cd = new CompositeDataset(mosaic);
164
                        } catch (MosaicNotValidException e) {
165
                                return null;
166
                        }
167
                        return cd;
168
                }
169
                return null;
170
        }
171 30008 nbrodin
172 13377 nacho
        /**
173
         * Valida que los extends del mosaico sean validos, es decir, que sean correlativos
174
         * formando la matriz. Adem?s tambi?n valida que el tama?o de pixel coincida en todos los
175 30008 nbrodin
         * raster que forman el mosaico.
176
         *
177
         * @param mos
178 13377 nacho
         * @throws MosaicNotValidException
179
         */
180 14431 nbrodin
        public boolean validateMosaic() {
181 13540 nacho
                int n = mosaic.length;
182
                int m = mosaic[0].length;
183
                //Comprobamos en Horizontal
184 30008 nbrodin
                if(m > 1)
185
                        for (int row = 0; row < n; row++)
186
                                for (int col = 0; col < m; col++)
187 16343 nbrodin
                                        if(col < (m - 1) && mosaic[row][col] != null) {
188 13540 nacho
                                                Extent a = mosaic[row][col].getExtent();
189
                                                Extent b = mosaic[row][col + 1].getExtent();
190 13377 nacho
                                                if(((int)a.maxX()) != ((int)b.minX()))
191
                                                        return false;
192 19623 bsanchez
                                                double psx = MathUtils.clipDecimals(mosaic[row][col].getPixelSizeX(), 2);
193
                                                double psx1 = MathUtils.clipDecimals(mosaic[row][col + 1].getPixelSizeX(), 2);
194
                                                double psy = MathUtils.clipDecimals(mosaic[row][col].getPixelSizeY(), 2);
195
                                                double psy1 = MathUtils.clipDecimals(mosaic[row][col + 1].getPixelSizeY(), 2);
196 13377 nacho
                                                if(psx != psx1 || psy != psy1)
197
                                                        return false;
198 13540 nacho
                                                if(mosaic[row][col].getBandCount() != mosaic[row][col + 1].getBandCount())
199 13377 nacho
                                                        return false;
200
                                        }
201 13540 nacho
202
                //Comprobamos en Vertical
203 30008 nbrodin
                if(n > 1)
204
                        for (int col = 0; col < m; col++)
205
                                for (int row = 0; row < n; row++)
206 16343 nbrodin
                                        if(row < (n - 1) && mosaic[row][col] != null) {
207 13540 nacho
                                                Extent a = mosaic[row][col].getExtent();
208
                                                Extent b = mosaic[row + 1][col].getExtent();
209 13377 nacho
                                                if(((int)a.minY()) != ((int)b.maxY()))
210
                                                        return false;
211 19623 bsanchez
                                                double psx = MathUtils.clipDecimals(mosaic[row][col].getPixelSizeX(), 2);
212
                                                double psx1 = MathUtils.clipDecimals(mosaic[row + 1][col].getPixelSizeX(), 2);
213
                                                double psy = MathUtils.clipDecimals(mosaic[row][col].getPixelSizeY(), 2);
214
                                                double psy1 = MathUtils.clipDecimals(mosaic[row + 1][col].getPixelSizeY(), 2);
215 13377 nacho
                                                if(psx != psx1 || psy != psy1)
216
                                                        return false;
217 13540 nacho
                                                if(mosaic[row][col].getBandCount() != mosaic[row + 1][col].getBandCount())
218 13377 nacho
                                                        return false;
219
                                        }
220 30008 nbrodin
221 13377 nacho
                return true;
222
        }
223
224
        /*
225
         * (non-Javadoc)
226
         * @see org.gvsig.raster.dataset.IRasterDataSource#addDataset(org.gvsig.raster.dataset.RasterDataset[])
227
         */
228
        public void addDataset(RasterDataset[] f) throws FileNotFoundInListException {
229
                if(mosaic != null) {
230 13540 nacho
                        int n = mosaic.length;
231
                        int m = mosaic[0].length;
232 30008 nbrodin
                        if(f.length == (n * m))
233
                                for (int i = 0; i < n; i++)
234 13377 nacho
                                        for (int j = 0; j < m; j++) {
235
                                                MultiRasterDataset mrd = new MultiRasterDataset();
236 30008 nbrodin
                                                mrd.addDataset(new RasterDataset[]{f[i * n + j]});
237 13377 nacho
                                        }
238 13540 nacho
                        init();
239 13377 nacho
                }
240
        }
241
242
        /*
243
         * (non-Javadoc)
244
         * @see org.gvsig.raster.dataset.IRasterDataSource#addDataset(java.lang.String[])
245
         */
246
        public void addDataset(String[] fileName) throws FileNotFoundInListException, NotSupportedExtensionException, RasterDriverException {
247
                if(mosaic != null) {
248 13540 nacho
                        int n = mosaic.length;
249
                        int m = mosaic[0].length;
250 30008 nbrodin
                        if(fileName.length == (n * m))
251
                                for (int i = 0; i < n; i++)
252 13377 nacho
                                        for (int j = 0; j < m; j++) {
253
                                                MultiRasterDataset mrd = new MultiRasterDataset();
254 30008 nbrodin
                                                mrd.addDataset(new RasterDataset[]{RasterDataset.open(null, fileName[i * n + j])});
255 13377 nacho
                                        }
256 13540 nacho
                        init();
257 13377 nacho
                }
258
        }
259
260 13540 nacho
        /**
261
         * Acciones de inicializaci?n cuando se crea el objeto o se
262
         * a?aden nuevos dataset a este
263
         */
264
        private void init() {
265
                stats = new DatasetListStatistics(mosaic);
266 30008 nbrodin
267 13540 nacho
                //Creamos la lista de bandas
268
                bandList = (BandList)mosaic[0][0].getBands().clone();
269
                int n = mosaic.length;
270
                int m = mosaic[0].length;
271 30008 nbrodin
                for (int row = 0; row < n; row++)
272
                        for (int col = 0; col < m; col++)
273
                                if(row != 0 && col != 0)
274
                                        for (int i = 0; i < mosaic[0][0].getBandCount(); i++)
275 16343 nbrodin
                                                if(mosaic[row][col] != null)
276
                                                        bandList.getBand(i).setAdditionalName(mosaic[row][col].getBands().getBand(i).getFileName());
277 30008 nbrodin
278 13540 nacho
        }
279 30008 nbrodin
280 13825 nacho
        /**
281 30008 nbrodin
         * Obtiene la lista de nombres de los dataset
282 13825 nacho
         * @return
283
         */
284
        public String[][] getFileNames() {
285
                String[][] s = new String[mosaic.length][mosaic[0].length];
286 30008 nbrodin
                for (int i = 0; i < s.length; i++)
287
                        for (int j = 0; j < s[i].length; j++)
288 16343 nbrodin
                                if(mosaic[i][j] != null)
289
                                        s[i][j] = mosaic[i][j].getDataset(0)[0].getFName();
290 13825 nacho
                return s;
291
        }
292 30008 nbrodin
293 13377 nacho
        /*
294
         * (non-Javadoc)
295
         * @see org.gvsig.raster.dataset.IRasterDataSource#calcSteps(double, double, double, double, double, double, int, int)
296
         */
297 15931 nbrodin
        public double[] calcSteps(double dWorldTLX, double dWorldTLY, double dWorldBRX, double dWorldBRY, double nWidth, double nHeight, int bufWidth, int bufHeight) {
298 13377 nacho
                if(mosaic != null)
299 13540 nacho
                        return mosaic[0][0].calcSteps(dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY, nWidth, nHeight, bufWidth, bufHeight);
300 13377 nacho
                return null;
301
        }
302
303
        /*
304
         * (non-Javadoc)
305
         * @see org.gvsig.raster.dataset.IRasterDataSource#close()
306
         */
307
        public void close() {
308 13540 nacho
                int n = mosaic.length;
309
                int m = mosaic[0].length;
310 30008 nbrodin
                for (int row = 0; row < n; row++)
311
                        for (int col = 0; col < m; col++)
312
                                if(mosaic[row][col] != null) {
313 16343 nbrodin
                                        mosaic[row][col].close();
314 30008 nbrodin
                                        mosaic[row][col] = null;
315
                                }
316
                mosaic = null;
317
                bandList.clear();
318
                bandList = null;
319 13377 nacho
        }
320
321
        /*
322
         * (non-Javadoc)
323
         * @see org.gvsig.raster.dataset.IRasterDataSource#copy()
324
         */
325 17876 bsanchez
        public IRasterDataSource newDataset() {
326 13540 nacho
                int n = mosaic.length;
327
                int m = mosaic[0].length;
328
                MultiRasterDataset[][] mrd = new MultiRasterDataset[n][m];
329 30008 nbrodin
                for (int row = 0; row < n; row++)
330
                        for (int col = 0; col < m; col++)
331 13540 nacho
                                if(mosaic[row][col] != null)
332 17876 bsanchez
                                        mrd[row][col] = (MultiRasterDataset)mosaic[row][col].newDataset();
333 30008 nbrodin
334 13377 nacho
                try {
335
                        return new CompositeDataset(mrd);
336
                } catch (MosaicNotValidException e) {
337
                        return null;
338
                }
339
        }
340
341
        /*
342
         * (non-Javadoc)
343 14134 nbrodin
         * @see org.gvsig.raster.dataset.IRasterDataSource#getOwnAffineTransform()
344
         */
345
        public AffineTransform getOwnAffineTransform() {
346
                if(mosaic != null && mosaic[0][0] != null)
347
                        return mosaic[0][0].getOwnAffineTransform();
348
                return new AffineTransform();
349
        }
350 30008 nbrodin
351 14134 nbrodin
        /*
352
         * (non-Javadoc)
353 13377 nacho
         * @see org.gvsig.raster.dataset.IRasterDataSource#getAffineTransform()
354
         */
355 24948 nbrodin
        public AffineTransform getAffineTransform(int band) {
356 13540 nacho
                if(mosaic != null && mosaic[0][0] != null)
357 24948 nbrodin
                        return mosaic[0][0].getAffineTransform(band);
358 13377 nacho
                return new AffineTransform();
359
        }
360 30008 nbrodin
361 13378 nacho
        /*
362
         * (non-Javadoc)
363
         * @see org.gvsig.raster.dataset.IRasterDataSource#getExtent()
364
         */
365
        public Extent getExtent() {
366 13540 nacho
                int n = mosaic.length;
367
                int m = mosaic[0].length;
368
                if(mosaic != null && mosaic[0][0] != null) {
369
                        double ulx = mosaic[0][0].getExtent().getULX();
370
                        double uly = mosaic[0][0].getExtent().getULY();
371 30008 nbrodin
372 13540 nacho
                        double urx = mosaic[n - 1][0].getExtent().getURX();
373
                        double ury = mosaic[n - 1][0].getExtent().getURY();
374 30008 nbrodin
375 13540 nacho
                        double llx = mosaic[0][m - 1].getExtent().getLLX();
376
                        double lly = mosaic[0][m - 1].getExtent().getLLY();
377 30008 nbrodin
378 13540 nacho
                        double lrx = mosaic[n - 1][m - 1].getExtent().getLRX();
379
                        double lry = mosaic[n - 1][m - 1].getExtent().getLRY();
380 30008 nbrodin
381
                        return new Extent(        new Point2D.Double(ulx, uly),
382
                                                                        new Point2D.Double(lrx, lry),
383
                                                                        new Point2D.Double(urx, ury),
384 13378 nacho
                                                                        new Point2D.Double(llx, lly));
385
                }
386
                return null;
387
        }
388 13377 nacho
389
        /*
390
         * (non-Javadoc)
391
         * @see org.gvsig.raster.dataset.IRasterDataSource#getBandCount()
392
         */
393
        public int getBandCount() {
394 13540 nacho
                if(mosaic != null && mosaic[0][0] != null)
395
                        return mosaic[0][0].getBandCount();
396 13377 nacho
                return 0;
397
        }
398
399 17686 nbrodin
        /**
400
         * Obtiene el ancho del mosaico completo en p?xeles, esto es la
401
         * suma de todos los raster que componen la extensi?n.
402 13377 nacho
         */
403
        public double getWidth() {
404
                double w = 0;
405 13540 nacho
                if(mosaic != null && mosaic[0][0] != null) {
406
                        int m = mosaic[0].length;
407 13377 nacho
                        for (int col = 0; col < m; col++)
408 13540 nacho
                                w += mosaic[0][col].getWidth();
409 13377 nacho
                }
410 13540 nacho
                return w;
411 13377 nacho
        }
412 30008 nbrodin
413 17686 nbrodin
        /**
414
         * Obtiene el alto del mosaico completo en p?xeles, esto es la
415
         * suma de todos los raster que componen la extensi?n.
416 13377 nacho
         */
417
        public double getHeight() {
418
                double h = 0;
419 13540 nacho
                if(mosaic != null && mosaic[0][0] != null) {
420
                        int n = mosaic.length;
421 13377 nacho
                        for (int row = 0; row < n; row++)
422 13540 nacho
                                h += mosaic[row][0].getHeight();
423 13377 nacho
                }
424 13540 nacho
                return h;
425 13377 nacho
        }
426
427
        /*
428
         * (non-Javadoc)
429 17686 nbrodin
         * @see org.gvsig.raster.dataset.IRasterDataSource#getCellSize()
430
         */
431
        public double getCellSize() {
432
                try {
433
                        Extent e = getExtent();
434
                        double dCellsize = (e.getMax().getX() - e.getMin().getX() ) / getWidth();
435
                        return dCellsize;
436
                } catch (NullPointerException e) {
437
                        return 1;
438
                }
439
        }
440 30008 nbrodin
441 17686 nbrodin
        /*
442
         * (non-Javadoc)
443 13377 nacho
         * @see org.gvsig.raster.dataset.IRasterDataSource#getDataType()
444
         */
445
        public int[] getDataType() {
446 13540 nacho
                if(mosaic != null && mosaic[0][0] != null)
447
                        return mosaic[0][0].getDataType();
448 13377 nacho
                return null;
449
        }
450 30008 nbrodin
451 13378 nacho
        /*
452
         * (non-Javadoc)
453
         * @see org.gvsig.raster.dataset.IRasterDataSource#getDatasetCount()
454
         */
455
        public int getDatasetCount() {
456 13540 nacho
                if(mosaic != null && mosaic[0][0] != null)
457
                        return mosaic[0][0].getDatasetCount();
458 13378 nacho
                return 0;
459
        }
460
461
        /*
462
         * (non-Javadoc)
463
         * @see org.gvsig.raster.dataset.IRasterDataSource#getFileSize()
464
         */
465
        public long getFileSize() {
466
                long size = 0;
467 13540 nacho
                int n = mosaic.length;
468
                int m = mosaic[0].length;
469 30008 nbrodin
                for (int row = 0; row < n; row++)
470
                        for (int col = 0; col < m; col++)
471 16343 nbrodin
                                if(mosaic[row][col] != null)
472
                                        size += mosaic[row][col].getFileSize();
473 13378 nacho
                return size;
474
        }
475 30008 nbrodin
476 13378 nacho
        /**
477
         * Obtiene el dataset cuyas coordenadas contienen el punto pasado por par?meto
478
         * @param x Coordenada X a comprobar
479
         * @param y Coordenada Y a comprobar
480 13393 nacho
         * @return Point2D Posici?n del MultiRasterDataset dentro del mosaico
481 13378 nacho
         */
482 14431 nbrodin
        public Point2D getDatasetByCoords(double x, double y) {
483 13540 nacho
                int n = mosaic.length;
484
                int m = mosaic[0].length;
485 30008 nbrodin
                for (int row = 0; row < n; row++)
486
                        for (int col = 0; col < m; col++)
487
                                if(mosaic[row][col] != null &&
488
                                         RasterUtilities.isInside(new Point2D.Double(x, y), mosaic[row][col].getExtent(), mosaic[row][col].getAffineTransform(0)))
489 13540 nacho
                                        return new Point2D.Double(row, col);
490
491 13378 nacho
                return null;
492
        }
493 30008 nbrodin
494 13540 nacho
        /**
495
         * Obtiene la lista de datasets del mosaico que intersectan con el extent proporcionado
496
         * @param ulx Coordenada X superior izquierda
497
         * @param uly Coordenada Y superior izquierda
498
         * @param lrx Coordenada X inferior derecha
499
         * @param lry Coordenada Y inferior derecha
500
         * @return MultiRasterDataset[][][]
501
         * @throws NoninvertibleTransformException
502
         */
503
        private MultiRasterDataset[][] getDatasetListInArea(double ulx, double uly, double lrx, double lry) throws NoninvertibleTransformException {
504
                int n = mosaic.length;
505
                int m = mosaic[0].length;
506 30008 nbrodin
507 13540 nacho
                MultiRasterDataset[][] result = new MultiRasterDataset[n][m];
508 30008 nbrodin
509
                for (int row = 0; row < n; row++)
510
                        for (int col = 0; col < m; col++)
511
                                if(mosaic[row][col] != null &&
512
                                         RasterUtilities.intersects(new Extent(ulx, uly, lrx, lry), mosaic[row][col].getExtent(), mosaic[row][col].getAffineTransform(0)))
513
                                        for (int k = 0; k < mosaic.length; k++)
514
                                                result[row][col] = mosaic[row][col];
515 13540 nacho
                return result;
516
        }
517 13393 nacho
518
        /*
519
         * (non-Javadoc)
520
         * @see org.gvsig.raster.dataset.IRasterDataSource#isInside(java.awt.geom.Point2D)
521
         */
522
        public boolean isInside(Point2D p) {
523 24948 nbrodin
                return RasterUtilities.isInside(p, getExtent(), getAffineTransform(0));
524 13393 nacho
        }
525
526
        /*
527
         * (non-Javadoc)
528
         * @see org.gvsig.raster.dataset.IRasterDataSource#rasterToWorld(java.awt.geom.Point2D)
529
         */
530
        public Point2D rasterToWorld(Point2D pt) {
531
                Point2D p = new Point2D.Double();
532 24948 nbrodin
                getAffineTransform(0).transform(pt, p);
533 13393 nacho
                return p;
534
        }
535 30008 nbrodin
536 13393 nacho
        /*
537
         * (non-Javadoc)
538
         * @see org.gvsig.raster.dataset.IRasterDataSource#worldToRaster(java.awt.geom.Point2D)
539
         */
540
        public Point2D worldToRaster(Point2D pt) {
541
                Point2D p = new Point2D.Double();
542
                try {
543 24948 nbrodin
                        getAffineTransform(0).inverseTransform(pt, p);
544 13393 nacho
                } catch (NoninvertibleTransformException e) {
545
                        return pt;
546
                }
547
                return p;
548
        }
549
550
        /*
551
         * (non-Javadoc)
552
         * @see org.gvsig.raster.dataset.IRasterDataSource#isRotated()
553
         */
554
        public boolean isRotated() {
555 24948 nbrodin
                if(getAffineTransform(0).getShearX() != 0 || getAffineTransform(0).getShearY() != 0)
556 13393 nacho
                        return true;
557
                return false;
558
        }
559
560
        /*
561
         * (non-Javadoc)
562
         * @see org.gvsig.raster.dataset.IRasterDataSource#isGeoreferenced()
563
         */
564
        public boolean isGeoreferenced() {
565
                //Este tipo de datasets siempre est? georreferenciado
566
                return true;
567
        }
568 13540 nacho
569
        /*
570
         * (non-Javadoc)
571
         * @see org.gvsig.raster.dataset.IRasterDataSource#getDataset(int)
572
         */
573
        public RasterDataset[] getDataset(int i) {
574
                RasterDataset[] d = new RasterDataset[mosaic.length * mosaic[0].length];
575
                int count = 0;
576 30008 nbrodin
                for (int row = 0; row < mosaic.length; row++)
577
                        for (int col = 0; col < mosaic[row].length; col++)
578 16343 nbrodin
                                if(mosaic[row][col] != null)
579
                                        d[count] = mosaic[row][col].getDataset(i)[0];
580 13540 nacho
                return d;
581
        }
582 30008 nbrodin
583 13540 nacho
        /*
584
         * (non-Javadoc)
585
         * @see org.gvsig.raster.dataset.IRasterDataSource#getStatistics()
586
         */
587
        public DatasetListStatistics getStatistics() {
588
                return stats;
589
        }
590 30008 nbrodin
591 13540 nacho
        /*
592
         * (non-Javadoc)
593
         * @see org.gvsig.raster.dataset.IRasterDataSource#getBands()
594
         */
595 13377 nacho
        public BandList getBands() {
596 13540 nacho
                return bandList;
597 13377 nacho
        }
598 30008 nbrodin
599 13540 nacho
        /**
600
         * Genera un buffer de datos ?nico a partir de una matriz de buffers donde puede haber
601 30008 nbrodin
         * elementos con valor nulo.
602 13540 nacho
         * @return
603
         */
604 13590 nacho
        public IBuffer generateBuffer(IBuffer[][] bufList, int drawableBands) {
605 13540 nacho
                int n = mosaic.length;
606
                int m = mosaic[0].length;
607
                int nCols = 0, nRows = 0;
608
                //Contamos el n?mero de filas y columnas del buffer nuevo
609 30008 nbrodin
                for (int row = 0; row < n; row++) {
610
                        for (int col = 0; col < m; col++)
611 13540 nacho
                                if(bufList[row][col] != null)
612
                                        nCols += bufList[row][col].getWidth();
613
                        if(nCols != 0) break;
614
                }
615 30008 nbrodin
                for (int col = 0; col < m; col++) {
616
                        for (int row = 0; row < n; row++)
617 13540 nacho
                                if(bufList[row][col] != null)
618
                                        nRows += bufList[row][col].getHeight();
619
                        if(nRows != 0) break;
620
                }
621 30008 nbrodin
622 13540 nacho
                //Creamos el buffer
623 13616 nacho
                IBuffer raster = RasterBuffer.getBuffer(bufList[0][0].getDataType(), nCols, nRows, drawableBands, true);
624 30008 nbrodin
625 13540 nacho
                //Hacemos la copia
626
                int[] pos = new int[2];
627
                int validCol = 0;
628 30008 nbrodin
                for (int row = 0; row < n; row++) {
629 13540 nacho
                        for (int col = 0; col < m; col++) {
630
                                pos[1] = (col == 0) ? 0 : pos[1];
631
                                if(bufList[row][col] == null)
632
                                        continue;
633
                                validCol = col;
634
                                copyTile(bufList[row][col], raster, pos[0], pos[1]);
635
                                pos[1] +=  bufList[row][col].getWidth();
636
                        }
637
                        if(bufList[row][validCol] != null)
638
                                pos[0] +=  bufList[row][validCol].getHeight();
639
                }
640
                return raster;
641 13377 nacho
        }
642 30008 nbrodin
643 13540 nacho
        /**
644
         * Copia un tile en el buffer que contendr? todos los tiles
645
         * @param origin Buffer de origen
646
         * @param dest Buffer de destino
647
         * @param col Columna del buffer de destino donde se empieza a escribir
648
         * @param row Fila del buffer de destino donde se empieza a escribir
649 30008 nbrodin
         * @return array con los valores que representan la ?ltima fila y
650
         * ?ltima columna que se escribieron
651 13540 nacho
         */
652 13574 nacho
        private void copyTile(IBuffer origin, IBuffer dest, int r, int c) {
653 13616 nacho
                switch(origin.getDataType()) {
654
                case IBuffer.TYPE_BYTE :
655 30008 nbrodin
                        for (int band = 0; band < origin.getBandCount(); band++)
656
                                for (int row = 0; row < origin.getHeight(); row++)
657
                                        for (int col = 0; col < origin.getWidth(); col++)
658 13616 nacho
                                                try {
659
                                                                dest.setElem(row + r, col + c, band, origin.getElemByte(row, col, band));
660
                                                        } catch (ArrayIndexOutOfBoundsException e) {break;}
661
                        break;
662
                case IBuffer.TYPE_SHORT :
663 30008 nbrodin
                        for (int band = 0; band < origin.getBandCount(); band++)
664
                                for (int row = 0; row < origin.getHeight(); row++)
665
                                        for (int col = 0; col < origin.getWidth(); col++)
666 13616 nacho
                                                try {
667
                                                                dest.setElem(row + r, col + c, band, origin.getElemShort(row, col, band));
668
                                                        } catch (ArrayIndexOutOfBoundsException e) {break;}
669
                        break;
670
                case IBuffer.TYPE_FLOAT :
671 30008 nbrodin
                        for (int band = 0; band < origin.getBandCount(); band++)
672
                                for (int row = 0; row < origin.getHeight(); row++)
673
                                        for (int col = 0; col < origin.getWidth(); col++)
674 13616 nacho
                                                try {
675
                                                                dest.setElem(row + r, col + c, band, origin.getElemFloat(row, col, band));
676
                                                        } catch (ArrayIndexOutOfBoundsException e) {break;}
677
                        break;
678
                case IBuffer.TYPE_DOUBLE:
679 30008 nbrodin
                        for (int band = 0; band < origin.getBandCount(); band++)
680
                                for (int row = 0; row < origin.getHeight(); row++)
681
                                        for (int col = 0; col < origin.getWidth(); col++)
682 13616 nacho
                                                try {
683
                                                                dest.setElem(row + r, col + c, band, origin.getElemDouble(row, col, band));
684
                                                        } catch (ArrayIndexOutOfBoundsException e) {break;}
685
                        break;
686
                }
687 13540 nacho
        }
688 30008 nbrodin
689 13540 nacho
        /*
690
         * (non-Javadoc)
691
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWindowRaster(double, double, double, double)
692
         */
693 30008 nbrodin
        public IBuffer getWindowRaster(double ulx, double uly, double lrx, double lry)
694 16590 nbrodin
                throws InvalidSetViewException, InterruptedException, RasterDriverException {
695 13540 nacho
                try {
696
                        MultiRasterDataset[][] datasetList = getDatasetListInArea(ulx, uly, lrx, lry);
697
                        int n = mosaic.length;
698
                        int m = mosaic[0].length;
699
                        IBuffer[][] bufferList = new IBuffer[n][m];
700 30008 nbrodin
                        for (int row = 0; row < n; row++)
701
                                for (int col = 0; col < m; col++)
702 13540 nacho
                                        if(datasetList[row][col] != null)
703
                                                bufferList[row][col] = datasetList[row][col].getWindowRaster(ulx, uly, lrx, lry);
704 30008 nbrodin
                        IBuffer endBuffer = generateBuffer(bufferList, mosaic[0][0]
705
                                        .getBands().getDrawableBandsCount());
706
                        for (int row = 0; row < n; row++)
707
                                for (int col = 0; col < m; col++)
708
                                        if (bufferList[row][col] != null)
709
                                                bufferList[row][col].free();
710
                        return endBuffer;
711 13540 nacho
                } catch (NoninvertibleTransformException e) {
712
                        throw new InvalidSetViewException("No se ha podido aplicar la transformaci?n inversa para esa vista.");
713
                }
714
        }
715 13377 nacho
716 20998 bsanchez
        /*
717
         * (non-Javadoc)
718
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWindowRaster(double, double, double, double, boolean)
719
         */
720 30008 nbrodin
        public IBuffer getWindowRaster(double ulx, double uly, double w, double h, boolean adjustToExtent)
721 16590 nbrodin
                throws InvalidSetViewException, RasterDriverException {
722
                //TODO: FUNCIONALIDAD: getWindowRaster en CompositeDataset sin implementar
723 13377 nacho
                return null;
724
        }
725
726 20998 bsanchez
        /*
727
         * (non-Javadoc)
728
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWindowRaster(double, double, double, double, int, int, boolean)
729
         */
730 30008 nbrodin
        public IBuffer getWindowRaster(double ulx, double uly, double lrx, double lry, int bufWidth, int bufHeight, boolean adjustToExtent)
731 16590 nbrodin
                throws InvalidSetViewException, InterruptedException, RasterDriverException {
732 13574 nacho
                try {
733
                        Point2D p1 = new Point2D.Double(ulx, uly);
734
                        Point2D p2 = new Point2D.Double(lrx, lry);
735
                        MultiRasterDataset[][] datasetList = getDatasetListInArea(p1.getX(), p1.getY(), p2.getX(), p2.getY());
736
                        Point2D px1 = mosaic[0][0].worldToRaster(p1);
737
                        Point2D px2 = mosaic[0][0].worldToRaster(p2);
738
                        int n = mosaic.length;
739
                        int m = mosaic[0].length;
740
                        IBuffer[][] bufferList = new IBuffer[n][m];
741 30008 nbrodin
                        for (int row = 0; row < n; row++)
742
                                for (int col = 0; col < m; col++)
743 13574 nacho
                                        if(datasetList[row][col] != null) {
744
                                                int[] values = getLocalRequest((int)px1.getX(), (int)px1.getY(), (int)Math.abs(px2.getX() - px1.getX()), (int)Math.abs(px2.getY() - px1.getY()), row, col);
745
                                                if(values != null) {
746
                                                        int bW = (int)Math.round((Math.abs(values[2] - values[0]) * bufWidth) / Math.abs(px2.getX() - px1.getX()));
747
                                                        int bH = (int)Math.round((Math.abs(values[3] - values[1]) * bufHeight) / Math.abs(px2.getY() - px1.getY()));
748 30008 nbrodin
                                                        int wTile = (Math.abs(values[2] - values[0]) > (int)datasetList[row][col].getWidth()) ? (int)datasetList[row][col].getWidth() : (int)Math.abs(values[2] - values[0]);
749
                                                        int hTile = (Math.abs(values[3] - values[1]) > (int)datasetList[row][col].getHeight()) ? (int)datasetList[row][col].getHeight() : (int)Math.abs(values[3] - values[1]);
750 13574 nacho
                                                        bufferList[row][col] = datasetList[row][col].getWindowRaster(values[0], values[1], wTile, hTile, bW, bH);
751
                                                }
752
                                        }
753 30008 nbrodin
                        IBuffer endBuffer = generateBuffer(bufferList, mosaic[0][0]
754
                                        .getBands().getDrawableBandsCount());
755
                        for (int row = 0; row < n; row++)
756
                                for (int col = 0; col < m; col++)
757
                                        if (bufferList[row][col] != null)
758
                                                bufferList[row][col].free();
759
                        return endBuffer;
760 13574 nacho
                } catch (NoninvertibleTransformException e) {
761
                        throw new InvalidSetViewException("No se ha podido aplicar la transformaci?n inversa para esa vista.");
762
                }
763 13377 nacho
        }
764
765 13540 nacho
        /*
766
         * (non-Javadoc)
767
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWindowRaster(int, int, int, int)
768
         */
769 30008 nbrodin
        public IBuffer getWindowRaster(int x, int y, int w, int h)
770 16590 nbrodin
                throws InvalidSetViewException, InterruptedException, RasterDriverException {
771 13540 nacho
                try {
772
                        Point2D p1 = mosaic[0][0].rasterToWorld(new Point2D.Double(x, y));
773
                        Point2D p2 = mosaic[0][0].rasterToWorld(new Point2D.Double(x + w, y + h));
774
                        MultiRasterDataset[][] datasetList = getDatasetListInArea(p1.getX(), p1.getY(), p2.getX(), p2.getY());
775
                        int n = mosaic.length;
776
                        int m = mosaic[0].length;
777
                        IBuffer[][] bufferList = new IBuffer[n][m];
778 30008 nbrodin
                        for (int row = 0; row < n; row++)
779
                                for (int col = 0; col < m; col++)
780 13540 nacho
                                        if(datasetList[row][col] != null) {
781
                                                int[] values = getLocalRequest(x, y, w, h, row, col);
782 30008 nbrodin
                                                int wTile = (Math.abs(values[2] - values[0]) > (int)datasetList[row][col].getWidth()) ? (int)datasetList[row][col].getWidth() : (int)Math.abs(values[2] - values[0]);
783
                                                int hTile = (Math.abs(values[3] - values[1]) > (int)datasetList[row][col].getHeight()) ? (int)datasetList[row][col].getHeight() : (int)Math.abs(values[3] - values[1]);
784
                                                if(values != null)
785 13540 nacho
                                                        bufferList[row][col] = datasetList[row][col].getWindowRaster(values[0], values[1], wTile, hTile);
786
                                        }
787 30008 nbrodin
                        IBuffer endBuffer = generateBuffer(bufferList, mosaic[0][0]
788
                                        .getBands().getDrawableBandsCount());
789
                        for (int row = 0; row < n; row++)
790
                                for (int col = 0; col < m; col++)
791
                                        if (bufferList[row][col] != null)
792
                                                bufferList[row][col].free();
793
                        return endBuffer;
794 13540 nacho
                } catch (NoninvertibleTransformException e) {
795
                        throw new InvalidSetViewException("No se ha podido aplicar la transformaci?n inversa para esa vista.");
796
                }
797 13377 nacho
        }
798
799 13540 nacho
        /*
800
         * (non-Javadoc)
801
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWindowRaster(int, int, int, int, int, int)
802
         */
803 30008 nbrodin
        public IBuffer getWindowRaster(int x, int y, int w, int h, int bufWidth, int bufHeight)
804 16590 nbrodin
                throws InvalidSetViewException, InterruptedException, RasterDriverException {
805 13540 nacho
                try {
806
                        Point2D p1 = mosaic[0][0].rasterToWorld(new Point2D.Double(x, y));
807
                        Point2D p2 = mosaic[0][0].rasterToWorld(new Point2D.Double(x + w, y + h));
808
                        MultiRasterDataset[][] datasetList = getDatasetListInArea(p1.getX(), p1.getY(), p2.getX(), p2.getY());
809
                        int n = mosaic.length;
810
                        int m = mosaic[0].length;
811
                        IBuffer[][] bufferList = new IBuffer[n][m];
812 30008 nbrodin
                        for (int row = 0; row < n; row++)
813
                                for (int col = 0; col < m; col++)
814 13540 nacho
                                        if(datasetList[row][col] != null) {
815
                                                int[] values = getLocalRequest(x, y, w, h, row, col);
816
                                                if(values != null) {
817 13783 nacho
                                                        int bW = Math.round((Math.abs(values[2] - values[0]) * bufWidth) / w);
818
                                                        int bH = Math.round((Math.abs(values[3] - values[1]) * bufHeight) / h);
819 30008 nbrodin
                                                        int wTile = (Math.abs(values[2] - values[0]) > (int)datasetList[row][col].getWidth()) ? (int)datasetList[row][col].getWidth() : (int)Math.abs(values[2] - values[0]);
820
                                                        int hTile = (Math.abs(values[3] - values[1]) > (int)datasetList[row][col].getHeight()) ? (int)datasetList[row][col].getHeight() : (int)Math.abs(values[3] - values[1]);
821 13540 nacho
                                                        bufferList[row][col] = datasetList[row][col].getWindowRaster(values[0], values[1], wTile, hTile, bW, bH);
822
                                                }
823
                                        }
824 30008 nbrodin
                        IBuffer endBuffer = generateBuffer(bufferList, mosaic[0][0]
825
                                        .getBands().getDrawableBandsCount());
826
                        for (int row = 0; row < n; row++)
827
                                for (int col = 0; col < m; col++)
828
                                        if (bufferList[row][col] != null)
829
                                                bufferList[row][col].free();
830
                        return endBuffer;
831 13540 nacho
                } catch (NoninvertibleTransformException e) {
832
                        throw new InvalidSetViewException("No se ha podido aplicar la transformaci?n inversa para esa vista.");
833
                }
834
        }
835 30008 nbrodin
836 13540 nacho
        /**
837
         * Convierte una petici?n global del mosaico en coordenadas pixel a una local al tile
838
         * concreto que se est? tratando
839
         * @param x Posici?n X de la petici?n a comprobar
840
         * @param y Posici?n Y de la petici?n a comprobar
841
         * @param w Ancho de la petici?n a comprobar
842
         * @param h Alto de la petici?n a comprobar
843
         * @param r File del tile
844
         * @param c Columna del tile
845 13554 nacho
         * @return cuatro valores correspondientes a la x1, y1, x2, y2 de la petici?n referente al tile.
846 13540 nacho
         */
847
        private int[] getLocalRequest(int x, int y, int w, int h, int r, int c) {
848
                Point2D p1 = null, p2 = null;
849 30008 nbrodin
850 16343 nbrodin
                if(!requestGoThroughTile(x, y, w, h, r, c))
851
                        return null;
852 30008 nbrodin
853 13540 nacho
                if(isInside(x, y, r, c))
854
                        p1 = getLocalPixel(x, y);
855 30008 nbrodin
                else if(getTileFromPixelPoint(x, y)[0] == r)  //Est? en la misma fila
856
                        p1 = new Point2D.Double(0, getLocalPixel(x, y).getY());
857
                else if(getTileFromPixelPoint(x, y)[1] == c) //Est? en la misma columna
858
                        p1 = new Point2D.Double(getLocalPixel(x, y).getX(), 0);
859
                else
860
                        p1 = new Point2D.Double(0, 0);
861 13540 nacho
                int fx = x + w;
862
                int fy = y + h;
863 30008 nbrodin
                if(isInside(fx, fy, r, c))
864 13540 nacho
                        p2 = getLocalPixel(fx, fy);
865 30008 nbrodin
                else if(getTileFromPixelPoint(fx, fy)[0] == r)
866
                        p2 = new Point2D.Double(mosaic[r][c].getWidth(), getLocalPixel(fx, fy).getY());
867
                else if(getTileFromPixelPoint(fx, fy)[1] == c)
868
                        p2 = new Point2D.Double(getLocalPixel(fx, fy).getX(), mosaic[r][c].getHeight());
869
                else
870
                        p2 = new Point2D.Double(mosaic[r][c].getWidth(), mosaic[r][c].getHeight());
871 13540 nacho
                if(p1 != null && p2 != null)
872
                        return new int[]{(int)p1.getX(), (int)p1.getY(), (int)p2.getX(), (int)p2.getY()};
873 13377 nacho
                return null;
874
        }
875 30008 nbrodin
876 13540 nacho
        /**
877 16343 nbrodin
         * Consulta si una petici?n en coordenadas pixel de la imagen completa pasa a trav?s de un
878
         * tile de posici?n r, c
879
         * @param x Posici?n X de la petici?n a comprobar
880
         * @param y Posici?n Y de la petici?n a comprobar
881
         * @param w Ancho de la petici?n a comprobar
882
         * @param h Alto de la petici?n a comprobar
883
         * @param r File del tile
884
         * @param c Columna del tile
885
         * @return true si la petici?n pasa a trav?s del tile y false si no pasa.
886
         */
887
        private boolean requestGoThroughTile(int x, int y, int w, int h, int r, int c) {
888
                if(isInside(x, y, r, c) || isInside(w, h, r, c))
889
                        return true;
890
                Point2D p1 = getGlobalPixel(x, y, r, c);
891
                Point2D p2 = getGlobalPixel(w, h, r, c);
892 30008 nbrodin
893
                //Intersecci?n entre el ?rea pedida y el tile. Si intersectan se devuelve true
894
                if(((x <= p1.getX() && w >= p1.getX()) || (x <= p2.getX() && w >= p2.getX())) &&
895
                         ((y <= p1.getY() && h >= p1.getY()) || (y <= p2.getY() && h >= p2.getY())))
896 16343 nbrodin
                        return true;
897
                return false;
898
        }
899 30008 nbrodin
900 16343 nbrodin
        /**
901 13540 nacho
         * Obtiene el tile correspondiente a un pixel dado
902
         * @param x Posici?n X del punto a comprobar
903
         * @param y Posici?n Y del punto a comprobar
904
         * @return Posici?n en fila-columna
905
         */
906
        public int[] getTileFromPixelPoint(int x, int y) {
907
                int n = mosaic.length;
908
                int m = mosaic[0].length;
909
                for (int row = 0; row < n; row++)
910
                        for (int col = 0; col < m; col++)
911
                                if(isInside(x, y, row, col))
912
                                        return new int[]{row, col};
913 13377 nacho
                return null;
914
        }
915 30008 nbrodin
916 13540 nacho
        /**
917
         * Comprueba si un punto (x, y) cae dentro de un Tile (r, c)
918
         * @param x Posici?n X del punto a comprobar
919
         * @param y Posici?n Y del punto a comprobar
920
         * @param r Fila del tile a comprobar
921
         * @param c Columna del tile a comprobar
922
         * @return true si el punto cae dentro del tile y false si no cae
923
         */
924
        private boolean isInside(int x, int y, int r, int c) {
925
                int posR = -1, posC = -1;
926
                int sum = 0;
927
                for (int row = 0; row < mosaic.length; row++) {
928
                        sum += mosaic[row][0].getHeight();
929
                        if(sum >= y) {
930
                                posR = row;
931
                                break;
932
                        }
933
                }
934
                sum = 0;
935
                for (int col = 0; col < mosaic[0].length; col++) {
936
                        sum += mosaic[0][col].getWidth();
937
                        if(sum >= x) {
938
                                posC = col;
939
                                break;
940
                        }
941
                }
942
                return (posR == r && posC == c) ? true : false;
943
        }
944 30008 nbrodin
945
946 13540 nacho
        /**
947
         * Obtiene la coordenada pixel local para un raster dada la coordenada pixel global
948
         * @param x Coordenada X global
949
         * @param y Coordenada Y global
950
         * @return Coordenada local
951
         */
952
        private Point2D getLocalPixel(int x, int y) {
953
                int w = 0, h = 0;
954
                int sum = 0;
955
                for (int row = 0; row < mosaic.length; row++) {
956
                        sum += mosaic[row][0].getHeight();
957
                        if(sum >= y) {
958
                                h = y - ((int)(sum - mosaic[row][0].getHeight()));
959
                                break;
960
                        }
961
                }
962
                sum = 0;
963
                for (int col = 0; col < mosaic[0].length; col++) {
964
                        sum += mosaic[0][col].getWidth();
965
                        if(sum >= x) {
966
                                w = x - ((int)(sum - mosaic[0][col].getWidth()));
967
                                break;
968
                        }
969
                }
970
                return new Point2D.Double(w, h);
971
        }
972 30008 nbrodin
973 16343 nbrodin
        /**
974
         * Obtiene la coordenada pixel global para un raster dada la coordenada pixel local
975
         * @param x Coordenada X local
976
         * @param y Coordenada Y local
977
         * @param r Fila del tile
978
         * @param c Columna del tile
979
         * @return Coordenada global
980
         */
981
        private Point2D getGlobalPixel(int x, int y, int r, int c) {
982
                int sumX = 0, sumY = 0;
983
                for (int row = 0; row < (r - 1); row++)
984
                        sumY += mosaic[row][0].getHeight();
985
                sumY += y;
986 30008 nbrodin
                for (int col = 0; col < (c - 1); col++)
987 16343 nbrodin
                        sumX += mosaic[0][col].getWidth();
988
                sumX += x;
989
                return new Point2D.Double(sumX, sumY);
990
        }
991 30008 nbrodin
992 13540 nacho
        /*
993
         * (non-Javadoc)
994
         * @see org.gvsig.raster.dataset.IRasterDataSource#setDrawableBands(int[])
995
         */
996
        public void setDrawableBands(int[] db) {
997
                int n = mosaic.length;
998
                int m = mosaic[0].length;
999 30008 nbrodin
                for (int row = 0; row < n; row++)
1000
                        for (int col = 0; col < m; col++)
1001 13540 nacho
                                mosaic[row][col].setDrawableBands(db);
1002
        }
1003 30008 nbrodin
1004 13540 nacho
        /*
1005
         * (non-Javadoc)
1006
         * @see org.gvsig.raster.dataset.IRasterDataSource#clearDrawableBands()
1007
         */
1008
        public void clearDrawableBands() {
1009
                int n = mosaic.length;
1010
                int m = mosaic[0].length;
1011 30008 nbrodin
                for (int row = 0; row < n; row++)
1012
                        for (int col = 0; col < m; col++)
1013 13540 nacho
                                mosaic[row][col].clearDrawableBands();
1014
        }
1015 30008 nbrodin
1016 13540 nacho
        /*
1017
         * (non-Javadoc)
1018
         * @see org.gvsig.raster.dataset.IRasterDataSource#addDrawableBand(int, int)
1019
         */
1020
        public void addDrawableBand(int posRasterBuf, int imageBand){
1021
                int n = mosaic.length;
1022
                int m = mosaic[0].length;
1023 30008 nbrodin
                for (int row = 0; row < n; row++)
1024
                        for (int col = 0; col < m; col++)
1025 13540 nacho
                                mosaic[row][col].addDrawableBand(posRasterBuf, imageBand);
1026
        }
1027 13552 nacho
1028
        /*
1029
         * (non-Javadoc)
1030
         * @see org.gvsig.raster.dataset.IRasterDataSource#getTransparencyFilesStatus()
1031
         */
1032
        public Transparency getTransparencyFilesStatus() {
1033
                if(mosaic != null && mosaic[0][0] != null)
1034
                        return mosaic[0][0].getTransparencyFilesStatus();
1035
                return new Transparency();
1036
        }
1037 30008 nbrodin
1038 13552 nacho
        /*
1039
         * (non-Javadoc)
1040
         * @see org.gvsig.raster.dataset.IRasterDataSource#getColorTables()
1041
         */
1042
        public ColorTable[] getColorTables() {
1043
                if(mosaic != null && mosaic[0][0] != null)
1044
                        return mosaic[0][0].getColorTables();
1045 13377 nacho
                return null;
1046
        }
1047 30008 nbrodin
1048 13590 nacho
        /*
1049
         * (non-Javadoc)
1050
         * @see org.gvsig.raster.hierarchy.IHistogramable#getHistogram()
1051
         */
1052 15598 bsanchez
        public Histogram getHistogram() throws InterruptedException, HistogramException {
1053 13590 nacho
                Histogram[][] histogram = new Histogram[mosaic.length][mosaic[0].length];
1054 30008 nbrodin
1055 13590 nacho
                try {
1056 30008 nbrodin
                        for (int i = 0; i < histogram.length; i++)
1057
                                for (int j = 0; j < histogram[i].length; j++) {
1058 13590 nacho
                                        histogram[i][j] = new DatasetListHistogram(mosaic[i][j]).getHistogram();
1059 30008 nbrodin
                                        if(i != 0 || j != 0)
1060 13590 nacho
                                                histogram[0][0].union(histogram[i][j]);
1061
                                }
1062
                        return histogram[0][0];
1063
                } catch (FileNotOpenException e) {
1064
                        throw new HistogramException("FileNotOpenException");
1065
                } catch (RasterDriverException e) {
1066
                        throw new HistogramException("RasterDriverException");
1067
                }
1068
        }
1069 30008 nbrodin
1070 13825 nacho
        /**
1071
         * Obtiene la paleta correspondiente a uno de los ficheros que forman el GeoMultiRasterFile
1072
         * @param i Posici?n del raster
1073
         * @return Paleta asociada a este o null si no tiene
1074
         */
1075
        public ColorTable getColorTable(int i){
1076
                return (mosaic != null) ? mosaic[0][0].getColorTable(i) : null;
1077 13377 nacho
        }
1078 30008 nbrodin
1079 13825 nacho
        /**
1080 30008 nbrodin
         * Obtiene la paleta correspondiente al nombre del fichero pasado por par?metro.
1081 13825 nacho
         * @param fileName Nombre del fichero
1082
         * @return Paleta o null si no la tiene
1083
         */
1084
        public ColorTable getColorTable(String fileName){
1085
                return (mosaic != null) ? mosaic[0][0].getColorTable(fileName) : null;
1086
        }
1087 30008 nbrodin
1088 14431 nbrodin
        /*
1089
         * (non-Javadoc)
1090
         * @see org.gvsig.raster.dataset.IRasterDataSource#getData(int, int, int)
1091
         */
1092 13540 nacho
        public Object getData(int x, int y, int band) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
1093 14431 nbrodin
                int[] posTile = getTileFromPixelPoint(x, y);
1094
                if(posTile != null) {
1095
                        RasterDataset dataset = mosaic[posTile[0]][posTile[1]].getDataset(0)[0];
1096
                        Point2D localPixel = getLocalPixel(x, y);
1097
                        return dataset.getData((int)localPixel.getX(), (int)localPixel.getY(), band);
1098
                }
1099 13377 nacho
                return null;
1100
        }
1101
1102 16539 nbrodin
        /*
1103
         * (non-Javadoc)
1104
         * @see org.gvsig.raster.dataset.IRasterDataSource#isReadOnly()
1105
         */
1106
        public boolean isReadOnly() {
1107
                return readOnly;
1108
        }
1109 13377 nacho
1110 16539 nbrodin
        /*
1111
         * (non-Javadoc)
1112
         * @see org.gvsig.raster.dataset.IRasterDataSource#setReadOnly(boolean)
1113
         */
1114
        public void setReadOnly(boolean readOnly) {
1115
                this.readOnly = readOnly;
1116
        }
1117 30008 nbrodin
1118 16539 nbrodin
        /*
1119
         * (non-Javadoc)
1120
         * @see org.gvsig.raster.dataset.IRasterDataSource#isMemoryBuffer()
1121
         */
1122
        public boolean isMemoryBuffer() {
1123
                return forceToMemory;
1124
        }
1125
1126
        /*
1127
         * (non-Javadoc)
1128
         * @see org.gvsig.raster.dataset.IRasterDataSource#setMemoryBuffer(boolean)
1129
         */
1130
        public void setMemoryBuffer(boolean memory) {
1131
                this.forceToMemory = memory;
1132
                if(memory)
1133
                        this.readOnly = false;
1134
        }
1135
1136 17233 nbrodin
        /*
1137
         * (non-Javadoc)
1138
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewCount(int)
1139
         */
1140
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
1141
                if(band >= getBandCount())
1142
                        throw new BandAccessException("Wrong band");
1143
                return 0;
1144
        }
1145
1146
        /*
1147
         * (non-Javadoc)
1148
         * @see org.gvsig.raster.dataset.IRasterDataSource#overviewsSupport()
1149
         */
1150
        public boolean overviewsSupport() {
1151
                return false;
1152
        }
1153 13377 nacho
1154 17661 bsanchez
        /*
1155
         * (non-Javadoc)
1156 20119 bsanchez
         * @see org.gvsig.raster.dataset.IRasterDataSource#getNoDataValue()
1157 17661 bsanchez
         */
1158 20119 bsanchez
        public double getNoDataValue() {
1159
                int n = mosaic.length;
1160
                int m = mosaic[0].length;
1161
                if ((n == 0) || (m == 0))
1162
                        return RasterLibrary.defaultNoDataValue;
1163
                return mosaic[0][0].getNoDataValue();
1164 17661 bsanchez
        }
1165 20119 bsanchez
1166 18482 nbrodin
        /*
1167
         * (non-Javadoc)
1168 20119 bsanchez
         * @see org.gvsig.raster.dataset.IRasterDataSource#resetNoDataValue()
1169 18482 nbrodin
         */
1170 20119 bsanchez
        public void resetNoDataValue() {
1171
                int n = mosaic.length;
1172
                int m = mosaic[0].length;
1173
                for (int row = 0; row < n; row++)
1174
                        for (int col = 0; col < m; col++)
1175
                                mosaic[row][col].resetNoDataValue();
1176 18482 nbrodin
        }
1177
1178
        /*
1179
         * (non-Javadoc)
1180 20119 bsanchez
         * @see org.gvsig.raster.dataset.IRasterDataSource#setNoDataValue(double)
1181 18482 nbrodin
         */
1182 20119 bsanchez
        public void setNoDataValue(double value) {
1183
                int n = mosaic.length;
1184
                int m = mosaic[0].length;
1185
                for (int row = 0; row < n; row++)
1186
                        for (int col = 0; col < m; col++)
1187
                                mosaic[row][col].setNoDataValue(value);
1188 18482 nbrodin
        }
1189 20116 bsanchez
1190 20119 bsanchez
        /*
1191
         * (non-Javadoc)
1192
         * @see org.gvsig.raster.dataset.IRasterDataSource#isNoDataEnabled()
1193
         */
1194
        public boolean isNoDataEnabled() {
1195
                int n = mosaic.length;
1196
                int m = mosaic[0].length;
1197
                if ((n == 0) || (m == 0))
1198
                        return false;
1199
                return mosaic[0][0].isNoDataEnabled();
1200 20116 bsanchez
        }
1201
1202 20119 bsanchez
        /*
1203
         * (non-Javadoc)
1204
         * @see org.gvsig.raster.dataset.IRasterDataSource#setNoDataEnabled(boolean)
1205
         */
1206
        public void setNoDataEnabled(boolean enabled) {
1207
                int n = mosaic.length;
1208
                int m = mosaic[0].length;
1209
                for (int row = 0; row < n; row++)
1210
                        for (int col = 0; col < m; col++)
1211
                                mosaic[row][col].setNoDataEnabled(enabled);
1212 20116 bsanchez
        }
1213 30008 nbrodin
1214 20998 bsanchez
        /*
1215
         * (non-Javadoc)
1216
         * @see org.gvsig.raster.dataset.IRasterDataSource#loadGeoPointsFromRmf()
1217
         */
1218 22337 bsanchez
        public GeoPointList loadGeoPointsFromRmf() throws IOException {
1219 20998 bsanchez
                throw new IOException("Not implemented yet");
1220
        }
1221
1222
        /*
1223
         * (non-Javadoc)
1224 22337 bsanchez
         * @see org.gvsig.raster.dataset.IRasterDataSource#saveGeoPointsToRmf(org.gvsig.raster.datastruct.GeoPointList)
1225 20998 bsanchez
         */
1226 22337 bsanchez
        public void saveGeoPointsToRmf(GeoPointList geoPoints) throws IOException {
1227 30008 nbrodin
                throw new IOException("Not implemented yet");
1228 20998 bsanchez
        }
1229 30008 nbrodin
1230 20998 bsanchez
        /*
1231
         * (non-Javadoc)
1232
         * @see org.gvsig.raster.dataset.IRasterDataSource#getDataset(java.lang.String)
1233
         */
1234
        public RasterDataset getDataset(String fileName) {
1235
                return null;
1236
        }
1237
1238
        /*
1239
         * (non-Javadoc)
1240
         * @see org.gvsig.raster.dataset.IRasterDataSource#getLastSelectedView()
1241
         */
1242
        public Extent getLastSelectedView() {
1243
                return null;
1244
        }
1245
1246
        /*
1247
         * (non-Javadoc)
1248
         * @see org.gvsig.raster.dataset.IRasterDataSource#getNameDatasetStringList(int, int)
1249
         */
1250
        public String[] getNameDatasetStringList(int i, int j) {
1251
                return null;
1252
        }
1253
1254
        /*
1255
         * (non-Javadoc)
1256
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWktProjection()
1257
         */
1258
        public String getWktProjection() throws RasterDriverException {
1259
                return null;
1260
        }
1261
1262
        /*
1263
         * (non-Javadoc)
1264
         * @see org.gvsig.raster.hierarchy.IHistogramable#getPercent()
1265
         */
1266
        public int getPercent() {
1267
                return 0;
1268
        }
1269 30008 nbrodin
1270 20998 bsanchez
        /*
1271
         * (non-Javadoc)
1272
         * @see org.gvsig.raster.dataset.IRasterDataSource#getColorInterpretation()
1273
         */
1274
        public DatasetColorInterpretation getColorInterpretation() {
1275
                return null;
1276
        }
1277
1278
        public void removeDataset(String fileName) {}
1279
        public void removeDataset(RasterDataset file) {}
1280
        public void saveGeoToRmf() throws IOException {}
1281
        public void saveRmfModification() throws IOException {}
1282
        public void setAffineTransform(AffineTransform transf) {}
1283
        public void resetPercent() {}
1284 22573 bsanchez
        public void saveObjectToRmf(int file, Class class1, Object value) throws RmfSerializerException {}
1285 22473 bsanchez
        public Object loadObjectFromRmf(Class class1, Object value) throws RmfSerializerException {
1286 22573 bsanchez
                return null;
1287 22473 bsanchez
        }
1288 17661 bsanchez
}