Statistics
| Revision:

gvsig-raster / org.gvsig.raster.netcdf / trunk / org.gvsig.raster.netcdf / org.gvsig.raster.netcdf.io / src / main / java / org / gvsig / raster / netcdf / io / NetCDFProvider.java @ 408

History | View | Annotate | Download (16.1 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.fmap.dal.coverage.dataset.io.netcdf;
23

    
24
import java.awt.geom.AffineTransform;
25
import java.io.IOException;
26

    
27
import org.gvsig.fmap.dal.DALFileLocator;
28
import org.gvsig.fmap.dal.DALLocator;
29
import org.gvsig.fmap.dal.DataStore;
30
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
31
import org.gvsig.fmap.dal.coverage.dataset.io.tile.downloader.FileTileServer;
32
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
33
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
34
import org.gvsig.fmap.dal.coverage.exception.BandAccessException;
35
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
36
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
37
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
38
import org.gvsig.fmap.dal.coverage.exception.ParsingException;
39
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
40
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
41
import org.gvsig.fmap.dal.coverage.store.RasterFileStoreParameters;
42
import org.gvsig.fmap.dal.exception.OpenException;
43
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
44
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
45
import org.gvsig.metadata.MetadataLocator;
46
import org.gvsig.raster.cache.tile.provider.TileListener;
47
import org.gvsig.raster.cache.tile.provider.TileServer;
48
import org.gvsig.raster.impl.datastruct.ExtentImpl;
49
import org.gvsig.raster.impl.provider.DefaultRasterProvider;
50
import org.gvsig.raster.impl.provider.RasterProvider;
51
import org.gvsig.raster.impl.store.AbstractRasterDataParameters;
52
import org.gvsig.raster.impl.store.AbstractRasterDataStore;
53
import org.gvsig.raster.impl.store.DefaultStoreFactory;
54
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
55
import org.gvsig.raster.impl.store.properties.DataStoreMetadata;
56
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
57
import org.gvsig.tools.ToolsLocator;
58
import org.gvsig.tools.extensionpoint.ExtensionPoint;
59
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
60

    
61
import ucar.nc2.NetcdfFile;
62
/**
63
 * Data provider for NetCDF files
64
 *
65
 * @author Nacho Brodin (nachobrodin@gmail.com)
66
 */
67
public class NetCDFProvider extends DefaultRasterProvider {
68
        public static String             NAME                     = "NetCDF Store";
69
        public static String             DESCRIPTION              = "NetCDF Raster file";
70
        public final String              METADATA_DEFINITION_NAME = "NetCDFStore";
71
        
72
        private static String[]          formatList               = new String[]{"nc"};
73

    
74
        private Extent                   viewRequest              = null;
75
        private TileServer               tileServer               = null;
76
        private boolean                  open                     = false;
77
        
78
    private NetcdfFile               netCDFFile               = null;
79

    
80
        public static void register() {
81
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
82
                ExtensionPoint point = extensionPoints.get("RasterReader");
83
                addFormatsToRegistry(point, NetCDFProvider.class);
84
                
85
                point = extensionPoints.get("DefaultDriver");
86
                point.append("reader", "", NetCDFProvider.class);
87
                
88
                DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
89
                if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
90
                        dataman.registerStoreProvider(NAME,
91
                                        NetCDFProvider.class, NetCDFDataParameters.class);
92
                }
93
                
94
                if(DALFileLocator.getFilesystemServerExplorerManager() != null)
95
                        DALFileLocator.getFilesystemServerExplorerManager().registerProvider(
96
                                        NAME, DESCRIPTION,
97
                                        NetCDFFilesystemServerExplorer.class);
98
                
99
                dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
100
        }
101
        
102
        /**
103
         * Adds the list format to the extension point
104
         * @param point
105
         */
106
        public static void addFormatsToRegistry(ExtensionPoint point, Class<?> c) {
107
                for (int i = 0; i < formatList.length; i++) {
108
                        point.append(formatList[i], "", c);
109
                }
110
        }
111
        
112
        /**
113
         * Returns true if the extension is supported and false if doesn't
114
         * @param ext
115
         * @return
116
         */
117
        public static boolean isExtensionSupported(String ext) {
118
                for (int i = 0; i < formatList.length; i++) {
119
                        if(formatList[i].compareTo(ext) == 0)
120
                                return true;
121
                }
122
                return false;
123
        }
124
        
125
        /**
126
         * Constructor. Abre el dataset.
127
         * @param proj Proyecci?n
128
         * @param fName Nombre del fichero
129
         * @throws NotSupportedExtensionException
130
         */
131
        public NetCDFProvider(String params) throws NotSupportedExtensionException, OpenException {
132
                super(params);
133
                if(params instanceof String) {
134
                        NetCDFDataParameters p = new NetCDFDataParameters();
135
                        p.setURI((String)params);
136
                        super.init(p, null, ToolsLocator.getDynObjectManager()
137
                                        .createDynObject(
138
                                                        MetadataLocator.getMetadataManager().getDefinition(
139
                                                                        DataStore.METADATA_DEFINITION_NAME)));
140
                        init(p, null);
141
                }
142
        }
143
        
144
        public NetCDFProvider (NetCDFDataParameters params,
145
                        AbstractRasterDataStore storeServices) throws NotSupportedExtensionException, OpenException {
146
                super(params, storeServices, ToolsLocator.getDynObjectManager()
147
                                .createDynObject(
148
                                                MetadataLocator.getMetadataManager().getDefinition(
149
                                                                DataStore.METADATA_DEFINITION_NAME)));
150
                init(params, storeServices);
151
        }
152

    
153
        /**
154
         * Crea las referencias al fichero y carga
155
         * las estructuras con la informaci?n y los metadatos.
156
         * @param proj Proyecci?n
157
         * @param param Parametros de carga
158
         * @throws NotSupportedExtensionException
159
         */
160
        public void init (AbstractRasterDataParameters params,
161
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException, OpenException {
162
                
163
                //TODO:
164
                
165
                if(((RasterFileStoreParameters)params).getFile().exists()) {
166
            try { 
167
                netCDFFile = NetcdfFile.open(((RasterFileStoreParameters)params).getFile().getAbsolutePath());
168
            } catch (IOException e) {
169
                throw new OpenException("Imposible to read the file", e);
170
            }  
171

    
172
                        setParam(params);
173
                        colorTable = null;
174
                        noData = 0;
175
                        wktProjection = null;
176
                        //CrsWkt crs = new CrsWkt(wktProjection);
177
                        //IProjection proj = CRSFactory.getCRS("EPSG:23030");
178
                        noDataEnabled = true;
179
                        ownTransformation = null;
180
                        externalTransformation = (AffineTransform)ownTransformation.clone();
181
                        load();
182
                } else
183
                        setParam(params);
184
                bandCount = 0;
185
                setDataType(null);
186
                super.init();
187

    
188
                try {
189
                        loadFromRmf(getRmfBlocksManager());
190
                } catch (ParsingException e) {
191
                        //No lee desde rmf
192
                }
193
        }
194
        
195
        /*
196
         * (non-Javadoc)
197
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#load()
198
         */
199
        public RasterProvider load() {
200
                return this;
201
        }
202
        
203
        /*
204
         * (non-Javadoc)
205
         * @see org.gvsig.raster.impl.provider.RasterProvider#isOpen()
206
         */
207
        public boolean isOpen() {
208
                return open;
209
        }
210

    
211
        /*
212
         * (non-Javadoc)
213
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#close()
214
         */
215
        public void close() {
216
                //TODO:
217
        }
218

    
219
        /*
220
         * (non-Javadoc)
221
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#translateFileName(java.lang.String)
222
         */
223
        public String translateFileName(String fileName) {
224
                return fileName;
225
        }
226

    
227
        /**
228
         * Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci?n
229
         * de la vista asignada ya que la petici?n viene en coordenadas del fichero .rmf y la vista (v)
230
         * ha de estar en coordenadas del fichero.
231
         */
232
        public void setView(Extent e) {
233
                viewRequest = new ExtentImpl(e);
234
        }
235

    
236
        /*
237
         * (non-Javadoc)
238
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getView()
239
         */
240
        public Extent getView() {
241
                return viewRequest;
242
        }
243

    
244
        /*
245
         * (non-Javadoc)
246
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWidth()
247
         */
248
        public double getWidth() {
249
                //TODO:
250
                return 0;
251
        }
252

    
253
        /*
254
         * (non-Javadoc)
255
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
256
         */
257
        public double getHeight() {
258
                //TODO:
259
                return 0;
260
        }
261

    
262
        /**
263
         * Read a line from the file
264
         * @param line
265
         * @param band
266
         * @return
267
         * @throws InvalidSetViewException
268
         * @throws FileNotOpenException
269
         * @throws RasterDriverException
270
         * @Deprecated This operation is deprecated because is not useful and in the future
271
         * it will not be maintained. The abstract operation has dissapear
272
         */
273
        public Object readCompleteLine(int line, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
274
                if(line > this.getHeight() || band > this.getBandCount())
275
                        throw new InvalidSetViewException("Request out of grid");
276

    
277
                //TODO:
278
                return null;
279
        }
280

    
281
        /*
282
         *  (non-Javadoc)
283
         * @see org.gvsig.raster.dataset.RasterDataset#readBlock(int, int)
284
         */
285
        public Object readBlock(int pos, int blockHeight)
286
                throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
287
                if(pos < 0)
288
                        throw new InvalidSetViewException("Request out of grid");
289

    
290
                if((pos + blockHeight) > getHeight())
291
                        blockHeight = Math.abs(((int)getHeight()) - pos);
292
                
293
                //TODO:
294
                return null;
295
        }
296

    
297
        /*
298
         * (non-Javadoc)
299
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getData(int, int, int)
300
         */
301
        public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
302
                if(x < 0 || y < 0 || x >= getWidth() || y >= getHeight())
303
                        throw new InvalidSetViewException("Request out of grid");
304
                
305
                //TODO
306
                return null;
307
        }
