root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / saveToWKT / SaveToWKTAlgorithm.java @ 59
History | View | Annotate | Download (3.52 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.vectorTools.saveToWKT; |
4 |
|
5 |
|
6 |
import java.io.BufferedWriter; |
7 |
import java.io.FileWriter; |
8 |
import java.io.IOException; |
9 |
import java.io.Writer; |
10 |
|
11 |
import com.vividsolutions.jts.geom.Geometry; |
12 |
import com.vividsolutions.jts.io.WKTWriter; |
13 |
|
14 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
15 |
import es.unex.sextante.core.GeoAlgorithm; |
16 |
import es.unex.sextante.core.Sextante; |
17 |
import es.unex.sextante.dataObjects.IFeature; |
18 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
19 |
import es.unex.sextante.dataObjects.IVectorLayer; |
20 |
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
21 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
22 |
import es.unex.sextante.exceptions.IteratorException; |
23 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
24 |
|
25 |
|
26 |
public class SaveToWKTAlgorithm |
27 |
extends
|
28 |
GeoAlgorithm { |
29 |
|
30 |
public static final String LAYER = "LAYER"; |
31 |
public static final String RESULT = "RESULT"; |
32 |
public static final String FILENAME = "FILENAME"; |
33 |
|
34 |
|
35 |
@Override
|
36 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
37 |
|
38 |
final IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
|
39 |
final String sFilename = m_Parameters.getParameterValueAsString(FILENAME); |
40 |
|
41 |
if (!m_bIsAutoExtent) {
|
42 |
layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
43 |
} |
44 |
|
45 |
if (sFilename != null) { |
46 |
Writer output = null; |
47 |
final WKTWriter wkt = new WKTWriter(); |
48 |
try {
|
49 |
output = new BufferedWriter(new FileWriter(sFilename)); |
50 |
final int iShapeCount = layer.getShapesCount(); |
51 |
int i = 0; |
52 |
final IFeatureIterator iter = layer.iterator();
|
53 |
while (iter.hasNext() && setProgress(i, iShapeCount)) {
|
54 |
try {
|
55 |
final IFeature feature = iter.next();
|
56 |
final Geometry geom = feature.getGeometry();
|
57 |
output.write("WKT=" + wkt.write(geom) + "\n"); |
58 |
final int iFieldCount = feature.getRecord().getValues().length; |
59 |
for (int j = 0; j < iFieldCount; j++) { |
60 |
//output.write(feature.getRecord().getValue(j).toString() + "\n");
|
61 |
} |
62 |
i++; |
63 |
} |
64 |
catch (final IteratorException e) { |
65 |
|
66 |
} |
67 |
} |
68 |
} |
69 |
catch (final IOException e) { |
70 |
throw new GeoAlgorithmExecutionException(e.getMessage()); |
71 |
} |
72 |
finally {
|
73 |
if (output != null) { |
74 |
try {
|
75 |
output.close(); |
76 |
} |
77 |
catch (final IOException e) { |
78 |
Sextante.addErrorToLog(e); |
79 |
} |
80 |
} |
81 |
} |
82 |
|
83 |
} |
84 |
|
85 |
return !m_Task.isCanceled();
|
86 |
|
87 |
} |
88 |
|
89 |
|
90 |
@Override
|
91 |
public void defineCharacteristics() { |
92 |
|
93 |
setName(Sextante.getText("Save_geometries_as_WKT"));
|
94 |
setGroup(Sextante.getText("Tools_for_vector_layers"));
|
95 |
setUserCanDefineAnalysisExtent(true);
|
96 |
|
97 |
try {
|
98 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true); |
99 |
m_Parameters.addFilepath(FILENAME, Sextante.getText("File"), false, false, false, "wkt"); |
100 |
} |
101 |
catch (final RepeatedParameterNameException e) { |
102 |
Sextante.addErrorToLog(e); |
103 |
} |
104 |
|
105 |
} |
106 |
|
107 |
|
108 |
@Override
|
109 |
public boolean isSuitableForModelling() { |
110 |
|
111 |
return false; |
112 |
|
113 |
} |
114 |
|
115 |
} |