Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / store / DefaultListRasterStore.java @ 186

History | View | Annotate | Download (33.5 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.impl.store;
23

    
24
import java.awt.geom.AffineTransform;
25
import java.awt.geom.NoninvertibleTransformException;
26
import java.awt.geom.Point2D;
27
import java.io.IOException;
28
import java.util.ArrayList;
29

    
30
import org.cresques.cts.IProjection;
31
import org.gvsig.fmap.dal.coverage.RasterLibrary;
32
import org.gvsig.fmap.dal.coverage.RasterLocator;
33
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
34
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
35
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
36
import org.gvsig.fmap.dal.coverage.exception.BandAccessException;
37
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
38
import org.gvsig.fmap.dal.coverage.exception.HistogramException;
39
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
40
import org.gvsig.fmap.dal.coverage.exception.MosaicNotValidException;
41
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
42
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
43
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
44
import org.gvsig.fmap.dal.coverage.store.ListRasterStore;
45
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
46
import org.gvsig.fmap.dal.coverage.store.RasterStoreParameters;
47
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
48
import org.gvsig.fmap.dal.coverage.store.props.Histogram;
49
import org.gvsig.fmap.dal.coverage.store.props.Metadata;
50
import org.gvsig.fmap.dal.coverage.store.props.SerialInfo;
51
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
52
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
53
import org.gvsig.fmap.dal.coverage.util.RasterUtils;
54
import org.gvsig.fmap.dal.exception.CloseException;
55
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProvider;
56
import org.gvsig.metadata.exceptions.MetadataException;
57
import org.gvsig.raster.impl.DefaultRasterManager;
58
import org.gvsig.raster.impl.datastruct.BandListImpl;
59
import org.gvsig.raster.impl.datastruct.ExtentImpl;
60
import org.gvsig.raster.impl.provider.RasterProvider;
61
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
62
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
63
import org.gvsig.raster.impl.store.properties.MosaicDataStoreHistogram;
64
/**
65
 * Esta clase est? compuestas de multiples datasets formando una rejilla de NxM
66
 * rasters. Un cliente de esta clase debe tener una visi?n de la rejilla como si
67
 * fuese un solo raster, gestionando esta el acceso la imagen que corresponda en
68
 * cada petici?n de usuario.
69
 *
70
 * @version 29/08/2007
71
 * @author Nacho Brodin (nachobrodin@gmail.com)
72
 */
73
public class DefaultListRasterStore extends AbstractRasterDataStore implements ListRasterStore {
74
        private ArrayList<RasterDataStore>    list          = null;
75
        private Statistics                    stats         = null;
76
        private BandListImpl                  bandList      = null;
77
        private boolean                       readOnly      = false;
78
        /**
79
         * Flag que fuerza al buffer en memoria
80
         */            
81
        private boolean                       forceToMemory = false;
82
        private int                           percent       = 0;
83

    
84
        /**
85
         * Constructor. Genera la estructura de n filas por n columnas de rasters.
86
         */
87
        public DefaultListRasterStore() {
88
                bandList = new BandListImpl();
89
                list = new ArrayList<RasterDataStore>();
90
        }
91

    
92
        /**
93
         * Constructor. Genera la estructura de n filas por n columnas de rasters y
94
         * las asigna a los raster que se le pasan por par?metro.
95
         * @param n N?mero de filas
96
         * @param m N?mero de columnas
97
         */
98
        public DefaultListRasterStore(RasterDataStore mos) throws MosaicNotValidException {
99
                bandList = (BandListImpl)mos.getBands();
100
                
101
                list = new ArrayList<RasterDataStore>();
102
                list.add(mos);
103
        }
104
        
105
        /**
106
         * Constructor. Genera la estructura de n filas por n columnas de rasters y
107
         * las asigna a los raster que se le pasan por par?metro.
108
         * @param n N?mero de filas
109
         * @param m N?mero de columnas
110
         */
111
        public DefaultListRasterStore(ArrayList<RasterDataStore> mos) throws MosaicNotValidException {
112
                bandList = (BandListImpl)mos.get(0).getBands();
113
                list = mos;
114
        }
115
        
116
        /*
117
         * (non-Javadoc)
118
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#newDataStore()
119
         */
120
        public RasterDataStore newDataStore() {
121
                DefaultListRasterStore newList = new DefaultListRasterStore();
122
                for (int i = 0; i < list.size(); i++) {
123
                        newList.addTile(list.get(i).newDataStore());
124
                }
125
                return newList;
126
        }
127

    
128
        /*
129
         * (non-Javadoc)
130
         * @see org.gvsig.fmap.dal.DataStore#getName()
131
         */
132
        public String getName() {
133
                if(list.size() >= 1) 
134
                        return list.get(0).getName();
135
                return null;
136
        }
137
        
138
        /*
139
         * (non-Javadoc)
140
         * @see org.gvsig.raster.impl.dataset.QueryableRaster#getProviders()
141
         */
142
        public ArrayList<RasterProvider> getProviders() {
143
                ArrayList<RasterProvider> provList = new ArrayList<RasterProvider>();
144
                for (int i = 0; i < list.size(); i++) {
145
                        provList.add(((QueryableRaster)list.get(0)).getProviders().get(0));
146
                }
147
                return provList;
148
        }
149
        
150
        /*
151
         * (non-Javadoc)
152
         * @see org.gvsig.raster.impl.dataset.QueryableRaster#getProvider(int)
153
         */
154
        public RasterProvider getProvider(int i) {
155
                if(list == null)
156
                        return null;
157
                return (RasterProvider)list.get(i).getProvider();
158
        }
159
        
160
        /*
161
         * (non-Javadoc)
162
         * @see org.gvsig.raster.impl.dataset.QueryableRaster#getDrawableBands()
163
         */
164
        public int[] getDrawableBands() {
165
                return getBands().getDrawableBands();
166
        }
167

    
168
        /*
169
         * (non-Javadoc)
170
         * @see org.gvsig.fmap.dal.coverage.store.ListRasterStore#addTile(org.gvsig.fmap.dal.coverage.store.RasterDataStore)
171
         */
172
        public void addTile(RasterDataStore store) {
173
                this.bandList = (BandListImpl)store.getBands();
174
                list.add(store);
175
        }
176

    
177
        /**
178
         * Obtiene la lista de nombres de los dataset
179
         * @return
180
         */
181
        public String[] getFileNames() {
182
                String[] s = new String[list.size()];
183
                for (int i = 0; i < s.length; i++)
184
                                if(list.get(i) != null)
185
                                        s[i] = ((RasterStoreParameters)list.get(i).getParameters()).getURI();
186
                return s;
187
        }
188

    
189
        /*
190
         * (non-Javadoc)
191
         * @see org.gvsig.raster.impl.dataset.QueryableRaster#calcSteps(double, double, double, double, double, double, int, int)
192
         */
193
        public double[] calcSteps(double dWorldTLX, double dWorldTLY, double dWorldBRX, double dWorldBRY, double nWidth, double nHeight, int bufWidth, int bufHeight) {
194
                return ((QueryableRaster)list.get(0)).calcSteps(dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY, nWidth, nHeight, bufWidth, bufHeight);
195
        }
196

    
197
        /*
198
         * (non-Javadoc)
199
         * @see org.gvsig.fmap.dal.raster.impl.DefaultCoverageStore#close()
200
         */
201
        public void close() throws CloseException {
202
                for (int i = 0; i < list.size(); i++) {
203
                        list.get(i).close();
204
                }
205
                list.clear();
206
                bandList.clear();
207
        }
208

    
209

    
210
        /*
211
         * (non-Javadoc)
212
         * @see org.gvsig.raster.dataset.IRasterDataSource#getOwnAffineTransform()
213
         */
214
        public AffineTransform getOwnAffineTransform() {
215
                return list.get(0).getOwnAffineTransform();
216
        }
217

    
218
        /*
219
         * (non-Javadoc)
220
         * @see org.gvsig.raster.dataset.IRasterDataSource#getAffineTransform()
221
         */
222
        public AffineTransform getAffineTransform() {
223
                return list.get(0).getAffineTransform();
224
        }
225

    
226
        /*
227
         * (non-Javadoc)
228
         * @see org.gvsig.raster.dataset.IRasterDataSource#getExtent()
229
         */
230
        public Extent getExtent() {
231
                //Ojo! no es rotable
232
                double ulx = 0;
233
                double uly = 0;
234
                double urx = 0;
235
                double ury = 0;
236
                double llx = 0;
237
                double lly = 0;
238
                double lrx = 0;
239
                double lry = 0;
240

    
241
                for (int i = 0; i < list.size(); i++) {
242
                        ulx = list.get(i).getExtent().getULX() < ulx ? list.get(i).getExtent().getULX() : ulx;
243
                        uly = list.get(i).getExtent().getULY() > uly ? list.get(i).getExtent().getULY() : uly;
244
                        urx = list.get(i).getExtent().getURX() > urx ? list.get(i).getExtent().getURX() : urx;
245
                        ury = list.get(i).getExtent().getURY() > ury ? list.get(i).getExtent().getURY() : ury;
246
                        llx = list.get(i).getExtent().getLLX() > llx ? list.get(i).getExtent().getLLX() : llx;
247
                        lly = list.get(i).getExtent().getLLY() < lly ? list.get(i).getExtent().getLLY() : lly;
248
                        lrx = list.get(i).getExtent().getLRX() < lrx ? list.get(i).getExtent().getLRX() : lrx;
249
                        lry = list.get(i).getExtent().getLRY() < lry ? list.get(i).getExtent().getLRY() : lry;
250
                }
251

    
252
                return new ExtentImpl(        new Point2D.Double(ulx, uly),
253
                                new Point2D.Double(lrx, lry),
254
                                new Point2D.Double(urx, ury),
255
                                new Point2D.Double(llx, lly));
256
        }
257

    
258
        /*
259
         * (non-Javadoc)
260
         * @see org.gvsig.raster.dataset.IRasterDataSource#getBandCount()
261
         */
262
        public int getBandCount() {
263
                return list.get(0).getBandCount();
264
        }
265

    
266
        /*
267
         * (non-Javadoc)
268
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getWidth()
269
         */
270
        public double getWidth() {
271
                double wReal = (getExtent().getMax().getX() - getExtent().getMin().getX());
272
                return Math.abs(wReal / getPixelSizeY());
273
        }
274

    
275
        /*
276
         * (non-Javadoc)
277
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getHeight()
278
         */
279
        public double getHeight() {
280
                double hReal = (getExtent().getMax().getY() - getExtent().getMin().getY());
281
                return Math.abs(hReal / getPixelSizeX());
282
        }
283

    
284
        /*
285
         * (non-Javadoc)
286
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getCellSize()
287
         */
288
        public double getCellSize() {
289
                try {
290
                        Extent e = getExtent();
291
                        double dCellsize = (e.getMax().getX() - e.getMin().getX() ) / getWidth();
292
                        return dCellsize;
293
                } catch (NullPointerException e) {
294
                        return 1;
295
                }
296
        }
297

    
298
        /*
299
         * (non-Javadoc)
300
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getDataType()
301
         */
302
        public int[] getDataType() {
303
                return list.get(0).getDataType();
304
        }
305

    
306
        /*
307
         * (non-Javadoc)
308
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getDataStoreCount()
309
         */
310
        public int getDataStoreCount() {
311
                return list.get(0).getDataStoreCount();
312
        }
313

    
314
        /*
315
         * (non-Javadoc)
316
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getFileSize()
317
         */
318
        public long getFileSize() {
319
                long size = 0;
320
                for (int i = 0; i < list.size(); i++) {
321
                        size += list.get(i).getFileSize();
322
                }
323
                return size;
324
        }
325

    
326
        /**
327
         * Obtiene el dataset cuyas coordenadas contienen el punto pasado por par?meto
328
         * @param x Coordenada X a comprobar
329
         * @param y Coordenada Y a comprobar
330
         * @return Point2D Posici?n del MultiRasterDataset dentro del mosaico
331
         */
332
        public int getDatasetByCoords(double x, double y) {
333
                RasterUtils util = RasterLocator.getManager().getRasterUtils();
334
                for (int i = 0; i < list.size(); i++) {
335
                        if(util.isInside(new Point2D.Double(x, y), list.get(i).getExtent(), list.get(i).getAffineTransform()))
336
                                return i;
337
                }
338
                return -1;
339
        }
340

    
341
        /**
342
         * Obtiene la lista de datasets del mosaico que intersectan con el extent proporcionado
343
         * @param ulx Coordenada X superior izquierda
344
         * @param uly Coordenada Y superior izquierda
345
         * @param lrx Coordenada X inferior derecha
346
         * @param lry Coordenada Y inferior derecha
347
         * @return QueryableRaster[][][]
348
         * @throws NoninvertibleTransformException
349
         */
350
        private ArrayList<QueryableRaster> getDatasetListInArea(double ulx, double uly, double lrx, double lry) throws NoninvertibleTransformException {
351
                RasterUtils util = RasterLocator.getManager().getRasterUtils();
352

    
353
                ArrayList<QueryableRaster> result = new ArrayList<QueryableRaster>();
354

    
355
                for (int i = 0; i < list.size(); i++) {
356
                        if(util.intersects(new ExtentImpl(ulx, uly, lrx, lry), list.get(i).getExtent(), list.get(i).getAffineTransform()))
357
                                result.add((QueryableRaster)list.get(i));
358
                }
359
                return result;
360
        }
361

    
362
        /*
363
         * (non-Javadoc)
364
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isInside(java.awt.geom.Point2D)
365
         */
366
        public boolean isInside(Point2D p) {
367
                return RasterLocator.getManager().getRasterUtils().isInside(p, getExtent(), getAffineTransform());
368
        }
369

    
370
        /*
371
         * (non-Javadoc)
372
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#rasterToWorld(java.awt.geom.Point2D)
373
         */
374
        public Point2D rasterToWorld(Point2D pt) {
375
                Point2D p = new Point2D.Double();
376
                getAffineTransform().transform(pt, p);
377
                return p;
378
        }
379

    
380
        /*
381
         * (non-Javadoc)
382
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#worldToRaster(java.awt.geom.Point2D)
383
         */
384
        public Point2D worldToRaster(Point2D pt) {
385
                Point2D p = new Point2D.Double();
386
                try {
387
                        getAffineTransform().inverseTransform(pt, p);
388
                } catch (NoninvertibleTransformException e) {
389
                        return pt;
390
                }
391
                return p;
392
        }
393

    
394
        /*
395
         * (non-Javadoc)
396
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isRotated()
397
         */
398
        public boolean isRotated() {
399
                if(getAffineTransform().getShearX() != 0 || getAffineTransform().getShearY() != 0)
400
                        return true;
401
                return false;
402
        }
403

    
404
        /*
405
         * (non-Javadoc)
406
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isGeoreferenced()
407
         */
408
        public boolean isGeoreferenced() {
409
                //Este tipo de datasets siempre est? georreferenciado
410
                return true;
411
        }
412

    
413
        /*
414
         * (non-Javadoc)
415
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getBands()
416
         */
417
        public BandList getBands() {
418
                return bandList;
419
        }
420

    
421
        /**
422
         * Copia un tile en el buffer que contendr? todos los tiles
423
         * @param origin Buffer de origen
424
         * @param dest Buffer de destino
425
         * @param col Columna del buffer de destino donde se empieza a escribir
426
         * @param row Fila del buffer de destino donde se empieza a escribir
427
         * @return array con los valores que representan la ?ltima fila y
428
         * ?ltima columna que se escribieron
429
         */
430
        @SuppressWarnings("unused")
431
        private void copyTile(Buffer origin, Buffer dest, int r, int c) {
432
                switch(origin.getDataType()) {
433
                case Buffer.TYPE_BYTE :
434
                        for (int band = 0; band < origin.getBandCount(); band++)
435
                                for (int row = 0; row < origin.getHeight(); row++)
436
                                        for (int col = 0; col < origin.getWidth(); col++)
437
                                                try {
438
                                                                dest.setElem(row + r, col + c, band, origin.getElemByte(row, col, band));
439
                                                        } catch (ArrayIndexOutOfBoundsException e) {break;}
440
                        break;
441
                case Buffer.TYPE_SHORT :
442
                        for (int band = 0; band < origin.getBandCount(); band++)
443
                                for (int row = 0; row < origin.getHeight(); row++)
444
                                        for (int col = 0; col < origin.getWidth(); col++)
445
                                                try {
446
                                                                dest.setElem(row + r, col + c, band, origin.getElemShort(row, col, band));
447
                                                        } catch (ArrayIndexOutOfBoundsException e) {break;}
448
                        break;
449
                case Buffer.TYPE_FLOAT :
450
                        for (int band = 0; band < origin.getBandCount(); band++)
451
                                for (int row = 0; row < origin.getHeight(); row++)
452
                                        for (int col = 0; col < origin.getWidth(); col++)
453
                                                try {
454
                                                                dest.setElem(row + r, col + c, band, origin.getElemFloat(row, col, band));
455
                                                        } catch (ArrayIndexOutOfBoundsException e) {break;}
456
                        break;
457
                case Buffer.TYPE_DOUBLE:
458
                        for (int band = 0; band < origin.getBandCount(); band++)
459
                                for (int row = 0; row < origin.getHeight(); row++)
460
                                        for (int col = 0; col < origin.getWidth(); col++)
461
                                                try {
462
                                                                dest.setElem(row + r, col + c, band, origin.getElemDouble(row, col, band));
463
                                                        } catch (ArrayIndexOutOfBoundsException e) {break;}
464
                        break;
465
                }
466
        }
467

    
468
        /*
469
         * (non-Javadoc)
470
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWindowRaster(double, double, double, double)
471
         */
472
        public Buffer getWindowRaster(double ulx, double uly, double lrx, double lry)
473
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
474
                try {
475
                        Point2D p1 = new Point2D.Double(ulx, uly);
476
                        Point2D p2 = new Point2D.Double(lrx, lry);
477
                        
478
                        //Para el valor mayor redondeamos por arriba. Para el valor menor redondeamos por abajo.
479
                        double p1X = (p1.getX() > p2.getX()) ? Math.ceil(p1.getX()) : Math.floor(p1.getX());
480
                        double p1Y = (p1.getY() > p2.getY()) ? Math.ceil(p1.getY()) : Math.floor(p1.getY());
481
                        double p2X = (p2.getX() > p1.getX()) ? Math.ceil(p2.getX()) : Math.floor(p2.getX());
482
                        double p2Y = (p2.getY() > p1.getY()) ? Math.ceil(p2.getY()) : Math.floor(p2.getY());
483

    
484
                        int width = (int)Math.abs(p1X - p2X);
485
                        int height = (int)Math.abs(p1Y - p2Y);
486

    
487
                        //Ajustamos por si nos hemos salido del raster
488
                        if(((int)(Math.min(p1X, p2X) + width)) > getWidth())
489
                                width = (int)(getWidth() - Math.min(p1X, p2X));
490
                        if(((int)(Math.min(p1Y, p2Y) + height)) > getHeight())
491
                                height = (int)(getHeight() - Math.min(p1Y, p2Y));
492
                        
493
                        ArrayList<QueryableRaster> datasetList = getDatasetListInArea(p1.getX(), p1.getY(), p2.getX(), p2.getY());
494
                        Buffer endBuffer = DefaultRasterManager.getInstance().createBuffer(getDataType()[0], width, height, bandList.getDrawableBandsCount(), true);
495
                        
496
                        for (int i = 0; i < list.size(); i++) {
497
                                Extent e = datasetList.get(i).adjustToExtent(new ExtentImpl(ulx, uly, lrx, lry));
498
                                Buffer buf = datasetList.get(i).getWindowRaster(e.getULX(), e.getULY(), e.getLRX(), e.getLRY());
499
                                Point2D pa = datasetList.get(i).worldToRaster(new Point2D.Double(e.getULX(), e.getULY()));
500
                                double ulPixX = (ulx * pa.getX()) / e.getULX();
501
                                double ulPixY = (uly * pa.getY()) / e.getULY();
502
                                copyBuffer(buf, endBuffer, pa, new Point2D.Double(ulPixX, ulPixY), buf.getWidth(), buf.getHeight());
503
                                buf.free();
504
                        }
505
                        return endBuffer;
506
                } catch (NoninvertibleTransformException e) {
507
                        throw new InvalidSetViewException("No se ha podido aplicar la transformaci?n inversa para esa vista.");
508
                }
509
        }
510

    
511
        /*
512
         * (non-Javadoc)
513
         * @see org.gvsig.raster.impl.dataset.QueryableRaster#getWindowRaster(double, double, double, double, boolean)
514
         */
515
        public Buffer getWindowRaster(double ulx, double uly, double w, double h, boolean adjustToExtent)
516
                throws InvalidSetViewException, RasterDriverException {
517
                //TODO: FUNCIONALIDAD: getWindowRaster en CompositeDataset sin implementar
518
                return null;
519
        }
520

    
521
        private void copyBuffer(Buffer orBuf, Buffer dstBuf, Point2D orUL, Point2D dstUL, int width, int height) {
522
                if(getDataType()[0] == Buffer.TYPE_BYTE) {
523
                        for (int iBand = 0; iBand < getBandCount(); iBand++) {
524
                                for (int j = 0; j < height; j++) {
525
                                        for (int i = 0; i < width; i++) {
526
                                                dstBuf.setElem((int)(dstUL.getY() + j), (int)(dstUL.getX() + i), iBand, 
527
                                                                orBuf.getElemByte((int)orUL.getY() + j, (int)orUL.getX() + i, iBand));
528
                                        }
529
                                }
530
                        }
531

    
532
                }
533
                if(getDataType()[0] == Buffer.TYPE_SHORT) {
534
                        for (int iBand = 0; iBand < getBandCount(); iBand++) {
535
                                for (int j = 0; j < height; j++) {
536
                                        for (int i = 0; i < width; i++) {
537
                                                dstBuf.setElem((int)(dstUL.getY() + j), (int)(dstUL.getX() + i), iBand, 
538
                                                                orBuf.getElemShort((int)orUL.getY() + j, (int)orUL.getX() + i, iBand));
539
                                        }
540
                                }
541
                        }
542

    
543
                }
544
                if(getDataType()[0] == Buffer.TYPE_INT) {
545
                        for (int iBand = 0; iBand < getBandCount(); iBand++) {
546
                                for (int j = 0; j < height; j++) {
547
                                        for (int i = 0; i < width; i++) {
548
                                                dstBuf.setElem((int)(dstUL.getY() + j), (int)(dstUL.getX() + i), iBand, 
549
                                                                orBuf.getElemInt((int)orUL.getY() + j, (int)orUL.getX() + i, iBand));
550
                                        }
551
                                }
552
                        }
553

    
554
                }
555
                if(getDataType()[0] == Buffer.TYPE_FLOAT) {
556
                        for (int iBand = 0; iBand < getBandCount(); iBand++) {
557
                                for (int j = 0; j < height; j++) {
558
                                        for (int i = 0; i < width; i++) {
559
                                                dstBuf.setElem((int)(dstUL.getY() + j), (int)(dstUL.getX() + i), iBand, 
560
                                                                orBuf.getElemFloat((int)orUL.getY() + j, (int)orUL.getX() + i, iBand));
561
                                        }
562
                                }
563
                        }
564

    
565
                }
566
                if(getDataType()[0] == Buffer.TYPE_DOUBLE) {
567
                        for (int iBand = 0; iBand < getBandCount(); iBand++) {
568
                                for (int j = 0; j < height; j++) {
569
                                        for (int i = 0; i < width; i++) {
570
                                                dstBuf.setElem((int)(dstUL.getY() + j), (int)(dstUL.getX() + i), iBand, 
571
                                                                orBuf.getElemDouble((int)orUL.getY() + j, (int)orUL.getX() + i, iBand));
572
                                        }
573
                                }
574
                        }
575

    
576
                }
577
        }
578

    
579
        /*
580
         * (non-Javadoc)
581
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWindowRaster(double, double, double, double, int, int, boolean)
582
         */
583
        public Buffer getWindowRaster(double ulx, double uly, double lrx, double lry, int bufWidth, int bufHeight, boolean adjustToExtent)
584
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
585
                try {
586
                        Point2D p1 = new Point2D.Double(ulx, uly);
587
                        Point2D p2 = new Point2D.Double(lrx, lry);
588
                        ArrayList<QueryableRaster> datasetList = getDatasetListInArea(p1.getX(), p1.getY(), p2.getX(), p2.getY());
589
                        Buffer endBuffer = DefaultRasterManager.getInstance().createBuffer(getDataType()[0], bufWidth, bufHeight, bandList.getDrawableBandsCount(), true);
590
                        
591
                        for (int i = 0; i < list.size(); i++) {
592
                                Extent e = datasetList.get(i).adjustToExtent(new ExtentImpl(ulx, uly, lrx, lry));
593
                                int w = (int)Math.ceil((e.width() * bufWidth) / (Math.abs(lrx - ulx)));
594
                                int h = (int)Math.ceil((e.height() * bufHeight) / (Math.abs(lry - uly)));
595
                                Buffer buf = datasetList.get(i).getWindowRaster(e.getULX(), e.getULY(), e.getLRX(), e.getLRY(), w, h, true);
596
                                Point2D pa = datasetList.get(i).worldToRaster(new Point2D.Double(e.getULX(), e.getULY()));
597
                                double ulPixX = (ulx * pa.getX()) / e.getULX();
598
                                double ulPixY = (uly * pa.getY()) / e.getULY();
599
                                copyBuffer(buf, endBuffer, pa, new Point2D.Double(ulPixX, ulPixY), w, h);
600
                                buf.free();
601
                        }
602
                        
603
                        return endBuffer;
604
                } catch (NoninvertibleTransformException e) {
605
                        throw new InvalidSetViewException("No se ha podido aplicar la transformaci?n inversa para esa vista.");
606
                }
607
        }
