Statistics
| Revision:

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

History | View | Annotate | Download (12.5 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 9056 nacho
import org.cresques.io.data.Grid;
67 673 luisw
import org.cresques.px.Extent;
68
import org.cresques.px.PxRaster;
69 514 jmorell
70 671 luisw
import com.iver.cit.gvsig.fmap.ViewPort;
71
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
72 519 jmorell
import com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver;
73 514 jmorell
74
/**
75 519 jmorell
 * Driver de raster (tal y como los abre CMS.
76
 * Primera aproximaci?n al raster. Borrador para revisar.
77 5737 caballero
 *
78 519 jmorell
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
79 514 jmorell
 */
80 519 jmorell
public class CmsRasterDriver implements GeorreferencedRasterDriver {
81 673 luisw
        private File file = null;
82 519 jmorell
        private IProjection proj = null;
83 673 luisw
        private GeoRasterFile rasterFile = null;
84
        private PxRaster raster = null;
85 2850 nacho
        private Extent tempExtent = null;
86 9056 nacho
        private Grid        grid = null;
87 5737 caballero
88 1455 luisw
        int trans = 255;
89 673 luisw
90 1528 luisw
        static {
91
                // Fuerza la carga del soporte de img (gdal):
92 3484 luisw2
                 System.err.println("CmsRasterDriver: inicializando");
93 4404 nacho
                 Class c[] = {GdalFile.class, GdalWriter.class, ErmapperWriter.class, JpegWriter.class};
94 1528 luisw
        }
95 673 luisw
        /* (non-Javadoc)
96
         * @see com.hardcode.driverManager.Driver#getName()
97
         */
98
        public String getName() {
99
                return "gvSIG Image Driver";
100
        }
101 5737 caballero
102 519 jmorell
        /**
103
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#open(java.io.File)
104
         */
105
        public void open(File f) throws IOException {
106 673 luisw
                file = f;
107 519 jmorell
        }
108 514 jmorell
109
        /* (non-Javadoc)
110 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#close()
111 514 jmorell
         */
112 5737 caballero
        public void close() throws IOException {
113 3256 nacho
                rasterFile.close();
114 514 jmorell
        }
115
116
        /* (non-Javadoc)
117 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#initialize()
118 514 jmorell
         */
119 519 jmorell
        public void initialize() throws IOException {
120 1502 luisw
                if (proj != null){
121
                        String fName = file.getAbsolutePath();
122
                        int test = fName.indexOf("ecwp:");
123
124
                        if (test != -1) {
125
                            String urlECW = fName.substring(test + 6);
126
                            //urlECW.replaceAll("\\", "/");
127
                            fName = "ecwp://" + urlECW;
128
                                System.err.println(test+" "+fName);
129
                    }
130
                        rasterFile = GeoRasterFile.openFile(proj, fName);
131 9056 nacho
                        if(rasterFile == null)
132
                                throw new IOException("Formato no valido");
133
                        grid = new Grid(rasterFile);
134 673 luisw
                        createPxRaster();
135
                } else
136
                        throw new IOException("Proyecci?n no asignada");
137
                System.out.println(getName()+": Inicializado (con PxRaster)");
138 514 jmorell
        }
139 5737 caballero
140 519 jmorell
        /**
141
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#initialize(org.cresques.cts.IProjection)
142
         */
143
        public void initialize(IProjection proj) throws IOException {
144
                this.proj = proj;
145 2850 nacho
                rasterFile = GeoRasterFile.openFile(proj, file.getAbsolutePath());
146 9056 nacho
                if(rasterFile == null)
147
                        return;
148
                grid = new Grid(rasterFile);
149 673 luisw
                createPxRaster();
150 519 jmorell
        }
151 5737 caballero
152 673 luisw
        private void createPxRaster() {
153
                raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
154
                raster.setTransparency(false);
155 9056 nacho
                grid.addRenderizer(raster);
156 673 luisw
        }
157 514 jmorell
158 2183 fernando
        /**
159
         * A?ade un fichero al PxRaste
160
         * @param fileName Nombre del fichero a a?adir
161
         */
162
        public void addFile(String fileName){
163 9056 nacho
                if(raster!=null){
164
                        GeoRasterFile grf = raster.addFile(fileName);
165
                        grid.addFile(grf);
166
                }
167 2183 fernando
        }
168 5737 caballero
169 2183 fernando
        /**
170
         * Elimina un fichero al PxRaste
171
         * @param fileName Nombre del fichero a a?adir
172
         */
173
        public void delFile(String fileName){
174 9056 nacho
                if(raster!=null){
175
                        GeoRasterFile grf = raster.delFile(fileName);
176
                        grid.removeFile(grf);
177
                }
178 2183 fernando
        }
179 5737 caballero
180 673 luisw
        /*
181
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#getFullExtent()
182
         */
183
        public Rectangle2D getFullExtent() {
184
                return rasterFile.getExtent().toRectangle2D();
185
        }
186 5737 caballero
187 1455 luisw
        public int getTransparency() {
188
                return 255-(raster.getAlpha());
189
        }
190 5737 caballero
191 1455 luisw
        public void setTransparency(int trans) {
192
                this.trans = trans;
193
                if (raster != null) {
194
                        raster.setTransparency(trans);
195
                }
196
        }
197 673 luisw
198
        /* (non-Javadoc)
199
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
200
         */
201
        public void draw(BufferedImage image, Graphics2D g, ViewPort vp) throws DriverIOException {
202 5811 nacho
                //Comentamos temporalmente el c?digo de Vicente y restauramos el que habia
203
                /*Dimension imgSz=new Dimension(image.getWidth(),image.getHeight());
204 5737 caballero
                Rectangle2D r=vp.getAdjustedExtent();
205 5811 nacho
                Extent e=new Extent(r);*/
206
                Extent e = new Extent(vp.getAdjustedExtent());
207
                Dimension imgSz = vp.getImageSize();
208 673 luisw
                ViewPortData vp2 = new ViewPortData(vp.getProjection(), e, imgSz );
209
                vp2.setMat(vp.getAffineTransform());
210
                raster.draw(g, vp2);
211
        }
212
213 5737 caballero
214 673 luisw
        /*
215 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#getProjection()
216 514 jmorell
         */
217 519 jmorell
        public IProjection getProjection() {
218
                return proj;
219 514 jmorell
        }
220
221 673 luisw
        /* (non-Javadoc)
222
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#setProjection(org.cresques.cts.IProjection)
223 514 jmorell
         */
224 673 luisw
        public void setProjection(IProjection proj) {
225
                this.proj = proj;
226 514 jmorell
        }
227
228 673 luisw
        /*
229 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getNumBands()
230
         */
231
        public int getNumBands() {
232 673 luisw
                return rasterFile.getBandCount();
233 519 jmorell
        }
234
235 514 jmorell
        /* (non-Javadoc)
236 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getPixel(int, int, byte[])
237 514 jmorell
         */
238 519 jmorell
        public byte[] getPixel(int x, int y, byte[] dArray) {
239 514 jmorell
                // TODO Auto-generated method stub
240
                return null;
241
        }
242
243
        /* (non-Javadoc)
244 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getPixel(int, int, int[])
245 514 jmorell
         */
246 519 jmorell
        public int[] getPixel(int x, int y, int[] dArray) {
247 514 jmorell
                // TODO Auto-generated method stub
248 519 jmorell
                return null;
249 514 jmorell
        }
250
251
        /* (non-Javadoc)
252 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getRasterDataType()
253 514 jmorell
         */
254 519 jmorell
        public int getRasterDataType() {
255 2183 fernando
                return raster.getDataType();
256 514 jmorell
        }
257
258
        /* (non-Javadoc)
259 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getData(int, int, int)
260 514 jmorell
         */
261 554 luisw
        public Object getData(int x, int y, int band) {
262 8765 jjdelcerro
                return raster.getFiles()[0].getData(x, y, band);
263 514 jmorell
        }
264
265
        /* (non-Javadoc)
266 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsByte(int, int, int)
267 514 jmorell
         */
268 519 jmorell
        public byte getDataAsByte(int x, int y, int band) {
269 514 jmorell
                // TODO Auto-generated method stub
270
                return 0;
271
        }
272
273
        /* (non-Javadoc)
274 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsFloat(int, int, int)
275 514 jmorell
         */
276 519 jmorell
        public float getDataAsFloat(int x, int y, int band) {
277 514 jmorell
                // TODO Auto-generated method stub
278 519 jmorell
                return 0;
279 514 jmorell
        }
280
281
        /* (non-Javadoc)
282 519 jmorell
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsDouble(int, int, int)
283 514 jmorell
         */
284 519 jmorell
        public double getDataAsDouble(int x, int y, int band) {
285 514 jmorell
                // TODO Auto-generated method stub
286 519 jmorell
                return 0;
287 514 jmorell
        }
288 554 luisw
289
        /* (non-Javadoc)
290
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsInt(int, int, int)
291
         */
292
        public int getDataAsInt(int x, int y, int band) {
293
                // TODO Auto-generated method stub
294
                return 0;
295
        }
296 569 luisw
297
        /* (non-Javadoc)
298
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#fileAccepted(java.io.File)
299
         */
300 673 luisw
        public boolean fileAccepted(File file) {
301
                return GeoRasterFile.fileIsSupported(file.getName());
302 569 luisw
        }
303 659 luisw
304
        /* (non-Javadoc)
305
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getImage(java.awt.Dimension, java.awt.geom.Rectangle2D, org.cresques.cts.ICoordTrans)
306
         */
307
        public Image getImage(Dimension size, Rectangle2D userSize, ICoordTrans rp) {
308
                // TODO Auto-generated method stub
309
                return null;
310
        }
311 1680 luisw
312
        /* (non-Javadoc)
313
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getAttributes()
314 5811 nacho
         * ?AVISO! Variar los tipos de datos devueltos puede hacer que alguna extensi?n
315
         * no funcione bien. Se pueden a?adir atributos pero no es recomendable quitar ninguno.
316 1680 luisw
         */
317
        public ArrayList getAttributes() {
318
                ArrayList attr = new ArrayList();
319
                String dataType = "Byte";
320
                if (rasterFile.getDataType() == DataBuffer.TYPE_BYTE) dataType = "Byte";
321
                else if (rasterFile.getDataType() == DataBuffer.TYPE_SHORT)
322
                        dataType = "Short";
323
                else if (rasterFile.getDataType() == DataBuffer.TYPE_USHORT)
324
                        dataType = "Unsigned Short";
325
                else if (rasterFile.getDataType() == DataBuffer.TYPE_INT)
326
                        dataType = "Integer";
327
                else if (rasterFile.getDataType() == DataBuffer.TYPE_FLOAT)
328
                        dataType = "Float";
329
                else if (rasterFile.getDataType() == DataBuffer.TYPE_DOUBLE)
330
                        dataType = "Double";
331
                else
332
                        dataType = "Unknown";
333
                Object [][] a = {
334
                        {"Filename",rasterFile.getName()},
335
                        {"Filesize",new Long(rasterFile.getFileSize())},
336
                        {"Width",new Integer(rasterFile.getWidth())},
337
                        {"Height", new Integer(rasterFile.getHeight())},
338
                        {"Bands", new Integer(rasterFile.getBandCount())}
339
//                        {"BandDataType", dataType}
340
                };
341
                for (int i=0; i<a.length; i++)
342
                        attr.add(a[i]);
343
                return attr;
344
        }
345 5737 caballero
346 2183 fernando
        /**
347
         * Devuelve el colorBand activo en la banda especificada.
348
         * @param flag banda.
349
         * @return color de banda activo
350
         */
351
        public int getBand(int flag){
352
                return raster.getBand(flag);
353
        }
354 5737 caballero
355 2183 fernando
        /**
356
         * Devuelve la posici?n del fichero para la banda especificada.
357
         * @param flag banda.
358
         * @return posici?n del fichero
359
         */
360
        public int getPosFile(int flag){
361
                return raster.getPosFile(flag);
362
        }
363 5737 caballero
364 2183 fernando
        /**
365
         * Activa o desactiva la transparencia
366
         * @param t        true activa la transparencia y false la desactiva
367
         */
368
        public void setTransparency(boolean t){
369
                raster.setTransparency(t);
370
        }
371 5737 caballero
372 2183 fernando
        /**
373
         * Asocia un colorBand al rojo, verde o azul.
374
         * @param flag cual (o cuales) de las bandas.
375
         * @param nBand        que colorBand
376
         */
377
        public void setBand(int flag, int nBand){
378
                raster.setBand(flag, nBand);
379
        }
380 5737 caballero
381 2183 fernando
        /**
382 5737 caballero
         * Obtiene la pila de filtros
383 2183 fernando
         * @return pila de filtros
384
         */
385
        public RasterFilterStack getFilterStack(){
386
                return raster.filterStack;
387
        }
388 5737 caballero
389 2183 fernando
        /**
390 5737 caballero
         * Asigna la pila de filtros
391 2623 nacho
         * @return pila de filtros
392
         */
393
        public void setFilterStack(RasterFilterStack stack){
394
                raster.filterStack = stack;
395
        }
396 5737 caballero
397 2623 nacho
        /**
398 2322 igbrotru
         * Obtiene el valor del pixel del Image en la posici?n x,y
399
         * @param x Posici?n x
400
         * @param y Posici?n y
401
         * @return valor de pixel
402
         */
403 2347 igbrotru
        public int[] getPixel(double wcx, double wcy){
404
                return raster.getPixel(wcx, wcy);
405 2322 igbrotru
        }
406 5737 caballero
407 2322 igbrotru
        /**
408 5737 caballero
         *
409 2183 fernando
         */
410
        public GeoRasterFile [] getFiles(){
411
                return raster.getFiles();
412
        }
413 6037 nacho
414
        /**
415
         * Obtiene el flag que dice si la imagen est? o no georreferenciada
416
         * @return true si est? georreferenciada y false si no lo est?.
417
         */
418
        public boolean isGeoreferenced() {
419
                return rasterFile.isGeoreferenced();
420
        }
421 8765 jjdelcerro
422
        /**
423
         * Asigna una transformaci?n al georrasterfile para la transformar la
424
         * asignaci?n de setView. Del mismo modo asigna los extents a PxRaster para
425
         * transformar la visualizaci?n.
426
         * @param t Matriz de transformaci?n
427
         */
428
        public void setAffineTransform(AffineTransform t){
429
                for (int i = 0; i < raster.getFiles().length; i++) {
430
                        raster.getFiles()[i].setAffineTransform(t);
431
                        raster.setExtent(raster.getFiles()[i].getExtent());
432
                        raster.setExtentForRequest(raster.getFiles()[i].getExtentForRequest());
433
                }
434
        }
435
436
        public AffineTransform getAffineTransform(){
437
                return raster.getFiles()[0].getAffineTransform();
438
        }
439 9056 nacho
440
        /**
441
         * Obtiene el grid asociado
442
         * @return grid
443
         */
444
        public Grid getGrid(){
445
                return grid;
446
        }
447
448
        /**
449
        ?* It returns a new file created from the selected
450
         * raster file. e.g: When the user name chooses a
451
         * RAW file the file to open is a VRT file. This
452
         * method creates the new file and returns its name.
453
         * @param file
454
         * Selected File
455
         * @return
456
         * New File to load
457
         */
458
        public File getTransformedFile(File file){
459
                return null;
460
        }
461 514 jmorell
}