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 @ 210

History | View | Annotate | Download (35.1 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.awt.geom.Rectangle2D;
28
import java.io.IOException;
29
import java.util.ArrayList;
30

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

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

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

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

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

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

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

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

    
212

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

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

    
229
        /*
230
         * (non-Javadoc)
231
         * @see org.gvsig.raster.dataset.IRasterDataSource#getExtent()
232
         */
233
        public Extent getExtent() {
234
                //Ojo! no es rotable
235
                if(list.size() <= 0)
236
                        return null;
237
                double ulx = list.get(0).getExtent().getULX();
238
                double uly = list.get(0).getExtent().getULY();
239
                double urx = list.get(0).getExtent().getURX();
240
                double ury = list.get(0).getExtent().getURY();
241
                double llx = list.get(0).getExtent().getLLX();
242
                double lly = list.get(0).getExtent().getLLY();
243
                double lrx = list.get(0).getExtent().getLRX();
244
                double lry = list.get(0).getExtent().getLRY();
245

    
246
                for (int i = 1; i < list.size(); i++) {
247
                        ulx = list.get(i).getExtent().getULX() < ulx ? list.get(i).getExtent().getULX() : ulx;
248
                        uly = list.get(i).getExtent().getULY() > uly ? list.get(i).getExtent().getULY() : uly;
249
                        urx = list.get(i).getExtent().getURX() > urx ? list.get(i).getExtent().getURX() : urx;
250
                        ury = list.get(i).getExtent().getURY() > ury ? list.get(i).getExtent().getURY() : ury;
251
                        llx = list.get(i).getExtent().getLLX() > llx ? list.get(i).getExtent().getLLX() : llx;
252
                        lly = list.get(i).getExtent().getLLY() < lly ? list.get(i).getExtent().getLLY() : lly;
253
                        lrx = list.get(i).getExtent().getLRX() < lrx ? list.get(i).getExtent().getLRX() : lrx;
254
                        lry = list.get(i).getExtent().getLRY() < lry ? list.get(i).getExtent().getLRY() : lry;
255
                }
256

    
257
                return new ExtentImpl(        new Point2D.Double(ulx, uly),
258
                                new Point2D.Double(lrx, lry),
259
                                new Point2D.Double(urx, ury),
260
                                new Point2D.Double(llx, lly));
261
        }
262

    
263
        /*
264
         * (non-Javadoc)
265
         * @see org.gvsig.raster.dataset.IRasterDataSource#getBandCount()
266
         */
267
        public int getBandCount() {
268
                return list.get(0).getBandCount();
269
        }
270

    
271
        /*
272
         * (non-Javadoc)
273
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getWidth()
274
         */
275
        public double getWidth() {
276
                double wReal = (getExtent().getMax().getX() - getExtent().getMin().getX());
277
                return Math.abs(wReal / getPixelSizeX());
278
        }
279

    
280
        /*
281
         * (non-Javadoc)
282
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getHeight()
283
         */
284
        public double getHeight() {
285
                double hReal = (getExtent().getMax().getY() - getExtent().getMin().getY());
286
                return Math.abs(hReal / getPixelSizeY());
287
        }
288

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

    
303
        /*
304
         * (non-Javadoc)
305
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getDataType()
306
         */
307
        public int[] getDataType() {
308
                return list.get(0).getDataType();
309
        }
310

    
311
        /*
312
         * (non-Javadoc)
313
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getDataStoreCount()
314
         */
315
        public int getDataStoreCount() {
316
                return list.get(0).getDataStoreCount();
317
        }
318

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

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

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

    
358
                ArrayList<QueryableRaster> result = new ArrayList<QueryableRaster>();
359

    
360
                for (int i = 0; i < list.size(); i++) {
361
                        if(util.intersects(new ExtentImpl(ulx, uly, lrx, lry), list.get(i).getExtent(), list.get(i).getAffineTransform()))
362
                                result.add((QueryableRaster)list.get(i));
363
                }
364
                return result;
365
        }
