Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_907 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / raster / CmsRasterDriver.java @ 11015

History | View | Annotate | Download (13.2 KB)

1
/*
2
 * Created on 17-dic-2004
3
 */
4
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.cit.gvsig.fmap.drivers.raster;
45

    
46
import java.awt.Dimension;
47
import java.awt.Graphics2D;
48
import java.awt.Image;
49
import java.awt.geom.AffineTransform;
50
import java.awt.geom.Rectangle2D;
51
import java.awt.image.BufferedImage;
52
import java.awt.image.DataBuffer;
53
import java.io.File;
54
import java.io.IOException;
55
import java.util.ArrayList;
56

    
57
import org.cresques.cts.ICoordTrans;
58
import org.cresques.cts.IProjection;
59
import org.cresques.filter.RasterFilterStack;
60
import org.cresques.geo.ViewPortData;
61
import org.cresques.io.ErmapperWriter;
62
import org.cresques.io.GdalFile;
63
import org.cresques.io.GdalWriter;
64
import org.cresques.io.GeoRasterFile;
65
import org.cresques.io.JpegWriter;
66
import org.cresques.io.MemoryRasterDriver;
67
import org.cresques.io.data.Grid;
68
import org.cresques.io.data.RasterBuf;
69
import org.cresques.io.exceptions.NotSupportedExtensionException;
70
import org.cresques.px.Extent;
71
import org.cresques.px.PxRaster;
72

    
73
import com.iver.cit.gvsig.fmap.ViewPort;
74
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
75
import com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver;
76

    
77
/**
78
 * Driver de raster (tal y como los abre CMS.
79
 * Primera aproximaci?n al raster. Borrador para revisar.
80
 *
81
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
82
 */
