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 @ 463

History | View | Annotate | Download (34.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.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.RasterLocator;
33
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
34
import org.gvsig.fmap.dal.coverage.dataset.io.tile.downloader.FileTileServer;
35
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
36
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
37
import org.gvsig.fmap.dal.coverage.exception.BandAccessException;
38
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
39
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
40
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
41
import org.gvsig.fmap.dal.coverage.exception.ParsingException;
42
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
43
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
44
import org.gvsig.fmap.dal.coverage.store.RasterDataParameters;
45
import org.gvsig.fmap.dal.coverage.store.RasterFileStoreParameters;
46
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
47
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
48
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
49
import org.gvsig.metadata.MetadataLocator;
50
import org.gvsig.raster.cache.tile.provider.TileListener;
51
import org.gvsig.raster.cache.tile.provider.TileServer;
52
import org.gvsig.raster.impl.datastruct.ExtentImpl;
53
import org.gvsig.raster.impl.process.RasterTask;
54
import org.gvsig.raster.impl.process.RasterTaskQueue;
55
import org.gvsig.raster.impl.provider.DefaultRasterProvider;
56
import org.gvsig.raster.impl.provider.RasterProvider;
57
import org.gvsig.raster.impl.store.AbstractRasterDataParameters;
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
                RasterLocator.getManager().registerFileProvidersTiled(NAME, ErmapperProvider.class);
95
                
96
                DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
97
                if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
98
                        dataman.registerStoreProvider(NAME,
99
                                        ErmapperProvider.class, 
100
                                        ErmapperDataParameters.class);
101
                }
102
                
103
                if(DALFileLocator.getFilesystemServerExplorerManager() != null)
104
                        DALFileLocator.getFilesystemServerExplorerManager().registerProvider(
105
                                        NAME, 
106
                                        DESCRIPTION,
107
                                        ErmapperFilesystemServerExplorer.class);
108
                
109
            dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
110
        }
111
        
112
        /**
113
         * Adds the list of formats to the extension point
114
         * @param point
115
         */
116
        public static void addFormatsToRegistry(ExtensionPoint point, Class<?> c) {
117
                for (int i = 0; i < formatList.length; i++) {
118
                        point.append(formatList[i], "", c);
119
                }
120
        }
121
        
122
        /*
123
         * (non-Javadoc)
124
         * @see org.gvsig.raster.impl.provider.RasterProvider#registerTileProviderFormats(java.lang.Class)
125
         */
126
        public void registerTileProviderFormats(Class<RasterProvider> c) {
127
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
128
                ExtensionPoint point = extensionPoints.get("RasterReader");
129
                for (int i = 0; i < formatList.length; i++) {
130
                        point.append(formatList[i], "", c);
131
                }
132
        }
133
        
134
        /**
135
         * Returns true if the extension is supported and false if doesn't
136
         * @param ext
137
         * @return
138
         */
139
        public static boolean isExtensionSupported(String ext) {
140
                for (int i = 0; i < formatList.length; i++) {
141
                        if(formatList[i].compareTo(ext) == 0)
142
                                return true;
143
                }
144
                return false;
145
        }
146
        
147
        /**
148
         * Constructor. Abre el dataset.
149
         * @param proj Proyecci?n
150
         * @param fName Nombre del fichero ecw
151
         * @throws NotSupportedExtensionException
152
         */
153
        public ErmapperProvider(String params) throws NotSupportedExtensionException {
154
                super(params);
155
                if(params instanceof String) {
156
                        ErmapperDataParameters p = new ErmapperDataParameters();
157
                        p.setURI((String)params);
158
                        super.init(p, null, ToolsLocator.getDynObjectManager()
159
                                        .createDynObject(
160
                                                        MetadataLocator.getMetadataManager().getDefinition(
161
                                                                        DataStore.METADATA_DEFINITION_NAME)));
162
                        init(p, null);
163
                }
164
        }
165
        
