Statistics
| Revision:

root / branches / FMap_03_raster / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / raster / CmsRasterDriver.java @ 1930

History | View | Annotate | Download (10 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.EcwWriter;
60
import org.cresques.io.GdalFile;
61
import org.cresques.io.GdalWriter;
62
import org.cresques.io.GeoRasterFile;
63
import org.cresques.io.raster.RasterFilterStack;
64
import org.cresques.px.Extent;
65
import org.cresques.px.PxRaster;
66

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

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

    
84
        static {
85
                // Fuerza la carga del soporte de img (gdal):
86
                 Class c[] = {GdalFile.class, GdalWriter.class, EcwWriter.class};
87
        }
88
        /* (non-Javadoc)
89
         * @see com.hardcode.driverManager.Driver#getName()
90
         */
91
        public String getName() {
92
                return "gvSIG Image Driver";
93
        }
94
        
95
        /**
96
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#open(java.io.File)
97
         */
98
        public void open(File f) throws IOException {
99
                file = f;
100
        }
101

    
102
        /* (non-Javadoc)
103
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#close()
104
         */
105
        public void close() throws IOException {
106
                // TODO Auto-generated method stub
107
        }
108

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

    
117
                        if (test != -1) {
118
                            String urlECW = fName.substring(test + 6);
119
                            //urlECW.replaceAll("\\", "/");
120
                            fName = "ecwp://" + urlECW;
121
                                System.err.println(test+" "+fName);
122
                    }
123

    
124
                        rasterFile = GeoRasterFile.openFile(proj, fName);
125
                        rasterFile.load();
126
                        
127
                        createPxRaster();
128
                } else
129
                        throw new IOException("Proyecci?n no asignada");
130
                System.out.println(getName()+": Inicializado (con PxRaster)");
131
        }
132
        
133
        /**
134
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#initialize(org.cresques.cts.IProjection)
135
         */
136
        public void initialize(IProjection proj) throws IOException {
137
                this.proj = proj;
138
                rasterFile = GeoRasterFile.openFile(proj, file.getAbsolutePath()); 
139
                rasterFile.load();
140
                
141
                createPxRaster();
142
        }
143
        
144
        private void createPxRaster() {
145
                raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
146
                raster.setTransparency(false);
147
        }
148

    
149
        /**
150
         * A?ade un fichero al PxRaste
151
         * @param fileName Nombre del fichero a a?adir
152
         */
153
        public void addFile(String fileName){
154
                if(raster!=null)
155
                        raster.addFile(fileName);
156
        }
157
        
158
        /**
159
         * Elimina un fichero al PxRaste
160
         * @param fileName Nombre del fichero a a?adir
161
         */
162
        public void delFile(String fileName){
163
                if(raster!=null)
164
                        raster.delFile(fileName);
165
        }
166
        /*
167
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#getFullExtent()
168
         */
169
        public Rectangle2D getFullExtent() {
170
                return rasterFile.getExtent().toRectangle2D();
171
        }
172
        
173
        public int getTransparency() {
174
                return 255-(raster.getAlpha());
175
        }
176
        
177
        public void setTransparency(int trans) {
178
                this.trans = trans;
179
                if (raster != null) {
180
                        raster.setTransparency(trans);
181
                }
182
        }
183

    
184
        /* (non-Javadoc)
185
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
186
         */
187
        public void draw(BufferedImage image, Graphics2D g, ViewPort vp) throws DriverIOException {
188
                Extent e = new Extent(vp.getAdjustedExtent());
189
                // System.err.println("CmsRasterDriver: Extent = "+e);
190
                Dimension imgSz = vp.getImageSize();
191
                ViewPortData vp2 = new ViewPortData(vp.getProjection(), e, imgSz );
192
                vp2.setMat(vp.getAffineTransform());
193
                raster.draw(g, vp2);
194
        }
195

    
196
        /*
197
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#getProjection()
198
         */
199
        public IProjection getProjection() {
200
                return proj;
201
        }
202

    
203
        /* (non-Javadoc)
204
         * @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#setProjection(org.cresques.cts.IProjection)
205
         */
206
        public void setProjection(IProjection proj) {
207
                this.proj = proj;
208
        }
209

    
210
        /*
211
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getNumBands()
212
         */
213
        public int getNumBands() {
214
                return rasterFile.getBandCount();
215
        }
216

    
217
        /* (non-Javadoc)
218
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getPixel(int, int, byte[])
219
         */
