Statistics
| Revision:

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

History | View | Annotate | Download (13.3 KB)

1 514 jmorell
/*
2 519 jmorell
 * Created on 17-dic-2004
3 514 jmorell
 */
4 1100 fjp
/* 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 514 jmorell
package com.iver.cit.gvsig.fmap.drivers.raster;
45
46 659 luisw
import java.awt.Dimension;
47 671 luisw
import java.awt.Graphics2D;
48 659 luisw
import java.awt.Image;
49 8765 jjdelcerro
import java.awt.geom.AffineTransform;
50 519 jmorell
import java.awt.geom.Rectangle2D;
51 671 luisw
import java.awt.image.BufferedImage;
52 1680 luisw
import java.awt.image.DataBuffer;
53 514 jmorell
import java.io.File;
54
import java.io.IOException;
55 1680 luisw
import java.util.ArrayList;
56 514 jmorell
57 659 luisw
import org.cresques.cts.ICoordTrans;
58 514 jmorell
import org.cresques.cts.IProjection;
59 5979 nacho
import org.cresques.filter.RasterFilterStack;
60 673 luisw
import org.cresques.geo.ViewPortData;
61 4404 nacho
import org.cresques.io.ErmapperWriter;
62 5991 nacho
import org.cresques.io.GdalFile;
63
import org.cresques.io.GdalWriter;
64 554 luisw
import org.cresques.io.GeoRasterFile;
65 5991 nacho
import org.cresques.io.JpegWriter;
66 10195 nacho
import org.cresques.io.MemoryRasterDriver;
67 9056 nacho
import org.cresques.io.data.Grid;
68 10195 nacho
import org.cresques.io.data.RasterBuf;
69
import org.cresques.io.exceptions.NotSupportedExtensionException;
70 673 luisw
import org.cresques.px.Extent;
71
import org.cresques.px.PxRaster;
72 514 jmorell
73 671 luisw
import com.iver.cit.gvsig.fmap.ViewPort;
74
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
75 519 jmorell
import com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver;
76 514 jmorell
77
/**
78 519 jmorell
 * Driver de raster (tal y como los abre CMS.
79
 * Primera aproximaci?n al raster. Borrador para revisar.
80 5737 caballero
 *
81 519 jmorell
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
82 514 jmorell
 */