166
        public ErmapperProvider(ErmapperDataParameters params,
167
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
168
                super(params, storeServices, ToolsLocator.getDynObjectManager()
169
                                .createDynObject(
170
                                                MetadataLocator.getMetadataManager().getDefinition(
171
                                                                DataStore.METADATA_DEFINITION_NAME)));
172
                init(params, storeServices);
173
        }
174

    
175
        /**
176
         * Abre el dataset.
177
         * @param proj Proyecci?n
178
         * @param fName Nombre del fichero ecw
179
         * @throws NotSupportedExtensionException
180
         */
181
        public void init (AbstractRasterDataParameters params,
182
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
183
                setParam(storeServices, params);
184
                if (!((RasterFileStoreParameters)param).getFile().exists() && !((RasterDataParameters)param).getURI().startsWith("ecwp:"))
185
                        throw new NotSupportedExtensionException("Extension not supported");
186

    
187
                try {
188
                        file = new JNCSFile(((RasterDataParameters)param).getURI(), false);
189
                } catch (JNCSException e1) {
190
                        throw new NotSupportedExtensionException("Error loading the ecw file", e1);
191
                }
192
                load();
193
                bandCount = file.numBands;
194
                getTransparency();
195

    
196
                int[] dt = new int[bandCount];
197
                for (int i = 0; i < bandCount; i++)
198
                        dt[i] = Buffer.TYPE_BYTE;
199
                setDataType(dt);
200

    
201
                super.init();
202

    
203
                try {
204
                        loadFromRmf(getRmfBlocksManager());
205
                } catch (ParsingException e) {
206
                        //No lee desde rmf
207
                }
208
                open = true;
209
        }
210

    
211
        /*
212
         * (non-Javadoc)
213
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#load()
214
         */
215
        public RasterProvider load() {
216
                ownTransformation = new AffineTransform(file.cellIncrementX, 0, 0, file.cellIncrementY, file.originX, file.originY);
217
                externalTransformation = (AffineTransform) ownTransformation.clone();
218
                return this;
219
        }
220
        
221
        /*
222
         * (non-Javadoc)
223
         * @see org.gvsig.raster.impl.provider.RasterProvider#isOpen()
224
         */
225
        public boolean isOpen() {
226
                return open;
227
        }
228

    
229
        /*
230
         * (non-Javadoc)
231
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#close()
232
         */
233
        public void close() {
234
                if(file != null) {
235
                        open = false;
236
                        file.close(false);
237
                        file = null;
238
                }
239
        }
240

    
241
        /*
242
         * (non-Javadoc)
243
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getTransparency()
244
         */
245
        public Transparency getTransparency() {
246
                if(fileTransparency == null)
247
                        fileTransparency = new DataStoreTransparency();
248
                return fileTransparency;
249
        }
250

    
251
        /*
252
         * (non-Javadoc)
253
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWidth()
254
         */
255
        public double getWidth() {
256
                return file.width;
257
        }
258

    
259
        /*
260
         * (non-Javadoc)
261
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
262
         */
263
        public double getHeight() {
264
                return file.height;
265
        }
266

    
267
        /*
268
         * (non-Javadoc)
269
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getView()
270
         */
271
        public Extent getView() {
272
                return viewRequest;
273
        }
274

    
275
        /*
276
         * (non-Javadoc)
277
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setView(org.gvsig.fmap.dal.coverage.datastruct.Extent)
278
         */
279
        public void setView(Extent e) {
280
                viewRequest = new ExtentImpl(e);
281
        }
282
        
283
        /*
284
         * (non-Javadoc)
285
         * @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)
286
         */
287
        public void getWindow(Extent ex, int bufWidth, int bufHeight, 
288
                        BandList bandList, TileListener listener)throws ProcessInterruptedException, RasterDriverException {
289
                 
290
        }
291

    
292
        /*
293
         * (non-Javadoc)
294
         * @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)
295
         */
296
        public Buffer getWindow(Extent ex, BandList bandList, Buffer rasterBuf) 
