root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / simplifyPolygons / SimplifyPolygonsAlgorithm.java @ 59
History | View | Annotate | Download (3.55 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.vectorTools.simplifyPolygons; |
4 |
|
5 |
import com.vividsolutions.jts.geom.Geometry; |
6 |
import com.vividsolutions.jts.simplify.DouglasPeuckerSimplifier; |
7 |
import com.vividsolutions.jts.simplify.TopologyPreservingSimplifier; |
8 |
|
9 |
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue; |
10 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
11 |
import es.unex.sextante.core.GeoAlgorithm; |
12 |
import es.unex.sextante.core.Sextante; |
13 |
import es.unex.sextante.dataObjects.IFeature; |
14 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
15 |
import es.unex.sextante.dataObjects.IVectorLayer; |
16 |
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
17 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
18 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
19 |
import es.unex.sextante.outputs.OutputVectorLayer; |
20 |
|
21 |
|
22 |
public class SimplifyPolygonsAlgorithm |
23 |
extends
|
24 |
GeoAlgorithm { |
25 |
|
26 |
public static final String RESULT = "RESULT"; |
27 |
public static final String PRESERVE = "PRESERVE"; |
28 |
public static final String TOLERANCE = "TOLERANCE"; |
29 |
public static final String LAYER = "LAYER"; |
30 |
private boolean m_bPreserve; |
31 |
|
32 |
|
33 |
@Override
|
34 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
35 |
|
36 |
final IVectorLayer layerIn = m_Parameters.getParameterValueAsVectorLayer(LAYER);
|
37 |
final double dTolerance = m_Parameters.getParameterValueAsDouble(TOLERANCE); |
38 |
m_bPreserve = m_Parameters.getParameterValueAsBoolean(PRESERVE); |
39 |
|
40 |
if (!m_bIsAutoExtent) {
|
41 |
layerIn.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
42 |
} |
43 |
|
44 |
final IVectorLayer output = getNewVectorLayer(RESULT, Sextante.getText("Simplified_polygons"), |
45 |
IVectorLayer.SHAPE_TYPE_POLYGON, layerIn.getFieldTypes(), layerIn.getFieldNames()); |
46 |
final IFeatureIterator iter = layerIn.iterator();
|
47 |
int i = 0; |
48 |
final int iCount = layerIn.getShapesCount(); |
49 |
while (iter.hasNext() && setProgress(i, iCount)) {
|
50 |
final IFeature feature = iter.next();
|
51 |
final Geometry simpleGeom = getSimpleLine(feature.getGeometry(), dTolerance);
|
52 |
output.addFeature(simpleGeom, feature.getRecord().getValues()); |
53 |
i++; |
54 |
} |
55 |
|
56 |
|
57 |
return !m_Task.isCanceled();
|
58 |
|
59 |
} |
60 |
|
61 |
|
62 |
@Override
|
63 |
public void defineCharacteristics() { |
64 |
|
65 |
setName(Sextante.getText("Simplify_polygons"));
|
66 |
setGroup(Sextante.getText("Tools_for_polygon_layers"));
|
67 |
setUserCanDefineAnalysisExtent(true);
|
68 |
|
69 |
try {
|
70 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Polygons"), AdditionalInfoVectorLayer.SHAPE_TYPE_POLYGON, true); |
71 |
m_Parameters.addNumericalValue(TOLERANCE, Sextante.getText("Tolerance"),
|
72 |
AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 10, 0, Double.MAX_VALUE); |
73 |
m_Parameters.addBoolean(PRESERVE, Sextante.getText("Preserve_topology"), false); |
74 |
addOutputVectorLayer(RESULT, Sextante.getText("Simplified_polygons"), OutputVectorLayer.SHAPE_TYPE_POLYGON);
|
75 |
} |
76 |
catch (final RepeatedParameterNameException e) { |
77 |
Sextante.addErrorToLog(e); |
78 |
} |
79 |
|
80 |
} |
81 |
|
82 |
|
83 |
private Geometry getSimpleLine(final Geometry geometry, |
84 |
final double dTolerance) { |
85 |
|
86 |
Geometry simple; |
87 |
if (m_bPreserve) {
|
88 |
simple = TopologyPreservingSimplifier.simplify(geometry, dTolerance); |
89 |
} |
90 |
else {
|
91 |
simple = DouglasPeuckerSimplifier.simplify(geometry, dTolerance); |
92 |
} |
93 |
return simple;
|
94 |
|
95 |
} |
96 |
|
97 |
} |