gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.io / org.gvsig.raster.io.base / src / main / java / org / gvsig / fmap / dal / coverage / dataset / io / tile / downloader / BaseTileDownloader.java @ 453
History | View | Annotate | Download (8.29 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.fmap.dal.coverage.dataset.io.tile.downloader; |
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.dataset.io.gdal.GdalDataParameters; |
31 |
import org.gvsig.fmap.dal.coverage.dataset.io.gdal.GdalProvider; |
32 |
import org.gvsig.fmap.dal.coverage.datastruct.BandList; |
33 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
34 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
35 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
36 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
37 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
38 |
import org.gvsig.fmap.dal.coverage.store.DataServerWriter; |
39 |
import org.gvsig.fmap.dal.coverage.store.RasterWriter; |
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 |
|
47 |
/**
|
48 |
* Base class for downloaders
|
49 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
50 |
*/
|
51 |
public abstract class BaseTileDownloader implements Downloader { |
52 |
protected int tilePxWidth = 0; |
53 |
protected int tilePxHeight = 0; |
54 |
protected DefaultRasterProvider prov = null; |
55 |
protected byte byteNoData = (byte)0x00; |
56 |
protected short shortNoData = Short.MIN_VALUE; |
57 |
protected int intNoData = Integer.MIN_VALUE; |
58 |
protected float floatNoData = -99999; |
59 |
protected double doubleNoData = -99999; |
60 |
|
61 |
public BaseTileDownloader(DefaultRasterProvider prov, int tilePxWidth, int tilePxHeight) { |
62 |
this.prov = prov;
|
63 |
this.tilePxWidth = tilePxWidth;
|
64 |
this.tilePxHeight = tilePxHeight;
|
65 |
} |
66 |
|
67 |
/**
|
68 |
* Sets the nodata value
|
69 |
* @param noData
|
70 |
*/
|
71 |
public void setNoDataValue(Object noData) { |
72 |
switch (prov.getDataType()[0]) { |
73 |
case Buffer.TYPE_BYTE: |
74 |
byteNoData = ((Byte)noData).byteValue();
|
75 |
break;
|
76 |
case Buffer.TYPE_SHORT: |
77 |
shortNoData = ((Short)noData).shortValue();
|
78 |
break;
|
79 |
case Buffer.TYPE_INT: |
80 |
intNoData = ((Integer)noData).intValue();
|
81 |
break;
|
82 |
case Buffer.TYPE_FLOAT: |
83 |
floatNoData = ((Float)noData).floatValue();
|
84 |
break;
|
85 |
case Buffer.TYPE_DOUBLE: |
86 |
doubleNoData = ((Double)noData).doubleValue();
|
87 |
break;
|
88 |
} |
89 |
} |
90 |
|
91 |
/*
|
92 |
* (non-Javadoc)
|
93 |
* @see org.gvsig.raster.cache.tile.provider.Downloader#readTile(org.gvsig.raster.cache.tile.Tile)
|
94 |
*/
|
95 |
public Tile readTileFromDisk(Tile tile) throws TileGettingException { |
96 |
if(!tile.getFile().exists())
|
97 |
return null; |
98 |
|
99 |
BandList bandList = (BandList)tile.getDownloaderParams("BandList");
|
100 |
for (int j = 0; j < bandList.getBandCount(); j++) { |
101 |
bandList.getBand(j).setFileName(tile.getFile().getAbsolutePath()); |
102 |
} |
103 |
|
104 |
int mallocNBands = 0; |
105 |
if(bandList.getDrawableBands() != null) |
106 |
mallocNBands = bandList.getDrawableBands().length; |
107 |
else
|
108 |
mallocNBands = bandList.getDrawableBandsCount(); |
109 |
|
110 |
Buffer buf = DefaultRasterManager.getInstance().createMemoryBuffer(prov.getDataType()[0], this.tilePxWidth, this.tilePxHeight, mallocNBands, true); |
111 |
Buffer transparency = DefaultRasterManager.getInstance().createMemoryBuffer(prov.getDataType()[0], this.tilePxWidth, this.tilePxHeight, 1, true); |
112 |
try {
|
113 |
GdalDataParameters p = new GdalDataParameters();
|
114 |
p.setFile(tile.getFile()); |
115 |
GdalProvider provider = new GdalProvider(p, null); |
116 |
buf = provider.getWindow((int)0, (int)0, bandList, buf); |
117 |
|
118 |
if(provider.getTransparency().getAlphaBandNumber() >= 0) { |
119 |
//Lee la banda de transparencia para eliminar la zona del tile que est? fuera de la imagen
|
120 |
BandList newBandList = new BandListImpl(tile.getFile().getAbsolutePath(), provider.getBandCount(), provider.getDataType()[0]); |
121 |
newBandList.clearDrawableBands(); |
122 |
newBandList.addDrawableBand(0, provider.getBandCount() - 1); |
123 |
transparency = provider.getWindow((int)0, (int)0, newBandList, transparency); |
124 |
tile.setData(new Object[]{buf, transparency, null}); |
125 |
} else
|
126 |
tile.setData(new Object[]{buf, null, provider.getColorTable()}); |
127 |
|
128 |
buf.setDataExtent(provider.getExtent().toRectangle2D()); |
129 |
provider.close(); |
130 |
return tile;
|
131 |
} catch (ProcessInterruptedException e) {
|
132 |
throw new TileGettingException(e); |
133 |
} catch (RasterDriverException e) {
|
134 |
throw new TileGettingException(e); |
135 |
} catch (NotSupportedExtensionException e) {
|
136 |
throw new TileGettingException(e); |
137 |
} |
138 |
} |
139 |
|
140 |
/**
|
141 |
* Sets a band to NODATA value
|
142 |
* @param bufResult
|
143 |
* @param tileExtent
|
144 |
* @param buf
|
145 |
* @param ex
|
146 |
* @param pixelSize
|
147 |
*/
|
148 |
protected void clearMaskBuffer(Buffer buf, int nBand) { |
149 |
if(buf.getDataType() == Buffer.TYPE_BYTE) { |
150 |
for (int i = 0; i < buf.getHeight(); i++) { |
151 |
for (int j = 0; j < buf.getWidth(); j++) { |
152 |
buf.setElem(i, j, nBand, byteNoData); |
153 |
} |
154 |
} |
155 |
return;
|
156 |
} |
157 |
if(buf.getDataType() == Buffer.TYPE_SHORT) { |
158 |
for (int i = 0; i < buf.getHeight(); i++) { |
159 |
for (int j = 0; j < buf.getWidth(); j++) { |
160 |
buf.setElem(i, j, nBand, shortNoData); |
161 |
} |
162 |
} |
163 |
return;
|
164 |
} |
165 |
if(buf.getDataType() == Buffer.TYPE_INT) { |
166 |
for (int i = 0; i < buf.getHeight(); i++) { |
167 |
for (int j = 0; j < buf.getWidth(); j++) { |
168 |
buf.setElem(i, j, nBand, intNoData); |
169 |
} |
170 |
} |
171 |
return;
|
172 |
} |
173 |
if(buf.getDataType() == Buffer.TYPE_FLOAT) { |
174 |
for (int i = 0; i < buf.getHeight(); i++) { |
175 |
for (int j = 0; j < buf.getWidth(); j++) { |
176 |
buf.setElem(i, j, nBand, floatNoData); |
177 |
} |
178 |
} |
179 |
return;
|
180 |
} |
181 |
if(buf.getDataType() == Buffer.TYPE_DOUBLE) { |
182 |
for (int i = 0; i < buf.getHeight(); i++) { |
183 |
for (int j = 0; j < buf.getWidth(); j++) { |
184 |
buf.setElem(i, j, nBand, doubleNoData); |
185 |
} |
186 |
} |
187 |
return;
|
188 |
} |
189 |
} |
190 |
|
191 |
/**
|
192 |
* Saves a buffer to disk
|
193 |
* @param bufResult Buffer to save
|
194 |
* @param pixelSize Pixel size
|
195 |
* @param extension output file format
|
196 |
* @param alphaBand true if it has alpha band
|
197 |
* @param path Path to the new file
|
198 |
* @param tileExtent Bounding box of the tile
|
199 |
* @throws NotSupportedExtensionException
|
200 |
* @throws RasterDriverException
|
201 |
* @throws ProcessInterruptedException
|
202 |
* @throws IOException
|
203 |
*/
|
204 |
protected void saveTile(Buffer bufResult, |
205 |
double pixelSize,
|
206 |
String extension,
|
207 |
boolean alphaBand,
|
208 |
String path,
|
209 |
Extent tileExtent) throws NotSupportedExtensionException, RasterDriverException, ProcessInterruptedException, IOException { |
210 |
//Escritura en disco del tile
|
211 |
RasterManager rManager = RasterLocator.getManager(); |
212 |
DataServerWriter dataWriter = RasterLocator.getManager().createDataServerWriter(); |
213 |
dataWriter.setBuffer(bufResult, -1);
|
214 |
Params params = rManager.createWriter("_." + extension).getParams();
|
215 |
AffineTransform affineTransform = new AffineTransform(pixelSize, 0, 0, -pixelSize, |
216 |
tileExtent.getULX(), |
217 |
tileExtent.getULY()); |
218 |
RasterWriter rw = rManager.createWriter(dataWriter, path, |
219 |
bufResult.getBandCount(), affineTransform, bufResult.getWidth(), |
220 |
bufResult.getHeight(), bufResult.getDataType(), params, null);
|
221 |
if(alphaBand) {
|
222 |
String[] ci = new String[bufResult.getBandCount()]; |
223 |
for (int i = 0; i < bufResult.getBandCount(); i++) { |
224 |
if(i == bufResult.getBandCount() - 1) |
225 |
ci[i] = "Alpha";
|
226 |
else
|
227 |
ci[i] = "Gray";
|
228 |
} |
229 |
rw.setColorBandsInterpretation(ci); |
230 |
} |
231 |
rw.dataWrite(); |
232 |
rw.writeClose(); |
233 |
} |
234 |
} |