297
                throws ProcessInterruptedException, RasterDriverException {
298
                Point2D p1 = new Point2D.Double(ex.getULX(), ex.getULY());
299
                Point2D p2 = new Point2D.Double(ex.getLRX(), ex.getLRY());
300
                try {
301
                        externalTransformation.inverseTransform(p1, p1);
302
                        externalTransformation.inverseTransform(p2, p2);
303
                        ownTransformation.transform(p1, p1);
304
                        ownTransformation.transform(p2, p2);
305
                } catch (NoninvertibleTransformException e) {
306
                        throw new RasterDriverException("Noninvertible transform");
307
                }
308

    
309
                Extent selectedExtent = new ExtentImpl(p1.getX(), p1.getY(), p2.getX(), p2.getY());
310

    
311
                setView(selectedExtent);
312
                int wPx = rasterBuf.getWidth();
313
                int hPx = rasterBuf.getHeight();
314
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
315

    
316
                loadBuffer(viewRequest, wPx, hPx, rasterBuf, bandList, stpBuffer);
317

    
318
                return rasterBuf;
319
        }
320

    
321
        /*
322
         * (non-Javadoc)
323
         * @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)
324
         */
325
        public Buffer getWindow(double ulx, double uly, double w, double h, 
326
                        BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
327
                //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
328
                //tenemos que averiguar si lrx es x + w o x -w, asi como si lry es y + h o y - h
329
                Extent ext = getExtent();
330
                Point2D pInit = rasterToWorld(new Point2D.Double(0, 0));
331
                Point2D pEnd = rasterToWorld(new Point2D.Double(getWidth(), getHeight()));
332
                double wRaster = Math.abs(pEnd.getX() - pInit.getX());
333
                double hRaster = Math.abs(pEnd.getY() - pInit.getY());
334
                double lrx = (((ext.getULX() - wRaster) > ext.maxX()) || ((ext.getULX() - wRaster) < ext.minX())) ? (ulx + w) : (ulx - w);
335
                double lry = (((ext.getULY() - hRaster) > ext.maxY()) || ((ext.getULY() - hRaster) < ext.minY())) ? (uly + h) : (uly - h);
336

    
337
                Point2D p1 = new Point2D.Double(ulx, uly);
338
                Point2D p2 = new Point2D.Double(lrx, lry);
339
                try {
340
                        externalTransformation.inverseTransform(p1, p1);
341
                        externalTransformation.inverseTransform(p2, p2);
342
                        p1.setLocation((int)p1.getX(), (int)p1.getY());
343
                        p2.setLocation((int)Math.ceil(p2.getX()), (int)Math.ceil(p2.getY()));
344
                        p1.setLocation(Math.max(p1.getX(), 0), Math.max(p1.getY(), 0));
345
                        p2.setLocation(Math.max(p2.getX(), 0), Math.max(p2.getY(), 0));
346
                        p1.setLocation(Math.min(p1.getX(), getWidth()), Math.min(p1.getY(), getHeight()));
347
                        p2.setLocation(Math.min(p2.getX(), getWidth()), Math.min(p2.getY(), getHeight()));
348
                        ownTransformation.transform(p1, p1);
349
                        ownTransformation.transform(p2, p2);
350
                } catch (NoninvertibleTransformException e) {
351
                        throw new RasterDriverException("Noninvertible transform");
352
                }
353

    
354
                Extent selectedExtent = new ExtentImpl(p1.getX(), p1.getY(), p2.getX(), p2.getY());
355

    
356
                setView(selectedExtent);
357
                int wPx = rasterBuf.getWidth();
358
                int hPx = rasterBuf.getHeight();
359
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
360

    
361
                loadBuffer(viewRequest, wPx, hPx, rasterBuf, bandList, stpBuffer);
362

    
363
                return rasterBuf;
364
        }
365

    
366
        /*
367
         * (non-Javadoc)
368
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
369
         */
370
        public Buffer getWindow(int x, int y, 
371
                        BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
372
                int w = rasterBuf.getWidth();
373
                int h = rasterBuf.getHeight();
374
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
375
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
376
                Extent selectedExtent = new ExtentImpl(init.getX(), init.getY(), end.getX(), end.getY());
377
                setView(selectedExtent);
378
                int[] stpBuffer = new int[]{0, 0 , w, h};
379

    
380
                loadBuffer(viewRequest, w, h, rasterBuf, bandList, stpBuffer);
381
                return rasterBuf;
382
        }