366

    
367
        /*
368
         * (non-Javadoc)
369
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isInside(java.awt.geom.Point2D)
370
         */
371
        public boolean isInside(Point2D p) {
372
                return RasterLocator.getManager().getRasterUtils().isInside(p, getExtent(), getAffineTransform());
373
        }
374

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

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

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

    
409
        /*
410
         * (non-Javadoc)
411
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isGeoreferenced()
412
         */
413
        public boolean isGeoreferenced() {
414
                //Este tipo de datasets siempre est? georreferenciado
415
                return true;
416
        }
417

    
418
        /*
419
         * (non-Javadoc)
420
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getBands()
421
         */
422
        public BandList getBands() {
423
                return bandList;
424
        }
425

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

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

    
489
                        int width = (int)Math.abs(p1X - p2X);
490
                        int height = (int)Math.abs(p1Y - p2Y);
491

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

    
525
        /*
526
         * (non-Javadoc)
527
         * @see org.gvsig.raster.impl.dataset.QueryableRaster#getWindowRaster(double, double, double, double, boolean)
528
         */
529
        public Buffer getWindowRaster(double ulx, double uly, double w, double h, boolean adjustToExtent)
530
                throws InvalidSetViewException, RasterDriverException {
531
                //TODO: FUNCIONALIDAD: getWindowRaster en CompositeDataset sin implementar
532
                return null;
533
        }
534

    
535
        private void copyBuffer(Buffer orBuf, Buffer dstBuf, Point2D dstUL, int width, int height) {
536
                if(getDataType()[0] == Buffer.TYPE_BYTE) {
537
                        for (int iBand = 0; iBand < getBandCount(); iBand++) {
538
                                for (int j = 0; j < height; j++) {
539
                                        for (int i = 0; i < width; i++) {
540
                                                dstBuf.setElem((int)(dstUL.getY() + j), (int)(dstUL.getX() + i), iBand, 
541
                                                                orBuf.getElemByte(j, i, iBand));
542
                                        }
543
                                }
544
                        }
545
                }
546
                if(getDataType()[0] == Buffer.TYPE_SHORT) {
547
                        for (int iBand = 0; iBand < getBandCount(); iBand++) {
548
                                for (int j = 0; j < height; j++) {
549
                                        for (int i = 0; i < width; i++) {
550
                                                dstBuf.setElem((int)(dstUL.getY() + j), (int)(dstUL.getX() + i), iBand, 
551
                                                                orBuf.getElemShort(j, i, iBand));
552
                                        }
553
                                }
554
                        }
555
                }
556
                if(getDataType()[0] == Buffer.TYPE_INT) {
557
                        for (int iBand = 0; iBand < getBandCount(); iBand++) {
558
                                for (int j = 0; j < height; j++) {
559
                                        for (int i = 0; i < width; i++) {
560
                                                dstBuf.setElem((int)(dstUL.getY() + j), (int)(dstUL.getX() + i), iBand, 
561
                                                                orBuf.getElemInt(j, i, iBand));
562
                                        }
563
                                }
564
                        }
565
                }
566
                if(getDataType()[0] == Buffer.TYPE_FLOAT) {
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.getElemFloat(j, i, iBand));
572
                                        }
573
                                }
574
                        }
575
                }
576
                if(getDataType()[0] == Buffer.TYPE_DOUBLE) {
577
                        for (int iBand = 0; iBand < getBandCount(); iBand++) {
578
                                for (int j = 0; j < height; j++) {
579
                                        for (int i = 0; i < width; i++) {
580
                                                dstBuf.setElem((int)(dstUL.getY() + j), (int)(dstUL.getX() + i), iBand, 
581
                                                                orBuf.getElemDouble(j, i, iBand));
582
                                        }
583
                                }
584
                        }
585
                }
586
        }
587
        
588
        /*
589
         * (non-Javadoc)
590
         * @see org.gvsig.raster.impl.store.QueryableRaster#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.dal.coverage.grid.render.TileListener, int)
591
         */
