Statistics
| Revision:

svn-gvsig-desktop / branches / v02_desarrollo / libraries / libCq CMS for java.old / src / org / cresques / px / PxRaster.java @ 1561

History | View | Annotate | Download (15.1 KB)

1
/*
2
 * PxRaster.java
3
 */
4
package org.cresques.px;
5

    
6
import java.awt.Color;
7
import java.awt.Component;
8
import java.awt.Graphics2D;
9
import java.awt.Image;
10
import java.awt.geom.GeneralPath;
11
import java.awt.geom.Point2D;
12
import java.awt.image.ImageObserver;
13
import java.util.Date;
14
import java.util.Vector;
15

    
16
import org.cresques.cts.ICoordTrans;
17
import org.cresques.cts.IProjection;
18
import org.cresques.geo.Projected;
19
import org.cresques.geo.ViewPortData;
20
//import org.cresques.io.GdalFile;
21
import org.cresques.io.GeoRasterFile;
22
//import org.cresques.io.raster.ComputeMinMaxFilter;
23
//import org.cresques.io.raster.ComputeMinMaxImageFilter;
24
//import org.cresques.io.raster.LinearEnhancementFilter;
25
//import org.cresques.io.raster.LinearEnhancementImageFilter;
26
//import org.cresques.io.raster.RasterBuf;
27
//import org.cresques.io.raster.RasterFilter;
28
//import org.cresques.io.raster.RasterStats;
29

    
30
public class PxRaster extends PxObj implements Projected {
31
        protected GeoRasterFile geoFile = null;
32
        protected ImageObserver component = null;
33
        Vector pts = null;
34
        // Soporte para n bandas, visibles de 3 en 3, en ficheros separados
35
        protected GeoRasterFile [] colorBand = null;
36
        protected int rBand = 1, gBand = 2, bBand = 3;
37
        
38
        //RasterStats stats = new RasterStats();
39
        int transparente = 0x10ffff80;
40

    
41
        String vName = null;
42
        protected boolean pintaMarco = false; //true;
43
        IProjection proj = null;
44
        protected Extent extentOrig = null;
45
        ICoordTrans rp = null;
46
        
47
        /**
48
         * Constructor.
49
         * @param component
50
         */
51
        public PxRaster(ImageObserver component) {
52
                this.component = component;
53
        }
54
                
55
        public PxRaster(IProjection proj, String fname, ImageObserver component) {
56
                geoFile = GeoRasterFile.openFile(proj, fname);//loadECW(fname);
57
                geoFile.setUpdatable((Component) component);
58
                this.proj = proj;
59
                this.component = component;
60
                setExtent(geoFile.getExtent());
61
                geoFile.setView(geoFile.getExtent());
62
                extentOrig = extent;
63
                if (geoFile.getBandCount()>2) {
64
                        setBand(GeoRasterFile.RED_BAND, 0);
65
                        setBand(GeoRasterFile.GREEN_BAND, 1);
66
                        setBand(GeoRasterFile.BLUE_BAND, 2);
67
                } else {
68
                        setBand(GeoRasterFile.RED_BAND|GeoRasterFile.GREEN_BAND|GeoRasterFile.BLUE_BAND, 0);
69
                }
70
        }
71
        
72
        /*public PxRaster(String fname, ImageObserver component, Extent view) {
73
                geoFile = GeoRasterFile.openFile(null, fname);//loadECW(fname);
74
                this.component = component;
75
                setExtent(geoFile.getExtent());
76
                geoFile.setView(view);
77
                extentOrig = extent;
78
                
79
                //double x = 680800,y = 4128600;
80
                //setView(new Extent(x, y, x+4200, y-4200),"Huelva capital");
81
        }*/
82
        
83
        public PxRaster(IProjection proj, String [] fnames, ImageObserver component) {
84
                this.proj = proj;
85
                this.component = component;
86
                colorBand = new GeoRasterFile[fnames.length];
87
                for (int i=0; i<colorBand.length; i++) {
88
                    colorBand[i] = GeoRasterFile.openFile(proj, fnames[i]);//loadECW(fname);
89
                    colorBand[i].setUpdatable((Component) component);
90
                    setExtent(colorBand[i].getExtent());
91
                    colorBand[i].setView(colorBand[i].getExtent());
92
                }
93
                geoFile = colorBand[0];
94
                extentOrig = extent;
95
                if (fnames.length >= 3 || geoFile.getBandCount()>2) {
96
                        setBand(GeoRasterFile.RED_BAND, 0);
97
                        setBand(GeoRasterFile.GREEN_BAND, 1);
98
                        setBand(GeoRasterFile.BLUE_BAND, 2);
99
                } else {
100
                        setBand(GeoRasterFile.RED_BAND|GeoRasterFile.GREEN_BAND|GeoRasterFile.BLUE_BAND, 0);
101
                }
102
        }
103
        
104
        public PxRaster(GeoRasterFile eFile, ImageObserver component, Extent view) {
105
                geoFile = eFile;//loadECW(fname);
106
                geoFile.setUpdatable((Component) component);
107
                setProjection(geoFile.getProjection());
108
                this.component = component;
109

    
110
                setExtent(geoFile.getExtent());
111
                geoFile.setView(view); //geoFile.getExtent());
112
                extentOrig = extent;
113
                if (geoFile.getBandCount()>2) {
114
                        setBand(GeoRasterFile.RED_BAND, 0);
115
                        setBand(GeoRasterFile.GREEN_BAND, 1);
116
                        setBand(GeoRasterFile.BLUE_BAND, 2);
117
                } else {
118
                        setBand(GeoRasterFile.RED_BAND|GeoRasterFile.GREEN_BAND|GeoRasterFile.BLUE_BAND, 0);
119
                }
120
        }
121
        
122
        public int getBandCount() { return geoFile.getBandCount(); }
123
        
124
        public GeoRasterFile [] getFiles() {
125
                if (colorBand != null)
126
                        return colorBand;
127
                
128
                GeoRasterFile [] ret = {geoFile};
129
                return ret;
130
        }
131
        
132
        /**
133
         * Asocia un colorBand al rojo, verde o azul.
134
         * @param flag cual (o cuales) de las bandas.
135
         * @param nBand        que colorBand
136
         */
137
        
138
        public void setBand(int flag, int nBand) {
139
                if (colorBand != null) {
140
                        if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND)
141
                                rBand = nBand;
142
                        else if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND)
143
                                gBand = nBand;
144
                        else if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND)
