root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / it / falciano / sextante / polygonize / PolygonizeAlgorithm.java @ 59
History | View | Annotate | Download (4.78 KB)
1 |
|
---|---|
2 |
|
3 |
package it.falciano.sextante.polygonize; |
4 |
|
5 |
import java.util.ArrayList; |
6 |
import java.util.Collection; |
7 |
|
8 |
import com.vividsolutions.jts.geom.Geometry; |
9 |
import com.vividsolutions.jts.geom.GeometryCollection; |
10 |
import com.vividsolutions.jts.geom.GeometryFactory; |
11 |
import com.vividsolutions.jts.geom.LineString; |
12 |
import com.vividsolutions.jts.geom.MultiLineString; |
13 |
import com.vividsolutions.jts.geom.Polygon; |
14 |
import com.vividsolutions.jts.geom.TopologyException; |
15 |
import com.vividsolutions.jts.operation.polygonize.Polygonizer; |
16 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
17 |
|
18 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
19 |
import es.unex.sextante.core.GeoAlgorithm; |
20 |
import es.unex.sextante.core.Sextante; |
21 |
import es.unex.sextante.dataObjects.IFeature; |
22 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
23 |
import es.unex.sextante.dataObjects.IVectorLayer; |
24 |
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
25 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
26 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
27 |
import es.unex.sextante.outputs.OutputVectorLayer; |
28 |
|
29 |
|
30 |
public class PolygonizeAlgorithm |
31 |
extends
|
32 |
GeoAlgorithm { |
33 |
|
34 |
public static final String LAYER = "LAYER"; |
35 |
public static final String RESULT = "RESULT"; |
36 |
|
37 |
private IVectorLayer m_Layer;
|
38 |
private IVectorLayer m_Output;
|
39 |
|
40 |
|
41 |
@Override
|
42 |
public void defineCharacteristics() { |
43 |
|
44 |
setName(Sextante.getText("Polygonize"));
|
45 |
setGroup(Sextante.getText("Tools_for_line_layers"));
|
46 |
setUserCanDefineAnalysisExtent(true);
|
47 |
|
48 |
try {
|
49 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Polylines"), AdditionalInfoVectorLayer.SHAPE_TYPE_LINE, true); |
50 |
addOutputVectorLayer(RESULT, Sextante.getText("Polygons"), OutputVectorLayer.SHAPE_TYPE_POLYGON);
|
51 |
} |
52 |
catch (final RepeatedParameterNameException e) { |
53 |
Sextante.addErrorToLog(e); |
54 |
} |
55 |
|
56 |
} |
57 |
|
58 |
|
59 |
@Override
|
60 |
@SuppressWarnings("unchecked") |
61 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
62 |
|
63 |
m_Layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
64 |
if (!m_bIsAutoExtent) {
|
65 |
m_Layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
66 |
} |
67 |
|
68 |
final IFeatureIterator iter = m_Layer.iterator();
|
69 |
int i = 0; |
70 |
int k = 0; |
71 |
final Collection<LineString> cLineStrings = new ArrayList<LineString>(); |
72 |
|
73 |
// Converting MultiLineString to Linestring
|
74 |
while (iter.hasNext()) {
|
75 |
final IFeature feature = iter.next();
|
76 |
final Geometry geom = feature.getGeometry();
|
77 |
if (MultiLineString.class.isAssignableFrom(geom.getClass())) {
|
78 |
final MultiLineString mls = (MultiLineString) geom;
|
79 |
final int N = mls.getNumGeometries(); |
80 |
for (int j = 0; j < N; j++) { |
81 |
cLineStrings.add((LineString) mls.getGeometryN(j)); |
82 |
k++; |
83 |
} |
84 |
} |
85 |
i++; |
86 |
} |
87 |
|
88 |
try {
|
89 |
// Noding a set of linestrings
|
90 |
final GeometryFactory factory = new GeometryFactory(); |
91 |
final GeometryCollection geometryCollection = (GeometryCollection) factory.buildGeometry(cLineStrings);
|
92 |
Geometry nodedLineStrings = (geometryCollection).getGeometryN(0);
|
93 |
|
94 |
// Union of noded linestrings
|
95 |
for (int j = 1; j < cLineStrings.size(); j++) { |
96 |
// here there could be a TopologyException, so I enhance the precision!!!
|
97 |
final LineString newLineString = ((ArrayList<LineString>) cLineStrings).get(j); |
98 |
nodedLineStrings = EnhancedPrecisionOp.union(nodedLineStrings, newLineString); |
99 |
setProgress(j, cLineStrings.size()); |
100 |
} |
101 |
|
102 |
// Polygonization
|
103 |
final Polygonizer polygonizer = new Polygonizer(); |
104 |
polygonizer.add(nodedLineStrings); |
105 |
final Collection<Polygon> cPolygons = polygonizer.getPolygons(); |
106 |
|
107 |
// Preparing the output(s)
|
108 |
final Class[] outputFieldTypes = new Class[1]; |
109 |
final String[] outputFieldNames = new String[1]; |
110 |
outputFieldTypes[0] = Integer.class; |
111 |
outputFieldNames[0] = "ID"; |
112 |
|
113 |
m_Output = getNewVectorLayer(RESULT, m_Layer.getName(), IVectorLayer.SHAPE_TYPE_POLYGON, outputFieldTypes, |
114 |
outputFieldNames); |
115 |
|
116 |
final Polygon[] aPolygons = cPolygons.toArray(new Polygon[cPolygons.size()]); |
117 |
final Integer[] outputValues = new Integer[1]; |
118 |
|
119 |
for (int k1 = 0; k1 < aPolygons.length; k1++) { |
120 |
outputValues[0] = k1;
|
121 |
m_Output.addFeature(aPolygons[k1].getGeometryN(0), outputValues);
|
122 |
} |
123 |
} |
124 |
catch (final TopologyException e) { |
125 |
Sextante.addErrorToLog(e); |
126 |
} |
127 |
|
128 |
return !m_Task.isCanceled();
|
129 |
|
130 |
} |
131 |
|
132 |
} |