83 519 jmorell
public class CmsRasterDriver implements GeorreferencedRasterDriver {
84 12413 maquerol
        protected File file = null;
85
        protected IProjection proj = null;
86
        protected GeoRasterFile rasterFile = null;
87
        protected PxRaster raster = null;
88 2850 nacho
        private Extent tempExtent = null;
89 12413 maquerol
        protected Grid        grid = null;
90 10195 nacho
        private RasterBuf buf = null;
91
        private Extent        ext = null;
92 5737 caballero
93 1455 luisw
        int trans = 255;
94 673 luisw
95 1528 luisw
        static {
96
                // Fuerza la carga del soporte de img (gdal):
97 3484 luisw2
                 System.err.println("CmsRasterDriver: inicializando");
98 4404 nacho
                 Class c[] = {GdalFile.class, GdalWriter.class, ErmapperWriter.class, JpegWriter.class};
99 1528 luisw
        }
100 673 luisw
        /* (non-Javadoc)
101
         * @see com.hardcode.driverManager.Driver#getName()
102
         */
103
        public String getName() {
104
                return "gvSIG Image Driver";
105
        }
106 5737 caballero
107 519 jmorell
        /**
108
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#open(java.io.File)
109
         */
110
        public void open(File f) throws IOException {
111 673 luisw
                file = f;
112 519 jmorell
        }
113 514 jmorell
114
        /* (non-Javadoc)
115 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#close()
116 514 jmorell
         */
117 5737 caballero
        public void close() throws IOException {
118 3256 nacho
                rasterFile.close();
119 514 jmorell
        }
120
121
        /* (non-Javadoc)
122 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#initialize()
123 514 jmorell
         */
124 519 jmorell
        public void initialize() throws IOException {
125 1502 luisw
                if (proj != null){
126 10195 nacho
                        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 9056 nacho
                        if(rasterFile == null)
147
                                throw new IOException("Formato no valido");
148
                        grid = new Grid(rasterFile);
149 673 luisw
                        createPxRaster();
150
                } else
151
                        throw new IOException("Proyecci?n no asignada");
152
                System.out.println(getName()+": Inicializado (con PxRaster)");
153 514 jmorell
        }
154 5737 caballero
155 519 jmorell
        /**
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 2850 nacho
                rasterFile = GeoRasterFile.openFile(proj, file.getAbsolutePath());
161 9056 nacho
                if(rasterFile == null)
162
                        return;
163
                grid = new Grid(rasterFile);
164 673 luisw
                createPxRaster();
165 519 jmorell
        }
166 5737 caballero
167 673 luisw
        private void createPxRaster() {
168
                raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
169
                raster.setTransparency(false);
170 9056 nacho
                grid.addRenderizer(raster);
171 673 luisw
        }
172 514 jmorell
173 2183 fernando
        /**
174
         * A?ade un fichero al PxRaste
175
         * @param fileName Nombre del fichero a a?adir
176
         */
177
        public void addFile(String fileName){
178 9056 nacho
                if(raster!=null){
179
                        GeoRasterFile grf = raster.addFile(fileName);
180
                        grid.addFile(grf);
181
                }
182 2183 fernando
        }
183 12413 maquerol
184 2183 fernando
        /**
185
         * Elimina un fichero al PxRaste
186
         * @param fileName Nombre del fichero a a?adir
187
         */
188
        public void delFile(String fileName){
189 9056 nacho
                if(raster!=null){
190
                        GeoRasterFile grf = raster.delFile(fileName);
191
                        grid.removeFile(grf);
192
                }
193 2183 fernando
        }
194 5737 caballero
195 673 luisw
        /*
196
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#getFullExtent()
197
         */
198
        public Rectangle2D getFullExtent() {
199
                return rasterFile.getExtent().toRectangle2D();
200
        }
201 5737 caballero
202 1455 luisw
        public int getTransparency() {
203
                return 255-(raster.getAlpha());
204
        }
205 5737 caballero
206 1455 luisw
        public void setTransparency(int trans) {
207
                this.trans = trans;
208
                if (raster != null) {
209
                        raster.setTransparency(trans);
210
                }
211
        }
212 673 luisw
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 5811 nacho
                //Comentamos temporalmente el c?digo de Vicente y restauramos el que habia
218
                /*Dimension imgSz=new Dimension(image.getWidth(),image.getHeight());
219 5737 caballero
                Rectangle2D r=vp.getAdjustedExtent();
220 5811 nacho
                Extent e=new Extent(r);*/
221
                Extent e = new Extent(vp.getAdjustedExtent());
222
                Dimension imgSz = vp.getImageSize();
223 673 luisw
                ViewPortData vp2 = new ViewPortData(vp.getProjection(), e, imgSz );
224
                vp2.setMat(vp.getAffineTransform());
225
                raster.draw(g, vp2);
226
        }
227
228 5737 caballero
229 673 luisw
        /*
230 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#getProjection()
231 514 jmorell
         */
232 519 jmorell
        public IProjection getProjection() {
233
                return proj;
234 514 jmorell
        }
235
236 673 luisw
        /* (non-Javadoc)
237
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#setProjection(org.cresques.cts.IProjection)
238 514 jmorell
         */
239 673 luisw
        public void setProjection(IProjection proj) {
240
                this.proj = proj;
241 514 jmorell
        }
242
243 673 luisw
        /*
244 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getNumBands()
245
         */
246
        public int getNumBands() {
247 673 luisw
                return rasterFile.getBandCount();
248 519 jmorell
        }
249
250 514 jmorell
        /* (non-Javadoc)
251 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getPixel(int, int, byte[])
252 514 jmorell
         */
