svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.tilecache / org.gvsig.fmap.dal.tilecache.raster / src / main / java / org / gvsig / raster / tilecache / provider / MemoryTileStructImage.java @ 43876
History | View | Annotate | Download (6.77 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2016 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.raster.tilecache.provider; |
24 |
|
25 |
import java.util.HashMap; |
26 |
import java.util.Iterator; |
27 |
import java.util.Map.Entry; |
28 |
import java.util.Set; |
29 |
|
30 |
import org.slf4j.Logger; |
31 |
import org.slf4j.LoggerFactory; |
32 |
|
33 |
import org.gvsig.fmap.dal.exception.DataException; |
34 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
35 |
import org.gvsig.fmap.dal.raster.RasterQuery; |
36 |
import org.gvsig.fmap.dal.raster.spi.RasterStoreProvider; |
37 |
import org.gvsig.fmap.geom.Geometry.DIMENSIONS; |
38 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
39 |
import org.gvsig.fmap.geom.GeometryLocator; |
40 |
import org.gvsig.fmap.geom.GeometryManager; |
41 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
42 |
import org.gvsig.fmap.geom.primitive.Envelope; |
43 |
import org.gvsig.raster.lib.buffer.api.Band; |
44 |
import org.gvsig.raster.lib.buffer.api.Buffer; |
45 |
import org.gvsig.raster.lib.buffer.api.TileStruct; |
46 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
47 |
import org.gvsig.tools.ToolsLocator; |
48 |
import org.gvsig.tools.dispose.Disposable; |
49 |
import org.gvsig.tools.dispose.DisposeUtils; |
50 |
import org.gvsig.tools.exception.BaseException; |
51 |
|
52 |
/**
|
53 |
* Represents a tiled image
|
54 |
*
|
55 |
* @author dmartinezizquierdo
|
56 |
*
|
57 |
*/
|
58 |
public class MemoryTileStructImage extends AbstractTileCacheStructImage { |
59 |
|
60 |
private static final Logger logger = LoggerFactory.getLogger(MemoryTileStructImage.class); |
61 |
private static final int MAX_RECENT_ACCEDED_TILES_NUMBER = 50; |
62 |
|
63 |
|
64 |
|
65 |
/**
|
66 |
* @param innerProvider
|
67 |
* @param query
|
68 |
*/
|
69 |
public MemoryTileStructImage(RasterStoreProvider innerProvider, RasterQuery query) {
|
70 |
super(innerProvider, query);
|
71 |
|
72 |
tileStruct = getTileStruct(); |
73 |
|
74 |
recentAccededTiles = new HashMap<String, Tile>(); |
75 |
|
76 |
} |
77 |
|
78 |
|
79 |
|
80 |
/**
|
81 |
* @param buffer
|
82 |
* @param bandNumber
|
83 |
* @param zoomLevel
|
84 |
* @param tileRow
|
85 |
* @param tileCol
|
86 |
* @return Band
|
87 |
* @throws CreateEnvelopeException
|
88 |
* @throws CloneNotSupportedException
|
89 |
* @throws ValidateDataParametersException
|
90 |
*/
|
91 |
@Override
|
92 |
public Band fetchTile(int bandNumber, int zoomLevel, int structRow, int structCol) { |
93 |
|
94 |
String keyTile = composeKeyForRecentTiles(zoomLevel, structRow, structCol);
|
95 |
Tile tile = recentAccededTiles.get(keyTile); |
96 |
|
97 |
if (tile != null) { |
98 |
// Devolver la banda del buffer del tile
|
99 |
return tile.getBuffer().getBand(bandNumber);
|
100 |
} else {
|
101 |
// Cargar un tile nuevo
|
102 |
Buffer buffer = null; |
103 |
try {
|
104 |
buffer = requestTile(zoomLevel, structRow, structCol); |
105 |
ToolsLocator.getDisposableManager().bind(buffer); |
106 |
|
107 |
if (recentAccededTiles.size() >= MAX_RECENT_ACCEDED_TILES_NUMBER) {
|
108 |
removeOlderTile(); |
109 |
} |
110 |
recentAccededTiles.put(keyTile, new Tile(buffer, keyTile));
|
111 |
|
112 |
Band band = buffer.getBand(bandNumber); |
113 |
// ToolsLocator.getDisposableManager().bind(band);
|
114 |
return band;
|
115 |
} catch (DataException | BufferException | ValidateDataParametersException | CreateEnvelopeException | CloneNotSupportedException e) { |
116 |
logger.warn("Can't fetch tile: zoomLevel = " + zoomLevel + ", tileRow = " + structRow |
117 |
+ ", tileColumn = " + structCol + ", band = " + bandNumber + ".", e); |
118 |
return null; |
119 |
} finally {
|
120 |
DisposeUtils.dispose(buffer); |
121 |
buffer = null;
|
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
private Buffer requestTile(int zoomLevel, int structRow, int structCol) throws CreateEnvelopeException, |
127 |
CloneNotSupportedException, BufferException, ValidateDataParametersException, DataException {
|
128 |
|
129 |
RasterQuery rasterQuery = (RasterQuery) this.query.clone();
|
130 |
|
131 |
Double pixelSize = this.tileStruct.getPixelSizePerZoomLevel().get(zoomLevel); |
132 |
rasterQuery.setPixelSize(pixelSize); |
133 |
|
134 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
135 |
|
136 |
Envelope structExtent = this.tileStruct.getEnvelope();
|
137 |
int rowsPerTile = this.tileStruct.getRowsPerTile(); |
138 |
int columnsPerTile = this.tileStruct.getColumnsPerTile(); |
139 |
double minX = structExtent.getMinimum(DIMENSIONS.X) + structCol * (pixelSize * columnsPerTile);
|
140 |
double minY = structExtent.getMaximum(DIMENSIONS.Y) - ((structRow + 1) * (pixelSize * rowsPerTile)); |
141 |
double maxX = minX + pixelSize * columnsPerTile;
|
142 |
double maxY = minY + pixelSize * rowsPerTile;
|
143 |
Envelope envelope = geomManager.createEnvelope(minX, minY, maxX, maxY, SUBTYPES.GEOM2D); |
144 |
|
145 |
Buffer buffer = null; |
146 |
Buffer clippedBuffer = null; |
147 |
Buffer interpolatedBuffer = null; |
148 |
|
149 |
try {
|
150 |
buffer = innerProvider.createBuffer(rasterQuery); |
151 |
clippedBuffer = buffer.clip(envelope); |
152 |
interpolatedBuffer = |
153 |
clippedBuffer.createInterpolated( |
154 |
(int) (Math.round(clippedBuffer.getDimensions().getPixelSizeY() * clippedBuffer.getRows() / pixelSize)), |
155 |
(int) (Math.round(clippedBuffer.getDimensions().getPixelSizeX() * clippedBuffer.getColumns() / pixelSize)), |
156 |
Buffer.INTERPOLATION_Bilinear, null); |
157 |
|
158 |
return interpolatedBuffer;
|
159 |
|
160 |
} finally {
|
161 |
DisposeUtils.dispose(buffer); |
162 |
DisposeUtils.dispose(clippedBuffer); |
163 |
// DisposeUtils.dispose(interpolatedBuffer);
|
164 |
} |
165 |
|
166 |
} |
167 |
|
168 |
// @Override
|
169 |
// protected void doDispose() throws BaseException {
|
170 |
// super.doDispose();
|
171 |
// }
|
172 |
|
173 |
@Override
|
174 |
public TileStruct getTileStruct() {
|
175 |
if(this.tileStruct==null){ |
176 |
if (innerProvider != null) { |
177 |
this.tileStruct = innerProvider.getTileStruct();
|
178 |
} else {
|
179 |
throw new UnsupportedOperationException("Can't be applied the memory tile cache to the provider "+innerProvider.getFullName()); |
180 |
} |
181 |
} |
182 |
return this.tileStruct; |
183 |
} |
184 |
|
185 |
} |