svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.bsq / src / main / java / org / gvsig / fmap / dal / file / bsq / AbstractFileRasterStoreProvider.java @ 43867
History | View | Annotate | Download (11.1 KB)
1 | 43867 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.fmap.dal.file.bsq; |
||
3 | |||
4 | import java.io.File; |
||
5 | import java.util.ArrayList; |
||
6 | import java.util.HashMap; |
||
7 | import java.util.List; |
||
8 | import java.util.Map; |
||
9 | import org.apache.commons.io.FilenameUtils; |
||
10 | import org.cresques.cts.IProjection; |
||
11 | import org.gvsig.fmap.dal.DALLocator; |
||
12 | import org.gvsig.fmap.dal.DataManager; |
||
13 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
14 | import org.gvsig.fmap.dal.DataStore; |
||
15 | import org.gvsig.fmap.dal.DataStoreNotification; |
||
16 | import org.gvsig.fmap.dal.FileHelper; |
||
17 | import org.gvsig.fmap.dal.exception.DataException; |
||
18 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
19 | import org.gvsig.fmap.dal.exception.OpenException; |
||
20 | import org.gvsig.fmap.dal.exception.ReadException; |
||
21 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
22 | import org.gvsig.fmap.dal.fileutils.CLRFile; |
||
23 | import org.gvsig.fmap.dal.fileutils.PRJFile; |
||
24 | import org.gvsig.fmap.dal.fileutils.STXFile; |
||
25 | import org.gvsig.fmap.dal.fileutils.WLDFile; |
||
26 | import org.gvsig.fmap.dal.fileutils.spi.UtilsFile; |
||
27 | import org.gvsig.fmap.dal.raster.OpenRasterStoreParameters; |
||
28 | import org.gvsig.fmap.dal.raster.api.BandQuery; |
||
29 | import org.gvsig.fmap.dal.raster.api.RasterQuery; |
||
30 | import org.gvsig.fmap.dal.raster.spi.AbstractAwtRasterStoreProvider; |
||
31 | import org.gvsig.fmap.dal.raster.spi.AbstractRasterStoreProvider; |
||
32 | import org.gvsig.fmap.dal.resource.file.FileResource; |
||
33 | import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
||
34 | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
||
35 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
||
36 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
||
37 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
||
38 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
39 | import org.gvsig.fmap.geom.Geometry; |
||
40 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
41 | import org.gvsig.raster.lib.buffer.api.Buffer; |
||
42 | import org.gvsig.raster.lib.buffer.api.BufferDimensions; |
||
43 | import org.gvsig.raster.lib.buffer.api.BufferLocator; |
||
44 | import org.gvsig.raster.lib.buffer.api.BufferManager; |
||
45 | import org.gvsig.raster.lib.buffer.api.NoData; |
||
46 | import org.gvsig.raster.lib.buffer.api.PageManager; |
||
47 | import org.gvsig.raster.lib.buffer.api.TileStruct; |
||
48 | import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
||
49 | import org.gvsig.raster.lib.buffer.spi.DefaultTileStruct; |
||
50 | import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
||
51 | import org.gvsig.tools.exception.BaseException; |
||
52 | import org.slf4j.Logger; |
||
53 | import org.slf4j.LoggerFactory; |
||
54 | |||
55 | |||
56 | public abstract class AbstractFileRasterStoreProvider |
||
57 | extends AbstractRasterStoreProvider
|
||
58 | implements ResourceConsumer
|
||
59 | { |
||
60 | |||
61 | private static final Logger logger = LoggerFactory.getLogger(AbstractAwtRasterStoreProvider.class); |
||
62 | |||
63 | protected Envelope envelope = null; |
||
64 | protected IProjection projection = null; |
||
65 | protected List<PageManager> pageManagers; |
||
66 | protected TileStruct tileStruct;
|
||
67 | protected String storeProviderName; |
||
68 | protected String storeProviderDescription; |
||
69 | protected ResourceProvider resource;
|
||
70 | |||
71 | protected WLDFile wldFile;
|
||
72 | protected PRJFile prjFile;
|
||
73 | protected STXFile stxFile;
|
||
74 | // protected CLRFile clrFile;
|
||
75 | |||
76 | @SuppressWarnings("OverridableMethodCallInConstructor") |
||
77 | public AbstractFileRasterStoreProvider(
|
||
78 | String storeProviderName,
|
||
79 | String storeProviderDescription,
|
||
80 | OpenRasterStoreParameters params, |
||
81 | DataStoreProviderServices storeServices |
||
82 | ) throws InitializeException {
|
||
83 | super(
|
||
84 | params, |
||
85 | storeServices, |
||
86 | FileHelper.newMetadataContainer(storeProviderName) |
||
87 | ); |
||
88 | this.storeProviderName = storeProviderName;
|
||
89 | this.storeProviderDescription = storeProviderDescription;
|
||
90 | this.init(params, storeServices);
|
||
91 | } |
||
92 | |||
93 | protected void init(OpenRasterStoreParameters params, DataStoreProviderServices storeServices) throws InitializeException { |
||
94 | File file = this.getFile(); |
||
95 | if (file == null) { |
||
96 | throw new InitializeException( |
||
97 | new NullPointerException(this.storeProviderName +" file is null")); |
||
98 | } |
||
99 | |||
100 | this.wldFile = UtilsFile.createWLDFile(this.getFile()); |
||
101 | this.prjFile = UtilsFile.createPRJFile(this.getFile()); |
||
102 | this.stxFile = UtilsFile.createSTXFile(this.getFile()); |
||
103 | // this.clrFile = UtilsFile.createCLRFile(this.getFile());
|
||
104 | |||
105 | this.projection = (IProjection) params.getDynValue(DataStore.METADATA_CRS);
|
||
106 | if( this.projection==null ) { |
||
107 | this.projection = this.prjFile.getCRS(); |
||
108 | } |
||
109 | |||
110 | resource = this.createResource(
|
||
111 | FileResource.NAME, |
||
112 | new Object[] { file.getAbsolutePath() } |
||
113 | ); |
||
114 | |||
115 | resource.addConsumer(this);
|
||
116 | } |
||
117 | |||
118 | @Override
|
||
119 | public ResourceProvider getResource() {
|
||
120 | return this.resource; |
||
121 | } |
||
122 | |||
123 | protected File getFile() { |
||
124 | return ((FilesystemStoreParameters)this.getParameters()).getFile(); |
||
125 | } |
||
126 | |||
127 | @Override
|
||
128 | public Object getSourceId() { |
||
129 | return this.getFile(); |
||
130 | } |
||
131 | |||
132 | @Override
|
||
133 | public String getName() { |
||
134 | String name = this.getFile().getName(); |
||
135 | return FilenameUtils.getBaseName(name);
|
||
136 | } |
||
137 | |||
138 | @Override
|
||
139 | public String getFullName() { |
||
140 | return this.getFile().getAbsolutePath(); |
||
141 | } |
||
142 | |||
143 | @Override
|
||
144 | public String getProviderName() { |
||
145 | return this.storeProviderName; |
||
146 | } |
||
147 | |||
148 | @Override
|
||
149 | public boolean closeResourceRequested(ResourceProvider resource) { |
||
150 | return true; |
||
151 | } |
||
152 | |||
153 | @Override
|
||
154 | public void resourceChanged(ResourceProvider resource) { |
||
155 | this.getStoreServices().notifyChange(
|
||
156 | DataStoreNotification.RESOURCE_CHANGED, |
||
157 | resource); |
||
158 | } |
||
159 | |||
160 | @Override
|
||
161 | protected void doDispose() throws BaseException { |
||
162 | super.doDispose();
|
||
163 | } |
||
164 | |||
165 | @Override
|
||
166 | public DataServerExplorer getExplorer() throws ReadException, ValidateDataParametersException { |
||
167 | DataManager manager = DALLocator.getDataManager(); |
||
168 | FilesystemServerExplorerParameters params; |
||
169 | try {
|
||
170 | params = (FilesystemServerExplorerParameters) manager |
||
171 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
172 | params.setRoot(this.getFile().getParent());
|
||
173 | return manager.openServerExplorer(FilesystemServerExplorer.NAME,params);
|
||
174 | } catch (DataException | ValidateDataParametersException e) {
|
||
175 | throw new ReadException(this.getProviderName(), e); |
||
176 | } |
||
177 | |||
178 | } |
||
179 | |||
180 | @Override
|
||
181 | public BufferDimensions getDimensions() {
|
||
182 | if(this.dimensions==null){ |
||
183 | this.dimensions = BufferLocator.getBufferManager().createBufferDimensions(
|
||
184 | this.getRows(),
|
||
185 | this.getColumns(),
|
||
186 | this.getEnvelope()
|
||
187 | ); |
||
188 | } |
||
189 | return this.dimensions; |
||
190 | } |
||
191 | |||
192 | @Override
|
||
193 | public TileStruct getTileStruct() {
|
||
194 | |||
195 | List<Double> pixelSizes = new ArrayList<>(); |
||
196 | Map<Integer, Double> zoomLevels = new HashMap<>(); |
||
197 | |||
198 | if (this.tileStruct == null) { |
||
199 | |||
200 | tileStruct = new DefaultTileStruct();
|
||
201 | |||
202 | int completeRows = getRows();
|
||
203 | int completeColumns = getColumns();
|
||
204 | double completePixelSizeX = envelope.getLength(Geometry.DIMENSIONS.X) / completeColumns;
|
||
205 | double completePixelSizeY = envelope.getLength(Geometry.DIMENSIONS.Y) / completeRows;
|
||
206 | |||
207 | int maximumLength = completeColumns;
|
||
208 | int tileMaximumLength = tileStruct.getColumnsPerTile(); // tileSizeX; |
||
209 | double maximumPixelSize = completePixelSizeX;
|
||
210 | |||
211 | if (completeRows * tileStruct.getRowsPerTile() > completeColumns * tileStruct.getColumnsPerTile()) {
|
||
212 | maximumLength = completeRows; |
||
213 | tileMaximumLength = tileStruct.getRowsPerTile(); |
||
214 | maximumPixelSize = completePixelSizeY; |
||
215 | } |
||
216 | |||
217 | double pixelSize = maximumPixelSize;
|
||
218 | while (maximumLength * maximumPixelSize / pixelSize > tileMaximumLength) {
|
||
219 | pixelSizes.add(pixelSize); |
||
220 | pixelSize = pixelSize * 2;
|
||
221 | } |
||
222 | pixelSizes.add(pixelSize); |
||
223 | int i = 0; |
||
224 | for (int zoomLevel = pixelSizes.size() - 1; zoomLevel >= 0; zoomLevel--) { |
||
225 | zoomLevels.put(i, pixelSizes.get(zoomLevel)); |
||
226 | i++; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | return tileStruct;
|
||
231 | |||
232 | } |
||
233 | |||
234 | @Override
|
||
235 | public Object getDynValue(String name) throws DynFieldNotFoundException { |
||
236 | switch(name) {
|
||
237 | case DataStore.METADATA_CRS:
|
||
238 | return this.getProjection(); |
||
239 | |||
240 | case DataStore.METADATA_ENVELOPE:
|
||
241 | return this.getProjection(); |
||
242 | |||
243 | default:
|
||
244 | return super.getDynValue(name); |
||
245 | } |
||
246 | } |
||
247 | |||
248 | @Override
|
||
249 | public void setDynValue(String name, Object value) |
||
250 | throws DynFieldNotFoundException {
|
||
251 | switch(name) {
|
||
252 | case DataStore.METADATA_CRS:
|
||
253 | this.projection = (IProjection) value;
|
||
254 | break;
|
||
255 | |||
256 | case DataStore.METADATA_ENVELOPE:
|
||
257 | this.envelope = (Envelope) value;
|
||
258 | break;
|
||
259 | |||
260 | default:
|
||
261 | super.setDynValue(name, value);
|
||
262 | break;
|
||
263 | } |
||
264 | } |
||
265 | |||
266 | protected List<PageManager> getPageManagers() { |
||
267 | this.autoOpen();
|
||
268 | return this.pageManagers; |
||
269 | } |
||
270 | |||
271 | protected IProjection getProjection() {
|
||
272 | this.autoOpen();
|
||
273 | return this.projection; |
||
274 | } |
||
275 | |||
276 | protected Envelope getEnvelope() {
|
||
277 | this.autoOpen();
|
||
278 | return this.envelope; |
||
279 | } |
||
280 | |||
281 | @Override
|
||
282 | public Buffer createBuffer(RasterQuery rasterQuery) throws BufferException { |
||
283 | BufferManager bufferManager = BufferLocator.getBufferManager(); |
||
284 | int[] bandDataTypes; |
||
285 | List<PageManager> thePageManagers = new ArrayList<>(); |
||
286 | NoData[] bandNoData;
|
||
287 | if (rasterQuery != null && !rasterQuery.getBands().isEmpty() ) { |
||
288 | List<BandQuery> bands;
|
||
289 | bands = rasterQuery.getBands(); |
||
290 | bandDataTypes = new int[bands.size()]; |
||
291 | bandNoData = new NoData[bands.size()];
|
||
292 | |||
293 | for (BandQuery bandQuery : bands) {
|
||
294 | int band = bandQuery.getBand();
|
||
295 | bandDataTypes[band] = this.getBandDescriptor(band).getDataType();
|
||
296 | bandNoData[band] = this.getNoData(rasterQuery, band);
|
||
297 | thePageManagers.add(this.getPageManagers().get(band));
|
||
298 | } |
||
299 | } else {
|
||
300 | bandDataTypes = this.getDataTypes();
|
||
301 | bandNoData = this.getNoDatas();
|
||
302 | thePageManagers = this.getPageManagers();
|
||
303 | } |
||
304 | |||
305 | Buffer buffer = bufferManager.createBuffer(
|
||
306 | this.getRows(),
|
||
307 | this.getColumns(),
|
||
308 | bandDataTypes, |
||
309 | bandNoData, |
||
310 | this.getProjection(),
|
||
311 | this.getEnvelope(),
|
||
312 | thePageManagers |
||
313 | ); |
||
314 | |||
315 | return buffer;
|
||
316 | } |
||
317 | |||
318 | @Override
|
||
319 | public abstract void open() throws OpenException; |
||
320 | |||
321 | @Override
|
||
322 | public abstract int getBands(); |
||
323 | |||
324 | public abstract int getRows(); |
||
325 | |||
326 | public abstract int getColumns(); |
||
327 | |||
328 | } |