Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.io / org.gvsig.raster.io.base / src / main / java / org / gvsig / fmap / dal / coverage / dataset / io / ermapper / ErmapperProvider.java @ 453

History | View | Annotate | Download (33.6 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.ermapper;
23

    
24
import java.awt.geom.AffineTransform;
25
import java.awt.geom.NoninvertibleTransformException;
26
import java.awt.geom.Point2D;
27

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

    
65
import com.ermapper.ecw.JNCSException;
66
import com.ermapper.ecw.JNCSFile;
67
import com.ermapper.ecw.JNCSFileNotOpenException;
68
import com.ermapper.ecw.JNCSInvalidSetViewException;
69
import com.ermapper.ecw.JNCSProgressiveUpdate;
70
/**
71
 * Driver de Ecw
72
 *
73
 * @author Nacho Brodin (nachobrodin@gmail.com)
74
 */
75
public class ErmapperProvider extends DefaultRasterProvider implements JNCSProgressiveUpdate {
76
        public static String                  NAME                     = "Ermapper Store";
77
        public static String                  DESCRIPTION              = "Ermapper Raster file";
78
        public static final String            METADATA_DEFINITION_NAME = "ErmapperStore";
79
        private JNCSFile                                       file                     = null;
80
        protected Transparency                        fileTransparency         = null;
81
        private Extent                        viewRequest              = null;
82
        private DataStoreColorInterpretation  colorInterpr             = null;
83
        private boolean                       open                     = false;
84
        private TileServer                    tileServer               = null;
85
        private static String[]               formatList               = new String[]{
86
                "ecw", 
87
                "jp2"};
88
        
89
        public static void register() {
90
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
91
                ExtensionPoint point = extensionPoints.get("RasterReader");
92
                addFormatsToRegistry(point, ErmapperProvider.class);
93
                
94
                DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
95
                if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
96
                        dataman.registerStoreProvider(NAME,
97
                                        ErmapperProvider.class, 
98
                                        ErmapperDataParameters.class);
99
                }
100
                
101
                if(DALFileLocator.getFilesystemServerExplorerManager() != null)
102
                        DALFileLocator.getFilesystemServerExplorerManager().registerProvider(
103
                                        NAME, 
104
                                        DESCRIPTION,
105
                                        ErmapperFilesystemServerExplorer.class);
106
                
107
            dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
108
        }
109
        
110
        /**
111
         * Adds the list format to the extension point
112
         * @param point
113
         */
114
        public static void addFormatsToRegistry(ExtensionPoint point, Class<?> c) {
115
                for (int i = 0; i < formatList.length; i++) {
116
                        point.append(formatList[i], "", c);
117
                }
118
        }
119
        
120
        /**
121
         * Returns true if the extension is supported and false if doesn't
122
         * @param ext
123
         * @return
124
         */
125
        public static boolean isExtensionSupported(String ext) {
126
                for (int i = 0; i < formatList.length; i++) {
127
                        if(formatList[i].compareTo(ext) == 0)
128
                                return true;
129
                }
130
                return false;
131
        }
132
        
133
        /**
134
         * Constructor. Abre el dataset.
135
         * @param proj Proyecci?n
136
         * @param fName Nombre del fichero ecw
137
         * @throws NotSupportedExtensionException
138
         */
139
        public ErmapperProvider(String params) throws NotSupportedExtensionException {
140
                super(params);
141
                if(params instanceof String) {
142
                        ErmapperDataParameters p = new ErmapperDataParameters();
143
                        p.setURI((String)params);
144
                        super.init(p, null, ToolsLocator.getDynObjectManager()
145
                                        .createDynObject(
146
                                                        MetadataLocator.getMetadataManager().getDefinition(
147
                                                                        DataStore.METADATA_DEFINITION_NAME)));
148
                        init(p, null);
149
                }
150
        }
151
        
152
        public ErmapperProvider(ErmapperDataParameters params,
153
                        AbstractRasterDataStore storeServices) throws NotSupportedExtensionException {
154
                super(params, storeServices, ToolsLocator.getDynObjectManager()
155
                                .createDynObject(
156
                                                MetadataLocator.getMetadataManager().getDefinition(
157
                                                                DataStore.METADATA_DEFINITION_NAME)));
158
                init(params, storeServices);
159
        }
160

    
161
        /**
162
         * Abre el dataset.
163
         * @param proj Proyecci?n
164
         * @param fName Nombre del fichero ecw
165
         * @throws NotSupportedExtensionException
166
         */
167
        public void init (AbstractRasterDataParameters params,
168
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
169
                setParam(params);
170
                if (!((RasterFileStoreParameters)param).getFile().exists() && !((RasterDataParameters)param).getURI().startsWith("ecwp:"))
171
                        throw new NotSupportedExtensionException("Extension not supported");
172

    
173
                try {
174
                        file = new JNCSFile(((RasterDataParameters)param).getURI(), false);
175
                } catch (JNCSException e1) {
176
                        throw new NotSupportedExtensionException("Error loading the ecw file", e1);
177
                }
178
                load();
179
                bandCount = file.numBands;
180
                getTransparency();
181

    
182
                int[] dt = new int[bandCount];
183
                for (int i = 0; i < bandCount; i++)
184
                        dt[i] = Buffer.TYPE_BYTE;
185
                setDataType(dt);
186

    
187
                super.init();
188

    
189
                try {
190
                        loadFromRmf(getRmfBlocksManager());
191
                } catch (ParsingException e) {
192
                        //No lee desde rmf
193
                }
194
                open = true;
195
        }
196

    
197
        /*
198
         * (non-Javadoc)
199
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#load()
200
         */
201
        public RasterProvider load() {
202
                ownTransformation = new AffineTransform(file.cellIncrementX, 0, 0, file.cellIncrementY, file.originX, file.originY);
203
                externalTransformation = (AffineTransform) ownTransformation.clone();
204
                return this;
205
        }
206
        
207
        /*
208
         * (non-Javadoc)
209
         * @see org.gvsig.raster.impl.provider.RasterProvider#isOpen()
210
         */
211
        public boolean isOpen() {
212
                return open;
213
        }
214

    
215
        /*
216
         * (non-Javadoc)
217
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#close()
218
         */
219
        public void close() {
220
                if(file != null) {
221
                        open = false;
222
                        file.close(false);
223
                        file = null;
224
                }
225
        }
226

    
227
        /*
228
         * (non-Javadoc)
229
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getTransparency()
230
         */
231
        public Transparency getTransparency() {
232
                if(fileTransparency == null)
233
                        fileTransparency = new DataStoreTransparency();
234
                return fileTransparency;
235
        }
236

    
237
        /*
238
         * (non-Javadoc)
239
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWidth()
240
         */
241
        public double getWidth() {
242
                return file.width;
243
        }
244

    
245
        /*
246
         * (non-Javadoc)
247
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
248
         */
249
        public double getHeight() {
250
                return file.height;
251
        }
252

    
253
        /*
254
         * (non-Javadoc)
255
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getView()
256
         */
257
        public Extent getView() {
258
                return viewRequest;
259
        }
260

    
261
        /*
262
         * (non-Javadoc)
263
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setView(org.gvsig.fmap.dal.coverage.datastruct.Extent)
264
         */
265
        public void setView(Extent e) {
266
                viewRequest = new ExtentImpl(e);
267
        }
268
        
269
        /*
270
         * (non-Javadoc)
271
         * @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)
272
         */
273
        public void getWindow(Extent ex, int bufWidth, int bufHeight, 
274
                        BandList bandList, TileListener listener)throws ProcessInterruptedException, RasterDriverException {
275
                 
276
        }
277

    
278
        /*
279
         * (non-Javadoc)
280
         * @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)
281
         */
282
        public Buffer getWindow(Extent ex, BandList bandList, Buffer rasterBuf) 
283
                throws ProcessInterruptedException, RasterDriverException {
284
                Point2D p1 = new Point2D.Double(ex.getULX(), ex.getULY());
285
                Point2D p2 = new Point2D.Double(ex.getLRX(), ex.getLRY());
286
                try {
287
                        externalTransformation.inverseTransform(p1, p1);
288
                        externalTransformation.inverseTransform(p2, p2);
289
                        ownTransformation.transform(p1, p1);
290
                        ownTransformation.transform(p2, p2);
291
                } catch (NoninvertibleTransformException e) {
292
                        throw new RasterDriverException("Noninvertible transform");
293
                }
294

    
295
                Extent selectedExtent = new ExtentImpl(p1.getX(), p1.getY(), p2.getX(), p2.getY());
296

    
297
                setView(selectedExtent);
298
                int wPx = rasterBuf.getWidth();
299
                int hPx = rasterBuf.getHeight();
300
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
301

    
302
                loadBuffer(viewRequest, wPx, hPx, rasterBuf, bandList, stpBuffer);
303

    
304
                return rasterBuf;
305
        }
306

    
307
        /*
308
         * (non-Javadoc)
309
         * @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)
310
         */
311
        public Buffer getWindow(double ulx, double uly, double w, double h, 
312
                        BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
313
                //El incremento o decremento de las X e Y depende de los signos de rotaci?n y escala en la matriz de transformaci?n. Por esto
314
                //tenemos que averiguar si lrx es x + w o x -w, asi como si lry es y + h o y - h
315
                Extent ext = getExtent();
316
                Point2D pInit = rasterToWorld(new Point2D.Double(0, 0));
317
                Point2D pEnd = rasterToWorld(new Point2D.Double(getWidth(), getHeight()));
318
                double wRaster = Math.abs(pEnd.getX() - pInit.getX());
319
                double hRaster = Math.abs(pEnd.getY() - pInit.getY());
320
                double lrx = (((ext.getULX() - wRaster) > ext.maxX()) || ((ext.getULX() - wRaster) < ext.minX())) ? (ulx + w) : (ulx - w);
321
                double lry = (((ext.getULY() - hRaster) > ext.maxY()) || ((ext.getULY() - hRaster) < ext.minY())) ? (uly + h) : (uly - h);
322

    
323
                Point2D p1 = new Point2D.Double(ulx, uly);
324
                Point2D p2 = new Point2D.Double(lrx, lry);
325
                try {
326
                        externalTransformation.inverseTransform(p1, p1);
327
                        externalTransformation.inverseTransform(p2, p2);
328
                        p1.setLocation((int)p1.getX(), (int)p1.getY());
329
                        p2.setLocation((int)Math.ceil(p2.getX()), (int)Math.ceil(p2.getY()));
330
                        p1.setLocation(Math.max(p1.getX(), 0), Math.max(p1.getY(), 0));
331
                        p2.setLocation(Math.max(p2.getX(), 0), Math.max(p2.getY(), 0));
332
                        p1.setLocation(Math.min(p1.getX(), getWidth()), Math.min(p1.getY(), getHeight()));
333
                        p2.setLocation(Math.min(p2.getX(), getWidth()), Math.min(p2.getY(), getHeight()));
334
                        ownTransformation.transform(p1, p1);
335
                        ownTransformation.transform(p2, p2);
336
                } catch (NoninvertibleTransformException e) {
337
                        throw new RasterDriverException("Noninvertible transform");
338
                }
339

    
340
                Extent selectedExtent = new ExtentImpl(p1.getX(), p1.getY(), p2.getX(), p2.getY());
341

    
342
                setView(selectedExtent);
343
                int wPx = rasterBuf.getWidth();
344
                int hPx = rasterBuf.getHeight();
345
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
346

    
347
                loadBuffer(viewRequest, wPx, hPx, rasterBuf, bandList, stpBuffer);
348

    
349
                return rasterBuf;
350
        }
351

    
352
        /*
353
         * (non-Javadoc)
354
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
355
         */
356
        public Buffer getWindow(int x, int y, 
357
                        BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
358
                int w = rasterBuf.getWidth();
359
                int h = rasterBuf.getHeight();
360
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
361
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
362
                Extent selectedExtent = new ExtentImpl(init.getX(), init.getY(), end.getX(), end.getY());
363
                setView(selectedExtent);
364
                int[] stpBuffer = new int[]{0, 0 , w, h};
365

    
366
                loadBuffer(viewRequest, w, h, rasterBuf, bandList, stpBuffer);
367
                return rasterBuf;
368
        }
369

    
370
        /*
371
         * (non-Javadoc)
372
         * @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)
373
         */
374
        public Buffer getWindow(Extent extent, 
375
                        int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
376
                Point2D p1 = new Point2D.Double(extent.getULX(), extent.getULY());
377
                Point2D p2 = new Point2D.Double(extent.getLRX(), extent.getLRY());
378
                try {
379
                        externalTransformation.inverseTransform(p1, p1);
380
                        externalTransformation.inverseTransform(p2, p2);
381
                        p1.setLocation((int)p1.getX(), (int)p1.getY());
382
                        p2.setLocation((int)Math.ceil(p2.getX()), (int)Math.ceil(p2.getY()));
383
                        p1.setLocation(Math.max(p1.getX(), 0), Math.max(p1.getY(), 0));
384
                        p2.setLocation(Math.max(p2.getX(), 0), Math.max(p2.getY(), 0));
385
                        p1.setLocation(Math.min(p1.getX(), getWidth()), Math.min(p1.getY(), getHeight()));
386
                        p2.setLocation(Math.min(p2.getX(), getWidth()), Math.min(p2.getY(), getHeight()));
387
                        ownTransformation.transform(p1, p1);
388
                        ownTransformation.transform(p2, p2);
389
                } catch (NoninvertibleTransformException e) {
390
                        throw new RasterDriverException("Noninvertible transform");
391
                }
392
                Extent selectedExtent = new ExtentImpl(p1, p2);
393
                setView(selectedExtent);
394
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
395

    
396
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
397

    
398
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
399
                return rasterBuf;
400
        }
401

    
402
        /*
403
         * (non-Javadoc)
404
         * @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)
405
         */
406
        public Buffer getWindow(int x, int y, int w, int h, 
407
                        int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
408
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
409
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
410
                Extent selectedExtent = new ExtentImpl(init.getX(), init.getY(), end.getX(), end.getY());
411
                setView(selectedExtent);
412
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
413

    
414
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
415
                return rasterBuf;
416
        }
417

    
418
        /**
419
         * Carga el buffer con las bandas RGB del raster con los par?metros especificados de extensi?n
420
         * y tama?o de buffer. El problema de ecw es que solo podemos leer 3 bandas de una vez ya que solo disponemos
421
         * de una llamada readLineRGBA. Para leer m?s bandas tendremos que hacer multiples llamadas a setView para leer
422
         * 3 cada vez.
423
         *
424
         * Si por ejemplo tenemos un ecw de 6 bandas [0, 1, 2, 3, 4, 5] y queremos cargar un buffer con el siguiente orden
425
         * [0, -, 2, -, 4, -] La variable readBandsFromECW har? la llamada a setView con los valores [0, 2, 4, 0, 0, 0]. La
426
         * funci?n drawRGB ser? la encargada de hacer el switch para obtener [0, -, 2, -, 4, -].
427
         *
428
         * Bug#1: Si se dispone de un ecw de m?s de tres bandas podemos llamar a setView con readBandsFromECW con el orden
429
         * que queramos, por ejemplo [3, 2, 5, 1, 0] pero para ecw de 3 bandas la llamada con las bandas cambiadas no
430
         * hace caso. El caso de readBandsFromECW = [2, 0, 1] ser? tomado siempre como [0, 1, 2].
431
         *
432
         * @param selectedExtent Extensi?n seleccionada
433
         * @param bufWidth Ancho de buffer
434
         * @param bufHeight Alto de buffer
435
         * @param rasterBuf Buffer de datos
436
         */
437
        private void loadBuffer(Extent selectedExtent, int bufWidth, int bufHeight, Buffer rasterBuf, BandList bandList, int[] stpBuffer) throws ProcessInterruptedException, RasterDriverException {
438
                try{
439
                        RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
440
                        
441
                        int[] readBandsFromECW = new int[Math.max(file.numBands, 3)];
442
                        
443
                        for(int i = 0; i < readBandsFromECW.length; i ++) {
444
                                readBandsFromECW[i] = i;
445
                        }
446

    
447
                        if(task.getEvent() != null)
448
                                task.manageEvent(task.getEvent());
449

    
450
                        if(bufWidth > Math.round(Math.abs(selectedExtent.width() / file.cellIncrementX)))
451
                                bufWidth = (int)Math.round(Math.abs(selectedExtent.width() / file.cellIncrementX));
452
                        if(bufHeight > Math.round(Math.abs(selectedExtent.height() / file.cellIncrementY)))
453
                                bufHeight = (int)Math.round(Math.abs(selectedExtent.height() / file.cellIncrementY));
454
                        file.setView(file.numBands, readBandsFromECW, selectedExtent.minX(), selectedExtent.maxY(), selectedExtent.maxX(), selectedExtent.minY(), bufWidth, bufHeight);
455
                        
456
                        //Escribimos el raster sobre un Buffer
457
                        int[] pRGBArray = new int[bufWidth];
458
                        drawRGB2(rasterBuf, pRGBArray, bandList, task);
459

    
460
                }catch(JNCSInvalidSetViewException exc){
461
                        throw new RasterDriverException("Error setting coords");
462
                }catch (JNCSFileNotOpenException e) {
463
                        throw new RasterDriverException("Error opening file");
464
                }catch (JNCSException ex) {
465
                        throw new RasterDriverException("Error reading data");
466
                }
467

    
468
        }
469
        
470
        private void drawRGB2(Buffer rasterBuf, int[] pRGBArray, BandList bandList, RasterTask task) throws JNCSException, ProcessInterruptedException {
471
                int[] drawableBands = null;
472
                
473
                for (int line = 0; line < rasterBuf.getHeight(); line++) {
474
                        try {
475
                                file.readLineRGBA(pRGBArray);
476
                                for(int col = 0; col < pRGBArray.length; col ++) {
477
                                        drawableBands = bandList.getBufferBandToDraw(getFName(), 0);
478
                                        if(drawableBands != null) {
479
                                                for (int i = 0; i < drawableBands.length; i++) {
480
                                                        rasterBuf.setElem(line, col, drawableBands[i], (byte)((pRGBArray[col] & 0x00ff0000) >> 16));                                                        
481
                                                }
482
                                        }
483
                                        drawableBands = bandList.getBufferBandToDraw(getFName(), 1);
484
                                        if(drawableBands != null) {
485
                                                for (int i = 0; i < drawableBands.length; i++) {
486
                                                        rasterBuf.setElem(line, col, drawableBands[i], (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
487
                                                }
488
                                        }
489
                                        drawableBands = bandList.getBufferBandToDraw(getFName(), 2);
490
                                        if(drawableBands != null) {
491
                                                for (int i = 0; i < drawableBands.length; i++) {
492
                                                        rasterBuf.setElem(line, col, drawableBands[i], (byte)(pRGBArray[col] & 0x000000ff));
493
                                                }
494
                                        }
495
                                }
496
                        } catch (JNCSException exc) {
497
                        }
498
                        if(task.getEvent() != null)
499
                                task.manageEvent(task.getEvent());
500
                }
501
                return;
502
        }
503

    
504
        @SuppressWarnings("unused")
505
        private void drawRGB(Buffer rasterBuf, int[] pRGBArray, int[] readBands, BandList bandList, RasterTask task) throws JNCSException, ProcessInterruptedException {
506
                int bandR = readBands[0];
507
                int bandG = (readBands.length > 1) ? readBands[1] : -1;
508
                int bandB = (readBands.length > 2) ? readBands[2] : -1;
509

    
510
                //********* caso especial que resuelve Bug#1 **********************
511
                if(file.numBands == 3 && bandList.getDrawableBandsCount() < 3) {
512
                        for(int i = 0; i < 3; i ++) {
513
                                int[] b = bandList.getBand(i).getBufferBandListToDraw();
514
                                if(b != null){
515
                                        bandG = 1; bandR = 0; bandB = 2;
516
                                }
517
                        }
518
                }
519
                if(file.numBands == 3 && bandR == bandG && bandG == bandB) { //caso especial que resuelve Bug#1
520
                        for(int i = 0; i < 3; i ++) {
521
                                int[] b = bandList.getBand(i).getBufferBandListToDraw();
522
                                if(b != null) {
523
                                        if(i == 0) {
524
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
525
                                                        try {
526
                                                                file.readLineRGBA(pRGBArray);
527
                                                                for(int col = 0; col < pRGBArray.length; col ++) {
528
                                                                        rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
529
                                                                        rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
530
                                                                        rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
531
                                                                }
532
                                                        } catch (JNCSException exc) {
533
                                                        }
534
                                                }
535
                                                return;
536
                                        }
537
                                        if(i == 1) {
538
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
539
                                                        try {
540
                                                                file.readLineRGBA(pRGBArray);
541
                                                                for(int col = 0; col < pRGBArray.length; col ++) {
542
                                                                        rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
543
                                                                        rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
544
                                                                        rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
545
                                                                }
546
                                                        } catch (JNCSException exc) {
547
                                                        }
548
                                                }
549
                                                return;
550
                                        }
551
                                        if(i == 2) {
552
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
553
                                                        try {
554
                                                                file.readLineRGBA(pRGBArray);
555
                                                                for(int col = 0; col < pRGBArray.length; col ++) {
556
                                                                        rasterBuf.setElem(line, col, bandR, (byte)(pRGBArray[col] & 0x000000ff));
557
                                                                        rasterBuf.setElem(line, col, bandG, (byte)(pRGBArray[col] & 0x000000ff));
558
                                                                        rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
559
                                                                }
560
                                                        } catch (JNCSException exc) {
561
                                                        }
562
                                                }
563
                                                return;
564
                                        }
565
                                }
566
                                if(task.getEvent() != null)
567
                                        task.manageEvent(task.getEvent());
568
                        }
569

    
570
                }
571
                //********* END caso especial que resuelve Bug#1 **********************
572

    
573
                if(bandR >= 0 && bandG >= 0 && bandB >= 0) {
574
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
575
                                try {
576
                                        file.readLineRGBA(pRGBArray);
577
                                        for(int col = 0; col < pRGBArray.length; col ++) {
578
                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
579
                                                rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
580
                                                rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
581
                                        }
582
                                } catch (JNCSException exc) {
583
                                }
584
                        }
585
                        return;
586
                }