145
                                bBand = nBand;
146
                } else
147
                        geoFile.setBand(flag, nBand);
148
        }
149

    
150
        /**
151
         * Devuelve el colorBand activo en la banda especificada.
152
         * @param flag banda.
153
         */
154
        
155
        public int getBand(int flag) {
156
                if (colorBand != null) {
157
                        if (flag == GeoRasterFile.RED_BAND)
158
                                return rBand;
159
                        else if (flag == GeoRasterFile.GREEN_BAND)
160
                                return gBand;
161
                        else if (flag == GeoRasterFile.BLUE_BAND)
162
                                return bBand;
163
                        else
164
                                return -1;
165
                } else
166
                        return geoFile.getBand(flag);
167
        }
168

    
169
        public void setDrawBorder(boolean pm) { pintaMarco = pm; }
170
        
171
        public String getFName() {
172
                return geoFile.getName();
173
        }
174
        
175
        /**
176
         * Devuelve la anchura total del fichero, en pixeles.
177
         * @return ancho en pixeles
178
         */
179
        public int getFWidth() {
180
                return geoFile.getWidth();
181
        }
182
        
183
        /**
184
         * Devuelve la altura total del fichero, en pixeles.
185
         * @return alto en pixeles
186
         */
187
        public int getFHeight() {
188
                return geoFile.getHeight();
189
        }
190
        
191
        public void setTransparency(boolean t) {geoFile.setTransparency(t);}
192
        public void setTransparency(int t) {geoFile.setTransparency(t);}
193
        
194
        public int getAlpha() { return geoFile.getAlpha(); }
195
        
196
        public void setExtent(Extent e) {
197
                super.extent = e;
198
                pts = new Vector();
199
                pts.add(proj.createPoint(e.minX(), e.minY()));
200
                pts.add(proj.createPoint(e.maxX(), e.minY()));
201
                pts.add(proj.createPoint(e.maxX(), e.maxY()));
202
                pts.add(proj.createPoint(e.minX(), e.maxY()));
203
        }
204
        
205
        /**
206
         * Cambia la vista (viewport) sobre el raster.
207
         * 
208
         * @param v
209
         * @param vName
210
         */
211
        public void setView(Extent v, String vName) {
212
                geoFile.setView(v);
213
                this.vName = vName;
214
        }