83
public class CmsRasterDriver implements GeorreferencedRasterDriver {
84
        private File file = null;
85
        private IProjection proj = null;
86
        private GeoRasterFile rasterFile = null;
87
        private PxRaster raster = null;
88
        private Extent tempExtent = null;
89
        private Grid        grid = null;
90
        private RasterBuf buf = null;
91
        private Extent        ext = null;
92

    
93
        int trans = 255;
94

    
95
        static {
96
                // Fuerza la carga del soporte de img (gdal):
97
                 System.err.println("CmsRasterDriver: inicializando");
98
                 Class c[] = {GdalFile.class, GdalWriter.class, ErmapperWriter.class, JpegWriter.class};
99
        }
100
        /* (non-Javadoc)
101
         * @see com.hardcode.driverManager.Driver#getName()
102
         */
103
        public String getName() {
104
                return "gvSIG Image Driver";
105
        }
106

    
107
        /**
108
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#open(java.io.File)
109
         */
110
        public void open(File f) throws IOException {
111
                file = f;
112
        }
113

    
114
        /* (non-Javadoc)
115
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#close()
116
         */
117
        public void close() throws IOException {
118
                rasterFile.close();
119
        }
120

    
121
        /* (non-Javadoc)
122
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#initialize()
123
         */
124
        public void initialize() throws IOException {
125
                if (proj != null){
126
                        String fName = null;
127
                        if(file != null){
128
                                fName = file.getAbsolutePath();
129
                                int test = fName.indexOf("ecwp:");
130
        
131
                                if (test != -1) {
132
                                    String urlECW = fName.substring(test + 6);
133
                                    //urlECW.replaceAll("\\", "/");
134
                                    fName = "ecwp://" + urlECW;
135
                                        System.err.println(test+" "+fName);
136
                            }
137
                        }
138
                        if(buf != null)
139
                                try {
140
                                        rasterFile = new MemoryRasterDriver(buf, ext);
141
                                } catch (NotSupportedExtensionException e) {
142
                                        //rasterFile sigue siendo null
143
                                }
144
                        else
145
                                rasterFile = GeoRasterFile.openFile(proj, fName);
146
                        if(rasterFile == null)
147
                                throw new IOException("Formato no valido");
148
                        grid = new Grid(rasterFile);
149
                        createPxRaster();
150
                } else
151
                        throw new IOException("Proyecci?n no asignada");
152
                System.out.println(getName()+": Inicializado (con PxRaster)");
153
        }
154

    
155
        /**
156
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#initialize(org.cresques.cts.IProjection)
157
         */
158
        public void initialize(IProjection proj) throws IOException {
159
                this.proj = proj;
160
                rasterFile = GeoRasterFile.openFile(proj, file.getAbsolutePath());
161
                if(rasterFile == null)
162
                        return;
163
                grid = new Grid(rasterFile);
164
                createPxRaster();
165
        }
166

    
167
        private void createPxRaster() {
168
                raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
169
                raster.setTransparency(false);
170
                grid.addRenderizer(raster);
171
        }
172

    
173
        /**
174
         * A?ade un fichero al PxRaste
175
         * @param fileName Nombre del fichero a a?adir
176
         */
177
        public void addFile(String fileName){
178
                if(raster!=null){
179
                        GeoRasterFile grf = raster.addFile(fileName);
180
                        grid.addFile(grf);
181
                }
182
        }
183

    
184
        /**
185
         * Elimina un fichero al PxRaste
186
         * @param fileName Nombre del fichero a a?adir
187
         */
188
        public void delFile(String fileName){
189
                if(raster!=null){
190
                        GeoRasterFile grf = raster.delFile(fileName);
191
                        grid.removeFile(grf);
192
                }
193
        }
194

    
195
        /*
196
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#getFullExtent()
197
         */
198
        public Rectangle2D getFullExtent() {
199
                return rasterFile.getExtent().toRectangle2D();
200
        }
201

    
202
        public int getTransparency() {
203
                return 255-(raster.getAlpha());
204
        }
205

    
206
        public void setTransparency(int trans) {
207
                this.trans = trans;
208
                if (raster != null) {
209
                        raster.setTransparency(trans);
210
                }
211
        }
212

    
213
        /* (non-Javadoc)
214
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
215
         */
216
        public void draw(BufferedImage image, Graphics2D g, ViewPort vp) throws DriverIOException {
217
                //Comentamos temporalmente el c?digo de Vicente y restauramos el que habia
218
                /*Dimension imgSz=new Dimension(image.getWidth(),image.getHeight());
219
                Rectangle2D r=vp.getAdjustedExtent();
220
                Extent e=new Extent(r);*/
221
                Extent e = new Extent(vp.getAdjustedExtent());
222
                Dimension imgSz = vp.getImageSize();
223
                ViewPortData vp2 = new ViewPortData(vp.getProjection(), e, imgSz );
224
                vp2.setMat(vp.getAffineTransform());
225
                raster.draw(g, vp2);
226
        }
227

    
228

    
229
        /*
230
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#getProjection()
231
         */
232
        public IProjection getProjection() {
233
                return proj;
234
        }
235

    
236
        /* (non-Javadoc)
237
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#setProjection(org.cresques.cts.IProjection)
238
         */
239
        public void setProjection(IProjection proj) {
240
                this.proj = proj;
241
        }
242

    
243
        /*
244
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getNumBands()
245
         */
246
        public int getNumBands() {
247
                return rasterFile.getBandCount();
248
        }
249

    
250
        /* (non-Javadoc)
251
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getPixel(int, int, byte[])
252
         */
253
        public byte[] getPixel(int x, int y, byte[] dArray) {
254
                // TODO Auto-generated method stub
255
                return null;
256
        }
257

    
258
        /* (non-Javadoc)
259
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getPixel(int, int, int[])
260
         */
261
        public int[] getPixel(int x, int y, int[] dArray) {
262
                // TODO Auto-generated method stub
263
                return null;
264
        }
265

    
266
        /* (non-Javadoc)
267
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getRasterDataType()
268
         */
269
        public int getRasterDataType() {
270
                return raster.getDataType();
271
        }
272

    
273
        /* (non-Javadoc)
274
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getData(int, int, int)
275
         */
276
        public Object getData(int x, int y, int band) {
277
                return raster.getFiles()[0].getData(x, y, band);
278
        }
279

    
280
        /* (non-Javadoc)
281
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsByte(int, int, int)
282
         */
283
        public byte getDataAsByte(int x, int y, int band) {
284
                // TODO Auto-generated method stub
285
                return 0;
286
        }
287

    
288
        /* (non-Javadoc)
289
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsFloat(int, int, int)
290
         */
291
        public float getDataAsFloat(int x, int y, int band) {
292
                // TODO Auto-generated method stub
293
                return 0;
294
        }
295

    
296
        /* (non-Javadoc)
297
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsDouble(int, int, int)
298
         */
299
        public double getDataAsDouble(int x, int y, int band) {
300
                // TODO Auto-generated method stub
301
                return 0;
302
        }
303

    
304
        /* (non-Javadoc)
305
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsInt(int, int, int)
306
         */
307
        public int getDataAsInt(int x, int y, int band) {
308
                // TODO Auto-generated method stub
309
                return 0;
310
        }
311

    
312
        /* (non-Javadoc)
313
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#fileAccepted(java.io.File)
314
         */
315
        public boolean fileAccepted(File file) {
316
                return GeoRasterFile.fileIsSupported(file.getName());
317
        }
318

    
319
        /* (non-Javadoc)
320
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getImage(java.awt.Dimension, java.awt.geom.Rectangle2D, org.cresques.cts.ICoordTrans)
321
         */
322
        public Image getImage(Dimension size, Rectangle2D userSize, ICoordTrans rp) {
323
                // TODO Auto-generated method stub
324
                return null;
325
        }
326

    
327
        /* (non-Javadoc)
328
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getAttributes()
329
         * ?AVISO! Variar los tipos de datos devueltos puede hacer que alguna extensi?n
330
         * no funcione bien. Se pueden a?adir atributos pero no es recomendable quitar ninguno. 
331
         */
332
        public ArrayList getAttributes() {
333
                ArrayList attr = new ArrayList();
334
                String dataType = "Byte";
335
                if (rasterFile.getDataType() == DataBuffer.TYPE_BYTE) dataType = "Byte";
336
                else if (rasterFile.getDataType() == DataBuffer.TYPE_SHORT)
337
                        dataType = "Short";
338
                else if (rasterFile.getDataType() == DataBuffer.TYPE_USHORT)
339
                        dataType = "Unsigned Short";
340
                else if (rasterFile.getDataType() == DataBuffer.TYPE_INT)
341
                        dataType = "Integer";
342
                else if (rasterFile.getDataType() == DataBuffer.TYPE_FLOAT)
343
                        dataType = "Float";
344
                else if (rasterFile.getDataType() == DataBuffer.TYPE_DOUBLE)
345
                        dataType = "Double";
346
                else
347
                        dataType = "Unknown";
348
                Object [][] a = {
349
                        {"Filename",rasterFile.getName()},
350
                        {"Filesize",new Long(rasterFile.getFileSize())},
351
                        {"Width",new Integer(rasterFile.getWidth())},
352
                        {"Height", new Integer(rasterFile.getHeight())},
353
                        {"Bands", new Integer(rasterFile.getBandCount())}
354
//                        {"BandDataType", dataType}
355
                };
356
                for (int i=0; i<a.length; i++)
357
                        attr.add(a[i]);
358
                return attr;
359
        }
360

    
361
        /**
362
         * Devuelve el colorBand activo en la banda especificada.
363
         * @param flag banda.
364
         * @return color de banda activo
365
         */
366
        public int getBand(int flag){
367
                return raster.getBand(flag);
368
        }
369

    
370
        /**
371
         * Devuelve la posici?n del fichero para la banda especificada.
372
         * @param flag banda.
373
         * @return posici?n del fichero
374
         */
375
        public int getPosFile(int flag){
376
                return raster.getPosFile(flag);
377
        }
378

    
379
        /**
380
         * Activa o desactiva la transparencia
381
         * @param t        true activa la transparencia y false la desactiva
382
         */
383
        public void setTransparency(boolean t){
384
                raster.setTransparency(t);
385
        }
386

    
387
        /**
388
         * Asocia un colorBand al rojo, verde o azul.
389
         * @param flag cual (o cuales) de las bandas.
390
         * @param nBand        que colorBand
391
         */
392
        public void setBand(int flag, int nBand){
393
                raster.setBand(flag, nBand);
394
        }
395

    
396
        /**
397
         * Obtiene la pila de filtros
398
         * @return pila de filtros
399
         */
400
        public RasterFilterStack getFilterStack(){
401
                return raster.filterStack;
402
        }
403

    
404
        /**
405
         * Asigna la pila de filtros
406
         * @return pila de filtros
407
         */
408
        public void setFilterStack(RasterFilterStack stack){
409
                raster.filterStack = stack;
410
        }
411

    
412
        /**
413
         * Obtiene el valor del pixel del Image en la posici?n x,y
414
         * @param x Posici?n x
415
         * @param y Posici?n y
416
         * @return valor de pixel
417
         */
418
        public int[] getPixel(double wcx, double wcy){
419
                return raster.getPixel(wcx, wcy);
420
        }
421

    
422
        /**
423
         *
424
         */
425
        public GeoRasterFile [] getFiles(){
426
                return raster.getFiles();
427
        }
428
        
429
        /**
430
         * Obtiene el flag que dice si la imagen est? o no georreferenciada
431
         * @return true si est? georreferenciada y false si no lo est?.
432
         */
433
        public boolean isGeoreferenced() {
434
                return rasterFile.isGeoreferenced();
435
        }
436
        
437
        /**
438
         * Asigna una transformaci?n al georrasterfile para la transformar la 
439
         * asignaci?n de setView. Del mismo modo asigna los extents a PxRaster para
440
         * transformar la visualizaci?n.
441
         * @param t Matriz de transformaci?n
442
         */
443
        public void setAffineTransform(AffineTransform t){ 
444
                for (int i = 0; i < raster.getFiles().length; i++) {
445
                        raster.getFiles()[i].setAffineTransform(t);
446
                        raster.setExtent(raster.getFiles()[i].getExtent());
447
                        raster.setExtentForRequest(raster.getFiles()[i].getExtentForRequest());
448
                }
449
        }
450
        
451
        public AffineTransform getAffineTransform(){
452
                return raster.getFiles()[0].getAffineTransform();
453
        }
454
        
455
        /**
456
         * Obtiene el grid asociado 
457
         * @return grid
458
         */
459
        public Grid getGrid(){
460
                return grid;
461
        }
462
        
463
        /**
464
        ?* It returns a new file created from the selected
465
         * raster file. e.g: When the user name chooses a
466
         * RAW file the file to open is a VRT file. This 
467
         * method creates the new file and returns its name.
468
         * @param file
469
         * Selected File
470
         * @return
471
         * New File to load
472
         */
473
        public File getTransformedFile(File file){
474
                return null;
475
        }
476
        
477
        /**
478
         * Set data buffer to create a memory raster driver
479
         * @param buf data buffer
480
         */
481
        public void setBuffer(RasterBuf buf){
482
                this.buf = buf;
483
        }
484
        
485
        /**
486
         * Set extent to create a memory raster driver
487
         * @param ext Extent
488
         */
489
        public void setExtent(Extent ext){
490
                this.ext = ext;
491
        }
492
}