592
        public void getWindowRaster(double ulx, double uly, double lrx, double lry, int bufWidth, int bufHeight, TileListener listener, int frameInPx)
593
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
594
                Buffer buf = getWindowRaster(ulx, uly, lrx, lry, bufWidth, bufHeight, true);
595
                buf.setDataExtent(new Rectangle2D.Double(ulx, uly, Math.abs(ulx - lrx), Math.abs(uly - lry)));
596
                listener.nextBuffer(buf, new ExtentImpl(ulx, uly, lrx, lry), this.getAffineTransform(), null);
597
                listener.endReading();
598
        }
599

    
600
        /*
601
         * (non-Javadoc)
602
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWindowRaster(double, double, double, double, int, int, boolean)
603
         */
604
        public Buffer getWindowRaster(double ulx, double uly, double lrx, double lry, int bufWidth, int bufHeight, boolean adjustToExtent)
605
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
606
                try {
607
                        Point2D p1 = new Point2D.Double(ulx, uly);
608
                        Point2D p2 = new Point2D.Double(lrx, lry);
609
                        ArrayList<QueryableRaster> datasetList = getDatasetListInArea(p1.getX(), p1.getY(), p2.getX(), p2.getY());
610
                        
611
                        //Buffer global de escritura
612
                        Buffer endBuffer = null;
613
                        if(list.size() > 1)
614
                                endBuffer = DefaultRasterManager.getInstance().createBuffer(getDataType()[0], bufWidth, bufHeight, bandList.getDrawableBandsCount(), true);
615
                        Buffer buf = null;
616
                        
617
                        DefaultMathUtils mathUtil = new DefaultMathUtils();
618
                        
619
                        for (int i = 0; i < list.size(); i++) {
620
                                Extent totalExtent = new ExtentImpl(ulx, uly, lrx, lry);
621
                                Extent e = datasetList.get(i).adjustToExtent(totalExtent);
622
                                //Ancho y alto en pixels de la imagen local
623
                                int w = (int)Math.ceil((mathUtil.format(e.width(), 5) * bufWidth) / (mathUtil.format(totalExtent.width(), 5)));
624
                                int h = (int)Math.ceil((mathUtil.format(e.height(), 5) * bufHeight) / (mathUtil.format(totalExtent.height(), 5)));
625
                                //Lectura de datos
626
                                buf = datasetList.get(i).getWindowRaster(e.getULX(), e.getULY(), e.getLRX(), e.getLRY(), w, h, true);
627
                                
628
                                Point2D pa = datasetList.get(i).worldToRaster(new Point2D.Double(e.getULX(), e.getULY()));
629
                                double ulPixX = pa.getX() - ((ulx * pa.getX()) / e.getULX());
630
                                double ulPixY = pa.getY() - ((uly * pa.getY()) / e.getULY());
631
                                if(endBuffer != null) {
632
                                        copyBuffer(buf, endBuffer, new Point2D.Double(ulPixX, ulPixY), w, h);
633
                                        buf.free();
634
                                }
635
                        }
636
                        if(list.size() > 1)
637
                                return endBuffer;
638
                        else
639
                                return buf;
640
                } catch (NoninvertibleTransformException e) {
641
                        throw new InvalidSetViewException("No se ha podido aplicar la transformaci?n inversa para esa vista.");
642
                }
643
        }
644

    
645
        /*
646
         * (non-Javadoc)
647
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWindowRaster(int, int, int, int)
648
         */
649
        public Buffer getWindowRaster(int x, int y, int w, int h)
650
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
651
                return null;
652
        }
653

    
654
        /*
655
         * (non-Javadoc)
656
         * @see org.gvsig.raster.dataset.IRasterDataSource#getWindowRaster(int, int, int, int, int, int)
657
         */
658
        public Buffer getWindowRaster(int x, int y, int w, int h, int bufWidth, int bufHeight)