587

    
588
                if(task.getEvent() != null)
589
                        task.manageEvent(task.getEvent());
590

    
591
                if(bandR >= 0 && bandG >= 0) {
592
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
593
                                try {
594
                                        file.readLineRGBA(pRGBArray);
595
                                        for(int col = 0; col < pRGBArray.length; col ++) {
596
                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
597
                                                rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
598
                                        }
599
                                } catch (JNCSException exc) {
600
                                }
601
                        }
602
                        return;
603
                }
604

    
605
                if(task.getEvent() != null)
606
                        task.manageEvent(task.getEvent());
607

    
608
                if(bandR >= 0){
609
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
610
                                try {
611
                                        file.readLineRGBA(pRGBArray);
612
                                        for(int col = 0; col < pRGBArray.length; col ++)
613
                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
614
                                } catch (JNCSException exc) {
615
                                }
616
                        }
617
                        return;
618
                }
619

    
620
                if(task.getEvent() != null)
621
                        task.manageEvent(task.getEvent());
622

    
623
        }
624

    
625
        public void reProject(ICoordTrans rp) {
626
        }
627

    
628
        /*
629
         * (non-Javadoc)
630
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getBlockSize()
631
         */
632
        public int getBlockSize() {
633
                return 0;
634
        }
635

    
636
        /*
637
         * (non-Javadoc)
638
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#readCompleteLine(int, int)
639
         */