383

    
384
        /*
385
         * (non-Javadoc)
386
         * @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)
387
         */
388
        public Buffer getWindow(Extent extent, 
389
                        int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
390
                Point2D p1 = new Point2D.Double(extent.getULX(), extent.getULY());
391
                Point2D p2 = new Point2D.Double(extent.getLRX(), extent.getLRY());
392
                try {
393
                        externalTransformation.inverseTransform(p1, p1);
394
                        externalTransformation.inverseTransform(p2, p2);
395
                        p1.setLocation((int)p1.getX(), (int)p1.getY());
396
                        p2.setLocation((int)Math.ceil(p2.getX()), (int)Math.ceil(p2.getY()));
397
                        p1.setLocation(Math.max(p1.getX(), 0), Math.max(p1.getY(), 0));
398
                        p2.setLocation(Math.max(p2.getX(), 0), Math.max(p2.getY(), 0));
399
                        p1.setLocation(Math.min(p1.getX(), getWidth()), Math.min(p1.getY(), getHeight()));
400
                        p2.setLocation(Math.min(p2.getX(), getWidth()), Math.min(p2.getY(), getHeight()));
401
                        ownTransformation.transform(p1, p1);
402
                        ownTransformation.transform(p2, p2);
403
                } catch (NoninvertibleTransformException e) {
404
                        throw new RasterDriverException("Noninvertible transform");
405
                }
406
                Extent selectedExtent = new ExtentImpl(p1, p2);
407
                setView(selectedExtent);
408
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
409

    
410
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
411

    
412
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
413
                return rasterBuf;
414
        }
415

    
416
        /*
417
         * (non-Javadoc)
418
         * @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)
419
         */
420
        public Buffer getWindow(int x, int y, int w, int h, 
421
                        int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
422
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
423
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
424
                Extent selectedExtent = new ExtentImpl(init.getX(), init.getY(), end.getX(), end.getY());
425
                setView(selectedExtent);
426
                int[] stpBuffer = new int[]{0, 0 , bufWidth, bufHeight};
427

    
428
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
429
                return rasterBuf;
430
        }
431

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

    
461
                        if(task.getEvent() != null)
462
                                task.manageEvent(task.getEvent());
463

    
464
                        if(bufWidth > Math.round(Math.abs(selectedExtent.width() / file.cellIncrementX)))
465
                                bufWidth = (int)Math.round(Math.abs(selectedExtent.width() / file.cellIncrementX));
466
                        if(bufHeight > Math.round(Math.abs(selectedExtent.height() / file.cellIncrementY)))
467
                                bufHeight = (int)Math.round(Math.abs(selectedExtent.height() / file.cellIncrementY));
468
                        file.setView(file.numBands, readBandsFromECW, selectedExtent.minX(), selectedExtent.maxY(), selectedExtent.maxX(), selectedExtent.minY(), bufWidth, bufHeight);
469
                        
470
                        //Escribimos el raster sobre un Buffer
471
                        int[] pRGBArray = new int[bufWidth];
472
                        drawRGB2(rasterBuf, pRGBArray, bandList, task);
473

    
474
                }catch(JNCSInvalidSetViewException exc){
475
                        throw new RasterDriverException("Error setting coords");
476
                }catch (JNCSFileNotOpenException e) {
477
                        throw new RasterDriverException("Error opening file");
478
                }catch (JNCSException ex) {
479
                        throw new RasterDriverException("Error reading data");
480
                }
481

    
482
        }
483
        