308

    
309
        /*
310
         * (non-Javadoc)
311
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.raster.cache.tile.provider.TileListener)
312
         */
313
        public void getWindow(Extent ex, int bufWidth, int bufHeight, 
314
                        BandList bandList, TileListener listener) throws ProcessInterruptedException, RasterDriverException {
315
                //TODO
316
        }
317

    
318
        /*
319
         * (non-Javadoc)
320
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(org.gvsig.fmap.dal.coverage.datastruct.Extent, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
321
         */
322
        public Buffer getWindow(Extent ex, BandList bandList, Buffer rasterBuf) 
323
                throws ProcessInterruptedException, RasterDriverException {
324
                //TODO
325

    
326
                return rasterBuf;
327
        }
328

    
329
        /*
330
         * (non-Javadoc)
331
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer, boolean)
332
         */
333
        public Buffer getWindow(double ulx, double uly, double w, double h, 
334
                        BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
335
                //TODO
336

    
337
                return rasterBuf;
338
        }
339

    
340
        /*
341
         * (non-Javadoc)
342
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer, boolean)
343
         */
344
        public Buffer getWindow(Extent extent, 
345
                        int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
346
                //TODO
347
                
348
                return rasterBuf;
349
        }
350

    
351
        /*
352
         * (non-Javadoc)
353
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
354
         */