640
        public Object readCompleteLine(int line, int band) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
641
                if(line < 0 || line >= file.height || band < 0 || band >= getBandCount())
642
                        throw new InvalidSetViewException("Request out of grid");
643

    
644
                Point2D begin = rasterToWorld(new Point2D.Double(0, line));
645
                Point2D end = rasterToWorld(new Point2D.Double(file.width, line + 1));
646
                int[] readBandsFromECW = new int[file.numBands];
647
                if(file.numBands <= 3) {
648
                        for(int i = 0; i < file.numBands; i++)
649
                                readBandsFromECW[i] = i;
650
                } else {
651
                        readBandsFromECW[0] = band;
652
                }
653

    
654
                Extent e = new ExtentImpl(begin.getX(), begin.getY(), end.getX(), end.getY());
655

    
656
                try {
657
                        int[] value = new int[file.width];
658
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, 1);
659
                        file.readLineRGBA(value);
660

    
661
                        if(file.numBands <= 3) {
662
                                switch(getDataType()[0]) {
663
                                case Buffer.TYPE_BYTE: byte[] b = new byte[file.width];
664
                                                                                switch(band) {
665
                                                                                case 0: for(int i = 0; i < file.width; i ++)
666
                                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
667
                                                                                                break;
668
                                                                                case 1: for(int i = 0; i < file.width; i ++)
669
                                                                                                        b[i] = (byte)(((value[i] & 0x0000ff00) >> 8) & 0xff);
670
                                                                                                break;
671
                                                                                case 2: for(int i = 0; i < file.width; i ++)
672
                                                                                                        b[i] = (byte)((value[i] & 0x000000ff) & 0xff);
673
                                                                                                break;
674
                                                                                }
675
                                                                                return b;
676
                                }
677
                        }else {
678
                                switch(getDataType()[0]) {
679
                                case Buffer.TYPE_BYTE: byte[] b = new byte[file.width];
680
                                                                                for(int i = 0; i < file.width; i ++)
681
                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
682
                                                                                break;
683
                                }
684
                        }