484
        private void drawRGB2(Buffer rasterBuf, int[] pRGBArray, BandList bandList, RasterTask task) throws JNCSException, ProcessInterruptedException {
485
                int[] drawableBands = null;
486
                
487
                for (int line = 0; line < rasterBuf.getHeight(); line++) {
488
                        try {
489
                                file.readLineRGBA(pRGBArray);
490
                                for(int col = 0; col < pRGBArray.length; col ++) {
491
                                        drawableBands = bandList.getBufferBandToDraw(getFName(), 0);
492
                                        if(drawableBands != null) {
493
                                                for (int i = 0; i < drawableBands.length; i++) {
494
                                                        rasterBuf.setElem(line, col, drawableBands[i], (byte)((pRGBArray[col] & 0x00ff0000) >> 16));                                                        
495
                                                }
496
                                        }
497
                                        drawableBands = bandList.getBufferBandToDraw(getFName(), 1);
498
                                        if(drawableBands != null) {
499
                                                for (int i = 0; i < drawableBands.length; i++) {
500
                                                        rasterBuf.setElem(line, col, drawableBands[i], (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
501
                                                }
502
                                        }
503
                                        drawableBands = bandList.getBufferBandToDraw(getFName(), 2);
504
                                        if(drawableBands != null) {
505
                                                for (int i = 0; i < drawableBands.length; i++) {
506
                                                        rasterBuf.setElem(line, col, drawableBands[i], (byte)(pRGBArray[col] & 0x000000ff));
507
                                                }
508
                                        }
509
                                }
510
                        } catch (JNCSException exc) {
511
                        }
512
                        if(task.getEvent() != null)
513
                                task.manageEvent(task.getEvent());
514
                }
515
                return;
516
        }
517

    
518
        @SuppressWarnings("unused")
519
        private void drawRGB(Buffer rasterBuf, int[] pRGBArray, int[] readBands, BandList bandList, RasterTask task) throws JNCSException, ProcessInterruptedException {
520
                int bandR = readBands[0];
521
                int bandG = (readBands.length > 1) ? readBands[1] : -1;
522
                int bandB = (readBands.length > 2) ? readBands[2] : -1;
523

    
524
                //********* caso especial que resuelve Bug#1 **********************
525
                if(file.numBands == 3 && bandList.getDrawableBandsCount() < 3) {
526
                        for(int i = 0; i < 3; i ++) {
527
                                int[] b = bandList.getBand(i).getBufferBandListToDraw();
528
                                if(b != null){
529
                                        bandG = 1; bandR = 0; bandB = 2;
530
                                }
531
                        }
532
                }
533
                if(file.numBands == 3 && bandR == bandG && bandG == bandB) { //caso especial que resuelve Bug#1
534
                        for(int i = 0; i < 3; i ++) {
535
                                int[] b = bandList.getBand(i).getBufferBandListToDraw();
536
                                if(b != null) {
537
                                        if(i == 0) {
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] & 0x00ff0000) >> 16));
543
                                                                        rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
544
                                                                        rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
545
                                                                }
546
                                                        } catch (JNCSException exc) {
547
                                                        }
548
                                                }
549
                                                return;
550
                                        }
551
                                        if(i == 1) {
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] & 0x0000ff00) >> 8));
557
                                                                        rasterBuf.setElem(line, col, bandG, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
558
                                                                        rasterBuf.setElem(line, col, bandB, (byte)((pRGBArray[col] & 0x0000ff00) >> 8));
559
                                                                }
560
                                                        } catch (JNCSException exc) {
561
                                                        }
562
                                                }
563
                                                return;
564
                                        }
565
                                        if(i == 2) {
566
                                                for (int line = 0; line < rasterBuf.getHeight(); line++) {
567
                                                        try {
568
                                                                file.readLineRGBA(pRGBArray);
569
                                                                for(int col = 0; col < pRGBArray.length; col ++) {
570
                                                                        rasterBuf.setElem(line, col, bandR, (byte)(pRGBArray[col] & 0x000000ff));
571
                                                                        rasterBuf.setElem(line, col, bandG, (byte)(pRGBArray[col] & 0x000000ff));
572
                                                                        rasterBuf.setElem(line, col, bandB, (byte)(pRGBArray[col] & 0x000000ff));
573
                                                                }
574
                                                        } catch (JNCSException exc) {
575
                                                        }
576
                                                }
577
                                                return;
578
                                        }
579
                                }
