Statistics
| Revision:

svn-gvsig-desktop / tags / Root_v06 / applications / appgvSIG / src / com / iver / cit / gvsig / gui / Panels / ProgressSaveRasterDialog.java @ 4811

History | View | Annotate | Download (12 KB)

1
/*
2
 * Created on 17-feb-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package com.iver.cit.gvsig.gui.Panels;
48

    
49
import java.awt.Dimension;
50
import java.awt.geom.Rectangle2D;
51
import java.io.File;
52
import java.io.IOException;
53

    
54
import org.cresques.cts.IProjection;
55
import org.cresques.geo.ViewPortData;
56
import org.cresques.io.EcwWriter;
57
import org.cresques.io.GdalWriter;
58
import org.cresques.io.GeoRasterWriter;
59
import org.cresques.io.IDataWriter;
60
import org.cresques.px.Extent;
61
import org.cresques.ui.raster.ProgressSaveRasterPanel;
62

    
63
import com.iver.andami.PluginServices;
64
import com.iver.andami.ui.mdiManager.View;
65
import com.iver.andami.ui.mdiManager.ViewInfo;
66
import com.iver.cit.gvsig.RasterizerLayer;
67
import com.iver.cit.gvsig.fmap.MapControl;
68
import com.iver.cit.gvsig.fmap.ViewPort;
69
import com.iver.cit.gvsig.fmap.layers.FLayers;
70
import com.iver.cit.gvsig.gui.Panels.SaveRasterDialog.DriverProperties;
71

    
72
import es.gva.cit.jgdal.Gdal;
73
import es.gva.cit.jgdal.GdalDriver;
74
import es.gva.cit.jgdal.GdalException;
75

    
76
/**
77
 * @author Nacho Brodin <brodin_ign@gva.es>
78
 */
