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