685
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
686
                } catch (JNCSFileNotOpenException e1) {
687
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
688
                } catch (JNCSInvalidSetViewException e1) {
689
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
690
                } catch (JNCSException e1) {
691
                        throw new RasterDriverException("Error la lectura de datos ecw");
692
                }
693

    
694
                return null;
695
        }
696

    
697
        /*
698
         * (non-Javadoc)
699
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#readBlock(int, int)
700
         */
701
        public Object readBlock(int pos, int blockHeight) throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
702
                RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
703
                if(pos < 0)
704
                        throw new InvalidSetViewException("Request out of grid");
705

    
706
                if((pos + blockHeight) > file.height)
707
                        blockHeight = Math.abs(file.height - pos);
708

    
709
                Point2D begin = rasterToWorld(new Point2D.Double(0, pos));
710
                Point2D end = rasterToWorld(new Point2D.Double(file.width, pos + blockHeight));
711
                int[] readBandsFromECW = new int[file.numBands];
712

    
713
                for(int i = 0; i < file.numBands; i++)
714
                        readBandsFromECW[i] = i;
715

    
716
                byte[][][] buf = new byte[file.numBands][blockHeight][file.width];
717
                Extent e = new ExtentImpl(begin.getX(), begin.getY(), end.getX(), end.getY());
