Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / provider / tile / TileDownloaderForFiles.java @ 2393

History | View | Annotate | Download (7.67 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.provider.tile;
23

    
24
import java.io.IOException;
25

    
26
import org.gvsig.fmap.dal.coverage.RasterLocator;
27
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
28
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
29
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
30
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
31
import org.gvsig.fmap.dal.coverage.exception.QueryException;
32
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
33
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
34
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
35
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
36
import org.gvsig.raster.cache.tile.Tile;
37
import org.gvsig.raster.cache.tile.exception.TileGettingException;
38
import org.gvsig.raster.cache.tile.provider.CacheStruct;
39
import org.gvsig.raster.impl.DefaultRasterManager;
40
import org.gvsig.raster.impl.datastruct.ExtentImpl;
41

    
42
/** 
43
 * Tile getter 
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 */
46
public class TileDownloaderForFiles extends BaseTileDownloader {
47
        protected CacheStruct               struct     = null;
48
        protected String                    extension  = null;
49
        
50
        public TileDownloaderForFiles(RasterDataStore store, 
51
                        CacheStruct struct,
52
                        int tileWidth,
53
                        int tileHeight,
54
                        String extension) {
55
                super(store, tileWidth, tileHeight);
56
                this.struct = struct;
57
                this.extension = extension;
58
        }
59
        
60
        public synchronized Tile downloadTile(Tile tile) throws TileGettingException {
61
                Extent tileExtent = new ExtentImpl(tile.getUl(), tile.getLr());
62
                ColorInterpretation ci = store.getColorInterpretation();
63
                
64
                //Escribe todas las bandas a disco
65
                //BandList newBandList = store.getDefaultBandList();//createBandList(prov);
66
                
67
                Buffer bufResult = null;
68
                
69
                double pixelSize = struct.getPixelSizeByLevel(tile.getLevel());
70
                
71
                try {
72
                        Extent ex = store.getExtent().intersection(tileExtent);
73
                        
74
                        //newBuf ser?n distintas de tilePx cuando haya zonas con valores no data. Las de los bordes
75
                        int newBufWidth = (int)Math.ceil((ex.width() * this.tilePxWidth) / tileExtent.width());
76
                        int newBufHeight = (int)Math.ceil((ex.height() * this.tilePxHeight) / tileExtent.height());
77
                        boolean alphaBand = false;
78
                        int nBandsBuffer = store.getBandCount();
79
                        
80
                        if(store.getColorInterpretation().hasAlphaBand())
81
                                alphaBand = true;
82
                        
83
                        //A partir de ahora todos los tiles RGB llevan banda de transparencia, por eso eliminamos este c?digo. Esto es para que 
84
                        //las capas tileadas puedan aparecer con banda transparente en el cuadro de propiedades. Si no da problemas la 
85
                        //modificaci?n eliminarlo permanentemente dentro de un par de versiones (19/11/2013 BN2207)
86
                        
87
                        /*if(isInternalTile(newBufWidth, newBufHeight)) {
88
                                bufResult = readSupersampledBuffer(tileExtent, this.tilePxWidth, this.tilePxHeight, false);
89
                                if(bufResult == null) {
90
                                        RasterQuery q = RasterLocator.getManager().createQuery();
91
                                        q.setAreaOfInterest(tileExtent, this.tilePxWidth, this.tilePxHeight);
92
                                        q.setAllDrawableBands();
93
                                        q.setAdjustToExtent(true); 
94
                                        bufResult = store.query(q);
95
                                }
96
                        } else {*/
97
                        
98
                        //Hay parte del tile que cae fuera de la imagen
99
                        if(store.getDataType()[0] == Buffer.TYPE_BYTE) {
100
                                //Para imagenes byte se crea una banda de transparencia
101
                                alphaBand = true;
102
                                if(!store.getColorInterpretation().hasAlphaBand())
103
                                        nBandsBuffer ++; 
104
                        } 
105

    
106
                        Buffer smallBuf = readSupersampledBuffer(ex, newBufWidth, newBufHeight, alphaBand);
107

    
108
                        if(smallBuf == null) { //No ha habido resampleo
109
                                RasterQuery q = RasterLocator.getManager().createQuery();
110
                                q.setAreaOfInterest(ex, newBufWidth, newBufHeight);
111
                                q.setAllDrawableBands();
112
                                q.setAdjustToExtent(true); 
113
                                if(alphaBand)
114
                                        q.setAlphaBand(store.getBandCount() - 1);
115
                                smallBuf = store.query(q);
116
                        }
117

    
118
                        bufResult = buildTileBuffer(nBandsBuffer, this.tilePxWidth, this.tilePxHeight);
119
                        RasterLocator.getManager().getRasterUtils().copyToBuffer(
120
                                        bufResult, 
121
                                        tileExtent, 
122
                                        smallBuf, 
123
                                        ex, 
124
                                        pixelSize, 
125
                                        store.getColorInterpretation().hasAlphaBand());
126
                        //}
127

    
128
                        smallBuf.dispose();
129
                        saveTile(bufResult, pixelSize, extension, alphaBand, tile, tileExtent, ci);
130
                        bufResult.dispose();
131
                        //Si borramos el rmf no se puede leer la etiqueta Alpha. En caso de que se modifique jgdal para
132
                        //poder guardar esta etiqueta deberiamos borrar el rmf para ahorrar ficheros
133
                        //File rmf = new File(tile.getFile().getAbsolutePath() + ".rmf");
134
                        //if(rmf.exists())
135
                        //rmf.delete();
136
                } catch (ProcessInterruptedException e) {
137
                } catch (RasterDriverException e) {
138
                        throw new TileGettingException(e);
139
                } catch (NotSupportedExtensionException e) {
140
                        throw new TileGettingException(e);
141
                } catch (IOException e) {
142
                        throw new TileGettingException(e);
143
                } catch (QueryException e) {
144
                        throw new TileGettingException(e);
145
                }
146
                readTileFromDisk(tile);
147
                return tile;
148
        }
149
        
150
        public boolean isInternalTile(int bufWidth, int bufHeight) {
151
                return (bufHeight == this.tilePxHeight && bufWidth == this.tilePxWidth); 
152
        }
153
        
154
        /**
155
         * When the buffer of the request is greater than the original raster (> scale 1:1) then the request 
156
         * has to be resampled. 
157
         * @param tileExtent
158
         * @param newBandList
159
         * @param bufWidth
160
         * @param bufHeight
161
         * @param nBands
162
         * @return
163
         * @throws ProcessInterruptedException
164
         * @throws TileGettingException
165
         */
166
        protected Buffer readSupersampledBuffer(Extent tileExtent, int bufWidth, int bufHeight, boolean hasAlphaBand) throws ProcessInterruptedException, QueryException, TileGettingException {
167
                int widthImgPx = (int)Math.abs(tileExtent.width() / store.getPixelSizeX());
168
                int heightImgPx = (int)Math.abs(tileExtent.height() / store.getPixelSizeY());
169
                
170
                if(widthImgPx <= 0)
171
                        widthImgPx = 1;
172
                if(heightImgPx <= 0)
173
                        heightImgPx = 1;
174
                
175
                boolean supersampling = ((bufWidth > widthImgPx) || (bufHeight > heightImgPx)) ? true : false;
176
                
177
                if(supersampling) {
178
                        RasterQuery q = RasterLocator.getManager().createQuery();
179
                        q.setAreaOfInterest(tileExtent, widthImgPx, heightImgPx);
180
                        q.setAllDrawableBands();
181
                        q.setAdjustToExtent(true); 
182
                        if(hasAlphaBand)
183
                                q.setAlphaBand(store.getBandCount() - 1);
184
                        Buffer buf = store.query(q);
185
                        Buffer result = buf.getAdjustedWindow(bufWidth, bufHeight, Buffer.INTERPOLATION_NearestNeighbour);
186
                        if(result != buf)
187
                                buf.dispose();
188
                        return result;
189
                }
190
                return null;
191
        }
192
        
193
        /**
194
         * Builds a buffer using the specified number of bands and initialize it
195
         * @param nbands
196
         * @return
197
         */
198
        protected Buffer buildTileBuffer(int nbands, int w, int h) {
199
                Buffer bufResult = DefaultRasterManager.getInstance().createMemoryBuffer(store.getDataType()[0], w, h, nbands, true);
200
                if(store.getDataType()[0] != Buffer.TYPE_BYTE) {
201
                        for (int i = 0; i < bufResult.getBandCount(); i++) {
202
                                clearMaskBuffer(bufResult, i);
203
                        }
204
                } else
205
                        clearMaskBuffer(bufResult, nbands - 1);
206
                return bufResult;
207
        }
208
}