659
                throws InvalidSetViewException, ProcessInterruptedException, RasterDriverException {
660
                return null;
661
        }
662
        
663
        /*
664
         * (non-Javadoc)
665
         * @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getPixelSizeX()
666
         */
667
        public double getPixelSizeX() {
668
                if(list == null || list.size() <= 0)
669
                        return 0;
670
                return list.get(0).getPixelSizeX();
671
        }
672

    
673
        /*
674
         * (non-Javadoc)
675
         * @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getPixelSizeY()
676
         */
677
        public double getPixelSizeY() {
678
                if(list == null || list.size() <= 0)
679
                        return 0;
680
                return list.get(0).getPixelSizeY();
681
        }
682

    
683
        /*
684
         * (non-Javadoc)
685
         * @see org.gvsig.raster.impl.store.QueryableRaster#setDrawableBands(int[])
686
         */
687
        public void setDrawableBands(int[] db) {
688
                for (int i = 0; i < list.size(); i++) {
689
                        ((QueryableRaster)list.get(i)).setDrawableBands(db);
690
                }
691
        }
692

    
693
        /*
694
         * (non-Javadoc)
695
         * @see org.gvsig.raster.impl.dataset.QueryableRaster#clearDrawableBands()
696
         */
697
        public void clearDrawableBands() {
698
                for (int i = 0; i < list.size(); i++) {
699
                        ((QueryableRaster)list.get(i)).clearDrawableBands();
700
                }
701
        }
702

    
703
        /*
704
         * (non-Javadoc)
705
         * @see org.gvsig.raster.impl.store.QueryableRaster#addDrawableBand(int, int)
706
         */
707
        public void addDrawableBand(int posRasterBuf, int imageBand) {
708
                for (int i = 0; i < list.size(); i++) {
709
                        ((QueryableRaster)list.get(i)).addDrawableBand(posRasterBuf, imageBand);
710
                }
711
        }
712

    
713
        /*
714
         * (non-Javadoc)
715
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getTransparency()
716
         */
717
        public Transparency getTransparency() {
718
                if(list != null && list.size() > 0)
719
                        return list.get(0).getTransparency();
720
                return new DataStoreTransparency();
721
        }
722

    
723
        /*
724
         * (non-Javadoc)
725
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getColorTable()
726
         */
727
        public ColorTable getColorTable() {
728
                if(list != null && list.size() > 0)
729
                        return list.get(0).getColorTable();
730
                return null;
731
        }
732

    
733
        /*
734
         * (non-Javadoc)
735
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getHistogram()
736
         */
737
        public Histogram getHistogram() throws HistogramException {
738
                percent = 0;
739
                Histogram[] histogram = new Histogram[list.size()];
740
                double parc = 100 / (double)(list.size());
741
                int cont = 0;
742
                for (int i = 0; i < list.size(); i++) {
743
                        histogram[cont] = list.get(i).getHistogram();
744
                        percent += cont * parc; 
745
                        cont ++;
746
                }
747
                MosaicDataStoreHistogram histRes = new MosaicDataStoreHistogram(histogram, this);
748
                return histRes;
749
        }
750

    
751
        /*
752
         * (non-Javadoc)
753
         * @see org.gvsig.raster.impl.store.QueryableRaster#getColorTable(int)
754
         */
755
        public ColorTable getColorTable(int i){
756
                return (list != null && list.size() > 0) ? ((QueryableRaster)list.get(0)).getColorTable(i) : null;
757
        }
758

    
759
        /*
760
         * (non-Javadoc)
761
         * @see org.gvsig.raster.impl.store.QueryableRaster#getColorTable(java.lang.String)
762
         */
763
        public ColorTable getColorTable(String fileName){
764
                return (list != null && list.size() > 0) ? ((QueryableRaster)list.get(0)).getColorTable(fileName) : null;
765
        }
766

    
767
        /*
768
         * (non-Javadoc)
769
         * @see org.gvsig.raster.dataset.MultiRasterStore#getData(int, int, int)
770
         */