79
public class ProgressSaveRasterDialog extends ProgressSaveRasterPanel implements View{
80
        
81
        private FLayers                                         layers = null;
82
        private IProjection                                 currentProjection = null;
83
        private MapControl                                        mapCtrl = null;
84
        private ViewPort                                         vp = null;
85
        private DriverProperties                         driverProps = null;
86
        private GeoRasterWriter                         writer = null;
87
        private String                                                 fName = null;
88
        private int                                                 sizeBlock = 64;
89
        private int                                                 compresion = 1;
90
        private Dimension                                         dimension = null;
91
        private Save                                                threadSave = null;
92
        private double[]                                        maxPercentBarList = new double[1];
93
        
94
        /**
95
         * @author Nacho Brodin <brodin_ign@gva.es>
96
         */
97
        class Save extends Thread{
98
                
99
                private ProgressSaveRasterDialog window = null;
100
                
101
                Save(ProgressSaveRasterDialog window){
102
                        this.window = window;
103
                }
104
                
105
                public void run(){
106
                        if (fName.endsWith(".ecw") || fName.endsWith(".jp2")) {
107
                                //System.out.println("Salvando vista a fichero "+fName+" a formato Ecw");
108
                                try{
109
                                
110
                                        RasterizerLayer p = new RasterizerLayer(layers, vp, sizeBlock, mapCtrl);
111
                                        p.setProgressBar(window.getJProgressBar(), maxPercentBarList[0]);
112
                                        Extent ex = new Extent(vp.getAdjustedExtent());
113
                                        Dimension imgSz = vp.getImageSize();
114
                                        ViewPortData vpData = new ViewPortData(vp.getProjection(), ex, imgSz );
115
                                        writer = new EcwWriter( ((IDataWriter)p), 
116
                                                                                        vpData, 
117
                                                                                        fName,
118
                                                                                        sizeBlock,
119
                                                                                        3, 
120
                                                                                        compresion);
121
                                        //Si han sido modificadas las propiedades se asignan los valores modificados
122
                                        String[] prp = driverProps.getProperties("ecw"); 
123
                                        if(prp!=null)
124
                                                writer.setProps(prp);
125
                                        ((EcwWriter)writer).dataWrite();
126
                                        ((EcwWriter)writer).writeClose(); 
127
                                                
128
                                                
129
                                }catch(IOException ev){
130
                                                ev.printStackTrace();
131
                                }catch(es.gva.cit.jecwcompress.EcwException r){ 
132
                                                r.printStackTrace();
133
                                }
134
                                
135
                        }else if (        fName.endsWith(".tif") || fName.endsWith(".jpg") || fName.endsWith(".jpeg")) {        
136
                                //System.out.println("Salvando fichero "+fName+" a formato Tif");
137
                                try{
138
                                        String drvName = null;
139
                                        String fJpegName = null;
140
                                        String drvJpegName = null;
141
                                        
142
                                        String end = fName.substring(fName.lastIndexOf("."), fName.length()); 
143
                                        if(end.equals(".tif"))
144
                                                drvName = "GTiff";
145
                                        
146
                                        if(end.equals(".jpg") || end.equals(".jpeg")){
147
                                                drvName = "GTiff";
148
                                                drvJpegName = "JPEG";
149
                                                fJpegName = fName;
150
                                                fName = fName.substring(0, fName.lastIndexOf("."))+".tif";
151
                                                maxPercentBarList = new double[2];
152
                                                maxPercentBarList[0] = 75D;
153
                                                maxPercentBarList[1] = 25D;
154
                                        }
155
                                        
156
                                        RasterizerLayer p = new RasterizerLayer(layers, vp, sizeBlock, mapCtrl);
157
                                        p.setProgressBar(window.getJProgressBar(),maxPercentBarList[0]);
158
                                        Extent ex = new Extent(vp.getAdjustedExtent());
159
                                        
160
                                    writer = new GdalWriter(         ((IDataWriter)p), 
161
                                                                                            dimension.width, 
162
                                                                                                dimension.height, 
163
                                                                                                fName, 
164
                                                                                                ex.maxX(), 
165
                                                                                                ex.minX(), 
166
                                                                                                ex.maxY(), 
167
                                                                                                ex.minY(), 
168
                                                                                                3, 
169
                                                                                                sizeBlock,
170
                                                                                                drvName);
171
                                    //Si han sido modificadas las propiedades se asignan los valores modificados
172
                                    String[] prp = driverProps.getProperties("tif");
173
                                    
174
                                    if(prp!=null)
175
                                                writer.setProps(prp);
176

    
177
                                    
178
                                        ((GdalWriter)writer).dataWrite();
179
                                        ((GdalWriter)writer).writeClose();
180
                                        
181
                                        //Barra de progreso de comprimir a Jpeg
182
                                        if(end.equals(".jpg") || end.equals(".jpeg")){
183
                                                
184
                                                CopyDataset copyDataset = new CopyDataset(        fName,
185
                                                                                                                        fName.substring(0, fName.lastIndexOf("."))+".jpg",
186
                                                                                                                        vp,
187
                                                                                                                        prp,
188
                                                                                                                        "JPEG",
189
                                                                                                                        "jpg",
190
                                                                                                                        window);
191
                                                copyDataset.setMaxPercentBar(maxPercentBarList[1]);
192
                                                //copyDataset.setPriority(Thread.MAX_PRIORITY);
193
                                                copyDataset.start();
194
                                                copyDataset.copy();
195
                                                new File(fName).delete();
196
                                        }
197
                                        
198
                                }catch(IOException ev){
199
                                        ev.printStackTrace();
200
                                }catch(es.gva.cit.jgdal.GdalException r){ 
201
                                        r.printStackTrace();
202
                                }
203
                                
204
                        } else {
205
                            System.err.println("Extensi?n del fichero " + fName + " no soportada.");
206
                        }
207
                        PluginServices.getMDIManager().closeView(this.window);
208
                }
209
        }
210
        
211
        
212
        /**
213
         * @author Nacho Brodin <brodin_ign@gva.es>
214
         * 
215
         * Esta clase copia un dataset desde disco en un formato soportado por Gdal
216
         * a otro especificado.
217
         */
218
        class CopyDataset extends Thread{
219
                
220
                private GdalDriver                                         driver = null;
221
                private String                                                 fDstName = null;
222
                private String                                                 fSrcName = null;
223
                private ViewPort                                         vp = null;
224
                private String[]                                        driverProps = null;
225
                private String                                                 extension = null;
226
                private double                                                 maxPercentBar = 0D;
227
                private boolean                                         runCopy = false;
228
                private ProgressSaveRasterDialog        window = null;
229
                
230
                /**
231
                 * Constructor
232
                 * @param src        Nombre del fichero fuente
233
                 * @param dst        Nombre del fichero destino
234
                 * @param viewPort        Viewport
235
                 * @param dp        DriverProperties
236
                 * @param drvName        Nombre del driver de Gdal
237
                 * @param extens        Extensi?n del fichero
238
                 */
239
                CopyDataset(String src, 
240
                                        String dst, 
241
                                        ViewPort viewPort, 
242
                                        String[] dp,
243
                                        String drvName,
244
                                        String extens,
245
                                        ProgressSaveRasterDialog window){
246
                        fSrcName = src;                        
247
                        fDstName = dst;
248
                        vp = viewPort;
249
                        this.window = window;
250
                        this.driverProps = dp;
251
                        this.extension = extens;
252
                        try{
253
                                driver = Gdal.getDriverByName(drvName);
254
                        }catch(GdalException exc){
255
                            System.err.println("No se ha podido obtener el driver.");
256
                        }
257
                        runCopy = true;
258
                }
259
                
260
                /**
261
                 * Funci?n que realiza la copia del dataset
262
                 */
263
                public void copy(){
264
                        try{        
265
                                GdalWriter.createCopy(        driver,
266
                                                                                fDstName, 
267
                                                                                fSrcName, 
268
                                                                                false, 
269
                                                                                driverProps,
270
                                                                                vp.getProjection());
271
                        }catch(IOException ev){
272
                                ev.printStackTrace();
273
                        }catch(GdalException ev){
274
                                ev.printStackTrace();
275
                        }                        
276
                        runCopy = false;
277
                }
278
                
279
                /**
280
                 * El thread maneja el incremento de la barra
281
                 */
282
                public void run(){
283
                        File f = new File(fSrcName);
284
                        long sizeSrc = f.length();
285
                        long sizeDst = 0;
286
                        if(extension.equals("jpg"))
287
                                sizeDst = (long)(sizeSrc * 0.085);
288
                        double incrProgressBar = 0;
289
                        int last = 0;
290
                        
291
                        String osname = System.getProperty("os.name").toLowerCase();
292
                        if(osname.indexOf("windows")>=0){
293
                                //Temporalmente hacemos esto en windows 
294
                                long l1 = System.currentTimeMillis();
295
                                long l2 = 0;
296
                                while(runCopy){
297
                                        if((l2 - l1) > 1000){
298
                                                l1 = System.currentTimeMillis();
299
                                                window.getJProgressBar().setValue(window.getJProgressBar().getValue() + 1);
300
                                        }
301
                                        l2 = System.currentTimeMillis();
302
                                        try{
303
                                                Thread.sleep(10);
304
                                        }catch(InterruptedException exc){}
305
                                }
306
                        }else{
307
                                while(runCopy){
308
                                        File fdst = new File(fDstName);
309
                                        incrProgressBar = (fdst.length() * maxPercentBar) / sizeDst;
310
                                        if(fdst.exists() && incrProgressBar < maxPercentBar){
311
                                                if(last != ((int)incrProgressBar)){
312
                                                        window.getJProgressBar().setValue(window.getJProgressBar().getValue() + 1);
313
                                                        //System.out.println(fdst.length()+" "+maxPercentBar+" "+sizeDst+"**>"+incrProgressBar);
314
                                                }
315
                                                last=(int)incrProgressBar;
316
                                        }
317
                                        try{
318
                                                Thread.sleep(10);
319
                                        }catch(InterruptedException exc){}
320
                                }
321
                        }
322
                        window.getJProgressBar().setValue(100);
323
                }
324
                
325
                /**
326
                 * Asigna el porcentaje m?ximo a la barra
327
                 * @param max        Porcentaje m?ximo de la barra
328
                 */
329
                public void setMaxPercentBar(double max){
330
                        this.maxPercentBar = max;
331
                }
332
        }
333
        
334
        /**
335
         * Contructor 
336
         * @param mapCtrl                        MapControl
337
         * @param layers                        Capas a rasterizar
338
         * @param currentProjection        Proyecci?n 
339
         * @param dimension                        Dimensi?n del marco en pixeles
340
         * @param ext                                Extend de las capas a rasterizar
341
         */
342
        public ProgressSaveRasterDialog(        MapControl mapCtrl, 
343
                                                                                FLayers layers, 
344
                                                                                IProjection currentProjection,
345
                                                                                Dimension dimension, 
346
                                                                                Rectangle2D ext){
347
                super();
348
                this.layers = layers;
349
                this.currentProjection = currentProjection;
350
                this.mapCtrl = mapCtrl;
351
                this.dimension = dimension;
352
                
353
                vp = new ViewPort(currentProjection);
354
                vp.setBackColor(mapCtrl.getViewPort().getBackColor());
355
                vp.setImageSize(dimension);
356
                vp.setExtent(ext);
357
                
358
                threadSave = new Save(this);
359
        }
360
        
361
        /**
362
         * Asigna los textos a los paneles en el idioma seleccionado
363
         */        
364
        private void setTranslation(){
365
                this.lwait.setText(PluginServices.getText(this,"espere"));
366
        }
367
        
368
        /**
369
         * Asigna el objeto que contiene las propiedades
370
         * @param driverProps
371
         */
372
        public void setProps(DriverProperties driverProps){
373
                this.driverProps = driverProps;
374
        }
375
        
376
        /**
377
         * Asigna el nombre del fichero
378
         * @param fName        Nombre del fichero
379
         */
380
        public void setFName(String fName){
381
                this.fName = fName;
382
        }
383
        
384
        /**
385
         * Asigna el GeoRasterWriter que se usar? para salvar
386
         * @param writer
387
         */
388
        public void setWriter(GeoRasterWriter writer){
389
                this.writer = writer;
390
        }
391
        
392
        /**
393
         * Asigna el tama?o de bloque que se lee de una sola vez desde el origen
394
         * Esto influye en la velocidad de escritura y depende de la memoria que 
395
         * tengamos disponible. Si disponemos de poca corremos el riesgo de que al
396
         * asignar un bloque muy grande nos de un error
397
         * @param sizeBlock Tama?o de bloque
398
         */
399
        public void setBlockSize(int sizeBlock){
400
                this.sizeBlock = sizeBlock;
401
        }
402
        
403
        /**
404
         * Asigna la compresi?n si el formato dispone de este par?metro
405
         * @param compresion Nivel de compresi?n
406
         */
407
        public void setCompresion(int compresion){
408
                this.compresion = compresion;
409
        }
410
        
411
        /**
412
         * Muestra la barra de incremento
413
         *
414
         */
415
        public void showWindow(){
416
                threadSave.start();
417
                PluginServices.getMDIManager().addView(this);
418
        }
419
                
420
        /**
421
         * @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
422
         */
423
        public ViewInfo getViewInfo() {
424
                ViewInfo m_viewinfo=new ViewInfo(ViewInfo.MODALDIALOG);
425
                    m_viewinfo.setTitle(PluginServices.getText(this, "tarea_en_progreso"));
426
                return m_viewinfo;
427
        }
428
}
429

    
430