gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.fmap.dal.file.jimi / src / main / java / org / gvsig / fmap / dal / file / jimi / JimiRasterStoreProvider.java @ 6069
History | View | Annotate | Download (14.9 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.fmap.dal.file.jimi; |
24 |
|
25 |
import java.awt.Canvas; |
26 |
import java.awt.Image; |
27 |
import java.awt.MediaTracker; |
28 |
import java.awt.Toolkit; |
29 |
import java.awt.image.ColorModel; |
30 |
import java.awt.image.ImageConsumer; |
31 |
import java.awt.image.ImageProducer; |
32 |
import java.io.ByteArrayInputStream; |
33 |
import java.io.File; |
34 |
import java.io.FileInputStream; |
35 |
import java.util.ArrayList; |
36 |
import java.util.Hashtable; |
37 |
import java.util.List; |
38 |
|
39 |
import javax.swing.JFrame; |
40 |
|
41 |
import org.apache.commons.io.FileUtils; |
42 |
import org.apache.commons.io.FilenameUtils; |
43 |
import org.cresques.cts.IProjection; |
44 |
import org.gvsig.fmap.dal.DALLocator; |
45 |
import org.gvsig.fmap.dal.DataManager; |
46 |
import org.gvsig.fmap.dal.DataServerExplorer; |
47 |
import org.gvsig.fmap.dal.DataStore; |
48 |
import org.gvsig.fmap.dal.DataStoreNotification; |
49 |
import org.gvsig.fmap.dal.FileHelper; |
50 |
import org.gvsig.fmap.dal.exception.CloseException; |
51 |
import org.gvsig.fmap.dal.exception.DataException; |
52 |
import org.gvsig.fmap.dal.exception.InitializeException; |
53 |
import org.gvsig.fmap.dal.exception.OpenException; |
54 |
import org.gvsig.fmap.dal.exception.ReadException; |
55 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
56 |
import org.gvsig.fmap.dal.raster.api.RasterQuery; |
57 |
import org.gvsig.fmap.dal.raster.spi.AbstractRasterStoreProvider; |
58 |
import org.gvsig.fmap.dal.raster.spi.CoverageStoreProviderServices; |
59 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
60 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
61 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
62 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
63 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
64 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
65 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
66 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
67 |
import org.gvsig.fmap.geom.Geometry; |
68 |
import org.gvsig.fmap.geom.GeometryLocator; |
69 |
import org.gvsig.fmap.geom.primitive.Envelope; |
70 |
import org.gvsig.metadata.MetadataLocator; |
71 |
import org.gvsig.metadata.MetadataManager; |
72 |
import org.gvsig.metadata.exceptions.MetadataException; |
73 |
import org.gvsig.raster.lib.buffer.api.Buffer; |
74 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
75 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
76 |
import org.gvsig.raster.lib.buffer.api.NoData; |
77 |
import org.gvsig.raster.lib.buffer.api.PageManager; |
78 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
79 |
import org.gvsig.tools.ToolsLocator; |
80 |
import org.gvsig.tools.dynobject.DynObject; |
81 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
82 |
import org.gvsig.tools.exception.BaseException; |
83 |
import org.gvsig.tools.task.SimpleTaskStatus; |
84 |
import org.gvsig.tools.task.TaskStatusManager; |
85 |
import org.slf4j.Logger; |
86 |
import org.slf4j.LoggerFactory; |
87 |
|
88 |
import com.sun.jimi.core.Jimi; |
89 |
import com.sun.jimi.core.JimiReader; |
90 |
import com.sun.jimi.core.raster.JimiRasterImage; |
91 |
import com.sun.jimi.core.util.ProgressListener; |
92 |
|
93 |
/**
|
94 |
* Provider for JIMI files
|
95 |
* @author dmartinezizquierdo
|
96 |
*
|
97 |
*/
|
98 |
public class JimiRasterStoreProvider extends AbstractRasterStoreProvider implements |
99 |
ResourceConsumer{ |
100 |
|
101 |
private static final Logger logger = |
102 |
LoggerFactory.getLogger(JimiRasterStoreProvider.class); |
103 |
|
104 |
public static String NAME = "JIMI"; |
105 |
public static String DESCRIPTION = "JIMI file"; |
106 |
public static final String METADATA_DEFINITION_NAME = NAME; |
107 |
|
108 |
private ResourceProvider resource;
|
109 |
private final SimpleTaskStatus taskStatus; |
110 |
|
111 |
private MemoryImage image = null; |
112 |
private Envelope envelope = null; |
113 |
private IProjection projection= null; |
114 |
private Canvas waitComponent = null; |
115 |
|
116 |
protected static void registerMetadataDefinition() |
117 |
throws MetadataException {
|
118 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
119 |
if (manager.getDefinition(METADATA_DEFINITION_NAME) == null) { |
120 |
manager.addDefinition(METADATA_DEFINITION_NAME, |
121 |
JimiRasterStoreProviderParameters.class |
122 |
.getResourceAsStream("JimiMetadata.xml"),
|
123 |
JimiRasterStoreProviderParameters.class.getClassLoader()); |
124 |
} |
125 |
} |
126 |
|
127 |
public JimiRasterStoreProvider(JimiRasterStoreProviderParameters params,
|
128 |
DataStoreProviderServices storeServices) |
129 |
throws InitializeException {
|
130 |
super(
|
131 |
params, |
132 |
storeServices, |
133 |
FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME) |
134 |
); |
135 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
136 |
this.taskStatus = manager.createDefaultSimpleTaskStatus("Jimi"); |
137 |
this.init(params, storeServices);
|
138 |
} |
139 |
|
140 |
protected JimiRasterStoreProvider(JimiRasterStoreProviderParameters params,
|
141 |
DataStoreProviderServices storeServices, DynObject metadata) |
142 |
throws InitializeException {
|
143 |
super(params, storeServices, metadata);
|
144 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
145 |
this.taskStatus = manager.createDefaultSimpleTaskStatus("Jimi"); |
146 |
this.init(params, storeServices);
|
147 |
} |
148 |
|
149 |
protected void init(JimiRasterStoreProviderParameters params, |
150 |
DataStoreProviderServices storeServices) throws InitializeException {
|
151 |
if (params == null) { |
152 |
throw new InitializeException( |
153 |
new NullPointerException("params is null")); |
154 |
} |
155 |
File file = getJimiParameters().getFile();
|
156 |
if (file == null) { |
157 |
throw new InitializeException( |
158 |
new NullPointerException("Jimi file is null")); |
159 |
} |
160 |
|
161 |
this.projection=params.getCRS();
|
162 |
|
163 |
resource = this.createResource(
|
164 |
FileResource.NAME, |
165 |
new Object[] { file.getAbsolutePath() } |
166 |
); |
167 |
|
168 |
resource.addConsumer(this);
|
169 |
|
170 |
} |
171 |
|
172 |
private JimiRasterStoreProviderParameters getJimiParameters() {
|
173 |
return (JimiRasterStoreProviderParameters) this.getParameters(); |
174 |
} |
175 |
|
176 |
@Override
|
177 |
public Buffer createBuffer(RasterQuery rasterQuery) throws BufferException { |
178 |
BufferManager bufferManager=BufferLocator.getBufferManager(); |
179 |
int[] bandDataTypes=new int[image.bands]; |
180 |
NoData[] bandNoData=new NoData[image.bands]; |
181 |
List<PageManager> pageManagers=new ArrayList<PageManager>(); |
182 |
for (int band=0;band<image.bands;band++){ |
183 |
bandDataTypes[band]=image.dataType; |
184 |
bandNoData[band]=null;
|
185 |
pageManagers.add(new MemoryImageBandPageManager(image, band));
|
186 |
} |
187 |
|
188 |
Buffer buffer=bufferManager.createBuffer(image.rows, image.columns, bandDataTypes, bandNoData, projection, envelope, pageManagers);
|
189 |
|
190 |
return buffer;
|
191 |
} |
192 |
|
193 |
|
194 |
@Override
|
195 |
public DataServerExplorer getExplorer()
|
196 |
throws ReadException, ValidateDataParametersException {
|
197 |
DataManager manager = DALLocator.getDataManager(); |
198 |
FilesystemServerExplorerParameters params; |
199 |
try {
|
200 |
params = (FilesystemServerExplorerParameters) manager |
201 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
202 |
params.setRoot(this.getJimiParameters().getFile().getParent());
|
203 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME,params);
|
204 |
} catch (DataException e) {
|
205 |
throw new ReadException(this.getProviderName(), e); |
206 |
} catch (ValidateDataParametersException e) {
|
207 |
throw new ReadException(this.getProviderName(), e); |
208 |
} |
209 |
|
210 |
} |
211 |
|
212 |
public void open() throws OpenException { |
213 |
if (this.image != null) { |
214 |
return;
|
215 |
} |
216 |
openEver(); |
217 |
} |
218 |
|
219 |
private void openEver() throws OpenException { |
220 |
try {
|
221 |
getResource().execute(new ResourceAction() {
|
222 |
|
223 |
public Object run() throws Exception { |
224 |
|
225 |
JimiReader reader = null;
|
226 |
try {
|
227 |
File file = (File) resource.get(); |
228 |
resource.notifyOpen(); |
229 |
reader = Jimi.createJimiReader(file.toURI().toURL(),Jimi.SYNCHRONOUS); |
230 |
reader.setProgressListener(new ProgressListener() {
|
231 |
@Override
|
232 |
public void setStarted() { |
233 |
taskStatus.add(); |
234 |
taskStatus.setRangeOfValues(0, 100); |
235 |
} |
236 |
@Override
|
237 |
public void setProgressLevel(int arg0) { |
238 |
taskStatus.setCurValue(arg0); |
239 |
} |
240 |
@Override
|
241 |
public void setFinished() { |
242 |
taskStatus.terminate(); |
243 |
} |
244 |
@Override
|
245 |
public void setAbort(String arg0) { |
246 |
taskStatus.abort(); |
247 |
} |
248 |
@Override
|
249 |
public void setAbort() { |
250 |
taskStatus.abort(); |
251 |
} |
252 |
}); |
253 |
ByteArrayInputStream is = new ByteArrayInputStream(FileUtils.readFileToByteArray(file)); |
254 |
|
255 |
JimiRasterImage jimiRasterImage = Jimi.getRasterImage(is, Jimi.SYNCHRONOUS); |
256 |
NewImageConsumer imageConsumer = new NewImageConsumer();
|
257 |
imageConsumer.addImage(jimiRasterImage); |
258 |
imageConsumer.waitFor(); |
259 |
image = new MemoryImage(jimiRasterImage);
|
260 |
|
261 |
envelope = GeometryLocator.getGeometryManager().createEnvelope(0, 0, image.columns, image.rows, Geometry.SUBTYPES.GEOM2D); |
262 |
|
263 |
resource.notifyClose(); |
264 |
|
265 |
resource.setData(image); |
266 |
} finally {
|
267 |
if( reader != null ) { |
268 |
reader.close(); |
269 |
} |
270 |
} |
271 |
return null; |
272 |
} |
273 |
|
274 |
}); |
275 |
} catch (Exception e) { |
276 |
this.image = null; |
277 |
try {
|
278 |
throw new OpenException(resource.getName(), e); |
279 |
} catch (AccessResourceException e1) {
|
280 |
throw new OpenException(getProviderName(), e); |
281 |
} |
282 |
} finally {
|
283 |
this.taskStatus.remove();
|
284 |
} |
285 |
} |
286 |
|
287 |
@Override
|
288 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
289 |
if( DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name) ) {
|
290 |
return this.envelope; |
291 |
} else if( DataStore.METADATA_CRS.equalsIgnoreCase(name) ) { |
292 |
IProjection pro = this.getJimiParameters().getCRS();
|
293 |
if (pro != null) { |
294 |
return pro;
|
295 |
} |
296 |
} |
297 |
return super.getDynValue(name); |
298 |
} |
299 |
|
300 |
@Override
|
301 |
public void close() throws CloseException { |
302 |
this.image = null; |
303 |
} |
304 |
|
305 |
@Override
|
306 |
public ResourceProvider getResource() {
|
307 |
return this.resource; |
308 |
} |
309 |
|
310 |
|
311 |
@Override
|
312 |
public Object getSourceId() { |
313 |
return this.getJimiParameters().getFile(); |
314 |
} |
315 |
|
316 |
@Override
|
317 |
public String getProviderName() { |
318 |
return NAME;
|
319 |
} |
320 |
|
321 |
@Override
|
322 |
public String getName() { |
323 |
String name = this.getJimiParameters().getFile().getName(); |
324 |
return FilenameUtils.getBaseName(name);
|
325 |
} |
326 |
|
327 |
@Override
|
328 |
public String getFullName() { |
329 |
return this.getJimiParameters().getFile().getAbsolutePath(); |
330 |
} |
331 |
|
332 |
@Override
|
333 |
public boolean closeResourceRequested(ResourceProvider resource) { |
334 |
return true; |
335 |
} |
336 |
|
337 |
@Override
|
338 |
/*
|
339 |
* (non-Javadoc)
|
340 |
*
|
341 |
* @see
|
342 |
* org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org.
|
343 |
* gvsig.fmap.dal.resource.spi.ResourceProvider)
|
344 |
*/
|
345 |
public void resourceChanged(ResourceProvider resource) { |
346 |
this.getStoreServices().notifyChange(
|
347 |
DataStoreNotification.RESOURCE_CHANGED, |
348 |
resource); |
349 |
} |
350 |
|
351 |
@Override
|
352 |
/* (non-Javadoc)
|
353 |
* @see org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider#doDispose()
|
354 |
*/
|
355 |
protected void doDispose() throws BaseException { |
356 |
super.doDispose();
|
357 |
resource.removeConsumer(this);
|
358 |
} |
359 |
|
360 |
private void waitImage(Image image) |
361 |
{ |
362 |
|
363 |
if (waitComponent == null) |
364 |
{ |
365 |
waitComponent = new Canvas(); |
366 |
} |
367 |
MediaTracker tracker = new MediaTracker(waitComponent); |
368 |
tracker.addImage(image, 0);
|
369 |
try
|
370 |
{ |
371 |
tracker.waitForAll(); |
372 |
} |
373 |
catch (InterruptedException e) |
374 |
{ |
375 |
// do nothing
|
376 |
} |
377 |
} |
378 |
|
379 |
public class NewImageConsumer implements ImageConsumer, Runnable |
380 |
{ |
381 |
private ImageProducer _ip; |
382 |
|
383 |
private Thread _thr; |
384 |
|
385 |
private boolean _f; |
386 |
|
387 |
private int _n; |
388 |
|
389 |
public void addImage(JimiRasterImage i) |
390 |
{ |
391 |
_ip = i.getImageProducer(); |
392 |
|
393 |
_ip.addConsumer(this);
|
394 |
|
395 |
_thr = Thread.currentThread();
|
396 |
|
397 |
_f = false;
|
398 |
|
399 |
_n = -1;
|
400 |
} |
401 |
|
402 |
public int waitFor() |
403 |
{ |
404 |
new Thread(this).start(); |
405 |
|
406 |
while (_f == false) _thr.suspend(); |
407 |
|
408 |
return _n;
|
409 |
} |
410 |
|
411 |
public void imageComplete(int nStatus) |
412 |
{ |
413 |
_f = true;
|
414 |
|
415 |
_n = nStatus; |
416 |
|
417 |
_ip.removeConsumer(this);
|
418 |
|
419 |
_thr.resume(); |
420 |
} |
421 |
|
422 |
public void run() |
423 |
{ |
424 |
_ip.startProduction(this);
|
425 |
} |
426 |
|
427 |
public void setColorModel(ColorModel cm) |
428 |
{ |
429 |
; |
430 |
} |
431 |
|
432 |
public void setDimensions(int w, int h) |
433 |
{ |
434 |
; |
435 |
} |
436 |
|
437 |
public void setHints(int nHints) |
438 |
{ |
439 |
; |
440 |
} |
441 |
|
442 |
public void setProperties(Hashtable htProperties) |
443 |
{ |
444 |
; |
445 |
} |
446 |
|
447 |
public void setPixels(int x, int y, int w, int h, |
448 |
ColorModel cm, byte rgb[], |
449 |
int nOffset, int nScansize) |
450 |
{ |
451 |
; |
452 |
} |
453 |
|
454 |
public void setPixels(int x, int y, int w, int h, |
455 |
ColorModel cm, int rgn[], |
456 |
int nOffset, int nScansize) |
457 |
{ |
458 |
; |
459 |
} |
460 |
} |
461 |
|
462 |
} |