771
        public Object getData(int x, int y, int band) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
772
                return null;
773
        }
774

    
775
        /*
776
         * (non-Javadoc)
777
         * @see org.gvsig.raster.dataset.IRasterDataSource#isReadOnly()
778
         */
779
        public boolean isReadOnly() {
780
                return readOnly;
781
        }
782

    
783
        /*
784
         * (non-Javadoc)
785
         * @see org.gvsig.raster.dataset.IRasterDataSource#setReadOnly(boolean)
786
         */
787
        public void setReadOnly(boolean readOnly) {
788
                this.readOnly = readOnly;
789
        }
790

    
791
        /*
792
         * (non-Javadoc)
793
         * @see org.gvsig.raster.dataset.IRasterDataSource#isMemoryBuffer()
794
         */
795
        public boolean isMemoryBuffer() {
796
                return forceToMemory;
797
        }
798

    
799
        /*
800
         * (non-Javadoc)
801
         * @see org.gvsig.raster.impl.store.QueryableRaster#setMemoryBuffer(boolean)
802
         */
803
        public void setMemoryBuffer(boolean memory) {
804
                this.forceToMemory = memory;
805
                if(memory)
806
                        this.readOnly = false;
807
        }
808

    
809
        /*
810
         * (non-Javadoc)
811
         * @see org.gvsig.raster.dataset.RasterDataset#getOverviewCount(int)
812
         */
813
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
814
                if(band >= getBandCount())
815
                        throw new BandAccessException("Wrong band");
816
                return 0;
817
        }
818

    
819
        /*
820
         * (non-Javadoc)
821
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#overviewsSupport()
822
         */
823
        public boolean overviewsSupport() {
824
                return false;
825
        }
826
        
827
        /*
828
         * (non-Javadoc)
829
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getOverviewWidth(int, int)
830
         */
831
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
832
                if(list == null || list.size() <= 0)
833
                        return 0;
834
                int[] providerBand = ((DefaultMultiRasterStore)list.get(0)).getProviderFromBandNumber(band);
835
                RasterProvider prov = ((DefaultMultiRasterStore)list.get(0)).getProvider(providerBand[0]);
836
                return prov.getOverviewWidth(providerBand[1], overview);
837
        }
838

    
839
        /*
840
         * (non-Javadoc)
841
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getOverviewHeight(int, int)
842
         */
843
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
844
                if(list == null || list.size() <= 0)
845
                        return 0;
846
                int[] providerBand = ((DefaultMultiRasterStore)list.get(0)).getProviderFromBandNumber(band);
847
                RasterProvider prov = ((DefaultMultiRasterStore)list.get(0)).getProvider(providerBand[0]);
848
                return prov.getOverviewHeight(providerBand[1], overview);
849
        }
850

    
851
        /*
852
         * (non-Javadoc)
853
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getNoDataValue()
854
         */
855
        public double getNoDataValue() {
856
                if (list.size() <= 0)
857
                        return RasterLibrary.defaultNoDataValue;
858
                return list.get(0).getNoDataValue();
859
        }
860

    
861
        /*
862
         * (non-Javadoc)
863
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#resetNoDataValue()
864
         */
865
        public void resetNoDataValue() {
866
                for (int i = 0; i < list.size(); i++) {
867
                        list.get(i).resetNoDataValue();
868
                }
869
        }
870

    
871
        /*
872
         * (non-Javadoc)
873
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#setNoDataValue(double)
874
         */
875
        public void setNoDataValue(double value) {
876
                for (int i = 0; i < list.size(); i++) {
877
                        list.get(i).setNoDataValue(value);
878
                }
879
        }
880

    
881
        /*
882
         * (non-Javadoc)
883
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isNoDataEnabled()
884
         */
885
        public boolean isNoDataEnabled() {
886
                return list.get(0).isNoDataEnabled();
887
        }
888

    
889
        /*
890
         * (non-Javadoc)
891
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#setNoDataEnabled(boolean)
892
         */