608

    
609
        /*
610
         * (non-Javadoc)
611
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWindowRaster(int, int, int, int)
612
         */
613
        public Buffer getWindowRaster(int x, int y, int w, int h)
614
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
615
                return null;
616
        }
617

    
618
        /*
619
         * (non-Javadoc)
620
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWindowRaster(int, int, int, int, int, int)
621
         */
622
        public Buffer getWindowRaster(int x, int y, int w, int h, int bufWidth, int bufHeight)
623
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
624
                return null;
625
        }
626
        
627
        /*
628
         * (non-Javadoc)
629
         * @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getPixelSizeX()
630
         */
631
        public double getPixelSizeX() {
632
                if(list == null || list.size() <= 0)
633
                        return 0;
634
                return list.get(0).getPixelSizeX();
635
        }
636

    
637
        /*
638
         * (non-Javadoc)
639
         * @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getPixelSizeY()
640
         */
641
        public double getPixelSizeY() {
642
                if(list == null || list.size() <= 0)
643
                        return 0;
644
                return list.get(0).getPixelSizeY();
645
        }
646

    
647
        /*
648
         * (non-Javadoc)
649
         * @see org.gvsig.raster.impl.store.QueryableRaster#setDrawableBands(int[])
650
         */
651
        public void setDrawableBands(int[] db) {
652
                for (int i = 0; i < list.size(); i++) {
653
                        ((QueryableRaster)list.get(i)).setDrawableBands(db);
654
                }
655
        }