718
                e = rasterUtil.calculateAdjustedView(getExtent(), e);
719

    
720
                try {
721
                        int[] value = new int[file.width];
722
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, blockHeight);
723

    
724
                        if(file.numBands <= 3) {
725
                                for (int row = 0; row < blockHeight; row++) {
726
                                        file.readLineRGBA(value);
727
                                        switch(getDataType()[0]) {
728
                                        case Buffer.TYPE_BYTE:
729
                                                for(int col = 0; col < file.width; col ++) {
730
                                                        buf[0][row][col] = (byte)(((value[col] & 0x00ff0000) >> 16) & 0xff);
731
                                                        buf[1][row][col] = (byte)(((value[col] & 0x0000ff00) >> 8) & 0xff);
732
                                                        buf[2][row][col] = (byte)((value[col] & 0x000000ff) & 0xff);
733
                                                }
734
                                                break;
735
                                        }
736
                                }
737

    
738
                                if(task.getEvent() != null)
739
                                        task.manageEvent(task.getEvent());
740

    
741
                        } else {
742
                                //TODO: FUNCIONALIDAD: file.numBands > 3
743
                        }
744

    
745
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
746
                } catch (JNCSFileNotOpenException e1) {
747
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
748
                } catch (JNCSInvalidSetViewException e1) {
749
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
750
                } catch (JNCSException e1) {
751
                        throw new RasterDriverException("Error la lectura de datos ecw");
752
                }