893
        public void setNoDataEnabled(boolean enabled) {
894
                for (int i = 0; i < list.size(); i++) {
895
                        list.get(i).setNoDataEnabled(enabled);
896
                }
897
        }
898

    
899
        /*
900
         * (non-Javadoc)
901
         * @see org.gvsig.raster.impl.store.QueryableRaster#getDataStore()
902
         */
903
        public RasterDataStore getDataStore() {
904
                return this;
905
        }
906
        
907
        /*
908
         * (non-Javadoc)
909
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getProjection()
910
         */
911
        public IProjection getProjection() {
912
                if(list == null && list.size() <= 0)
913
                        return null;
914
                return list.get(0).getProjection();
915
        }
916
        
917
        /*
918
         * (non-Javadoc)
919
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getView()
920
         */
921
        public Extent getView() {
922
                //TODO:Sin implementar
923
                return null;
924
        }
925
        
926
        /*
927
         * (non-Javadoc)
928
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getExtentWithoutRot()
929
         */
930
        public Extent getExtentWithoutRot() {
931
                //TODO:Sin implementar
932
                return null;
933
        }
934
        
935
        /*
936
         * (non-Javadoc)
937
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getMetadata()
938
         */
939
        public Metadata getMetadata() {
940
                //TODO:Sin implementar
941
                return null;
942
        }
943
        
944
        /*
945
         * (non-Javadoc)
946
         * @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getWktProjection()
947
         */
948
        public String getWktProjection() throws RasterDriverException {
949
                return null;
950
        }
951

    
952
        /*
953
         * (non-Javadoc)
954
         * @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getColorInterpretation()
955
         */
956
        public DataStoreColorInterpretation getColorInterpretation() {
957
                return null;
958
        }
959
        
960
        /*
961
         * (non-Javadoc)
962
         * @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getStatistics()
963
         */
964
        public Statistics getStatistics() {
965
                return stats;
966
        }
967
        
968
        /*
969
         * (non-Javadoc)
970
         * @see org.gvsig.fmap.dal.coverage.store.MosaicRasterStore#getTile(int)
971
         */
972
        public RasterDataStore getTile(int i) {
973
                return (RasterDataStore)list.get(i);
974
        }
975
        
976
        /*
977
         * (non-Javadoc)
978
         * @see org.gvsig.fmap.dal.coverage.store.MosaicRasterStore#getNumberOfTiles()
979
         */
980
        public int getNumberOfTiles() {
981
                return list.size();
982
        }
983
        
984
        /*
985
         * (non-Javadoc)
986
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isReproyectable()
987
         */
988
        public boolean isReproyectable() {
989
                return false;
990
        }
991
        
992
        /*
993
         * (non-Javadoc)
994
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#isOpen()
995
         */
996
        public boolean isOpen() {
997
                for (int i = 0; i < list.size(); i++) {
998
                        if(!list.get(i).isOpen())
999
                                return false;
1000
                }
1001
                return true;
1002
        }
1003

    
1004
        /*
1005
         * (non-Javadoc)
1006
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#getTransparencyFilesStatus()
1007
         */
1008
        public Transparency getTransparencyFilesStatus() {
1009
                Transparency t = null;
1010
                for (int i = 0; i < list.size(); i++) {
1011
                        if(t != null) {
1012
                                Transparency t1 = list.get(i).getTransparencyFilesStatus();
1013
                                t.merge(t1);
1014
                        } else 
1015
                                t = list.get(i).getTransparencyFilesStatus();
1016
                }
1017
                return t;
1018
        }
1019
        
1020
        /*
1021
         * (non-Javadoc)
1022
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#saveGeoreferencingToRmf()
1023
         */
1024
        public void saveGeoreferencingToRmf() throws RmfSerializerException {
1025
                for (int i = 0; i < list.size(); i++) {
1026
                        list.get(i).saveGeoreferencingToRmf();
1027
                }
1028
        }
1029
        