656

    
657
        /*
658
         * (non-Javadoc)
659
         * @see org.gvsig.raster.impl.dataset.QueryableRaster#clearDrawableBands()
660
         */
661
        public void clearDrawableBands() {
662
                for (int i = 0; i < list.size(); i++) {
663
                        ((QueryableRaster)list.get(i)).clearDrawableBands();
664
                }
665
        }
666

    
667
        /*
668
         * (non-Javadoc)
669
         * @see org.gvsig.raster.impl.store.QueryableRaster#addDrawableBand(int, int)
670
         */
671
        public void addDrawableBand(int posRasterBuf, int imageBand) {
672
                for (int i = 0; i < list.size(); i++) {
673
                        ((QueryableRaster)list.get(i)).addDrawableBand(posRasterBuf, imageBand);
674
                }
675
        }
676

    
677
        /*
678
         * (non-Javadoc)
679
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getTransparency()
680
         */
681
        public Transparency getTransparency() {
682
                if(list != null && list.size() > 0)
683
                        return list.get(0).getTransparency();
684
                return new DataStoreTransparency();
685
        }
686

    
687
        /*
688
         * (non-Javadoc)
689
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getColorTable()
690
         */
691
        public ColorTable getColorTable() {
692
                if(list != null && list.size() > 0)
693
                        return list.get(0).getColorTable();
694
                return null;
695
        }
