Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_912 / extensions / extRasterTools / src / org / gvsig / rasterTools / saveRaster / operations / CopyDatasetThread.java @ 11422

History | View | Annotate | Download (3.43 KB)

1
package org.gvsig.rasterTools.saveRaster.operations;
2

    
3
import java.io.File;
4
import java.io.IOException;
5

    
6
import org.cresques.io.GdalWriter;
7
import org.gvsig.rasterTools.saveRaster.ui.properties.ProgressSaveRasterDialog;
8

    
9
import com.iver.cit.gvsig.fmap.ViewPort;
10

    
11
import es.gva.cit.jgdal.Gdal;
12
import es.gva.cit.jgdal.GdalDriver;
13
import es.gva.cit.jgdal.GdalException;
14

    
15
/**
16
 * @author Nacho Brodin <brodin_ign@gva.es>
17
 * 
18
 * Esta clase copia un dataset desde disco en un formato soportado por Gdal
19
 * a otro especificado.
20
 */
21
class CopyDatasetThread extends Thread{
22
        
23
        private GdalDriver                                         driver = null;
24
        private String                                                 fDstName = null;
25
        private String                                                 fSrcName = null;
26
        private ViewPort                                         vp = null;
27
        private String[]                                        driverProps = null;
28
        private String                                                 extension = null;
29
        private double                                                 maxPercentBar = 0D;
30
        private boolean                                         runCopy = false;
31
        private ProgressSaveRasterDialog        window = null;
32
        
33
        /**
34
         * Constructor
35
         * @param src        Nombre del fichero fuente
36
         * @param dst        Nombre del fichero destino
37
         * @param viewPort        Viewport
38
         * @param dp        DriverProperties
39
         * @param drvName        Nombre del driver de Gdal
40
         * @param extens        Extensi?n del fichero
41
         */
42
        public CopyDatasetThread(String src, String dst, ViewPort viewPort, String[] dp,
43
                                String drvName, String extens, ProgressSaveRasterDialog window){
44
                fSrcName = src;                        
45
                fDstName = dst;
46
                vp = viewPort;
47
                this.window = window;
48
                this.driverProps = dp;
49
                this.extension = extens;
50
                try{
51
                        driver = Gdal.getDriverByName(drvName);
52
                }catch(GdalException exc){
53
                    System.err.println("No se ha podido obtener el driver.");
54
                }
55
                runCopy = true;
56
        }
57
        
58
        /**
59
         * Funci?n que realiza la copia del dataset
60
         */
61
        public void copy(){
62
                try{        
63
                        GdalWriter.createCopy(        driver,
64
                                                                        fDstName, 
65
                                                                        fSrcName, 
66
                                                                        false, 
67
                                                                        driverProps,
68
                                                                        vp.getProjection());
69
                }catch(IOException ev){
70
                        ev.printStackTrace();
71
                }catch(GdalException ev){
72
                        ev.printStackTrace();
73
                }                        
74
                runCopy = false;
75
        }
76
        
77
        /**
78
         * El thread maneja el incremento de la barra
79
         */
80
        public void run(){
81
                File f = new File(fSrcName);
82
                long sizeSrc = f.length();
83
                long sizeDst = 0;
84
                if(extension.equals("jpg"))
85
                        sizeDst = (long)(sizeSrc * 0.085);
86
                double incrProgressBar = 0;
87
                int last = 0;
88
                
89
                String osname = System.getProperty("os.name").toLowerCase();
90
                if(osname.indexOf("windows")>=0){
91
                        //Temporalmente hacemos esto en windows 
92
                        long l1 = System.currentTimeMillis();
93
                        long l2 = 0;
94
                        while(runCopy){
95
                                if((l2 - l1) > 1000){
96
                                        l1 = System.currentTimeMillis();
97
                                        window.getJProgressBar().setValue(window.getJProgressBar().getValue() + 1);
98
                                }
99
                                l2 = System.currentTimeMillis();
100
                                try{
101
                                        Thread.sleep(10);
102
                                }catch(InterruptedException exc){}
103
                        }
104
                }else{
105
                        while(runCopy){
106
                                File fdst = new File(fDstName);
107
                                incrProgressBar = (fdst.length() * maxPercentBar) / sizeDst;
108
                                if(fdst.exists() && incrProgressBar < maxPercentBar){
109
                                        if(last != ((int)incrProgressBar)){
110
                                                window.getJProgressBar().setValue(window.getJProgressBar().getValue() + 1);
111
                                                //System.out.println(fdst.length()+" "+maxPercentBar+" "+sizeDst+"**>"+incrProgressBar);
112
                                        }
113
                                        last=(int)incrProgressBar;
114
                                }
115
                                try{
116
                                        Thread.sleep(10);
117
                                }catch(InterruptedException exc){}
118
                        }
119
                }
120
                window.getJProgressBar().setValue(100);
121
        }
122
        
123
        /**
124
         * Asigna el porcentaje m?ximo a la barra
125
         * @param max        Porcentaje m?ximo de la barra
126
         */
127
        public void setMaxPercentBar(double max){
128
                this.maxPercentBar = max;
129
        }
130
}