753

    
754
                return buf;
755
        }
756

    
757
        /*
758
         * (non-Javadoc)
759
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getData(int, int, int)
760
         */
761
        public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
762
                if(x < 0 || y < 0 || x >= file.width || y >= file.height)
763
                        throw new InvalidSetViewException("Request out of grid");
764

    
765
                Point2D begin = new Point2D.Double(x, y);
766
                Point2D end = new Point2D.Double(x + 1, y + 1);
767

    
768
                ownTransformation.transform(begin, begin);
769
                ownTransformation.transform(end, end);
770

    
771
                int[] readBandsFromECW = new int[file.numBands];
772
                if(file.numBands <= 3){
773
                        for(int i = 0; i < file.numBands; i++)
774
                                readBandsFromECW[i] = i;
775
                }else{
776
                        readBandsFromECW[0] = band;
777
                }
778

    
779
                Extent e = new ExtentImpl(begin.getX(), begin.getY(), end.getX(), end.getY());
780
                try {
781
                        int[] value = new int[1];
782
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), 1, 1);
783
                        file.readLineRGBA(value);
784
                        if(file.numBands <= 3){
785
                                switch(band){
786
                                case 0: return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
787
                                case 1: return new Integer((((value[0] & 0x0000ff00) >> 8) & 0xffffffff));
788
                                case 2: return new Integer((((value[0] & 0x000000ff)) & 0xffffffff));
789
                                }
790
                        }