696

    
697
        /*
698
         * (non-Javadoc)
699
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getHistogram()
700
         */
701
        public Histogram getHistogram() throws HistogramException {
702
                percent = 0;
703
                Histogram[] histogram = new Histogram[list.size()];
704
                double parc = 100 / (double)(list.size());
705
                int cont = 0;
706
                for (int i = 0; i < list.size(); i++) {
707
                        histogram[cont] = list.get(i).getHistogram();
708
                        percent += cont * parc; 
709
                        cont ++;
710
                }
711
                MosaicDataStoreHistogram histRes = new MosaicDataStoreHistogram(histogram, this);
712
                return histRes;
713
        }
714

    
715
        /*
716
         * (non-Javadoc)
717
         * @see org.gvsig.raster.impl.store.QueryableRaster#getColorTable(int)
718
         */
719
        public ColorTable getColorTable(int i){
720
                return (list != null && list.size() > 0) ? ((QueryableRaster)list.get(0)).getColorTable(i) : null;
721
        }
722

    
723
        /*
724
         * (non-Javadoc)
725
         * @see org.gvsig.raster.impl.store.QueryableRaster#getColorTable(java.lang.String)
726
         */
727
        public ColorTable getColorTable(String fileName){
728
                return (list != null && list.size() > 0) ? ((QueryableRaster)list.get(0)).getColorTable(fileName) : null;
729
        }