215
        
216
        /**
217
         * Obtiene la escala.
218
         * 
219
         * @param width
220
         * @param height
221
         * @return
222
         */
223
        
224
        public double[] getScale(int width, int height) {
225
                double scale[] = new double[2];
226
                scale[0] = ((double) width) /geoFile.getView().width();                
227
                scale[1] = ((double) height)/geoFile.getView().height();                
228
                return scale;
229
        }
230
        
231
        /**
232
         * 'Normaliza' la vista en funci?n del extent del raster.
233
         * 
234
         * @param mat
235
         * @param sz
236
         */
237

    
238
        protected void calculateNewView(Extent sz) {
239
                double vx = sz.minX(), vy = sz.minY(), vx2 = sz.maxX(), vy2 = sz.maxY();
240
                if (sz.minX() < extent.minX()) vx = extent.minX();
241
                if (sz.minY() < extent.minY()) vy = extent.minY();
242
                if (sz.maxX() > extent.maxX()) vx2 = extent.maxX();
243
                if (sz.maxY() > extent.maxY()) vy2 = extent.maxY();
244
                if (colorBand != null) { // multiBands
245
                        for (int i=0; i<colorBand.length; i++)
246
                                colorBand[i].setView(new Extent(vx, vy, vx2, vy2));
247
                } else if (geoFile != null) { // Una sola banda
248
                        geoFile.setView(new Extent(vx, vy, vx2, vy2));
249
                } else { // no cargada
250
                        System.err.println("PxRaster.calculateNewView(): Foto no cargada.");
251
                }
252
        }
253

    
254
        /**
255
         * Dibuja el raster
256
         */        
257

    
258
        public void draw(Graphics2D g, ViewPortData vp) {
259
                Image geoImage = null;
260
                long t2, t1 = new Date().getTime();
261
                System.out.println("PxRaster.draw(): vp.extent = "+ vp.getExtent());
262
                System.out.println("PxRaster.draw():    extent = "+ getExtent());
263
                if (vp.getExtent().minX()> extent.maxX()) return;
264
                if (vp.getExtent().minY()> extent.maxY()) return;
265
                if (vp.getExtent().maxX()< extent.minX()) return;
266
                if (vp.getExtent().maxY()< extent.minY()) return;
267
                calculateNewView(vp.getExtent()); 
268
                Extent v = geoFile.getView();
269
                double x = v.minX();
270
                double y = v.minY();
271
                double w = v.width();
272
                double h = v.height();
273
                System.out.println("Pinto PxRaster:" + v);
274
                
275
                double scalex = vp.mat.getScaleX()        /* g.getTransform().getScaleX()*/ ,
276
                        scaley = vp.mat.getScaleY()                /* g.getTransform().getScaleY() */;
277
                int wImg = (int) Math.abs(w*scalex)+1, hImg = (int) Math.abs(h*scaley)+1;
278
                System.out.println("Sz=("+wImg+","+hImg+"); scale=("+scalex+","+scaley+")");
279
                if (wImg <= 0 || hImg <= 0) return;
280
                
281
                Point2D.Double pt = new Point2D.Double(x, y+h);
282
                try {
283
                        System.out.println("Dibujando PxRaster: pt0 = "+pt);
284
                        vp.mat.transform(pt, pt);
285
                        System.out.println("Dibujando PxRaster: pt1 = "+pt);
286
                        /*if (colorBand == null && geoFile instanceof GdalFile &&
287
                                (geoFile.getDataType() != DataBuffer.TYPE_BYTE)) {
288
                                System.out.println("PxRaster: Has dado con un Raster de 16 bits");
289
                                System.out.println("Dibujando PxRaster sz=("+wImg+","+hImg+"...");
290
                                RasterBuf raster = ((GdalFile) geoFile).getRaster(wImg, hImg, rp);
291
                                t2 = new Date().getTime();
292
                                System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs. Obteniendo"); t1 = t2;
293
                                geoImage = renderizeRaster(raster);
294
                                t2 = new Date().getTime();
295
                                System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs. Filtrando/Renderizando");t1 = t2;
296

297
                                g.drawImage(geoImage, (int) Math.round(pt.getX()), (int) Math.round(pt.getY()), component);
298
                                t2 = new Date().getTime();
299
                                System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs. Dibujando");
300
                        } else */if (colorBand != null) { // multiBands
301
                                System.out.println("Dibujando PxRaster (Multifile) ...");
302
                            //if (doTransparency)
303
                            //geoImage = new BufferedImage(wImg, hImg, BufferedImage.TYPE_INT_ARGB);
304
                            //else
305
                            //        geoImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
306
                        geoImage = colorBand[rBand].updateImage(wImg, hImg, rp); //, geoImage, GeoRasterFile.RED_BAND);
307
                        geoImage = colorBand[gBand].updateImage(wImg, hImg, rp, geoImage, GeoRasterFile.GREEN_BAND);
308
                        geoImage = colorBand[bBand].updateImage(wImg, hImg, rp, geoImage, GeoRasterFile.BLUE_BAND);
309

    
310
                                //geoImage = renderizeRaster(geoImage);
311
                        g.drawImage(geoImage, (int) Math.round(pt.getX()), (int) Math.round(pt.getY()), component);
312
                                t2 = new Date().getTime();
313
                                System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs.");
314
                        
315
                        } else if (geoFile != null) { // Una sola banda
316
                        geoImage = geoFile.updateImage(wImg, hImg, rp);
317
                                System.out.println("Dibujando PxRaster sz=("+wImg+","+hImg+"...");
318

    
319
                                //geoImage = renderizeRaster(geoImage);
320
                                g.drawImage(geoImage, (int) Math.round(pt.getX()), (int) Math.round(pt.getY()), component);
321
                                t2 = new Date().getTime();
322
                                System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs.");
323
                        } else { // no cargada
324
                                System.err.println("Dibujando PxRaster: Foto no cargada.");
325
                        }
326
                } catch (Exception e) {
327
                        e.printStackTrace();
328
                };
329

    
330
                if (pintaMarco) {
331
                        drawMarco(g, vp);
332
                }
333
        }