791
                        return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
792
                } catch (JNCSFileNotOpenException e1) {
793
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
794
                } catch (JNCSInvalidSetViewException e1) {
795
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
796
                } catch (JNCSException e1) {
797
                        throw new RasterDriverException("Error reading ecw data");
798
                }
799
        }
800

    
801
        /*
802
         * (non-Javadoc)
803
         * @see com.ermapper.ecw.JNCSProgressiveUpdate#refreshUpdate(int, int, double, double, double, double)
804
         */
805
        public void refreshUpdate(int arg0, int arg1, double arg2, double arg3, double arg4, double arg5) {
806
        }
807

    
808
        /*
809
         * (non-Javadoc)
810
         * @see com.ermapper.ecw.JNCSProgressiveUpdate#refreshUpdate(int, int, int, int, int, int)
811
         */
812
        public void refreshUpdate(int arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
813
        }
814

    
815
        /*
816
         * (non-Javadoc)
817
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getColorInterpretation()
818
         */
819
        public DataStoreColorInterpretation getColorInterpretation(){
820
                if(colorInterpr == null) {
821
                        colorInterpr = new DataStoreColorInterpretation();
822
                        colorInterpr.initColorInterpretation(getBandCount());
823
                        if(getBandCount() == 1)
824
                                colorInterpr.setColorInterpValue(0, DataStoreColorInterpretation.GRAY_BAND);
825
                        if(getBandCount() >= 3) {
826
                                colorInterpr.setColorInterpValue(0, DataStoreColorInterpretation.RED_BAND);
827
                                colorInterpr.setColorInterpValue(1, DataStoreColorInterpretation.GREEN_BAND);
828
                                colorInterpr.setColorInterpValue(2, DataStoreColorInterpretation.BLUE_BAND);
829
                        }
830
                }
831
                return colorInterpr;
832
        }