220
        public byte[] getPixel(int x, int y, byte[] dArray) {
221
                // TODO Auto-generated method stub
222
                return null;
223
        }
224

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

    
233
        /* (non-Javadoc)
234
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getRasterDataType()
235
         */
236
        public int getRasterDataType() {
237
                return raster.getDataType();
238
        }
239

    
240
        /* (non-Javadoc)
241
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getData(int, int, int)
242
         */
243
        public Object getData(int x, int y, int band) {
244
                // TODO Auto-generated method stub
245
                return null;
246
        }
247

    
248
        /* (non-Javadoc)
249
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsByte(int, int, int)
250
         */
251
        public byte getDataAsByte(int x, int y, int band) {
252
                // TODO Auto-generated method stub
253
                return 0;
254
        }
255

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

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

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

    
280
        /* (non-Javadoc)
281
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#fileAccepted(java.io.File)
282
         */
283
        public boolean fileAccepted(File file) {
284
                return GeoRasterFile.fileIsSupported(file.getName());
285
        }
286

    
287
        /* (non-Javadoc)
288
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getImage(java.awt.Dimension, java.awt.geom.Rectangle2D, org.cresques.cts.ICoordTrans)
289
         */
290
        public Image getImage(Dimension size, Rectangle2D userSize, ICoordTrans rp) {
291
                // TODO Auto-generated method stub
292
                return null;
293
        }
294

    
295
        /* (non-Javadoc)
296
         * @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getAttributes()
297
         */
298
        public ArrayList getAttributes() {
299
                ArrayList attr = new ArrayList();
300
                String dataType = "Byte";
301
                if (rasterFile.getDataType() == DataBuffer.TYPE_BYTE) dataType = "Byte";
302
                else if (rasterFile.getDataType() == DataBuffer.TYPE_SHORT)
303
                        dataType = "Short";
304
                else if (rasterFile.getDataType() == DataBuffer.TYPE_USHORT)
305
                        dataType = "Unsigned Short";
306
                else if (rasterFile.getDataType() == DataBuffer.TYPE_INT)
307
                        dataType = "Integer";
308
                else if (rasterFile.getDataType() == DataBuffer.TYPE_FLOAT)
309
                        dataType = "Float";
310
                else if (rasterFile.getDataType() == DataBuffer.TYPE_DOUBLE)
311
                        dataType = "Double";
312
                else
313
                        dataType = "Unknown";
314
                Object [][] a = {
315
                        {"Filename",rasterFile.getName()},
316
                        {"Filesize",new Long(rasterFile.getFileSize())},
317
                        {"Width",new Integer(rasterFile.getWidth())},
318
                        {"Height", new Integer(rasterFile.getHeight())},
319
                        {"Bands", new Integer(rasterFile.getBandCount())},
320
                        {"BandDataType", dataType}
321
                };
322
                for (int i=0; i<a.length; i++)
323
                        attr.add(a[i]);
324
                return attr;
325
        }
326
        
327
        /**
328
         * Devuelve el colorBand activo en la banda especificada.
329
         * @param flag banda.
330
         * @return color de banda activo
331
         */
332
        public int getBand(int flag){
333
                return raster.getBand(flag);
334
        }
335
        
336
        /**
337
         * Devuelve la posici?n del fichero para la banda especificada.
338
         * @param flag banda.
339
         * @return posici?n del fichero
340
         */
341
        public int getPosFile(int flag){
342
                return raster.getPosFile(flag);
343
        }
344
                
345
        /**
346
         * Activa o desactiva la transparencia
347
         * @param t        true activa la transparencia y false la desactiva
348
         */
349
        public void setTransparency(boolean t){
350
                raster.setTransparency(t);
351
        }
352
        
353
        /**
354
         * Asocia un colorBand al rojo, verde o azul.
355
         * @param flag cual (o cuales) de las bandas.
356
         * @param nBand        que colorBand
357
         */
358
        public void setBand(int flag, int nBand){
359
                raster.setBand(flag, nBand);
360
        }
361
        
362
        /**
363
         * Obtiene la pila de filtros 
364
         * @return pila de filtros
365
         */
366
        public RasterFilterStack getFilterStack(){
367
                return raster.filterStack;
368
        }
369
        
370
        /**
371
         * 
372
         */
373
        public GeoRasterFile [] getFiles(){
374
                return raster.getFiles();
375
        }
376
}