Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / DefaultRasterManager.java @ 2348

History | View | Annotate | Download (16.2 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.raster.impl;
23

    
24
import java.awt.geom.AffineTransform;
25
import java.lang.reflect.Constructor;
26
import java.lang.reflect.InvocationTargetException;
27
import java.util.HashMap;
28
import java.util.Set;
29

    
30
import org.cresques.cts.IProjection;
31
import org.gvsig.fmap.dal.coverage.BufferFactory;
32
import org.gvsig.fmap.dal.coverage.RasterLibrary;
33
import org.gvsig.fmap.dal.coverage.RasterManager;
34
import org.gvsig.fmap.dal.coverage.RasterService;
35
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
36
import org.gvsig.fmap.dal.coverage.datastruct.DataStructFactory;
37
import org.gvsig.fmap.dal.coverage.datastruct.Params;
38
import org.gvsig.fmap.dal.coverage.datastruct.RasterLegend;
39
import org.gvsig.fmap.dal.coverage.exception.FileNotExistsException;
40
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
41
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
42
import org.gvsig.fmap.dal.coverage.exception.QueryException;
43
import org.gvsig.fmap.dal.coverage.exception.ROIException;
44
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
45
import org.gvsig.fmap.dal.coverage.exception.RasterLegendIONotFound;
46
import org.gvsig.fmap.dal.coverage.filter.FilterLoader;
47
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
48
import org.gvsig.fmap.dal.coverage.grid.render.ImageDrawer;
49
import org.gvsig.fmap.dal.coverage.process.TaskEventManager;
50
import org.gvsig.fmap.dal.coverage.process.overview.OverviewBuilder;
51
import org.gvsig.fmap.dal.coverage.process.vector.Vectorization;
52
import org.gvsig.fmap.dal.coverage.store.DataServerWriter;
53
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
54
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
55
import org.gvsig.fmap.dal.coverage.store.RasterWriter;
56
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
57
import org.gvsig.fmap.dal.coverage.util.CRSUtils;
58
import org.gvsig.fmap.dal.coverage.util.ColorConversion;
59
import org.gvsig.fmap.dal.coverage.util.FileUtils;
60
import org.gvsig.fmap.dal.coverage.util.Historical;
61
import org.gvsig.fmap.dal.coverage.util.MathUtils;
62
import org.gvsig.fmap.dal.coverage.util.ProviderServices;
63
import org.gvsig.fmap.dal.coverage.util.RasterUtils;
64
import org.gvsig.fmap.dal.feature.FeatureStore;
65
import org.gvsig.raster.impl.buffer.DefaultDataServerWriter;
66
import org.gvsig.raster.impl.buffer.DefaultRasterQuery;
67
import org.gvsig.raster.impl.buffer.RasterBuffer;
68
import org.gvsig.raster.impl.buffer.RasterMemoryBuffer;
69
import org.gvsig.raster.impl.buffer.cache.RasterCache;
70
import org.gvsig.raster.impl.buffer.cache.RasterReadOnlyBuffer;
71
import org.gvsig.raster.impl.datastruct.DefaultDataStructFactory;
72
import org.gvsig.raster.impl.datastruct.legend.GimpGradients;
73
import org.gvsig.raster.impl.datastruct.legend.GimpPalettes;
74
import org.gvsig.raster.impl.datastruct.legend.LegendgvSIG;
75
import org.gvsig.raster.impl.datastruct.legend.RasterLegendIO;
76
import org.gvsig.raster.impl.grid.filter.DefaultRasterFilterList;
77
import org.gvsig.raster.impl.grid.filter.FilterLoaderImpl;
78
import org.gvsig.raster.impl.grid.render.ImageDrawerImpl;
79
import org.gvsig.raster.impl.grid.roi.ROIStatistic;
80
import org.gvsig.raster.impl.grid.roi.VectorialROIImpl;
81
import org.gvsig.raster.impl.grid.roi.VectorialROIsReader;
82
import org.gvsig.raster.impl.grid.roi.VectorialROIsWriter;
83
import org.gvsig.raster.impl.process.DefaultTaskEventManager;
84
import org.gvsig.raster.impl.process.RasterTask;
85
import org.gvsig.raster.impl.process.RasterTaskQueue;
86
import org.gvsig.raster.impl.process.vector.PotraceVectorization;
87
import org.gvsig.raster.impl.store.ParamsImpl;
88
import org.gvsig.raster.impl.store.QueryableRaster;
89
import org.gvsig.raster.impl.store.writer.DefaultRasterWriter;
90
import org.gvsig.raster.roi.AbstractROI;
91
import org.gvsig.raster.roi.ROIReader;
92
import org.gvsig.raster.roi.ROIWriter;
93
import org.gvsig.raster.roi.VectorialROI;
94
import org.gvsig.raster.util.DefaultColorConversion;
95
import org.gvsig.raster.util.DefaultFileUtils;
96
import org.gvsig.raster.util.DefaultHistorical;
97
import org.gvsig.raster.util.DefaultMathUtils;
98
import org.gvsig.raster.util.DefaultProviderServices;
99
import org.gvsig.raster.util.DefaultRasterUtils;
100
import org.gvsig.tools.ToolsLocator;
101
import org.gvsig.tools.extensionpoint.ExtensionPoint;
102
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
103
import org.gvsig.tools.service.ServiceException;
104

    
105
/**
106
 * Default {@link RasterManager} implementation.
107
 * 
108
 * @author gvSIG Team
109
 * @version $Id$
110
 */
111
public class DefaultRasterManager implements RasterManager {
112
        private static DefaultRasterManager internalInstance  = new DefaultRasterManager();
113
        //private static final Logger         logger            = LoggerFactory.getLogger(DefaultRasterManager.class);
114
        private FileUtils                   file              = null;
115
        private MathUtils                   math              = null;
116
        private RasterUtils                 util              = null;
117
        private ProviderServices            pInfo             = null;
118
        private DefaultColorConversion      colorConversion   = null;
119
        private DataStructFactory           dataStructFactory = null;
120
        
121
        public static HashMap<String, OverviewBuilder>
122
                                                                            overviewBuilderRegistry   = new HashMap<String, OverviewBuilder>();
123
        public static CRSUtils              crsUtilRegistry   = null;
124
        
125
        /**
126
         * Gets an instance of this object for internal use.
127
         * @return DefaultRasterManager
128
         */
129
        public static DefaultRasterManager getInstance() {
130
                return internalInstance;
131
        }
132
        
133
        public DataStructFactory getDataStructFactory() {
134
                if(dataStructFactory == null)
135
                        dataStructFactory = new DefaultDataStructFactory();
136
                return dataStructFactory;
137
        }
138
        
139
        public ProviderServices getProviderServices() {
140
                if(pInfo == null)
141
                        pInfo = new DefaultProviderServices();
142
                return pInfo;
143
        }
144
        
145
        public boolean isCRSUtilSupported() {
146
                return crsUtilRegistry != null;
147
        }
148
        
149
        public CRSUtils getCRSUtils() {
150
                return crsUtilRegistry;
151
        }
152
        
153
        public FileUtils getFileUtils() {
154
                if(file == null)
155
                        file = new DefaultFileUtils();
156
                return file;
157
        }
158
        
159
        public RasterUtils getRasterUtils() {
160
                if(util == null)
161
                        util = new DefaultRasterUtils();
162
                return util;
163
        }
164
        
165
        public MathUtils getMathUtils() {
166
                if(math == null)
167
                        math = new DefaultMathUtils();
168
                return math;
169
        }
170
        
171
    public RasterService getRasterService()
172
        throws ServiceException {
173
        RasterService fc = new DefaultRasterService(this);
174
        return fc;
175
    }
176
    
177
        public String[] getLegendFormats() {
178
                return RasterLegendIO.formats;
179
        }
180
        
181
        public RasterLegend getRasterLegendIO(String formatName) throws RasterLegendIONotFound {
182
                if (formatName.equals(RasterLegendIO.formats[0]))
183
                        return new LegendgvSIG();
184

    
185
                if (formatName.equals(RasterLegendIO.formats[1]))
186
                        return new GimpGradients();
187

    
188
                if (formatName.equals(RasterLegendIO.formats[2]))
189
                        return new GimpPalettes();
190

    
191
                // Incluir mas formatos de la misma manera
192
                throw new RasterLegendIONotFound();
193
        }
194
        
195
        public DataServerWriter createDataServerWriter() {
196
                return new DefaultDataServerWriter();
197
        }
198
        
199
        public RasterQuery createQuery() {
200
                return new DefaultRasterQuery();
201
        }
202
        
203
        public Buffer createBuffer(int dataType, int width, int height, int bandNr, boolean malloc) {
204
                //Opci?n de cachear siempre activada (Solo DEBUG)
205
                if(RasterBuffer.forceToLoadInCache)
206
                        return new RasterCache(dataType, width, height, bandNr);
207
                if(RasterBuffer.forceToLoadInReadOnlyCache){
208
                return new RasterReadOnlyBuffer(dataType, width, height, bandNr);
209
                }
210
                        
211
                if(RasterBuffer.cacheOn){
212
                        long size = (long)((long)getRasterUtils().getBytesFromRasterBufType(dataType) * (long)width * (long)height * (long)bandNr) / 1024L;
213
                        long ms1 = RasterLibrary.cacheSize * 1024L;
214
                        if(size <= ms1)
215
                                return new RasterMemoryBuffer(dataType, width, height, bandNr, malloc);
216
                        else
217
                                return new RasterCache(dataType, width, height, bandNr);
218
                }else
219
                        return new RasterMemoryBuffer(dataType, width, height, bandNr, malloc);
220
        }
221
        
222
        public Buffer createReadOnlyBuffer(int dataType, int width, int height, int bandNr) {
223
                return new RasterReadOnlyBuffer(dataType, width, height, bandNr);
224
        }
225
        
226
        public Buffer createMemoryBuffer(int dataType, int width, int height, int bandNr, boolean malloc) {
227
                return new RasterMemoryBuffer(dataType, width, height, bandNr, malloc);
228
        }
229

    
230
        public RasterWriter createWriter(String fName) throws NotSupportedExtensionException, RasterDriverException {
231
                String ext = getFileUtils().getExtensionFromFileName(fName);
232
                DefaultRasterWriter grw = null;
233
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
234
                ExtensionPoint point = extensionPoints.get("RasterWriter");
235
//                ExtensionPoint extensionPoint = ExtensionPoint.getExtensionPoint("RasterWriter");
236

    
237
                if (!point.has(ext))
238
                        return grw;
239

    
240
                Class<?> clase = point.get(ext).getExtension();
241
                Class<?>[] args = { String.class };
242
                try {
243
                        Constructor<?> hazNuevo = clase.getConstructor(args);
244
                        Object[] args2 = { fName };
245
                        grw = (DefaultRasterWriter) hazNuevo.newInstance(args2);
246
                } catch (SecurityException e) {
247
                        throw new RasterDriverException("Error SecurityException in open", e);
248
                } catch (NoSuchMethodException e) {
249
                        throw new RasterDriverException("Error NoSuchMethodException in open", e);
250
                } catch (IllegalArgumentException e) {
251
                        throw new RasterDriverException("Error IllegalArgumentException in open", e);
252
                } catch (InstantiationException e) {
253
                        throw new RasterDriverException("Error InstantiationException in open", e);
254
                } catch (IllegalAccessException e) {
255
                        throw new RasterDriverException("Error IllegalAccessException in open", e);
256
                } catch (InvocationTargetException e) {
257
                        throw new NotSupportedExtensionException("Error in open", e);
258
                }
259
                return grw;
260
        }
261

    
262
        public RasterWriter createWriter(DataServerWriter dataWriter,
263
                                                                                                 String outFileName,
264
                                                                                                 int nBands,
265
                                                                                                 AffineTransform at,
266
                                                                                                 int outSizeX,
267
                                                                                                 int outSizeY,
268
                                                                                                 int dataType,
269
                                                                                                 Params params,
270
                                                                                                 IProjection proj) throws NotSupportedExtensionException, RasterDriverException {
271
                return createWriter(dataWriter, outFileName, nBands, at, outSizeX, outSizeY, dataType, params, proj, true);
272
        }
273

    
274
        public RasterWriter createWriter(DataServerWriter dataWriter,
275
                                                                                                 String outFileName,
276
                                                                                                 int nBands,
277
                                                                                                 AffineTransform at,
278
                                                                                                 int outSizeX,
279
                                                                                                 int outSizeY,
280
                                                                                                 int dataType,
281
                                                                                                 Params params,
282
                                                                                                 IProjection proj,
283
                                                                                                 boolean geo) throws NotSupportedExtensionException, RasterDriverException {
284
                String ext = outFileName.toLowerCase().substring(outFileName.lastIndexOf('.') + 1);
285
                DefaultRasterWriter grw = null;
286
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
287
                ExtensionPoint point = extensionPoints.get("RasterWriter");
288

    
289
                if (!point.has(ext))
290
                        return grw;
291

    
292
                Class<?> clase = point.get(ext).getExtension();
293
                Class<?>[] args = { DataServerWriter.class, String.class, Integer.class, AffineTransform.class, Integer.class, Integer.class, Integer.class, Params.class, IProjection.class, Boolean.class };
294
                try {
295
                        Constructor<?> hazNuevo = clase.getConstructor(args);
296
                        Object [] args2 = {dataWriter, outFileName, new Integer(nBands), at,
297
                                                                new Integer(outSizeX), new Integer(outSizeY), new Integer(dataType),
298
                                                                params, proj, new Boolean(geo)};
299
                        grw = (DefaultRasterWriter) hazNuevo.newInstance(args2);
300
                } catch (SecurityException e) {
301
                        throw new RasterDriverException("Error SecurityException in open", e);
302
                } catch (NoSuchMethodException e) {
303
                        throw new RasterDriverException("Error NoSuchMethodException in open", e);
304
                } catch (IllegalArgumentException e) {
305
                        throw new RasterDriverException("Error IllegalArgumentException in open", e);
306
                } catch (InstantiationException e) {
307
                        throw new RasterDriverException("Error InstantiationException in open", e);
308
                } catch (IllegalAccessException e) {
309
                        throw new RasterDriverException("Error IllegalAccessException in open", e);
310
                } catch (InvocationTargetException e) {
311
                        throw new NotSupportedExtensionException("Error in open. Problemas con las librer?as nativas.", e);
312
                }
313
                return grw;
314
        }
315
        
316
        public Params createWriterParams(String fileName) {
317
                try {
318
                        return createWriter(fileName).getParams();
319
                } catch (NotSupportedExtensionException e) {
320
                        return null;
321
                } catch (RasterDriverException e) {
322
                        return null;
323
                }
324
        }
325
        
326
        /**
327
         * Gets an object which vectorize a raster
328
         * @param dataStore
329
         * @return
330
         * @throws RasterDriverException
331
         * @throws ProcessInterruptedException
332
         *         When the object Vectorization is built the raster data buffer is loaded. 
333
         *         This operation can be interrupted
334
         */
335
        public Vectorization getVectorizeObject(QueryableRaster queryable) throws QueryException, ProcessInterruptedException {
336
                return new PotraceVectorization(queryable);
337
        }
338
        
339
        public OverviewBuilder getOverviewBuilder(String id) {
340
                return overviewBuilderRegistry.get(id);
341
        }
342
        
343
        public boolean isOverviewBuilderSupported() {
344
                return overviewBuilderRegistry.size() > 0;
345
        }
346
        
347

    
348
        public Set<String> getOverviewBuilderKeyList() {
349
                return overviewBuilderRegistry.keySet();
350
        }
351
        
352
        public FilterLoader createFilterLoader(int dataType) {
353
                return new FilterLoaderImpl(dataType);
354
        }
355
        
356
        public FilterLoader createFilterLoader(RasterFilterList filterList) {
357
                return new FilterLoaderImpl(filterList);
358
        }
359
        
360
    public Historical createHistoricalService() {
361
            return new DefaultHistorical();
362
    }
363
    
364
    public ColorConversion getColorConversion() {
365
            if(colorConversion == null)
366
                    colorConversion = new DefaultColorConversion();
367
            return colorConversion;
368
    }
369
        
370
        public Statistics createROIStatistics(AbstractROI roi) {
371
                return new ROIStatistic(roi);
372
        }
373
        
374
        public RasterFilterList createEmptyFilterList(int type) {
375
                DefaultRasterFilterList fl = new DefaultRasterFilterList();
376
                fl.setInitDataType(type);
377
                return fl;
378
        }
379
        
380
        public TaskEventManager getRasterTask() {
381
                return DefaultTaskEventManager.get();
382
        }
383
        
384
        public TaskEventManager createRasterTask(Object process) {
385
                RasterTask task = RasterTaskQueue.getRasterTask(Thread.currentThread().getId() + "");
386
                if(task != null) {
387
                        DefaultTaskEventManager man = new DefaultTaskEventManager();
388
                        man.setTask(task);
389
                        return man;
390
                }
391
                DefaultTaskEventManager man = new DefaultTaskEventManager(process);
392
                return man;
393
        }
394
        
395
        /**
396
         * Builds a new parameter object. These parameters represents the fieds of a filter
397
         * panel.
398
         * @param id
399
         * @param value
400
         * @param type
401
         * @param list
402
         * @return
403
         */
404
        public Params createParams(String id, Object value, int type, String[] list) {
405
                ParamsImpl params = new ParamsImpl();
406
                params.setParam(id, 
407
                                value, 
408
                                type, 
409
                                list);
410
                return params;
411
        }
412
        
413
        public ImageDrawer createImageDrawerService() {
414
                return new ImageDrawerImpl();
415
        }
416

    
417
        public BufferFactory getBufferFactory() {
418
                return new BufferFactoryImpl();
419
        }
420

    
421
        public ROIReader createROIReader(String filename, RasterDataStore store,
422
                        IProjection projection) throws ROIException, FileNotExistsException {
423
                //Actualmente solo puede ser vectorial pero filename puede ser raster tambi?n
424
                return new VectorialROIsReader(filename, store, projection);
425
        }
426

    
427
        public ROIWriter createROIWriter(String filename, IProjection projection) {
428
                //Actualmente solo puede ser vectorial pero filename puede ser raster tambi?n
429
                return new VectorialROIsWriter(filename, projection);
430
        }
431

    
432
        public VectorialROI createVectorialROI(RasterDataStore store) {
433
                return new VectorialROIImpl(store);
434
        }
435

    
436
        public ROIReader createVectorialROIReader(FeatureStore roiStore,
437
                        RasterDataStore store, IProjection projection) {
438
                return new VectorialROIsReader(roiStore, store, projection);
439
        }
440
}