334
        
335
        
336
        
337
        /*        
338
        /**
339
         * Renderiza el raster.
340
         * @author Luis W. Sevilla (sevilla_lui@gva.es)
341
         * /
342
        
343
        class RasterToImageFilter extends RasterFilter {
344
                private BufferedImage image;
345
                private int alpha;
346
                private int rgb;
347
                public RasterToImageFilter(RasterBuf raster, int a) {
348
                        super(raster, null);
349
                        this.alpha = (a & 0xff) << 24;
350
                        image = new BufferedImage(raster.getWidth(), raster.getHeight(), BufferedImage.TYPE_INT_ARGB);
351
                        execute();
352
                }
353
                
354
                /* (non-Javadoc)
355
                 * @see org.cresques.px.PxRaster.RasterFilter#process(int, int)
356
                 * /
357
                public void process(int x, int y) {
358
                        raster.getPixelInt(x, y, px);
359
                        rgb = alpha | ((px[0] & 0xff) << 16) |
360
                                        ((px[1] & 0xff) << 8) | (px[2] & 0xff);
361
                        
362
                        image.setRGB(x, y, rgb);
363
                }
364
                
365
                public Image getImage() { return image; }
366

367
                /* (non-Javadoc)
368
                 * @see org.cresques.px.PxRaster.RasterFilter#processLines(int)
369
                 * /
370
                public void processLine(int y) {
371
                        for (int x=0; x<width; x++) {
372
                                int [][] line = raster.getLineInt(y);
373
                                rgb = alpha | ((line[x][0] & 0xff) << 16) |
374
                                        ((line[x][1] & 0xff) << 8) | (line[x][2] & 0xff);
375
                
376
                                        image.setRGB(x, y, rgb);
377
                        }
378
                }
379
        }
380
        
381
        public Image renderizeRaster(RasterBuf raster) {
382
                if (true) {
383
                        ComputeMinMaxFilter filter = 
384
                                new ComputeMinMaxFilter(raster, stats);
385
                        filter = null;
386
                }
387

388
                if (true) {
389
                        LinearEnhancementFilter lEFilter = 
390
                                new LinearEnhancementFilter(raster, stats);
391
                        lEFilter = null;
392
                }
393

394
                Image image = new RasterToImageFilter(raster, getAlpha()).getImage();
395
                raster = null;
396
                if (true)
397
                        stats.pinta();
398
                return image;
399
        }
400
        
401
        public Image renderizeRaster(Image image) {
402
                if (true) {
403
                        ComputeMinMaxFilter filter = 
404
                                new ComputeMinMaxImageFilter(image, stats);
405
                        filter = null;
406
                }
407

408
                if (true) {
409
                        LinearEnhancementFilter lEFilter = 
410
                                new LinearEnhancementImageFilter(image, stats);
411
                        lEFilter = null;
412
                }
413
                if (true)
414
                        stats.pinta();
415
                return image;
416
        }
417
        */