580
                                if(task.getEvent() != null)
581
                                        task.manageEvent(task.getEvent());
582
                        }
583

    
584
                }
585
                //********* END caso especial que resuelve Bug#1 **********************
586

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

    
602
                if(task.getEvent() != null)
603
                        task.manageEvent(task.getEvent());
604

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

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

    
622
                if(bandR >= 0){
623
                        for (int line = 0; line < rasterBuf.getHeight(); line++) {
624
                                try {
625
                                        file.readLineRGBA(pRGBArray);
626
                                        for(int col = 0; col < pRGBArray.length; col ++)
627
                                                rasterBuf.setElem(line, col, bandR, (byte)((pRGBArray[col] & 0x00ff0000) >> 16));
628
                                } catch (JNCSException exc) {
629
                                }
630
                        }
631
                        return;
632
                }
633

    
634
                if(task.getEvent() != null)
635
                        task.manageEvent(task.getEvent());
636

    
637
        }
638

    
639
        public void reProject(ICoordTrans rp) {
640
        }
641

    
642
        /*
643
         * (non-Javadoc)
644
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getBlockSize()
645
         */
646
        public int getBlockSize() {
647
                return 0;
648
        }
649

    
650
        /*
651
         * (non-Javadoc)
652
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#readCompleteLine(int, int)
653
         */
654
        public Object readCompleteLine(int line, int band) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
655
                if(line < 0 || line >= file.height || band < 0 || band >= getBandCount())
656
                        throw new InvalidSetViewException("Request out of grid");
657

    
658
                Point2D begin = rasterToWorld(new Point2D.Double(0, line));
659
                Point2D end = rasterToWorld(new Point2D.Double(file.width, line + 1));
660
                int[] readBandsFromECW = new int[file.numBands];
661
                if(file.numBands <= 3) {
662
                        for(int i = 0; i < file.numBands; i++)
663
                                readBandsFromECW[i] = i;
664
                } else {
665
                        readBandsFromECW[0] = band;
666
                }
667

    
668
                Extent e = new ExtentImpl(begin.getX(), begin.getY(), end.getX(), end.getY());
669

    
670
                try {
671
                        int[] value = new int[file.width];
672
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, 1);
673
                        file.readLineRGBA(value);
674

    
675
                        if(file.numBands <= 3) {
676
                                switch(getDataType()[0]) {
677
                                case Buffer.TYPE_BYTE: byte[] b = new byte[file.width];
678
                                                                                switch(band) {
679
                                                                                case 0: for(int i = 0; i < file.width; i ++)
680
                                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
681
                                                                                                break;
682
                                                                                case 1: for(int i = 0; i < file.width; i ++)
683
                                                                                                        b[i] = (byte)(((value[i] & 0x0000ff00) >> 8) & 0xff);
684
                                                                                                break;
685
                                                                                case 2: for(int i = 0; i < file.width; i ++)
686
                                                                                                        b[i] = (byte)((value[i] & 0x000000ff) & 0xff);
687
                                                                                                break;
688
                                                                                }
689
                                                                                return b;
690
                                }
691
                        }else {
692
                                switch(getDataType()[0]) {
693
                                case Buffer.TYPE_BYTE: byte[] b = new byte[file.width];
694
                                                                                for(int i = 0; i < file.width; i ++)
695
                                                                                        b[i] = (byte)(((value[i] & 0x00ff0000) >> 16) & 0xff);
696
                                                                                break;
697
                                }
698
                        }
699
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
700
                } catch (JNCSFileNotOpenException e1) {
701
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
702
                } catch (JNCSInvalidSetViewException e1) {
703
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
704
                } catch (JNCSException e1) {
705
                        throw new RasterDriverException("Error la lectura de datos ecw");
706
                }
707

    
708
                return null;
709
        }
710

    
711
        /*
712
         * (non-Javadoc)
713
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#readBlock(int, int)
714
         */
715
        public Object readBlock(int pos, int blockHeight) throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
716
                RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
717
                if(pos < 0)
718
                        throw new InvalidSetViewException("Request out of grid");