355
        public Buffer getWindow(int x, int y, int w, int h, 
356
                        BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
357
                //TODO
358
                
359
                return rasterBuf;
360
        }
361

    
362
        /*
363
         * (non-Javadoc)
364
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, int, int, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
365
         */
366
        public Buffer getWindow(int x, int y, int w, int h, 
367
                        int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
368
                //TODO
369
                
370
                return rasterBuf;
371
        }
372

    
373
        /*
374
         * (non-Javadoc)
375
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getBlockSize()
376
         */
377
        public int getBlockSize(){
378
                //TODO
379
                
380
                return 0;
381
        }
382

    
383
        /*
384
         * (non-Javadoc)
385
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getMetadata()
386
         */
387
        public DataStoreMetadata getMetadata() {
388
                //TODO
389
                
390
                return null;
391
        }
392

    
393
        /*
394
         * (non-Javadoc)
395
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getColorInterpretation()
396
         */
397
        public DataStoreColorInterpretation getColorInterpretation(){
398
                //TODO
399
                
400
                return null;
401
        }
402

    
403
        /*
404
         * (non-Javadoc)
405
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setColorInterpretation(org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation)
406
         */
407
        public void setColorInterpretation(DataStoreColorInterpretation colorInterpretation){
408
                //TODO
409
        }
