Statistics
| Revision:

svn-gvsig-desktop / trunk / prototypes / mobile / desktop / extensions / extExportMobile / src / es / prodevelop / gvsig / exportMobile / layerexporters / RasterExporter.java @ 19124

History | View | Annotate | Download (5.88 KB)

1
package es.prodevelop.gvsig.exportMobile.layerexporters;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileOutputStream;
7

    
8
import org.apache.log4j.Logger;
9

    
10
import com.hardcode.driverManager.Driver;
11
import com.iver.andami.PluginServices;
12
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
13
import com.iver.cit.gvsig.fmap.drivers.raster.CmsRasterDriver;
14
import com.iver.cit.gvsig.fmap.layers.FLayer;
15
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
16
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
17
import com.iver.cit.gvsig.fmap.rendering.XmlBuilder;
18
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
19

    
20
import es.prodevelop.gvsig.exportMobile.xml.XmlProjectTags;
21

    
22
public class RasterExporter extends ExporterSubTask{
23

    
24
        private Logger logger = Logger.getLogger(RasterExporter.class.getClass());
25
        private File outputdir;
26
        private static int INDX_PATH = 10;
27
        private FLyrRaster inLyrRast;
28

    
29
        public RasterExporter(AbstractMonitorableTask parentProcess, FLayer layer,
30
                        Rectangle2D rect, File outputdir, XmlBuilder xml) {
31
                super(parentProcess, layer, rect, xml);
32
                this.outputdir=outputdir;
33
                inLyrRast = (FLyrRaster)layer;
34
        }
35

    
36
        
37
        /**
38
         * Get old path file, and copy this file to a new file
39
         */
40
        public void export() {
41
                setNote(PluginServices.getText(this, "exporting_")  + " " +  inLayer.getName());
42
                try {
43
                        /*String pathLayer = inLayer.getXMLEntity().getXmlTag()
44
                        .getProperty(INDX_PATH).getValue();
45
                        File oldFile = new File(pathLayer);*/
46
                        Driver driver =  inLyrRast.getSource().getDriver();
47
                        if (driver instanceof CmsRasterDriver){
48
                                initXML();
49
                                CmsRasterDriver drR =(CmsRasterDriver)driver;
50
                                File oldFile = drR.getFile();
51
                                if (oldFile.exists()) {
52
                                        File destFile = new File(outputdir + File.separator + oldFile.getName());
53
                                        destFile = getFreeFile(destFile);
54
                                        File wldFile = findWldFile(oldFile);
55
                                        if (wldFile == null) {
56
                                                logger.warn("Did not find world file for: " + oldFile.getName());
57
                                        } else {
58
                                                String newWldPath = destFile.getAbsolutePath();
59
                                                newWldPath = newWldPath.substring(0, newWldPath.lastIndexOf("."));
60
                                                File newWldFile = new File(newWldPath + ".wld");
61
                                                copyFileToFile(wldFile, newWldFile, 5);
62
                                        }
63
                                        copyFileToFile(oldFile, destFile, 95);
64
                                        writeXML(inLayer.getName(), destFile.getName());
65
                                }
66
                        }
67
                        
68
                } catch (Exception e) {
69
                        // TODO Auto-generated catch block
70
                        e.printStackTrace();
71
                } finally {
72
                        closeXML();
73
                        reportToEnd();
74
                }
75
        }
76

    
77

    
78
        private File findWldFile(File img_file) {
79
                
80
                String img_path = img_file.getAbsolutePath();
81
                String img_name = img_file.getName();
82
                
83
                int lastp = img_name.lastIndexOf(".");
84
                String img_name_no_ext = img_name.substring(0, lastp); 
85
                
86
                String ext = img_path.substring(img_path.length() - 4, img_path.length());
87
                File resp = null;
88
                
89
                if ((ext.compareToIgnoreCase(".jpg") == 0) || (ext.compareToIgnoreCase("jpeg") == 0)) {
90
                        resp = new File(img_file.getParent() + File.separator + img_name_no_ext + ".jgw");
91
                        if (resp.exists()) return resp;
92
                        resp = new File(img_file.getParent() + File.separator + img_name_no_ext + ".jpw");
93
                        if (resp.exists()) return resp;
94
                        resp = new File(img_file.getParent() + File.separator + img_name_no_ext + ".jpgw");
95
                        if (resp.exists()) return resp;
96
                }
97
                
98
                if ((ext.compareToIgnoreCase(".tif") == 0) || (ext.compareToIgnoreCase("tiff") == 0)) {
99
                        resp = new File(img_file.getParent() + File.separator + img_name_no_ext + ".tfw");
100
                        if (resp.exists()) return resp;
101
                }
102
                
103
                resp = new File(img_file.getParent() + File.separator + img_name_no_ext + ".wld");
104
                if (resp.exists()) return resp;
105
                return null;
106
        }
107

    
108
        /**
109
         * Create the new output file in output file
110
         * 
111
         * @param in,
112
         *            old file
113
         * @param outdir,
114
         *            output dir
115
         * @throws Exception
116
         */
117
        public void copyFileToDir(File in, File outdir, int numStepsToReport) throws Exception {
118

    
119
                File f = new File(outdir.getAbsolutePath() + File.separator + in.getName());
120
                /*
121
                long flenght = f.length();
122
                int currStep = 0;
123
                long bytesPerStep = flenght / numStepsToReport;
124
                long bytesCopied = 0;
125
                
126
                
127
                FileInputStream fis = new FileInputStream(in);
128
                FileOutputStream fos = new FileOutputStream(f);
129
                byte[] buf = new byte[1024];
130
                int i = 0;
131
                while ((i = fis.read(buf)) != -1) {
132
                        fos.write(buf, 0, i);
133
                        bytesCopied+=i;
134
                        int newStep = (int)(bytesCopied % bytesPerStep);
135
                        if (newStep>currStep){
136
                                reportSteps(newStep-currStep);
137
                                currStep=newStep;
138
                        }
139
                }
140
                fis.close();
141
                fos.close();*/
142
                
143
                copyFileToFile(in, f, numStepsToReport);
144
        }
145
        /**
146
         * Create the new output file in output file
147
         * 
148
         * @param in,
149
         *            old file
150
         * @param outdir,
151
         *            output dir
152
         * @throws Exception
153
         */
154
        public void copyFileToFile(File in, File outFile, int numStepsToReport) throws Exception {
155

    
156
                File f = outFile;
157
                
158
                long flenght = in.length();
159
                int currStep = 0;
160
                long bytesPerStep = flenght / numStepsToReport;
161
                long bytesCopied = 0;
162
                
163
                FileInputStream fis = new FileInputStream(in);
164
                FileOutputStream fos = new FileOutputStream(f);
165
                byte[] buf = new byte[1024];
166
                int i = 0;
167
                while ((i = fis.read(buf)) != -1) {
168
                        fos.write(buf, 0, i);
169
                        bytesCopied+=i;
170
                        int newStep = (int)(bytesCopied / bytesPerStep);
171
                        if (newStep>currStep){
172
                                reportSteps(newStep-currStep);
173
                                currStep=newStep;
174
                        }
175
                }
176
                fis.close();
177
                fos.close();
178
        }
179

    
180

    
181

    
182
        @Override
183
        public int getFinalStep() {
184
                // TODO Auto-generated method stub
185
                return 100;
186
        }
187

    
188

    
189

    
190

    
191
        @Override
192
        public void run() {
193
                export();
194
        }
195
        
196
        private void writeXML(String name, String path){
197
                //<Type>
198
                xml.writeTag(XmlProjectTags.TYPE, "RASTER");
199
                //</Type>
200
                
201
                //<Path>
202
                xml.writeTag(XmlProjectTags.PATH, path);
203
                //</Path>
204
                
205
                //<Name>
206
                xml.writeTag(XmlProjectTags.NAME, name);
207
                //</Name>
208
        }
209

    
210
}