719

    
720
                if((pos + blockHeight) > file.height)
721
                        blockHeight = Math.abs(file.height - pos);
722

    
723
                Point2D begin = rasterToWorld(new Point2D.Double(0, pos));
724
                Point2D end = rasterToWorld(new Point2D.Double(file.width, pos + blockHeight));
725
                int[] readBandsFromECW = new int[file.numBands];
726

    
727
                for(int i = 0; i < file.numBands; i++)
728
                        readBandsFromECW[i] = i;
729

    
730
                byte[][][] buf = new byte[file.numBands][blockHeight][file.width];
731
                Extent e = new ExtentImpl(begin.getX(), begin.getY(), end.getX(), end.getY());
732
                e = rasterUtil.calculateAdjustedView(getExtent(), e);
733

    
734
                try {
735
                        int[] value = new int[file.width];
736
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, blockHeight);
737

    
738
                        if(file.numBands <= 3) {
739
                                for (int row = 0; row < blockHeight; row++) {
740
                                        file.readLineRGBA(value);
741
                                        switch(getDataType()[0]) {
742
                                        case Buffer.TYPE_BYTE:
743
                                                for(int col = 0; col < file.width; col ++) {
744
                                                        buf[0][row][col] = (byte)(((value[col] & 0x00ff0000) >> 16) & 0xff);
745
                                                        buf[1][row][col] = (byte)(((value[col] & 0x0000ff00) >> 8) & 0xff);
746
                                                        buf[2][row][col] = (byte)((value[col] & 0x000000ff) & 0xff);
747
                                                }
748
                                                break;
749
                                        }
750
                                }
751

    
752
                                if(task.getEvent() != null)
753
                                        task.manageEvent(task.getEvent());
754

    
755
                        } else {
756
                                //TODO: FUNCIONALIDAD: file.numBands > 3
757
                        }
758

    
759
                        //TODO: FUNCIONALIDAD: Ecw con otro tipo de dato != Byte
760
                } catch (JNCSFileNotOpenException e1) {
761
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
762
                } catch (JNCSInvalidSetViewException e1) {
763
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
764
                } catch (JNCSException e1) {
765
                        throw new RasterDriverException("Error la lectura de datos ecw");
766
                }
767

    
768
                return buf;
769
        }
770

    
771
        /*
772
         * (non-Javadoc)
773
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getData(int, int, int)
774
         */
775
        public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
776
                if(x < 0 || y < 0 || x >= file.width || y >= file.height)
777
                        throw new InvalidSetViewException("Request out of grid");
778

    
779
                Point2D begin = new Point2D.Double(x, y);
780
                Point2D end = new Point2D.Double(x + 1, y + 1);
781

    
782
                ownTransformation.transform(begin, begin);
783
                ownTransformation.transform(end, end);
784

    
785
                int[] readBandsFromECW = new int[file.numBands];
786
                if(file.numBands <= 3){
787
                        for(int i = 0; i < file.numBands; i++)
788
                                readBandsFromECW[i] = i;
789
                }else{
790
                        readBandsFromECW[0] = band;
791
                }
792

    
793
                Extent e = new ExtentImpl(begin.getX(), begin.getY(), end.getX(), end.getY());
794
                try {
795
                        int[] value = new int[1];
796
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), 1, 1);
797
                        file.readLineRGBA(value);
798
                        if(file.numBands <= 3){
799
                                switch(band){
800
                                case 0: return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
801
                                case 1: return new Integer((((value[0] & 0x0000ff00) >> 8) & 0xffffffff));
802
                                case 2: return new Integer((((value[0] & 0x000000ff)) & 0xffffffff));
803
                                }
804
                        }
805
                        return new Integer((((value[0] & 0x00ff0000) >> 16) & 0xffffffff));
806
                } catch (JNCSFileNotOpenException e1) {
807
                        throw new FileNotOpenException("Error en jecw: JNCSFileNotOpenException");
808
                } catch (JNCSInvalidSetViewException e1) {
809
                        throw new FileNotOpenException("Error en jecw: JNCSInvalidSetViewException");
810
                } catch (JNCSException e1) {
811
                        throw new RasterDriverException("Error reading ecw data");
812
                }