730

    
731
        /*
732
         * (non-Javadoc)
733
         * @see org.gvsig.raster.dataset.MultiRasterStore#getData(int, int, int)
734
         */
735
        public Object getData(int x, int y, int band) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
736
                return null;
737
        }
738

    
739
        /*
740
         * (non-Javadoc)
741
         * @see org.gvsig.raster.dataset.IRasterDataSource#isReadOnly()
742
         */
743
        public boolean isReadOnly() {
744
                return readOnly;
745
        }
746

    
747
        /*
748
         * (non-Javadoc)
749
         * @see org.gvsig.raster.dataset.IRasterDataSource#setReadOnly(boolean)
750
         */
751
        public void setReadOnly(boolean readOnly) {
752
                this.readOnly = readOnly;
753
        }
754

    
755
        /*
756
         * (non-Javadoc)
757
         * @see org.gvsig.raster.dataset.IRasterDataSource#isMemoryBuffer()
758
         */
759
        public boolean isMemoryBuffer() {
760
                return forceToMemory;
761
        }
762

    
763
        /*
764
         * (non-Javadoc)
765
         * @see org.gvsig.raster.impl.store.QueryableRaster#setMemoryBuffer(boolean)
766
         */
767
        public void setMemoryBuffer(boolean memory) {
768
                this.forceToMemory = memory;
769
                if(memory)
770
                        this.readOnly = false;
771
        }
