Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / CompositeDataset.java @ 13393

History | View | Annotate | Download (16.6 KB)

1
/* 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
import java.awt.geom.NoninvertibleTransformException;
23
import java.awt.geom.Point2D;
24
import java.io.IOException;
25

    
26
import org.cresques.cts.IProjection;
27
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
28
import org.gvsig.raster.dataset.properties.DatasetListStatistics;
29
import org.gvsig.raster.datastruct.ColorTable;
30
import org.gvsig.raster.datastruct.Extent;
31
import org.gvsig.raster.datastruct.Histogram;
32
import org.gvsig.raster.datastruct.HistogramException;
33
import org.gvsig.raster.datastruct.Transparency;
34
import org.gvsig.raster.util.MathUtils;
35
import org.gvsig.raster.util.RasterUtilities;
36

    
37
/**
38
 * Esta clase est? compuestas de multiples datasets formando una rejilla de NxM 
39
 * rasters. Un cliente de esta clase debe tener una visi?n de la rejilla como si fuese un 
40
 * solo raster, gestionando esta el acceso la imagen que corresponda en cada petici?n de 
41
 * usuario.
42
 * 
43
 * @version 29/08/2007
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 *
46
 */
47
public class CompositeDataset implements IRasterDataSource {
48
        
49
        private MultiRasterDataset[][][]          mosaic = null;
50
        
51
        /**
52
         * Constructor. Genera la estructura de n filas por n columnas por k niveles de rasters.
53
         * @param n N?mero de filas
54
         * @param m N?mero de columnas
55
         * @param k N?mero de niveles.
56
         */
57
        public CompositeDataset(int n, int m, int k) {
58
                mosaic = new MultiRasterDataset[k][n][m];
59
        }
60

    
61
        /**
62
         * Constructor. Genera la estructura de n filas por n columnas de rasters y
63
         * las asigna a los raster que se le pasan por par?metro.
64
         * @param n N?mero de filas
65
         * @param m N?mero de columnas
66
         */
67
        public CompositeDataset(MultiRasterDataset[][] mos) throws MosaicNotValidException {
68
                this.mosaic = new MultiRasterDataset[1][0][0];
69
                this.mosaic[0] = mos;
70
                if(!validateMosaic(mos)) {
71
                        this.mosaic = null;
72
                        throw new MosaicNotValidException("Extends no validos para montar un mosaico");
73
                }
74
        }
75
        
76
        /**
77
         * Constructor. Genera la estructura de n filas por n columnas por k niveles de rasters y
78
         * las asigna a los raster que se le pasan por par?metro.
79
         * @param n N?mero de filas
80
         * @param m N?mero de columnas
81
         * @param k N?mero de niveles
82
         */
83
        public CompositeDataset(MultiRasterDataset[][][] mos) throws MosaicNotValidException {
84
                this.mosaic = mos;
85
                if(!validateMosaic(mos)) {
86
                        this.mosaic = null;
87
                        throw new MosaicNotValidException("Extends no validos para montar un mosaico");
88
                }
89
        }
90
        
91
        /**
92
         * Abre un dataset pasando como par?metros la proyecci?n y un objeto identificador del dataset. Este
93
         * objeto puede ser una ruta a un fichero en disco. En este caso la extensi?n del fichero servir? para 
94
         * buscar el driver que lo gestiona. Si proporcionamos un array de cadenas se tratar?n como la ruta a N ficheros
95
         * de disco. Tambi?n puede ser un buffer de datos en memoria o cualquier otro objeto
96
         * que pueda aceptar un driver.  
97
         * @param proj PRoyecci?n
98
         * @param datasetOpenParam Par?metros al driver
99
         * @return RasterMultiDatset
100
         * @throws NotSupportedExtensionException
101
         * @throws RasterDriverException
102
         */
103
        public static CompositeDataset open(IProjection proj, Object datasetOpenParam) throws NotSupportedExtensionException, RasterDriverException {
104
                if(datasetOpenParam instanceof String[][]) {
105
                        String[][] param = (String[][])datasetOpenParam;
106
                        MultiRasterDataset[][][] mosaic = new MultiRasterDataset[1][param.length][param[0].length];
107
                        for (int i = 0; i < param.length; i++) {
108
                                for (int j = 0; j < param[i].length; j++) 
109
                                        mosaic[0][i][j] = MultiRasterDataset.open(proj, param[i][j]);
110
                        }
111
                        CompositeDataset cd;
112
                        try {
113
                                cd = new CompositeDataset(mosaic);
114
                        } catch (MosaicNotValidException e) {
115
                                return null;
116
                        }
117
                        return cd;
118
                }
119
                return null;
120
        }
121

    
122
        /**
123
         * Valida que los extends del mosaico sean validos, es decir, que sean correlativos
124
         * formando la matriz.
125
         * 
126
         * @param mos 
127
         * @throws MosaicNotValidException
128
         */
129
        private boolean validateMosaic(MultiRasterDataset[][] mosaic) {
130
                MultiRasterDataset[][][] m = new MultiRasterDataset[1][0][0];
131
                m[0] = mosaic;
132
                return validateMosaic(m);
133
        }
134
        
135
        /**
136
         * Valida que los extends del mosaico sean validos, es decir, que sean correlativos
137
         * formando la matriz. Adem?s tambi?n valida que el tama?o de pixel coincida en todos los
138
         * raster que forman el mosaico. 
139
         * 
140
         * @param mos 
141
         * @throws MosaicNotValidException
142
         */
143
        private boolean validateMosaic(MultiRasterDataset[][][] mosaic) {
144
                int k = mosaic.length;
145
                int n = mosaic[0].length;
146
                int m = mosaic[0][0].length;
147
                for (int level = 0; level < k; level++) {
148
                        //Comprobamos en Horizontal
149
                        for (int row = 0; row < n; row++) {
150
                                for (int col = 0; col < m; col++) {
151
                                        if(col != (m - 2)) {
152
                                                Extent a = mosaic[level][row][col].getExtent();
153
                                                Extent b = mosaic[level][row][col + 1].getExtent();
154
                                                if(((int)a.maxX()) != ((int)b.minX()))
155
                                                        return false;
156
                                                double psx = MathUtils.tailDecimals(mosaic[level][row][col].getPixelSizeX(), 2);
157
                                                double psx1 = MathUtils.tailDecimals(mosaic[level][row][col + 1].getPixelSizeX(), 2);
158
                                                double psy = MathUtils.tailDecimals(mosaic[level][row][col].getPixelSizeY(), 2);
159
                                                double psy1 = MathUtils.tailDecimals(mosaic[level][row][col + 1].getPixelSizeY(), 2);
160
                                                if(psx != psx1 || psy != psy1)
161
                                                        return false;
162
                                                if(mosaic[level][row][col].getBandCount() != mosaic[level][row][col + 1].getBandCount())
163
                                                        return false;
164
                                        }
165
                                }
166
                        }
167
                        
168
                        //Comprobamos en Vertical
169
                        for (int col = 0; col < m; col++) {
170
                                for (int row = 0; row < n; row++) {
171
                                        if(row != (n - 2)) {
172
                                                Extent a = mosaic[level][row][col].getExtent();
173
                                                Extent b = mosaic[level][row + 1][col].getExtent();
174
                                                if(((int)a.minY()) != ((int)b.maxY()))
175
                                                        return false;
176
                                                double psx = MathUtils.tailDecimals(mosaic[level][row][col].getPixelSizeX(), 2);
177
                                                double psx1 = MathUtils.tailDecimals(mosaic[level][row + 1][col].getPixelSizeX(), 2);
178
                                                double psy = MathUtils.tailDecimals(mosaic[level][row][col].getPixelSizeY(), 2);
179
                                                double psy1 = MathUtils.tailDecimals(mosaic[level][row + 1][col].getPixelSizeY(), 2);
180
                                                if(psx != psx1 || psy != psy1)
181
                                                        return false;
182
                                                if(mosaic[level][row][col].getBandCount() != mosaic[level][row + 1][col].getBandCount())
183
                                                        return false;
184
                                        }
185
                                }
186
                        }
187
                }
188
                
189
                return true;
190
        }
191

    
192
        /*
193
         * (non-Javadoc)
194
         * @see org.gvsig.raster.dataset.IRasterDataSource#addDataset(org.gvsig.raster.dataset.RasterDataset[])
195
         */
196
        public void addDataset(RasterDataset[] f) throws FileNotFoundInListException {
197
                if(mosaic != null) {
198
                        int n = mosaic[0].length;
199
                        int m = mosaic[0][0].length;
200
                        if(f.length == (n * m)) {
201
                                for (int i = 0; i < n; i++) {
202
                                        for (int j = 0; j < m; j++) {
203
                                                MultiRasterDataset mrd = new MultiRasterDataset();
204
                                                mrd.addDataset(new RasterDataset[]{f[i * n + j]});                
205
                                        }
206
                                }
207
                        }
208
                }
209
        }
210

    
211
        /*
212
         * (non-Javadoc)
213
         * @see org.gvsig.raster.dataset.IRasterDataSource#addDataset(java.lang.String[])
214
         */
215
        public void addDataset(String[] fileName) throws FileNotFoundInListException, NotSupportedExtensionException, RasterDriverException {
216
                if(mosaic != null) {
217
                        int n = mosaic[0].length;
218
                        int m = mosaic[0][0].length;
219
                        if(fileName.length == (n * m)) {
220
                                for (int i = 0; i < n; i++) {
221
                                        for (int j = 0; j < m; j++) {
222
                                                MultiRasterDataset mrd = new MultiRasterDataset();
223
                                                mrd.addDataset(new RasterDataset[]{RasterDataset.open(null, fileName[i * n + j])});                
224
                                        }
225
                                }
226
                        }
227
                }
228
        }
229

    
230
        /*
231
         * (non-Javadoc)
232
         * @see org.gvsig.raster.dataset.IRasterDataSource#calcSteps(double, double, double, double, double, double, int, int)
233
         */
234
        public int[] calcSteps(double dWorldTLX, double dWorldTLY, double dWorldBRX, double dWorldBRY, double nWidth, double nHeight, int bufWidth, int bufHeight) {
235
                if(mosaic != null)
236
                        return mosaic[0][0][0].calcSteps(dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY, nWidth, nHeight, bufWidth, bufHeight);
237
                return null;
238
        }
239

    
240
        /*
241
         * (non-Javadoc)
242
         * @see org.gvsig.raster.dataset.IRasterDataSource#close()
243
         */
244
        public void close() {
245
                int k = mosaic.length;
246
                int n = mosaic[0].length;
247
                int m = mosaic[0][0].length;
248
                for (int level = 0; level < k; level++) {
249
                        for (int row = 0; row < n; row++) {
250
                                for (int col = 0; col < m; col++) {
251
                                        mosaic[level][row][col].close();
252
                                }
253
                        }
254
                }
255
        }
256

    
257
        /*
258
         * (non-Javadoc)
259
         * @see org.gvsig.raster.dataset.IRasterDataSource#copy()
260
         */
261
        public IRasterDataSource copy() {
262
                int k = mosaic.length;
263
                int n = mosaic[0].length;
264
                int m = mosaic[0][0].length;
265
                MultiRasterDataset[][][] mrd = new MultiRasterDataset[k][n][m];
266
                for (int level = 0; level < k; level++) {
267
                        for (int row = 0; row < n; row++) {
268
                                for (int col = 0; col < m; col++) {
269
                                        mrd[level][row][col] = (MultiRasterDataset)mosaic[level][row][col].copy();
270
                                }
271
                        }
272
                }
273
                try {
274
                        return new CompositeDataset(mrd);
275
                } catch (MosaicNotValidException e) {
276
                        return null;
277
                }
278
        }
279

    
280
        /*
281
         * (non-Javadoc)
282
         * @see org.gvsig.raster.dataset.IRasterDataSource#getAffineTransform()
283
         */
284
        public AffineTransform getAffineTransform() {
285
                if(mosaic != null && mosaic[0][0][0] != null)
286
                        return mosaic[0][0][0].getAffineTransform();
287
                return new AffineTransform();
288
        }
289
        
290
        /*
291
         * (non-Javadoc)
292
         * @see org.gvsig.raster.dataset.IRasterDataSource#getExtent()
293
         */
294
        public Extent getExtent() {
295
                int n = mosaic[0].length;
296
                int m = mosaic[0][0].length;
297
                if(mosaic != null && mosaic[0][0][0] != null) {
298
                        double ulx = mosaic[0][0][0].getExtent().getULX();
299
                        double uly = mosaic[0][0][0].getExtent().getULY();
300
                        
301
                        double urx = mosaic[0][n - 1][0].getExtent().getURX();
302
                        double ury = mosaic[0][n - 1][0].getExtent().getURY();
303
                        
304
                        double llx = mosaic[0][0][m - 1].getExtent().getLLX();
305
                        double lly = mosaic[0][0][m - 1].getExtent().getLLY();
306
                        
307
                        double lrx = mosaic[0][n - 1][m - 1].getExtent().getLRX();
308
                        double lry = mosaic[0][n - 1][m - 1].getExtent().getLRY();
309
                        
310
                        return new Extent(        new Point2D.Double(ulx, uly), 
311
                                                                        new Point2D.Double(lrx, lry), 
312
                                                                        new Point2D.Double(urx, ury), 
313
                                                                        new Point2D.Double(llx, lly));
314
                }
315
                return null;
316
        }
317

    
318
        /*
319
         * (non-Javadoc)
320
         * @see org.gvsig.raster.dataset.IRasterDataSource#getBandCount()
321
         */
322
        public int getBandCount() {
323
                if(mosaic != null && mosaic[0][0][0] != null)
324
                        return mosaic[0][0][0].getBandCount();
325
                return 0;
326
        }
327

    
328
        /*
329
         * (non-Javadoc)
330
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWidth()
331
         */
332
        public double getWidth() {
333
                double w = 0;
334
                if(mosaic != null && mosaic[0][0][0] != null) {
335
                        int m = mosaic[0][0].length;
336
                        for (int col = 0; col < m; col++)
337
                                w += mosaic[0][0][col].getWidth();
338
                }
339
                return 0;
340
        }
341
        
342
        /*
343
         * (non-Javadoc)
344
         * @see org.gvsig.raster.dataset.IRasterDataSource#getHeight()
345
         */
346
        public double getHeight() {
347
                double h = 0;
348
                if(mosaic != null && mosaic[0][0][0] != null) {
349
                        int n = mosaic[0].length;
350
                        for (int row = 0; row < n; row++)
351
                                h += mosaic[0][row][0].getHeight();
352
                }
353
                return 0;
354
        }
355

    
356
        /*
357
         * (non-Javadoc)
358
         * @see org.gvsig.raster.dataset.IRasterDataSource#getDataType()
359
         */
360
        public int[] getDataType() {
361
                if(mosaic != null && mosaic[0][0][0] != null)
362
                        return mosaic[0][0][0].getDataType();
363
                return null;
364
        }
365
        
366
        /*
367
         * (non-Javadoc)
368
         * @see org.gvsig.raster.dataset.IRasterDataSource#getDatasetCount()
369
         */
370
        public int getDatasetCount() {
371
                if(mosaic != null && mosaic[0][0][0] != null)
372
                        return mosaic[0][0][0].getDatasetCount();
373
                return 0;
374
        }
375

    
376
        /*
377
         * (non-Javadoc)
378
         * @see org.gvsig.raster.dataset.IRasterDataSource#getFileSize()
379
         */
380
        public long getFileSize() {
381
                long size = 0;
382
                int k = mosaic.length;
383
                int n = mosaic[0].length;
384
                int m = mosaic[0][0].length;
385
                for (int level = 0; level < k; level++) {
386
                        for (int row = 0; row < n; row++) {
387
                                for (int col = 0; col < m; col++) {
388
                                        size += mosaic[level][row][col].getFileSize();
389
                                }
390
                        }
391
                }
392
                return size;
393
        }
394
        
395
        /**
396
         * Obtiene el dataset cuyas coordenadas contienen el punto pasado por par?meto
397
         * @param x Coordenada X a comprobar
398
         * @param y Coordenada Y a comprobar
399
         * @return Point2D Posici?n del MultiRasterDataset dentro del mosaico
400
         */
401
        private Point2D getDatasetByCoords(double x, double y) {
402
                int k = mosaic.length;
403
                int n = mosaic[0].length;
404
                int m = mosaic[0][0].length;
405
                for (int level = 0; level < k; level++) {
406
                        for (int row = 0; row < n; row++) {
407
                                for (int col = 0; col < m; col++) {
408
                                        if(RasterUtilities.isInside(new Point2D.Double(x, y), 
409
                                                                                                mosaic[level][row][col].getExtent(), 
410
                                                                                                mosaic[level][row][col].getAffineTransform())) 
411
                                                return new Point2D.Double(row, col);
412
                                }
413
                        }
414
                }
415
                return null;
416
        }
417

    
418
        /*
419
         * (non-Javadoc)
420
         * @see org.gvsig.raster.dataset.IRasterDataSource#isInside(java.awt.geom.Point2D)
421
         */
422
        public boolean isInside(Point2D p) {
423
                return RasterUtilities.isInside(p, getExtent(), getAffineTransform());
424
        }
425

    
426
        /*
427
         * (non-Javadoc)
428
         * @see org.gvsig.raster.dataset.IRasterDataSource#rasterToWorld(java.awt.geom.Point2D)
429
         */
430
        public Point2D rasterToWorld(Point2D pt) {
431
                Point2D p = new Point2D.Double();
432
                getAffineTransform().transform(pt, p);
433
                return p;
434
        }
435
        
436
        /*
437
         * (non-Javadoc)
438
         * @see org.gvsig.raster.dataset.IRasterDataSource#worldToRaster(java.awt.geom.Point2D)
439
         */
440
        public Point2D worldToRaster(Point2D pt) {
441
                Point2D p = new Point2D.Double();
442
                try {
443
                        getAffineTransform().inverseTransform(pt, p);
444
                } catch (NoninvertibleTransformException e) {
445
                        return pt;
446
                }
447
                return p;
448
        }
449

    
450
        /*
451
         * (non-Javadoc)
452
         * @see org.gvsig.raster.dataset.IRasterDataSource#isRotated()
453
         */
454
        public boolean isRotated() {
455
                if(getAffineTransform().getShearX() != 0 || getAffineTransform().getShearY() != 0)
456
                        return true;
457
                return false;
458
        }
459

    
460
        /*
461
         * (non-Javadoc)
462
         * @see org.gvsig.raster.dataset.IRasterDataSource#isGeoreferenced()
463
         */
464
        public boolean isGeoreferenced() {
465
                //Este tipo de datasets siempre est? georreferenciado
466
                return true;
467
        }
468
        
469
        public BandList getBands() {
470
                return null;
471
        }
472

    
473
        public DatasetColorInterpretation getColorInterpretation(int dataset) {
474
                return null;
475
        }
476

    
477
        public ColorTable getColorTable(String fileName) {
478
                return null;
479
        }
480

    
481
        public ColorTable[] getColorTables() {
482
                return null;
483
        }
484

    
485
        public Object getData(int x, int y, int band) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
486
                return null;
487
        }