813
        }
814

    
815
        /*
816
         * (non-Javadoc)
817
         * @see com.ermapper.ecw.JNCSProgressiveUpdate#refreshUpdate(int, int, double, double, double, double)
818
         */
819
        public void refreshUpdate(int arg0, int arg1, double arg2, double arg3, double arg4, double arg5) {
820
        }
821

    
822
        /*
823
         * (non-Javadoc)
824
         * @see com.ermapper.ecw.JNCSProgressiveUpdate#refreshUpdate(int, int, int, int, int, int)
825
         */
826
        public void refreshUpdate(int arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
827
        }
828

    
829
        /*
830
         * (non-Javadoc)
831
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getColorInterpretation()
832
         */
833
        public DataStoreColorInterpretation getColorInterpretation(){
834
                if(colorInterpr == null) {
835
                        colorInterpr = new DataStoreColorInterpretation();
836
                        colorInterpr.initColorInterpretation(getBandCount());
837
                        if(getBandCount() == 1)
838
                                colorInterpr.setColorInterpValue(0, DataStoreColorInterpretation.GRAY_BAND);
839
                        if(getBandCount() >= 3) {
840
                                colorInterpr.setColorInterpValue(0, DataStoreColorInterpretation.RED_BAND);
841
                                colorInterpr.setColorInterpValue(1, DataStoreColorInterpretation.GREEN_BAND);
842
                                colorInterpr.setColorInterpValue(2, DataStoreColorInterpretation.BLUE_BAND);
843
                        }
844
                }
845
                return colorInterpr;
846
        }
847

    
848
        /*
849
         * (non-Javadoc)
850
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setColorInterpretation(org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation)
851
         */
852
        public void setColorInterpretation(DataStoreColorInterpretation colorInterpretation){
853
                this.colorInterpretation = colorInterpretation;
854
        }
855

    
856
        /*
857
         * (non-Javadoc)
858
         * @see org.gvsig.raster.impl.provider.RasterProvider#getOverviewCount(int)
859
         */
860
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
861
                if(band >= getBandCount())
862
                        throw new BandAccessException("Wrong band");
863
                return 0;
864
        }
865

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

    
876
        /*
877
         * (non-Javadoc)
878
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewHeight(int, int)
879
         */
880
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
881
                if (band >= getBandCount())
882
                        throw new BandAccessException("Wrong band");
883
                return 0;
884
        }
885

    
886
        /*
887
         * (non-Javadoc)
888
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#overviewsSupport()
889
         */
890
        public boolean overviewsSupport() {
891
                return false;
892
        }
893

    
894
        
895
        public String getStringProjection() throws RasterDriverException{
896
                return file.projection;
897
        }
898

    
899
        /*
900
         * (non-Javadoc)
901
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWktProjection()
902
         */
903
        public String getWktProjection() {
904
                return null;
905
        }
906
        
907
        /*
908
         * (non-Javadoc)
909
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getName()
910
         */
911
        public String getName() {
912
                return NAME;
913
        }
914
        
915
        /*
916
         * (non-Javadoc)
917
         * @see org.gvsig.raster.impl.provider.RasterProvider#setStatus(org.gvsig.raster.impl.provider.RasterProvider)
918
         */
919
        public void setStatus(RasterProvider provider) {
920
                if(provider instanceof ErmapperProvider) {
921
                        //Not implemented yet
922
                }
923
        }
924
        
925
        /*
926
         * (non-Javadoc)
927
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isSupersamplingSupported()
928
         */
929
        public boolean isSupersamplingSupported() {
930
                return false;
931
        }
932
        
933
        /*
934
         * (non-Javadoc)
935
         * @see org.gvsig.raster.impl.provider.RasterProvider#getTileServer()
936
         */
937
        public TileServer getTileServer() {
938
                if(tileServer == null)
939
                        tileServer = new FileTileServer(this);
940
                return tileServer;
941
        }
942
        
943
}