772

    
773
        /*
774
         * (non-Javadoc)
775
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewCount(int)
776
         */
777
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
778
                if(band >= getBandCount())
779
                        throw new BandAccessException("Wrong band");
780
                return 0;
781
        }
782

    
783
        /*
784
         * (non-Javadoc)
785
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#overviewsSupport()
786
         */
787
        public boolean overviewsSupport() {
788
                return false;
789
        }
790
        
791
        /*
792
         * (non-Javadoc)
793
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getOverviewWidth(int, int)
794
         */
795
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
796
                if(list == null || list.size() <= 0)
797
                        return 0;
798
                int[] providerBand = ((DefaultMultiRasterStore)list.get(0)).getProviderFromBandNumber(band);
799
                RasterProvider prov = ((DefaultMultiRasterStore)list.get(0)).getProvider(providerBand[0]);
800
                return prov.getOverviewWidth(providerBand[1], overview);
801
        }
802

    
803
        /*
804
         * (non-Javadoc)
805
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getOverviewHeight(int, int)
806
         */
807
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
808
                if(list == null || list.size() <= 0)
809
                        return 0;
810
                int[] providerBand = ((DefaultMultiRasterStore)list.get(0)).getProviderFromBandNumber(band);
811
                RasterProvider prov = ((DefaultMultiRasterStore)list.get(0)).getProvider(providerBand[0]);