488

    
489
        public RasterDataset[] getDataset(int i) {
490
                return null;
491
        }
492

    
493
        public RasterDataset getDataset(String fileName) {
494
                return null;
495
        }
496

    
497
        public Extent getLastSelectedView() {
498
                return null;
499
        }
500

    
501
        public String[] getNameDatasetStringList(int i, int j) {
502
                return null;
503
        }
504

    
505
        public DatasetListStatistics getStatistics() {
506
                return null;
507
        }
508

    
509
        public Transparency getTransparencyFilesStatus() {
510
                return null;
511
        }
512

    
513

    
514

    
515
        public IBuffer getWindowRaster(double ulx, double uly, double lrx, double lry) throws InvalidSetViewException {
516
                return null;
517
        }
518

    
519
        public IBuffer getWindowRaster(double ulx, double uly, double w, double h, boolean adjustToExtent) throws InvalidSetViewException {
520
                return null;
521
        }
522

    
523
        public IBuffer getWindowRaster(double ulx, double uly, double lrx, double lry, int bufWidth, int bufHeight, boolean adjustToExtent) throws InvalidSetViewException {
524
                return null;
525
        }
526

    
527
        public IBuffer getWindowRaster(int x, int y, int w, int h) throws InvalidSetViewException {
528
                return null;
529
        }
530

    
531
        public IBuffer getWindowRaster(int x, int y, int w, int h, int bufWidth, int bufHeight) throws InvalidSetViewException {
532
                return null;
533
        }
534

    
535
        public String getWktProjection() throws RasterDriverException {
536
                return null;
537
        }
538

    
539
        public void removeDataset(String fileName) {
540
        }
541

    
542
        public void removeDataset(RasterDataset file) {
543
        }
544

    
545
        public void saveGeoToRmf() throws IOException {
546
        }
547

    
548
        public void saveRmfModification() throws IOException {
549
        }
550

    
551
        public void setAffineTransform(AffineTransform transf) {
552
        }
553

    
554
        
555
        public Histogram getHistogram() throws HistogramException {
556
                return null;
557
        }
558

    
559
        public int getPercent() {
560
                return 0;
561
        }
562

    
563
        public void resetPercent() {
564
        }
565

    
566
        public boolean isCanceled(int process) {
567
                return false;
568
        }
569

    
570
        public void setCanceled(boolean value, int process) {
571
        }
572

    
573
}