833

    
834
        /*
835
         * (non-Javadoc)
836
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setColorInterpretation(org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation)
837
         */
838
        public void setColorInterpretation(DataStoreColorInterpretation colorInterpretation){
839
                this.colorInterpretation = colorInterpretation;
840
        }
841

    
842
        /*
843
         * (non-Javadoc)
844
         * @see org.gvsig.raster.impl.provider.RasterProvider#getOverviewCount(int)
845
         */
846
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
847
                if(band >= getBandCount())
848
                        throw new BandAccessException("Wrong band");
849
                return 0;
850
        }
851

    
852
        /*
853
         * (non-Javadoc)
854
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewWidth(int, int)
855
         */
856
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
857
                if (band >= getBandCount())
858
                        throw new BandAccessException("Wrong band");
859
                return 0;
860
        }
861

    
862
        /*
863
         * (non-Javadoc)
864
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewHeight(int, int)
865
         */
866
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
867
                if (band >= getBandCount())
868
                        throw new BandAccessException("Wrong band");
869
                return 0;
870
        }
871

    
872
        /*
873
         * (non-Javadoc)
874
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#overviewsSupport()
875
         */
876
        public boolean overviewsSupport() {
877
                return false;
878
        }
879

    
880
        
881
        public String getStringProjection() throws RasterDriverException{
882
                return file.projection;
883
        }
884

    
885
        /*
886
         * (non-Javadoc)
887
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWktProjection()
888
         */
889
        public String getWktProjection() {
890
                return null;
891
        }
892
        
893
        /*
894
         * (non-Javadoc)
895
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getName()
896
         */
897
        public String getName() {
898
                return NAME;
899
        }
900
        
901
        /*
902
         * (non-Javadoc)
903
         * @see org.gvsig.raster.impl.provider.RasterProvider#setStatus(org.gvsig.raster.impl.provider.RasterProvider)
904
         */
905
        public void setStatus(RasterProvider provider) {
906
                if(provider instanceof ErmapperProvider) {
907
                        //Not implemented yet
908
                }
909
        }
910
        
911
        /*
912
         * (non-Javadoc)
913
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isSupersamplingSupported()
914
         */
915
        public boolean isSupersamplingSupported() {
916
                return false;
917
        }
918
        
919
        /*
920
         * (non-Javadoc)
921
         * @see org.gvsig.raster.impl.provider.RasterProvider#getTileServer()
922
         */
923
        public TileServer getTileServer() {
924
                if(tileServer == null)
925
                        tileServer = new FileTileServer(this);
926
                return tileServer;
927
        }
928
        
929
}