Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / raster / CmsRasterDriver.java @ 5811

History | View | Annotate | Download (10.9 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.Rectangle2D;
50
import java.awt.image.BufferedImage;
51
import java.awt.image.DataBuffer;
52
import java.io.File;
53
import java.io.IOException;
54
import java.util.ArrayList;
55

    
56
import org.cresques.cts.ICoordTrans;
57
import org.cresques.cts.IProjection;
58
import org.cresques.geo.ViewPortData;
59
import org.cresques.io.ErmapperWriter;
60
import org.cresques.io.GdalFile;
61
import org.cresques.io.GdalWriter;
62
import org.cresques.io.GeoRasterFile;
63
import org.cresques.io.JpegWriter;
64
import org.cresques.io.raster.RasterFilterStack;
65
import org.cresques.px.Extent;
66
import org.cresques.px.PxRaster;
67

    
68
import com.iver.cit.gvsig.fmap.ViewPort;
69
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
70
import com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver;
71

    
72
/**
73
 * Driver de raster (tal y como los abre CMS.
74
 * Primera aproximaci?n al raster. Borrador para revisar.
75
 *
76
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
77
 */
78
public class CmsRasterDriver implements GeorreferencedRasterDriver {
79
        private File file = null;
80
        private IProjection proj = null;
81
        private GeoRasterFile rasterFile = null;
82
        private PxRaster raster = null;
83
        private Extent tempExtent = null;
84

    
85
        int trans = 255;
86

    
87
        static {
88
                // Fuerza la carga del soporte de img (gdal):
89
                 System.err.println("CmsRasterDriver: inicializando");
90
                 Class c[] = {GdalFile.class, GdalWriter.class, ErmapperWriter.class, JpegWriter.class};
91
        }
92
        /* (non-Javadoc)
93
         * @see com.hardcode.driverManager.Driver#getName()
94
         */
95
        public String getName() {
96
                return "gvSIG Image Driver";
97
        }
98

    
99
        /**
100
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#open(java.io.File)
101
         */
102
        public void open(File f) throws IOException {
103
                file = f;
104
        }
105

    
106
        /* (non-Javadoc)
107
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#close()
108
         */
109
        public void close() throws IOException {
110
                rasterFile.close();
111
        }
112

    
113
        /* (non-Javadoc)
114
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#initialize()
115
         */
116
        public void initialize() throws IOException {
117
                if (proj != null){
118
                        String fName = file.getAbsolutePath();
119
                        int test = fName.indexOf("ecwp:");
120

    
121
                        if (test != -1) {
122
                            String urlECW = fName.substring(test + 6);
123
                            //urlECW.replaceAll("\\", "/");
124
                            fName = "ecwp://" + urlECW;
125
                                System.err.println(test+" "+fName);
126
                    }
127
                        rasterFile = GeoRasterFile.openFile(proj, fName);
128
                        //rasterFile.load();
129
                        createPxRaster();
130
                } else
131
                        throw new IOException("Proyecci?n no asignada");
132
                System.out.println(getName()+": Inicializado (con PxRaster)");
133
        }
134

    
135
        /**
136
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#initialize(org.cresques.cts.IProjection)
137
         */
138
        public void initialize(IProjection proj) throws IOException {
139
                this.proj = proj;
140
                rasterFile = GeoRasterFile.openFile(proj, file.getAbsolutePath());
141
                //rasterFile.load();
142
                createPxRaster();
143
        }
144

    
145
        private void createPxRaster() {
146
                raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
147
                raster.setTransparency(false);
148
        }
149

    
150
        /**
151
         * A?ade un fichero al PxRaste
152
         * @param fileName Nombre del fichero a a?adir
153
         */
154
        public void addFile(String fileName){
155
                if(raster!=null)
156
                        raster.addFile(fileName);
157
        }
158

    
159
        /**
160
         * Elimina un fichero al PxRaste
161
         * @param fileName Nombre del fichero a a?adir
162
         */
163
        public void delFile(String fileName){
164
                if(raster!=null)
165
                        raster.delFile(fileName);
166
        }
167

    
168
        /*
169
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#getFullExtent()
170
         */
171
        public Rectangle2D getFullExtent() {
172
                return rasterFile.getExtent().toRectangle2D();
173
        }
174

    
175
        public int getTransparency() {
176
                return 255-(raster.getAlpha());
177
        }
178

    
179
        public void setTransparency(int trans) {
180
                this.trans = trans;
181
                if (raster != null) {
182
                        raster.setTransparency(trans);
183
                }
184
        }
185

    
186
        /* (non-Javadoc)
187
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
188
         */
189
        public void draw(BufferedImage image, Graphics2D g, ViewPort vp) throws DriverIOException {
190
                //Comentamos temporalmente el c?digo de Vicente y restauramos el que habia
191
                /*Dimension imgSz=new Dimension(image.getWidth(),image.getHeight());
192
                Rectangle2D r=vp.getAdjustedExtent();
193
                Extent e=new Extent(r);*/
194
                Extent e = new Extent(vp.getAdjustedExtent());
195
                Dimension imgSz = vp.getImageSize();
196
                ViewPortData vp2 = new ViewPortData(vp.getProjection(), e, imgSz );
197
                vp2.setMat(vp.getAffineTransform());
198
                raster.draw(g, vp2);
199
        }
200

    
201

    
202
        /*
203
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#getProjection()
204
         */
205
        public IProjection getProjection() {
206
                return proj;
207
        }
208

    
209
        /* (non-Javadoc)
210
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#setProjection(org.cresques.cts.IProjection)
211
         */
212
        public void setProjection(IProjection proj) {
213
                this.proj = proj;
214
        }
215

    
216
        /*
217
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getNumBands()
218
         */
219
        public int getNumBands() {
220
                return rasterFile.getBandCount();
221
        }
222

    
223
        /* (non-Javadoc)
224
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getPixel(int, int, byte[])
225
         */
226
        public byte[] getPixel(int x, int y, byte[] dArray) {
227
                // TODO Auto-generated method stub
228
                return null;
229
        }
230

    
231
        /* (non-Javadoc)
232
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getPixel(int, int, int[])
233
         */
234
        public int[] getPixel(int x, int y, int[] dArray) {
235
                // TODO Auto-generated method stub
236
                return null;
237
        }
238

    
239
        /* (non-Javadoc)
240
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getRasterDataType()
241
         */
242
        public int getRasterDataType() {
243
                return raster.getDataType();
244
        }
245

    
246
        /* (non-Javadoc)
247
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getData(int, int, int)
248
         */
249
        public Object getData(int x, int y, int band) {
250
                // TODO Auto-generated method stub
251
                return null;
252
        }
253

    
254
        /* (non-Javadoc)
255
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsByte(int, int, int)
256
         */
257
        public byte getDataAsByte(int x, int y, int band) {
258
                // TODO Auto-generated method stub
259
                return 0;
260
        }
261

    
262
        /* (non-Javadoc)
263
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsFloat(int, int, int)
264
         */
265
        public float getDataAsFloat(int x, int y, int band) {
266
                // TODO Auto-generated method stub
267
                return 0;
268
        }
269

    
270
        /* (non-Javadoc)
271
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsDouble(int, int, int)
272
         */
273
        public double getDataAsDouble(int x, int y, int band) {
274
                // TODO Auto-generated method stub
275
                return 0;
276
        }
277

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

    
286
        /* (non-Javadoc)
287
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#fileAccepted(java.io.File)
288
         */
289
        public boolean fileAccepted(File file) {
290
                return GeoRasterFile.fileIsSupported(file.getName());
291
        }
292

    
293
        /* (non-Javadoc)
294
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getImage(java.awt.Dimension, java.awt.geom.Rectangle2D, org.cresques.cts.ICoordTrans)
295
         */
296
        public Image getImage(Dimension size, Rectangle2D userSize, ICoordTrans rp) {
297
                // TODO Auto-generated method stub
298
                return null;
299
        }
300

    
301
        /* (non-Javadoc)
302
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getAttributes()
303
         * ?AVISO! Variar los tipos de datos devueltos puede hacer que alguna extensi?n
304
         * no funcione bien. Se pueden a?adir atributos pero no es recomendable quitar ninguno. 
305
         */
306
        public ArrayList getAttributes() {
307
                ArrayList attr = new ArrayList();
308
                String dataType = "Byte";
309
                if (rasterFile.getDataType() == DataBuffer.TYPE_BYTE) dataType = "Byte";
310
                else if (rasterFile.getDataType() == DataBuffer.TYPE_SHORT)
311
                        dataType = "Short";
312
                else if (rasterFile.getDataType() == DataBuffer.TYPE_USHORT)
313
                        dataType = "Unsigned Short";
314
                else if (rasterFile.getDataType() == DataBuffer.TYPE_INT)
315
                        dataType = "Integer";
316
                else if (rasterFile.getDataType() == DataBuffer.TYPE_FLOAT)
317
                        dataType = "Float";
318
                else if (rasterFile.getDataType() == DataBuffer.TYPE_DOUBLE)
319
                        dataType = "Double";
320
                else
321
                        dataType = "Unknown";
322
                Object [][] a = {
323
                        {"Filename",rasterFile.getName()},
324
                        {"Filesize",new Long(rasterFile.getFileSize())},
325
                        {"Width",new Integer(rasterFile.getWidth())},
326
                        {"Height", new Integer(rasterFile.getHeight())},
327
                        {"Bands", new Integer(rasterFile.getBandCount())}
328
//                        {"BandDataType", dataType}
329
                };
330
                for (int i=0; i<a.length; i++)
331
                        attr.add(a[i]);
332
                return attr;
333
        }
334

    
335
        /**
336
         * Devuelve el colorBand activo en la banda especificada.
337
         * @param flag banda.
338
         * @return color de banda activo
339
         */
340
        public int getBand(int flag){
341
                return raster.getBand(flag);
342
        }
343

    
344
        /**
345
         * Devuelve la posici?n del fichero para la banda especificada.
346
         * @param flag banda.
347
         * @return posici?n del fichero
348
         */
349
        public int getPosFile(int flag){
350
                return raster.getPosFile(flag);
351
        }
352

    
353
        /**
354
         * Activa o desactiva la transparencia
355
         * @param t        true activa la transparencia y false la desactiva
356
         */
357
        public void setTransparency(boolean t){
358
                raster.setTransparency(t);
359
        }
360

    
361
        /**
362
         * Asocia un colorBand al rojo, verde o azul.
363
         * @param flag cual (o cuales) de las bandas.
364
         * @param nBand        que colorBand
365
         */
366
        public void setBand(int flag, int nBand){
367
                raster.setBand(flag, nBand);
368
        }
369

    
370
        /**
371
         * Obtiene la pila de filtros
372
         * @return pila de filtros
373
         */
374
        public RasterFilterStack getFilterStack(){
375
                return raster.filterStack;
376
        }
377

    
378
        /**
379
         * Asigna la pila de filtros
380
         * @return pila de filtros
381
         */
382
        public void setFilterStack(RasterFilterStack stack){
383
                raster.filterStack = stack;
384
        }
385

    
386
        /**
387
         * Obtiene el valor del pixel del Image en la posici?n x,y
388
         * @param x Posici?n x
389
         * @param y Posici?n y
390
         * @return valor de pixel
391
         */
392
        public int[] getPixel(double wcx, double wcy){
393
                return raster.getPixel(wcx, wcy);
394
        }
395

    
396
        /**
397
         *
398
         */
399
        public GeoRasterFile [] getFiles(){
400
                return raster.getFiles();
401
        }
402

    
403
}