812
                return prov.getOverviewHeight(providerBand[1], overview);
813
        }
814

    
815
        /*
816
         * (non-Javadoc)
817
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getNoDataValue()
818
         */
819
        public double getNoDataValue() {
820
                if (list.size() <= 0)
821
                        return RasterLibrary.defaultNoDataValue;
822
                return list.get(0).getNoDataValue();
823
        }
824

    
825
        /*
826
         * (non-Javadoc)
827
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#resetNoDataValue()
828
         */
829
        public void resetNoDataValue() {
830
                for (int i = 0; i < list.size(); i++) {
831
                        list.get(i).resetNoDataValue();
832
                }
833
        }
834

    
835
        /*
836
         * (non-Javadoc)
837
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#setNoDataValue(double)
838
         */
839
        public void setNoDataValue(double value) {
840
                for (int i = 0; i < list.size(); i++) {
841
                        list.get(i).setNoDataValue(value);
842
                }
843
        }
844

    
845
        /*
846
         * (non-Javadoc)
847
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isNoDataEnabled()
848
         */
849
        public boolean isNoDataEnabled() {
850
                return list.get(0).isNoDataEnabled();
851
        }
852

    
853
        /*
854
         * (non-Javadoc)
855
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#setNoDataEnabled(boolean)
856
         */
857
        public void setNoDataEnabled(boolean enabled) {
858
                for (int i = 0; i < list.size(); i++) {
859
                        list.get(i).setNoDataEnabled(enabled);
860
                }
861
        }
862

    
863
        /*
864
         * (non-Javadoc)
865
         * @see org.gvsig.raster.impl.store.QueryableRaster#getDataStore()
866
         */
867
        public RasterDataStore getDataStore() {
868
                return this;
869
        }
870
        
871
        /*
872
         * (non-Javadoc)
873
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getProjection()
874
         */
875
        public IProjection getProjection() {
876
                if(list == null && list.size() <= 0)
877
                        return null;
878
                return list.get(0).getProjection();
879
        }
880
        
881
        /*
882
         * (non-Javadoc)
883
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getView()
884
         */
885
        public Extent getView() {
886
                //TODO:Sin implementar
887
                return null;
888
        }
889
        
890
        /*
891
         * (non-Javadoc)
892
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getExtentWithoutRot()
893
         */
894
        public Extent getExtentWithoutRot() {
895
                //TODO:Sin implementar
896
                return null;
897
        }
898
        
899
        /*
900
         * (non-Javadoc)
901
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getMetadata()
902
         */
903
        public Metadata getMetadata() {
904
                //TODO:Sin implementar
905
                return null;
906
        }
907
        
908
        /*
909
         * (non-Javadoc)
910
         * @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getWktProjection()
911
         */
912
        public String getWktProjection() throws RasterDriverException {
913
                return null;
914
        }
915

    
916
        /*
917
         * (non-Javadoc)
918
         * @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getColorInterpretation()
919
         */
920
        public DataStoreColorInterpretation getColorInterpretation() {
921
                return null;
922
        }
923
        
924
        /*
925
         * (non-Javadoc)
926
         * @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getStatistics()
927
         */
928
        public Statistics getStatistics() {
929
                return stats;
930
        }
931
        
932
        /*
933
         * (non-Javadoc)
934
         * @see org.gvsig.fmap.dal.coverage.store.MosaicRasterStore#getTile(int)
935
         */
936
        public RasterDataStore getTile(int i) {
937
                return (RasterDataStore)list.get(i);
938
        }
939
        
940
        /*
941
         * (non-Javadoc)
942
         * @see org.gvsig.fmap.dal.coverage.store.MosaicRasterStore#getNumberOfTiles()
943
         */
