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 / provider / tile / BaseTileDownloader.java @ 595

History | View | Annotate | Download (8.37 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.awt.geom.AffineTransform;
25
import java.io.IOException;
26

    
27
import org.gvsig.fmap.dal.coverage.RasterLocator;
28
import org.gvsig.fmap.dal.coverage.RasterManager;
29
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
30
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
31
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
32
import org.gvsig.fmap.dal.coverage.datastruct.Params;
33
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
34
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
35
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
36
import org.gvsig.fmap.dal.coverage.store.DataServerWriter;
37
import org.gvsig.fmap.dal.coverage.store.RasterWriter;
38
import org.gvsig.fmap.dal.exception.InitializeException;
39
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
40
import org.gvsig.raster.cache.tile.Tile;
41
import org.gvsig.raster.cache.tile.exception.TileGettingException;
42
import org.gvsig.raster.cache.tile.provider.Downloader;
43
import org.gvsig.raster.impl.DefaultRasterManager;
44
import org.gvsig.raster.impl.datastruct.BandListImpl;
45
import org.gvsig.raster.impl.provider.DefaultRasterProvider;
46
import org.gvsig.raster.util.DefaultProviderServices;
47

    
48
/** 
49
 * Base class for downloaders  
50
 * @author Nacho Brodin (nachobrodin@gmail.com)
51
 */
52
public abstract class BaseTileDownloader implements Downloader {
53
        protected int                       tilePxWidth  = 0;
54
        protected int                       tilePxHeight = 0;
55
        protected DefaultRasterProvider     prov         = null;
56
        protected byte                      byteNoData   = (byte)0x00;
57
        protected short                     shortNoData  = Short.MIN_VALUE;
58
        protected int                       intNoData    = Integer.MIN_VALUE;
59
        protected float                     floatNoData  = -99999;
60
        protected double                    doubleNoData = -99999;
61
        
62
        public BaseTileDownloader(DefaultRasterProvider prov, int tilePxWidth, int tilePxHeight) {
63
                this.prov = prov;
64
                this.tilePxWidth = tilePxWidth;
65
                this.tilePxHeight = tilePxHeight;
66
        }
67
        
68
        /**
69
         * Sets the nodata value
70
         * @param noData
71
         */
72
        public void setNoDataValue(Object noData) {
73
                switch (prov.getDataType()[0]) {
74
                case Buffer.TYPE_BYTE:
75
                        byteNoData = ((Byte)noData).byteValue();
76
                        break;
77
                case Buffer.TYPE_SHORT:
78
                        shortNoData = ((Short)noData).shortValue();
79
                        break;
80
                case Buffer.TYPE_INT:
81
                        intNoData = ((Integer)noData).intValue();
82
                        break;
83
                case Buffer.TYPE_FLOAT:
84
                        floatNoData = ((Float)noData).floatValue();
85
                        break;
86
                case Buffer.TYPE_DOUBLE:
87
                        doubleNoData = ((Double)noData).doubleValue();
88
                        break;
89
                }
90
        }
91

    
92
        /*
93
         * (non-Javadoc)
94
         * @see org.gvsig.raster.cache.tile.provider.Downloader#readTile(org.gvsig.raster.cache.tile.Tile)
95
         */
96
        public Tile readTileFromDisk(Tile tile) throws TileGettingException {
97
                if(!tile.getFile().exists()) {
98
                        tile.setCorrupt();
99
                        return null;
100
                }
101
                
102
                BandList bandList = (BandList)tile.getDownloaderParams("BandList");
103
                for (int j = 0; j < bandList.getBandCount(); j++) {
104
                        bandList.getBand(j).setFileName(tile.getFile().getAbsolutePath());
105
                }
106
                
107
                int mallocNBands = 0;
108
                if(bandList.getDrawableBands() != null)
109
                        mallocNBands = bandList.getDrawableBands().length;
110
                else
111
                        mallocNBands = bandList.getDrawableBandsCount();
112
                
113
                Buffer buf = DefaultRasterManager.getInstance().createMemoryBuffer(prov.getDataType()[0], this.tilePxWidth, this.tilePxHeight, mallocNBands, true);
114
                Buffer transparency = DefaultRasterManager.getInstance().createMemoryBuffer(prov.getDataType()[0], this.tilePxWidth, this.tilePxHeight, 1, true);
115
                try {
116
                        DefaultRasterProvider provider = DefaultProviderServices.loadProvider(tile.getFile());
117
                        buf = provider.getWindow((int)0, (int)0, bandList, buf);
118
                        
119
                        if(provider.getTransparency().getAlphaBandNumber() >= 0) {
120
                                //Lee la banda de transparencia para eliminar la zona del tile que est? fuera de la imagen
121
                                BandList newBandList = new BandListImpl(tile.getFile().getAbsolutePath(), provider.getBandCount(), provider.getDataType()[0]);
122
                                newBandList.clearDrawableBands();
123
                                newBandList.addDrawableBand(0, provider.getBandCount() - 1);
124
                                transparency = provider.getWindow((int)0, (int)0, newBandList, transparency);
125
                                tile.setData(new Object[]{buf, transparency, null});
126
                        } else
127
                                tile.setData(new Object[]{buf, null, provider.getColorTable()});
128
                        
129
                        buf.setDataExtent(provider.getExtent().toRectangle2D());
130
                        provider.close();
131
                        return tile;
132
                } catch (ProcessInterruptedException e) {
133
                        throw new TileGettingException(e);
134
                } catch (RasterDriverException e) {
135
                        throw new TileGettingException(e);
136
                } catch (ProviderNotRegisteredException e) {
137
                        throw new TileGettingException(e);
138
                } catch (InitializeException e) {
139
                        throw new TileGettingException(e);
140
                }
141
        }
142
        
143
        /**
144
         * Sets a band to NODATA value 
145
         * @param bufResult
146
         * @param tileExtent
147
         * @param buf
148
         * @param ex
149
         * @param pixelSize
150
         */
151
        protected void clearMaskBuffer(Buffer buf, int nBand) {
152
                if(buf.getDataType() == Buffer.TYPE_BYTE) {
153
                        for (int i = 0; i < buf.getHeight(); i++) {
154
                                for (int j = 0; j < buf.getWidth(); j++) {
155
                                        buf.setElem(i, j, nBand, byteNoData);
156
                                }
157
                        }
158
                        return;
159
                }
160
                if(buf.getDataType() == Buffer.TYPE_SHORT) {
161
                        for (int i = 0; i < buf.getHeight(); i++) {
162
                                for (int j = 0; j < buf.getWidth(); j++) {
163
                                        buf.setElem(i, j, nBand, shortNoData);
164
                                }
165
                        }
166
                        return;
167
                }
168
                if(buf.getDataType() == Buffer.TYPE_INT) {
169
                        for (int i = 0; i < buf.getHeight(); i++) {
170
                                for (int j = 0; j < buf.getWidth(); j++) {
171
                                        buf.setElem(i, j, nBand, intNoData);
172
                                }
173
                        }
174
                        return;
175
                }
176
                if(buf.getDataType() == Buffer.TYPE_FLOAT) {
177
                        for (int i = 0; i < buf.getHeight(); i++) {
178
                                for (int j = 0; j < buf.getWidth(); j++) {
179
                                        buf.setElem(i, j, nBand, floatNoData);
180
                                }
181
                        }
182
                        return;
183
                }
184
                if(buf.getDataType() == Buffer.TYPE_DOUBLE) {
185
                        for (int i = 0; i < buf.getHeight(); i++) {
186
                                for (int j = 0; j < buf.getWidth(); j++) {
187
                                        buf.setElem(i, j, nBand, doubleNoData);
188
                                }
189
                        }
190
                        return;
191
                }
192
        }
193
        
194
        /**
195
         * Saves a buffer to disk
196
         * @param bufResult Buffer to save
197
         * @param pixelSize Pixel size
198
         * @param extension output file format
199
         * @param alphaBand true if it has alpha band
200
         * @param path Path to the new file
201
         * @param tileExtent Bounding box of the tile
202
         * @throws NotSupportedExtensionException
203
         * @throws RasterDriverException
204
         * @throws ProcessInterruptedException
205
         * @throws IOException
206
         */
207
        protected void saveTile(Buffer bufResult, 
208
                        double pixelSize, 
209
                        String extension, 
210
                        boolean alphaBand, 
211
                        String path, 
212
                        Extent tileExtent) throws NotSupportedExtensionException, RasterDriverException, ProcessInterruptedException, IOException {
213
                //Escritura en disco del tile
214
                RasterManager rManager = RasterLocator.getManager();
215
                DataServerWriter dataWriter = RasterLocator.getManager().createDataServerWriter();
216
                dataWriter.setBuffer(bufResult, -1);
217
                Params params = rManager.createWriter("_." + extension).getParams();
218
                AffineTransform affineTransform = new AffineTransform(pixelSize, 0, 0, -pixelSize,
219
                                tileExtent.getULX(),
220
                                tileExtent.getULY());
221
                RasterWriter rw = rManager.createWriter(dataWriter, path,
222
                                bufResult.getBandCount(), affineTransform, bufResult.getWidth(),
223
                                bufResult.getHeight(), bufResult.getDataType(), params, null);
224
                if(alphaBand) {
225
                        String[] ci = new String[bufResult.getBandCount()];
226
                        for (int i = 0; i < bufResult.getBandCount(); i++) {
227
                                if(i == bufResult.getBandCount() - 1)
228
                                        ci[i] = "Alpha";
229
                                else
230
                                        ci[i] = "Gray";
231
                        }
232
                        rw.setColorBandsInterpretation(ci);
233
                }
234
                rw.dataWrite();
235
                rw.writeClose();
236
        }
237
}