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

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