1030
        /*
1031
         * (non-Javadoc)
1032
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#cloneDataStore()
1033
         */
1034
        public RasterDataStore cloneDataStore() {
1035
                ArrayList<RasterDataStore> mos = new ArrayList<RasterDataStore>();
1036
                for (int i = 0; i < list.size(); i++) {
1037
                                mos.add(list.get(i).cloneDataStore());
1038
                }
1039
                DefaultListRasterStore result = null;
1040
                try {
1041
                        result = new DefaultListRasterStore(mos);
1042
                } catch (MosaicNotValidException e) {
1043
                        return null;
1044
                }
1045
                return result;
1046
        }
1047

    
1048
        /*
1049
         * (non-Javadoc)
1050
         * @see org.gvsig.metadata.Metadata#getMetadataName()
1051
         */
1052
        public String getMetadataName() {
1053
                if(list != null && list.size() > 0)
1054
                        try {
1055
                                return list.get(0).getMetadataName();
1056
                        } catch (MetadataException e) {
1057
                                return null;
1058
                        }
1059
                return null;
1060
        }
1061
        
1062
        /*
1063
         * (non-Javadoc)
1064
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#saveSerialInfoToRmf(org.gvsig.fmap.dal.coverage.store.props.SerialInfo)
1065
         */
1066
        public void saveSerialInfoToRmf(SerialInfo serialInfo) throws RmfSerializerException {
1067
                for (int i = 0; i < list.size(); i++) {
1068
                                ((AbstractRasterDataStore)list.get(i)).saveSerialInfoToRmf(serialInfo);
1069
                }
1070
        }
1071
        
1072
        /*
1073
         * (non-Javadoc)
1074
         * @see org.gvsig.raster.impl.store.AbstractRasterDataStore#saveColorTableToRmf(org.gvsig.fmap.dal.coverage.store.props.ColorTable)
1075
         */
1076
        public void saveColorTableToRmf(ColorTable table) throws RmfSerializerException {
1077
                for (int i = 0; i < list.size(); i++) {
1078
                                ((AbstractRasterDataStore)list.get(i)).saveColorTableToRmf(table);
1079
                }
1080
        }
1081
        
1082
        /*
1083
         * (non-Javadoc)
1084
         * @see org.gvsig.fmap.dal.coverage.store.RasterDataStore#loadSerialInfoFromRmf(org.gvsig.fmap.dal.coverage.store.props.SerialInfo)
1085
         */
1086
        public boolean loadSerialInfoFromRmf(SerialInfo serialInfo) {
1087
                for (int i = 0; i < list.size(); i++) {
1088
                        if(((AbstractRasterDataStore)list.get(i)).loadSerialInfoFromRmf(serialInfo))
1089
                                return true;
1090
                }
1091
                return false;
1092
        }
1093
        
1094
        /*
1095
         * (non-Javadoc)
1096
         * @see org.gvsig.raster.util.IHistogramable#getPercent()
1097
         */
1098
        public int getPercent() {
1099
                return percent;
1100
        }
1101

    
1102
        /*
1103
         * (non-Javadoc)
1104
         * @see org.gvsig.raster.util.IHistogramable#resetPercent()
1105
         */
1106
        public void resetPercent() {
1107
                percent = 0;
1108
        }
1109
        
1110
        /*
1111
         * (non-Javadoc)
1112
         * @see org.gvsig.raster.impl.store.AbstractRasterDataStore#getProvider()
1113
         */
1114
        public CoverageStoreProvider getProvider() {
1115
                if(list == null && list.size() <= 0)
1116
                        return null;
1117
                return list.get(0).getProvider();
1118
        }
1119

    
1120
        public void removeDataset(String fileName) {}
1121
        public void removeDataset(RasterDataStore file) {}
1122
        public void saveGeoToRmf() throws IOException {}
1123
        public void saveRmfModification() throws IOException {}
1124
        public void setAffineTransform(AffineTransform transf) {}
1125

    
1126
}