418
        public void drawMarco(Graphics2D g, ViewPortData vp) {
419
//                Color color = new Color(255,222,165,128), fillColor = new Color(255,214,132,128);
420
                Color color = new Color(128,128,128), fillColor = new Color(255,220,220,0x20);
421
                GeneralPath gp = newGP(vp);
422
                g.setColor(fillColor);
423
                g.fill(gp);
424
                g.setColor(color);
425
                g.draw(gp);
426
        }
427
        
428
        private GeneralPath newGP(ViewPortData vp) {
429
                //if (gp != null) return;
430
                GeneralPath gp = new GeneralPath();
431
                Point2D.Double pt0 = new Point2D.Double(0.0, 0.0);
432
                Point2D.Double pt1 = new Point2D.Double(0.0, 0.0);
433
                Point2D.Double pt2 = new Point2D.Double(0.0, 0.0);
434
                Point2D.Double pt3 = new Point2D.Double(0.0, 0.0);
435
                vp.mat.transform((Point2D) pts.get(0), pt0);
436
                vp.mat.transform((Point2D) pts.get(1), pt1);
437
                vp.mat.transform((Point2D) pts.get(2), pt2);
438
                vp.mat.transform((Point2D) pts.get(3), pt3);
439
                // Aspa desde el extent
440
                gp.moveTo( (float) pt0.getX(), (float) pt0.getY() );
441
                gp.lineTo( (float) pt2.getX(), (float) pt2.getY() );
442
                gp.moveTo( (float) pt1.getX(), (float) pt1.getY() );
443
                gp.lineTo( (float) pt3.getX(), (float) pt3.getY() );
444
                // Extent
445
                gp.moveTo( (float) pt0.getX(), (float) pt0.getY() );
446
                gp.lineTo( (float) pt1.getX(), (float) pt1.getY() );
447
                gp.lineTo( (float) pt2.getX(), (float) pt2.getY() );
448
                gp.lineTo( (float) pt3.getX(), (float) pt3.getY() );
449
                if (extentOrig != extent) {
450
                        gp.lineTo( (float) pt0.getX(), (float) pt0.getY() );
451
                        Vector pts = new Vector();
452
                        pts.add(proj.createPoint(extentOrig.minX(), extentOrig.minY()));
453
                        pts.add(proj.createPoint(extentOrig.maxX(), extentOrig.minY()));
454
                        pts.add(proj.createPoint(extentOrig.maxX(), extentOrig.maxY()));
455
                        pts.add(proj.createPoint(extentOrig.minX(), extentOrig.maxY()));
456

    
457
                        vp.mat.transform((Point2D) pts.get(0), pt0);
458
                        vp.mat.transform((Point2D) pts.get(1), pt1);
459
                        vp.mat.transform((Point2D) pts.get(2), pt2);
460
                        vp.mat.transform((Point2D) pts.get(3), pt3);
461
                        gp.moveTo( (float) pt0.getX(), (float) pt0.getY() );
462
                        gp.lineTo( (float) pt1.getX(), (float) pt1.getY() );
463
                        gp.lineTo( (float) pt2.getX(), (float) pt2.getY() );
464
                        gp.lineTo( (float) pt3.getX(), (float) pt3.getY() );
465
                }
466
                gp.closePath();
467
                return gp;
468
        }
469

    
470
        public IProjection getProjection() { return proj; }
471
        public void setProjection(IProjection p) { proj = p; }
472

    
473
        public void reProject(ICoordTrans rp) {
474
                this.rp = rp.getInverted();
475
                System.out.println("PxRaster: reProject()");
476
                //geoFile.reProject(rp);
477
                Vector savePts = pts;
478

    
479
                pts = new Vector();
480
                extent = new Extent();
481
                Point2D ptDest = null;
482
                for (int i=0; i<savePts.size(); i++) {
483
                        ptDest = rp.getPDest().createPoint(0.0,0.0);
484
                        ptDest = rp.convert((Point2D) savePts.get(i), ptDest);
485
                        pts.add(ptDest);
486
                        extent.add(ptDest);
487
                }
488
                setProjection(rp.getPDest());
489
        }
490
}