253 519 jmorell
        public byte[] getPixel(int x, int y, byte[] dArray) {
254 514 jmorell
                // TODO Auto-generated method stub
255
                return null;
256
        }
257
258
        /* (non-Javadoc)
259 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getPixel(int, int, int[])
260 514 jmorell
         */
261 519 jmorell
        public int[] getPixel(int x, int y, int[] dArray) {
262 514 jmorell
                // TODO Auto-generated method stub
263 519 jmorell
                return null;
264 514 jmorell
        }
265
266
        /* (non-Javadoc)
267 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getRasterDataType()
268 514 jmorell
         */
269 519 jmorell
        public int getRasterDataType() {
270 2183 fernando
                return raster.getDataType();
271 514 jmorell
        }
272
273
        /* (non-Javadoc)
274 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getData(int, int, int)
275 514 jmorell
         */
276 554 luisw
        public Object getData(int x, int y, int band) {
277 8765 jjdelcerro
                return raster.getFiles()[0].getData(x, y, band);
278 514 jmorell
        }
279
280
        /* (non-Javadoc)
281 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsByte(int, int, int)
282 514 jmorell
         */
283 519 jmorell
        public byte getDataAsByte(int x, int y, int band) {
284 514 jmorell
                // TODO Auto-generated method stub
285
                return 0;
286
        }
287
288
        /* (non-Javadoc)
289 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsFloat(int, int, int)
290 514 jmorell
         */
291 519 jmorell
        public float getDataAsFloat(int x, int y, int band) {
292 514 jmorell
                // TODO Auto-generated method stub
293 519 jmorell
                return 0;
294 514 jmorell
        }
295
296
        /* (non-Javadoc)
297 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsDouble(int, int, int)
298 514 jmorell
         */
299 519 jmorell
        public double getDataAsDouble(int x, int y, int band) {
300 514 jmorell
                // TODO Auto-generated method stub
301 519 jmorell
                return 0;
302 514 jmorell
        }
303 554 luisw
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 569 luisw
312
        /* (non-Javadoc)
313
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#fileAccepted(java.io.File)
314
         */
315 673 luisw
        public boolean fileAccepted(File file) {
316
                return GeoRasterFile.fileIsSupported(file.getName());
317 569 luisw
        }
318 659 luisw
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 1680 luisw
327
        /* (non-Javadoc)
328
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getAttributes()
329 5811 nacho
         * ?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 1680 luisw
         */
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 5737 caballero
361 2183 fernando
        /**
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 5737 caballero
370 2183 fernando
        /**
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 5737 caballero
379 2183 fernando
        /**
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 5737 caballero
387 2183 fernando
        /**
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 5737 caballero
396 2183 fernando
        /**
397 5737 caballero
         * Obtiene la pila de filtros
398 2183 fernando
         * @return pila de filtros
399
         */
400
        public RasterFilterStack getFilterStack(){
401
                return raster.filterStack;
402
        }
403 5737 caballero
404 2183 fernando
        /**
405 5737 caballero
         * Asigna la pila de filtros
406 2623 nacho
         * @return pila de filtros
407
         */
408
        public void setFilterStack(RasterFilterStack stack){
409
                raster.filterStack = stack;
410
        }
411 5737 caballero
412 2623 nacho
        /**
413 2322 igbrotru
         * 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 2347 igbrotru
        public int[] getPixel(double wcx, double wcy){
419
                return raster.getPixel(wcx, wcy);
420 2322 igbrotru
        }
421 5737 caballero
422 2322 igbrotru
        /**
423 5737 caballero
         *
424 2183 fernando
         */
425
        public GeoRasterFile [] getFiles(){
426
                return raster.getFiles();
427
        }
428 6037 nacho
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 8765 jjdelcerro
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 9056 nacho
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 10195 nacho
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 12413 maquerol
        }
492
493
        public GeoRasterFile getRasterFile(){
494
                return this.rasterFile;
495 10195 nacho
        }
496 12413 maquerol
497
        public File getFile(){
498
                return file;
499
        }
500
501 514 jmorell
}