410

    
411
        /*
412
         * (non-Javadoc)
413
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getTransparency()
414
         */
415
        public DataStoreTransparency getTransparency() {
416
                //TODO
417
                
418
                return null;
419
        }
420

    
421
        /*
422
         * (non-Javadoc)
423
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isGeoreferenced()
424
         */
425
        public boolean isGeoreferenced() {
426
                //TODO
427
                
428
                return false;
429
        }
430

    
431
        /**
432
         * Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
433
         * el valor de esta variable cada vez que dibuja.
434
         * @return true si se ha supersampleado y false si no se ha hecho.
435
         */
436
        public boolean isSupersampling() {
437
                //TODO
438
                
439
                return false;
440
        }
441

    
442
        /*
443
         * (non-Javadoc)
444
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setAffineTransform(java.awt.geom.AffineTransform)
445
         */
446
        public void setAffineTransform(AffineTransform t){
447
                super.setAffineTransform(t);
448
        }
449

    
450
        /*
451
         * (non-Javadoc)
452
         * @see org.gvsig.raster.impl.provider.RasterProvider#getOverviewCount(int)
453
         */
454
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
455
                if(band >= getBandCount())
456
                        throw new BandAccessException("Wrong band");
457
                //TODO
458
                
459
                return 0;
460
        }
461

    
462
        /*
463
         * (non-Javadoc)
464
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewWidth(int, int)
465
         */
466
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
467
                if (band >= getBandCount())
468
                        throw new BandAccessException("Wrong band");
469
                //TODO
470
                
471
                return 0;
472
        }
473

    
474
        /*
475
         * (non-Javadoc)
476
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewHeight(int, int)
477
         */
478
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
479
                if (band >= getBandCount())
480
                        throw new BandAccessException("Wrong band");
481
                //TODO
482
                
483
                return 0;
484
        }
485

    
486
        /*
487
         * (non-Javadoc)
488
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#overviewsSupport()
489
         */
490
        public boolean overviewsSupport() {
491
                return true;
492
        }
493

    
494
        /*
495
         * (non-Javadoc)
496
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isReproyectable()
497
         */
498
        public boolean isReproyectable() {
499
                return true;
500
        }
501

    
502
        /*
503
         * (non-Javadoc)
504
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getName()
505
         */
506
        public String getName() {
507
                return NAME;
508
        }
509
        
510
        /*
511
         * (non-Javadoc)
512
         * @see org.gvsig.raster.impl.provider.RasterProvider#setStatus(org.gvsig.raster.impl.provider.RasterProvider)
513
         */
514
        public void setStatus(RasterProvider provider) {
515
                if(provider instanceof NetCDFProvider) {
516
                        //Not implemented yet
517
                }
518
        }
519
        
520
        /*
521
         * (non-Javadoc)
522
         * @see org.gvsig.raster.impl.provider.RasterProvider#getTileServer()
523
         */
524
        public TileServer getTileServer() {
525
                if(tileServer == null)
526
                        tileServer = new FileTileServer(this);
527
                return tileServer;
528
        }
529
}