Revision 2438 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/TileDownloaderForFiles.java

View differences:

TileDownloaderForFiles.java
25 25

  
26 26
import org.gvsig.fmap.dal.coverage.RasterLocator;
27 27
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
28
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
29 28
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
30 29
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
31 30
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
31
import org.gvsig.fmap.dal.coverage.exception.QueryException;
32 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;
33 35
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
34
import org.gvsig.fmap.dal.coverage.util.RasterUtils;
35 36
import org.gvsig.raster.cache.tile.Tile;
36 37
import org.gvsig.raster.cache.tile.exception.TileGettingException;
37 38
import org.gvsig.raster.cache.tile.provider.CacheStruct;
38 39
import org.gvsig.raster.impl.DefaultRasterManager;
39
import org.gvsig.raster.impl.buffer.DefaultRasterQuery;
40 40
import org.gvsig.raster.impl.datastruct.ExtentImpl;
41
import org.gvsig.raster.impl.provider.DefaultRasterProvider;
42 41

  
43 42
/** 
44 43
 * Tile getter 
45 44
 * @author Nacho Brodin (nachobrodin@gmail.com)
46 45
 */
47 46
public class TileDownloaderForFiles extends BaseTileDownloader {
48
	private CacheStruct               struct     = null;
49
	private RasterUtils               util       = RasterLocator.getManager().getRasterUtils();
50
	private String                    extension  = null;
47
	protected CacheStruct               struct     = null;
48
	protected String                    extension  = null;
51 49
	
52
	public TileDownloaderForFiles(DefaultRasterProvider prov, 
50
	public TileDownloaderForFiles(RasterDataStore store, 
53 51
			CacheStruct struct,
54 52
			int tileWidth,
55 53
			int tileHeight,
56 54
			String extension) {
57
		super(prov, tileWidth, tileHeight);
55
		super(store, tileWidth, tileHeight);
58 56
		this.struct = struct;
59 57
		this.extension = extension;
60 58
	}
61 59
	
62
	/*
63
	 * (non-Javadoc)
64
	 * @see org.gvsig.raster.cache.tile.provider.Downloader#getTile(org.gvsig.raster.cache.tile.Tile)
65
	 */
66 60
	public synchronized Tile downloadTile(Tile tile) throws TileGettingException {
67 61
		Extent tileExtent = new ExtentImpl(tile.getUl(), tile.getLr());
68
		ColorInterpretation ci = prov.getColorInterpretation();
62
		ColorInterpretation ci = store.getColorInterpretation();
69 63
		
70 64
		//Escribe todas las bandas a disco
71
		BandList newBandList = prov.getDefaultBandList();//createBandList(prov);
65
		//BandList newBandList = store.getDefaultBandList();//createBandList(prov);
72 66
		
73 67
		Buffer bufResult = null;
74 68
		
75 69
		double pixelSize = struct.getPixelSizeByLevel(tile.getLevel());
76 70
		
77 71
		try {
78
			Extent ex = util.intersection(prov.getExtent(), tileExtent);
72
			Extent ex = store.getExtent().intersection(tileExtent);
79 73
			
74
			//newBuf ser?n distintas de tilePx cuando haya zonas con valores no data. Las de los bordes
80 75
			int newBufWidth = (int)Math.ceil((ex.width() * this.tilePxWidth) / tileExtent.width());
81 76
			int newBufHeight = (int)Math.ceil((ex.height() * this.tilePxHeight) / tileExtent.height());
82 77
			boolean alphaBand = false;
83
			int nBandsBuffer = prov.getBandCount();
78
			int nBandsBuffer = store.getBandCount();
84 79
			
85
			if(prov.getColorInterpretation().hasAlphaBand())
80
			if(store.getColorInterpretation().hasAlphaBand())
86 81
				alphaBand = true;
87 82
			
88
			//Si no hay parte del tile que cae fuera de la imagen no se usa una nueva banda de transparencia ni valores nodata
89
			if(newBufHeight == this.tilePxHeight && newBufWidth == this.tilePxWidth) {
90
				bufResult = readSupersampledBuffer(tileExtent, newBandList, this.tilePxWidth, this.tilePxHeight, nBandsBuffer);
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);
91 89
				if(bufResult == null) {
92
					bufResult = buildTileBuffer(nBandsBuffer, this.tilePxWidth, this.tilePxHeight);
93
					DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
90
					RasterQuery q = RasterLocator.getManager().createQuery();
94 91
					q.setAreaOfInterest(tileExtent, this.tilePxWidth, this.tilePxHeight);
95
					q.setBandList(newBandList);
96
					q.setBuffer(bufResult);
97
					q.setAdjustToExtent(true);
98
					bufResult = prov.getDataSet(q);
99
					//bufResult = prov.getWindow(tileExtent, this.tilePxWidth, this.tilePxHeight, newBandList, bufResult, true);
92
					q.setAllDrawableBands();
93
					q.setAdjustToExtent(true); 
94
					bufResult = store.query(q);
100 95
				}
101
			} else {
102
				//Hay parte del tile que cae fuera de la imagen
103
				if(prov.getDataType()[0] == Buffer.TYPE_BYTE) {
104
					//Para imagenes byte se crea una banda de transparencia
105
					alphaBand = true;
106
					if(!prov.getColorInterpretation().hasAlphaBand())
107
						nBandsBuffer ++; 
108
				} 
109
				
110
				bufResult = buildTileBuffer(nBandsBuffer, this.tilePxWidth, this.tilePxHeight);
111
				Buffer smallBuf = readSupersampledBuffer(ex, newBandList, newBufWidth, newBufHeight, nBandsBuffer);
112
				
113
				if(smallBuf == null) { //No ha habido resampleo
114
					smallBuf = DefaultRasterManager.getInstance().createMemoryBuffer(prov.getDataType()[0], newBufWidth, newBufHeight, nBandsBuffer, true);
115
					DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
116
					q.setAreaOfInterest(tileExtent, newBufWidth, newBufHeight);
117
					q.setBandList(newBandList);
118
					q.setBuffer(smallBuf);
119
					q.setAdjustToExtent(true);
120
					smallBuf = prov.getDataSet(q);
121
					//smallBuf = prov.getWindow(ex, newBufWidth, newBufHeight, newBandList, smallBuf, true);
122
				}
123
				RasterLocator.getManager().getRasterUtils().copyToBuffer(
124
						bufResult, 
125
						tileExtent, 
126
						smallBuf, 
127
						ex, 
128
						pixelSize, 
129
						prov.getColorInterpretation().hasAlphaBand());
130
			}
96
			} else {*/
131 97
			
132
			saveTile(bufResult, pixelSize, extension, 
133
					alphaBand, tile, tileExtent, ci);
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();
134 131
			//Si borramos el rmf no se puede leer la etiqueta Alpha. En caso de que se modifique jgdal para
135 132
			//poder guardar esta etiqueta deberiamos borrar el rmf para ahorrar ficheros
136 133
			//File rmf = new File(tile.getFile().getAbsolutePath() + ".rmf");
137 134
			//if(rmf.exists())
138
				//rmf.delete();
135
			//rmf.delete();
139 136
		} catch (ProcessInterruptedException e) {
140 137
		} catch (RasterDriverException e) {
141 138
			throw new TileGettingException(e);
......
143 140
			throw new TileGettingException(e);
144 141
		} catch (IOException e) {
145 142
			throw new TileGettingException(e);
143
		} catch (QueryException e) {
144
			throw new TileGettingException(e);
146 145
		}
147 146
		readTileFromDisk(tile);
148 147
		return tile;
149 148
	}
150 149
	
151
	/**
152
	 * Creates a <code>BandList</code> for the tile downloaded. 
153
	 * @param provider
154
	 * @return
155
	 */
156
//	private BandList createBandList(RasterProvider provider) {
157
//		BandList bandList = new BandListImpl();
158
//		String[] uriByProvider = provider.getURIByProvider();
159
//		int[] nBandsByProvider = provider.getBandCountByProvider();
160
//		
161
//		for (int iProvider = 0; iProvider < uriByProvider.length; iProvider++) {
162
//			for (int iBand = 0; iBand < nBandsByProvider[iProvider]; iBand++) {
163
//				try {
164
//					bandList.addBand(new DatasetBandImpl(uriByProvider[iProvider], iBand, provider.getDataType()[0], nBandsByProvider[iProvider]));
165
//				} catch (BandNotFoundInListException e1) {
166
//				}
167
//			}
168
//		}
169
//		
170
//		int[] drawableBands = new int[bandList.getBandCount()];
171
//		for (int i = 0; i < bandList.getBandCount(); i++) {
172
//			drawableBands[i] = i;
173
//		}
174
//		
175
//		bandList.setDrawableBands(drawableBands);
176
//		return bandList;
177
//	}
150
	public boolean isInternalTile(int bufWidth, int bufHeight) {
151
		return (bufHeight == this.tilePxHeight && bufWidth == this.tilePxWidth); 
152
	}
178 153
	
179 154
	/**
180
	 * If the provider doesn't support resampling then this method reamples the result manually
155
	 * When the buffer of the request is greater than the original raster (> scale 1:1) then the request 
156
	 * has to be resampled. 
181 157
	 * @param tileExtent
182 158
	 * @param newBandList
183 159
	 * @param bufWidth
......
185 161
	 * @param nBands
186 162
	 * @return
187 163
	 * @throws ProcessInterruptedException
188
	 * @throws RasterDriverException
164
	 * @throws TileGettingException
189 165
	 */
190
	private Buffer readSupersampledBuffer(Extent tileExtent, BandList newBandList, int bufWidth, int bufHeight, int nBands) throws ProcessInterruptedException, RasterDriverException {
191
		int widthImgPx = (int)Math.abs(tileExtent.width() / prov.getPixelSizeX());
192
		int heightImgPx = (int)Math.abs(tileExtent.height() / prov.getPixelSizeY());
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
		
193 175
		boolean supersampling = ((bufWidth > widthImgPx) || (bufHeight > heightImgPx)) ? true : false;
194 176
		
195
		if(supersampling && !prov.isSupersamplingSupported()) {
196
			//Ecw no supersamplea por lo que habr? que hacerlo a mano
197
			Buffer buf = buildTileBuffer(nBands, widthImgPx, heightImgPx);
198
			DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
177
		if(supersampling) {
178
			RasterQuery q = RasterLocator.getManager().createQuery();
199 179
			q.setAreaOfInterest(tileExtent, widthImgPx, heightImgPx);
200
			q.setBandList(newBandList);
201
			q.setBuffer(buf);
202
			q.setAdjustToExtent(true);
203
			buf = prov.getDataSet(q);
204
			//prov.getWindow(tileExtent, widthImgPx, heightImgPx, newBandList, buf, true);
205
			Buffer result = null;
206
			result = buf.getAdjustedWindow(bufWidth, bufHeight, Buffer.INTERPOLATION_NearestNeighbour);
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);
207 186
			if(result != buf)
208 187
				buf.dispose();
209 188
			return result;
......
216 195
	 * @param nbands
217 196
	 * @return
218 197
	 */
219
	private Buffer buildTileBuffer(int nbands, int w, int h) {
220
		Buffer bufResult = DefaultRasterManager.getInstance().createMemoryBuffer(prov.getDataType()[0], w, h, nbands, true);
221
		if(prov.getDataType()[0] != Buffer.TYPE_BYTE) {
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) {
222 201
			for (int i = 0; i < bufResult.getBandCount(); i++) {
223 202
				clearMaskBuffer(bufResult, i);
224 203
			}

Also available in: Unified diff