root / trunk / prototypes / mobile / desktop / extensions / extExportMobile / src / es / prodevelop / gvsig / exportMobile / layerexporters / RasterExporter.java @ 19196
History | View | Annotate | Download (5.92 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 |
/**
|
23 |
* Exportation subtask
|
24 |
* Allows a raster layer to be exported
|
25 |
*
|
26 |
* @author jcarras
|
27 |
*
|
28 |
*/
|
29 |
public class RasterExporter extends ExporterSubTask{ |
30 |
|
31 |
private Logger logger = Logger.getLogger(RasterExporter.class.getClass()); |
32 |
private File outputdir; |
33 |
private static int INDX_PATH = 10; |
34 |
private FLyrRaster inLyrRast;
|
35 |
|
36 |
/**
|
37 |
* COnstructor with all the needed attributes
|
38 |
*
|
39 |
* @param parentProcess
|
40 |
* @param layer
|
41 |
* @param rect this parameter is not used currently
|
42 |
* @param outputdir
|
43 |
* @param xml
|
44 |
*/
|
45 |
public RasterExporter(AbstractMonitorableTask parentProcess, FLayer layer,
|
46 |
Rectangle2D rect, File outputdir, XmlBuilder xml) { |
47 |
super(parentProcess, layer, rect, xml);
|
48 |
this.outputdir=outputdir;
|
49 |
inLyrRast = (FLyrRaster)layer; |
50 |
} |
51 |
|
52 |
|
53 |
/**
|
54 |
* Get old path file, and copy this file to a new file
|
55 |
*/
|
56 |
public void export() { |
57 |
setNote(PluginServices.getText(this, "exporting_") + " " + inLayer.getName()); |
58 |
try {
|
59 |
/*String pathLayer = inLayer.getXMLEntity().getXmlTag()
|
60 |
.getProperty(INDX_PATH).getValue();
|
61 |
File oldFile = new File(pathLayer);*/
|
62 |
Driver driver = inLyrRast.getSource().getDriver();
|
63 |
if (driver instanceof CmsRasterDriver){ |
64 |
initXML(); |
65 |
CmsRasterDriver drR =(CmsRasterDriver)driver; |
66 |
File oldFile = drR.getFile();
|
67 |
if (oldFile.exists()) {
|
68 |
File destFile = new File(outputdir + File.separator + oldFile.getName()); |
69 |
destFile = getFreeFile(destFile); |
70 |
File wldFile = findWldFile(oldFile);
|
71 |
if (wldFile == null) { |
72 |
logger.warn("Did not find world file for: " + oldFile.getName());
|
73 |
} else {
|
74 |
String newWldPath = destFile.getAbsolutePath();
|
75 |
newWldPath = newWldPath.substring(0, newWldPath.lastIndexOf(".")); |
76 |
File newWldFile = new File(newWldPath + ".wld"); |
77 |
copyFileToFile(wldFile, newWldFile, 5);
|
78 |
} |
79 |
copyFileToFile(oldFile, destFile, 95);
|
80 |
writeXML(inLayer.getName(), destFile.getName()); |
81 |
} |
82 |
} |
83 |
|
84 |
} catch (Exception e) { |
85 |
// TODO Auto-generated catch block
|
86 |
e.printStackTrace(); |
87 |
} finally {
|
88 |
closeXML(); |
89 |
reportToEnd(); |
90 |
} |
91 |
} |
92 |
|
93 |
|
94 |
/**
|
95 |
* Searches the WLD file associated with the layer
|
96 |
*
|
97 |
* @param img_file
|
98 |
* @return
|
99 |
*/
|
100 |
private File findWldFile(File img_file) { |
101 |
|
102 |
String img_path = img_file.getAbsolutePath();
|
103 |
String img_name = img_file.getName();
|
104 |
|
105 |
int lastp = img_name.lastIndexOf("."); |
106 |
String img_name_no_ext = img_name.substring(0, lastp); |
107 |
|
108 |
String ext = img_path.substring(img_path.length() - 4, img_path.length()); |
109 |
File resp = null; |
110 |
|
111 |
if ((ext.compareToIgnoreCase(".jpg") == 0) || (ext.compareToIgnoreCase("jpeg") == 0)) { |
112 |
resp = new File(img_file.getParent() + File.separator + img_name_no_ext + ".jgw"); |
113 |
if (resp.exists()) return resp; |
114 |
resp = new File(img_file.getParent() + File.separator + img_name_no_ext + ".jpw"); |
115 |
if (resp.exists()) return resp; |
116 |
resp = new File(img_file.getParent() + File.separator + img_name_no_ext + ".jpgw"); |
117 |
if (resp.exists()) return resp; |
118 |
} |
119 |
|
120 |
if ((ext.compareToIgnoreCase(".tif") == 0) || (ext.compareToIgnoreCase("tiff") == 0)) { |
121 |
resp = new File(img_file.getParent() + File.separator + img_name_no_ext + ".tfw"); |
122 |
if (resp.exists()) return resp; |
123 |
} |
124 |
|
125 |
resp = new File(img_file.getParent() + File.separator + img_name_no_ext + ".wld"); |
126 |
if (resp.exists()) return resp; |
127 |
return null; |
128 |
} |
129 |
|
130 |
/**
|
131 |
* Copies the in file to the outdir directory
|
132 |
*
|
133 |
* @param in
|
134 |
* @param outdir
|
135 |
* @param numStepsToReport the copy will report this number of steps
|
136 |
* @throws Exception
|
137 |
*/
|
138 |
public void copyFileToDir(File in, File outdir, int numStepsToReport) throws Exception { |
139 |
|
140 |
File f = new File(outdir.getAbsolutePath() + File.separator + in.getName()); |
141 |
|
142 |
copyFileToFile(in, f, numStepsToReport); |
143 |
} |
144 |
/**
|
145 |
* Copies the in file data to the outFile file
|
146 |
*
|
147 |
* @param in
|
148 |
* @param outFile
|
149 |
* @param numStepsToReport the copy will report this number of steps
|
150 |
* @throws Exception
|
151 |
*/
|
152 |
public void copyFileToFile(File in, File outFile, int numStepsToReport) throws Exception { |
153 |
|
154 |
File f = outFile;
|
155 |
|
156 |
long flenght = in.length();
|
157 |
int currStep = 0; |
158 |
long bytesPerStep = flenght / numStepsToReport;
|
159 |
long bytesCopied = 0; |
160 |
|
161 |
FileInputStream fis = new FileInputStream(in); |
162 |
FileOutputStream fos = new FileOutputStream(f); |
163 |
byte[] buf = new byte[1024]; |
164 |
int i = 0; |
165 |
while ((i = fis.read(buf)) != -1) { |
166 |
fos.write(buf, 0, i);
|
167 |
bytesCopied+=i; |
168 |
int newStep = (int)(bytesCopied / bytesPerStep); |
169 |
if (newStep>currStep){
|
170 |
reportSteps(newStep-currStep); |
171 |
currStep=newStep; |
172 |
} |
173 |
} |
174 |
fis.close(); |
175 |
fos.close(); |
176 |
} |
177 |
|
178 |
|
179 |
/**
|
180 |
* Number of steps this subtask report
|
181 |
*/
|
182 |
public int getFinalStep() { |
183 |
return 100; |
184 |
} |
185 |
|
186 |
|
187 |
/**
|
188 |
* runs the exportation
|
189 |
*/
|
190 |
public void run() { |
191 |
export();
|
192 |
} |
193 |
|
194 |
/**
|
195 |
* Writes the layer attributes on the gvsig mobile xml project file
|
196 |
*
|
197 |
* @param name
|
198 |
* @param path
|
199 |
*/
|
200 |
private void writeXML(String name, String path){ |
201 |
//<Type>
|
202 |
xml.writeTag(XmlProjectTags.TYPE, "RASTER");
|
203 |
//</Type>
|
204 |
|
205 |
//<Path>
|
206 |
xml.writeTag(XmlProjectTags.PATH, path); |
207 |
//</Path>
|
208 |
|
209 |
//<Name>
|
210 |
xml.writeTag(XmlProjectTags.NAME, name); |
211 |
//</Name>
|
212 |
} |
213 |
|
214 |
} |