Statistics
| Revision:

gvsig-raster / org.gvsig.raster.ermapper / trunk / org.gvsig.raster.ermapper / org.gvsig.raster.ermapper.io / src / main / java / org / gvsig / raster / ermapper / io / ErmapperProvider.java @ 1054

History | View | Annotate | Download (33.3 KB)

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

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

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

    
182
                try {
183
                        file = new JNCSFile(((RasterDataParameters)param).getURI(), false);
184
                } catch (JNCSException e1) {
185
                        throw new NotSupportedExtensionException("Error loading the ecw file", e1);
186
                }
187
                load();
188
                bandCount = file.numBands;
189

    
190
                int[] dt = new int[bandCount];
191
                for (int i = 0; i < bandCount; i++)
192
                        dt[i] = Buffer.TYPE_BYTE;
193
                setDataType(dt);
194

    
195
                super.init();
196

    
197
                try {
198
                        loadFromRmf(getRmfBlocksManager());
199
                } catch (ParsingException e) {
200
                        //No lee desde rmf
201
                }
202
                open = true;
203
        }
204

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

    
223
        /*
224
         * (non-Javadoc)
225
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#close()
226
         */
227
        public void close() {
228
                if(file != null) {
229
                        open = false;
230
                        //Cuando se abren varios datastores del mismo fichero y se cierra uno de ellos se cierra la aplicaci?n en el siguiente dibujado
231
                        //Se deber?a buscar una soluci?n ya que en los PrepareLayer se abre y cierra para hacer comprobaciones.
232
                        //file.close(false);
233
                        file = null;
234
                }
235
        }
236

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

    
247
        /*
248
         * (non-Javadoc)
249
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWidth()
250
         */
251
        public double getWidth() {
252
                return file.width;
253
        }
254

    
255
        /*
256
         * (non-Javadoc)
257
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
258
         */
259
        public double getHeight() {
260
                return file.height;
261
        }
262

    
263
        /*
264
         * (non-Javadoc)
265
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getView()
266
         */
267
        public Extent getView() {
268
                return viewRequest;
269
        }
270

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

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

    
305
                Extent selectedExtent = new ExtentImpl(p1.getX(), p1.getY(), p2.getX(), p2.getY());
306

    
307
                setView(selectedExtent);
308
                int wPx = rasterBuf.getWidth();
309
                int hPx = rasterBuf.getHeight();
310
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
311

    
312
                loadBuffer(viewRequest, wPx, hPx, rasterBuf, bandList, stpBuffer);
313

    
314
                return rasterBuf;
315
        }
316

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

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

    
350
                Extent selectedExtent = new ExtentImpl(p1.getX(), p1.getY(), p2.getX(), p2.getY());
351

    
352
                setView(selectedExtent);
353
                int wPx = rasterBuf.getWidth();
354
                int hPx = rasterBuf.getHeight();
355
                int[] stpBuffer = new int[]{0, 0 , wPx, hPx};
356

    
357
                loadBuffer(viewRequest, wPx, hPx, rasterBuf, bandList, stpBuffer);
358

    
359
                return rasterBuf;
360
        }
361

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

    
388
                //TODO: FUNCIONALIDAD: Implementar adjustToExtent = false
389

    
390
                loadBuffer(viewRequest, bufWidth, bufHeight, rasterBuf, bandList, stpBuffer);
391
                return rasterBuf;
392
        }
393
        
394
        /*
395
         * (non-Javadoc)
396
         * @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)
397
         */
398
        public Buffer getWindow(int x, int y, int w, int h, 
399
                        BandList bandList, Buffer rasterBuf, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
400
                Point2D init = this.rasterToWorld(new Point2D.Double(x, y));
401
                Point2D end = this.rasterToWorld(new Point2D.Double(x + w, y + h));
402
                Extent selectedExtent = new ExtentImpl(init.getX(), init.getY(), end.getX(), end.getY());
403
                setView(selectedExtent);
404
                int[] stpBuffer = new int[]{0, 0 , rasterBuf.getWidth(), rasterBuf.getHeight()};
405

    
406
                loadBuffer(viewRequest, rasterBuf.getWidth(), rasterBuf.getHeight(), rasterBuf, bandList, stpBuffer);
407
                return rasterBuf;
408
        }
409

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

    
439
                        if(task.getEvent() != null)
440
                                task.manageEvent(task.getEvent());
441

    
442
                        if(bufWidth > Math.round(Math.abs(selectedExtent.width() / file.cellIncrementX)))
443
                                bufWidth = (int)Math.round(Math.abs(selectedExtent.width() / file.cellIncrementX));
444
                        if(bufHeight > Math.round(Math.abs(selectedExtent.height() / file.cellIncrementY)))
445
                                bufHeight = (int)Math.round(Math.abs(selectedExtent.height() / file.cellIncrementY));
446
                        file.setView(file.numBands, readBandsFromECW, selectedExtent.minX(), selectedExtent.maxY(), selectedExtent.maxX(), selectedExtent.minY(), bufWidth, bufHeight);
447
                        