944
        public int getNumberOfTiles() {
945
                return list.size();
946
        }
947
        
948
        /*
949
         * (non-Javadoc)
950
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isReproyectable()
951
         */
952
        public boolean isReproyectable() {
953
                return false;
954
        }
955
        
956
        /*
957
         * (non-Javadoc)
958
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isOpen()
959
         */
960
        public boolean isOpen() {
961
                for (int i = 0; i < list.size(); i++) {
962
                        if(!list.get(i).isOpen())
963
                                return false;
964
                }
965
                return true;
966
        }
967

    
968
        /*
969
         * (non-Javadoc)
970
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getTransparencyFilesStatus()
971
         */
972
        public Transparency getTransparencyFilesStatus() {
973
                Transparency t = null;
974
                for (int i = 0; i < list.size(); i++) {
975
                        if(t != null) {
976
                                Transparency t1 = list.get(i).getTransparencyFilesStatus();
977
                                t.merge(t1);
978
                        } else 
979
                                t = list.get(i).getTransparencyFilesStatus();
980
                }
981
                return t;
982
        }
983
        
984
        /*
985
         * (non-Javadoc)
986
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#saveGeoreferencingToRmf()
987
         */
988
        public void saveGeoreferencingToRmf() throws RmfSerializerException {
989
                for (int i = 0; i < list.size(); i++) {
990
                        list.get(i).saveGeoreferencingToRmf();
991
                }
992
        }
993
        
994
        /*
995
         * (non-Javadoc)
996
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#cloneDataStore()
997
         */
998
        public RasterDataStore cloneDataStore() {
999
                ArrayList<RasterDataStore> mos = new ArrayList<RasterDataStore>();
1000
                for (int i = 0; i < list.size(); i++) {
1001
                                mos.add(list.get(i).cloneDataStore());
1002
                }
1003
                DefaultListRasterStore result = null;
1004
                try {
1005
                        result = new DefaultListRasterStore(mos);
1006
                } catch (MosaicNotValidException e) {
1007
                        return null;
1008
                }
1009
                return result;
1010
        }
1011

    
1012
        /*
1013
         * (non-Javadoc)
1014
         * @see org.gvsig.metadata.Metadata#getMetadataName()
1015
         */
1016
        public String getMetadataName() {
1017
                if(list != null && list.size() > 0)
1018
                        try {
1019
                                return list.get(0).getMetadataName();
1020
                        } catch (MetadataException e) {
1021
                                return null;
1022
                        }
1023
                return null;
1024
        }
1025
        
1026
        /*
1027
         * (non-Javadoc)
1028
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#saveSerialInfoToRmf(org.gvsig.fmap.dal.coverage.store.props.SerialInfo)
1029
         */
1030
        public void saveSerialInfoToRmf(SerialInfo serialInfo) throws RmfSerializerException {
1031
                for (int i = 0; i < list.size(); i++) {
1032
                                ((AbstractRasterDataStore)list.get(i)).saveSerialInfoToRmf(serialInfo);
1033
                }
1034
        }
1035
        
1036
        /*
1037
         * (non-Javadoc)
1038
         * @see org.gvsig.raster.impl.store.AbstractRasterDataStore#saveColorTableToRmf(org.gvsig.fmap.dal.coverage.store.props.ColorTable)
1039
         */
1040
        public void saveColorTableToRmf(ColorTable table) throws RmfSerializerException {
1041
                for (int i = 0; i < list.size(); i++) {
1042
                                ((AbstractRasterDataStore)list.get(i)).saveColorTableToRmf(table);
1043
                }
1044
        }
1045
        
1046
        /*
1047
         * (non-Javadoc)
1048
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#loadSerialInfoFromRmf(org.gvsig.fmap.dal.coverage.store.props.SerialInfo)
1049
         */
1050
        public boolean loadSerialInfoFromRmf(SerialInfo serialInfo) {
1051
                for (int i = 0; i < list.size(); i++) {
1052
                        if(((AbstractRasterDataStore)list.get(i)).loadSerialInfoFromRmf(serialInfo))
1053
                                return true;
1054
                }
1055
                return false;
1056
        }
1057
        
1058
        /*
1059
         * (non-Javadoc)
1060
         * @see org.gvsig.raster.util.IHistogramable#getPercent()
1061
         */
1062
        public int getPercent() {
1063
                return percent;
1064
        }
1065

    
1066
        /*
1067
         * (non-Javadoc)
1068
         * @see org.gvsig.raster.util.IHistogramable#resetPercent()
1069
         */
1070
        public void resetPercent() {
1071
                percent = 0;
1072
        }
1073
        
1074
        /*
1075
         * (non-Javadoc)
1076
         * @see org.gvsig.raster.impl.store.AbstractRasterDataStore#getProvider()
1077
         */
1078
        public CoverageStoreProvider getProvider() {
1079
                if(list == null && list.size() <= 0)
1080
                        return null;
1081
                return list.get(0).getProvider();
1082
        }
1083

    
1084
        public void removeDataset(String fileName) {}
1085
        public void removeDataset(RasterDataStore file) {}
1086
        public void saveGeoToRmf() throws IOException {}
1087
        public void saveRmfModification() throws IOException {}
1088
        public void setAffineTransform(AffineTransform transf) {}
1089

    
1090
}