448
                        //Escribimos el raster sobre un Buffer
449
                        int[] pRGBArray = new int[bufWidth];
450
                        drawRGB2(rasterBuf, pRGBArray, bandList, task);
451

    
452
                }catch(JNCSInvalidSetViewException exc){
453
                        throw new RasterDriverException("Error setting coords");
454
                }catch (JNCSFileNotOpenException e) {
455
                        throw new RasterDriverException("Error opening file");
456
                }catch (JNCSException ex) {
457
                        throw new RasterDriverException("Error reading data");
458
                }
459

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

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

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

    
565
                }
566
                //********* END caso especial que resuelve Bug#1 **********************
567

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

    
583
                if(task.getEvent() != null)
584
                        task.manageEvent(task.getEvent());
585

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

    
600
                if(task.getEvent() != null)
601
                        task.manageEvent(task.getEvent());
602

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

    
615
                if(task.getEvent() != null)
616
                        task.manageEvent(task.getEvent());
617

    
618
        }
619

    
620
        public void reProject(ICoordTrans rp) {
621
        }
622

    
623
        /*
624
         * (non-Javadoc)
625
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getBlockSize()
626
         */
627
        public int getBlockSize() {
628
                return 0;
629
        }
630

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

    
639
                Point2D begin = rasterToWorld(new Point2D.Double(0, line));
640
                Point2D end = rasterToWorld(new Point2D.Double(file.width, line + 1));
641
                int[] readBandsFromECW = new int[file.numBands];
642
                if(file.numBands <= 3) {
643
                        for(int i = 0; i < file.numBands; i++)
644
                                readBandsFromECW[i] = i;
645
                } else {
646
                        readBandsFromECW[0] = band;
647
                }
648

    
649
                Extent e = new ExtentImpl(begin.getX(), begin.getY(), end.getX(), end.getY());
650

    
651
                try {
652
                        int[] value = new int[file.width];
653
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, 1);
654
                        file.readLineRGBA(value);
655

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

    
689
                return null;
690
        }
691

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

    
701
                if((pos + blockHeight) > file.height)
702
                        blockHeight = Math.abs(file.height - pos);
703

    
704
                Point2D begin = rasterToWorld(new Point2D.Double(0, pos));
705
                Point2D end = rasterToWorld(new Point2D.Double(file.width, pos + blockHeight));
706
                int[] readBandsFromECW = new int[file.numBands];
707

    
708
                for(int i = 0; i < file.numBands; i++)
709
                        readBandsFromECW[i] = i;
710

    
711
                byte[][][] buf = new byte[file.numBands][blockHeight][file.width];
712
                Extent e = new ExtentImpl(begin.getX(), begin.getY(), end.getX(), end.getY());
713
                e = rasterUtil.calculateAdjustedView(getExtent(), e);
714

    
715
                try {
716
                        int[] value = new int[file.width];
717
                        file.setView(file.numBands, readBandsFromECW, e.minX(), e.maxY(), e.maxX(), e.minY(), file.width, blockHeight);
718

    
719
                        if(file.numBands <= 3) {
720
                                for (int row = 0; row < blockHeight; row++) {
721
                                        file.readLineRGBA(value);
722
                                        switch(getDataType()[0]) {
723
                                        case Buffer.TYPE_BYTE:
724
                                                if(buf.length >= 3) {
725
                                                        for(int col = 0; col < file.width; col ++) {
726
                                                                buf[0][row][col] = (byte)(((value[col] & 0x00ff0000) >> 16) & 0xff);
727
                                                                buf[1][row][col] = (byte)(((value[col] & 0x0000ff00) >> 8) & 0xff);
728
                                                                buf[2][row][col] = (byte)((value[col] & 0x000000ff) & 0xff);
729
                                                        }
730
                                                } else { //Si es monobanda solo se usa cualquiera uno de los tres valores
731
                                                        for(int col = 0; col < file.width; col ++) {
732
                                                                buf[0][row][col] = (byte)(((value[col] & 0x00ff0000) >> 16) & 0xff);
733
                                                        }
734
                                                }
735
                                                break;
736
                                        }
737
                                }
738

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

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

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

    
755
                return buf;
756
        }
757

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

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

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

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

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

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

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

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

    
835
        /*
836
         * (non-Javadoc)
837
         * @see org.gvsig.raster.impl.provider.RasterProvider#getOverviewCount(int)
838
         */
839
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
840
                if(band >= getBandCount())
841
                        throw new BandAccessException("Wrong band");
842
                return 0;
843
        }
844

    
845
        /*
846
         * (non-Javadoc)
847
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewWidth(int, int)
848
         */
849
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
850
                if (band >= getBandCount())
851
                        throw new BandAccessException("Wrong band");
852
                return 0;
853
        }
854

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

    
865
        /*
866
         * (non-Javadoc)
867
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isOverviewsSupported()
868
         */
869
        public boolean isOverviewsSupported() {
870
                return false;
871
        }
872

    
873
        
874
        public String getStringProjection() throws RasterDriverException{
875
                return file.projection;
876
        }
877

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