root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / it / falciano / sextante / polygonize / PolygonizeAlgorithm.java @ 88
History | View | Annotate | Download (5.14 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 |
int errorcount = 0; |
73 |
|
74 |
while (iter.hasNext()) {
|
75 |
final IFeature feature = iter.next();
|
76 |
final Geometry geom = feature.getGeometry();
|
77 |
// Converting MultiLineString to Linestring
|
78 |
if (MultiLineString.class.isAssignableFrom(geom.getClass())) {
|
79 |
final MultiLineString mls = (MultiLineString) geom;
|
80 |
final int N = mls.getNumGeometries(); |
81 |
for (int j = 0; j < N; j++) { |
82 |
cLineStrings.add((LineString) mls.getGeometryN(j)); |
83 |
k++; |
84 |
} |
85 |
} else {
|
86 |
if(geom instanceof LineString){ |
87 |
cLineStrings.add((LineString)geom); |
88 |
} else {
|
89 |
if (errorcount < 10) { |
90 |
Sextante.addWarningToLog("Polygonize Algorithm require a LineString, found at " + i + " a " |
91 |
+ geom.getClass().getName()); |
92 |
errorcount++; |
93 |
} |
94 |
} |
95 |
|
96 |
} |
97 |
i++; |
98 |
} |
99 |
|
100 |
try {
|
101 |
// Noding a set of linestrings
|
102 |
final GeometryFactory factory = new GeometryFactory(); |
103 |
final GeometryCollection geometryCollection = (GeometryCollection) factory.buildGeometry(cLineStrings);
|
104 |
Geometry nodedLineStrings = (geometryCollection).getGeometryN(0);
|
105 |
|
106 |
// Union of noded linestrings
|
107 |
for (int j = 1; j < cLineStrings.size(); j++) { |
108 |
// here there could be a TopologyException, so I enhance the precision!!!
|
109 |
final LineString newLineString = ((ArrayList<LineString>) cLineStrings).get(j); |
110 |
nodedLineStrings = EnhancedPrecisionOp.union(nodedLineStrings, newLineString); |
111 |
setProgress(j, cLineStrings.size()); |
112 |
} |
113 |
|
114 |
// Polygonization
|
115 |
final Polygonizer polygonizer = new Polygonizer(); |
116 |
polygonizer.add(nodedLineStrings); |
117 |
final Collection<Polygon> cPolygons = polygonizer.getPolygons(); |
118 |
|
119 |
// Preparing the output(s)
|
120 |
final Class[] outputFieldTypes = new Class[1]; |
121 |
final String[] outputFieldNames = new String[1]; |
122 |
outputFieldTypes[0] = Integer.class; |
123 |
outputFieldNames[0] = "ID"; |
124 |
|
125 |
m_Output = getNewVectorLayer(RESULT, m_Layer.getName(), IVectorLayer.SHAPE_TYPE_POLYGON, outputFieldTypes, |
126 |
outputFieldNames); |
127 |
|
128 |
final Polygon[] aPolygons = cPolygons.toArray(new Polygon[cPolygons.size()]); |
129 |
final Integer[] outputValues = new Integer[1]; |
130 |
|
131 |
for (int k1 = 0; k1 < aPolygons.length; k1++) { |
132 |
outputValues[0] = k1;
|
133 |
m_Output.addFeature(aPolygons[k1].getGeometryN(0), outputValues);
|
134 |
} |
135 |
} |
136 |
catch (final TopologyException e) { |
137 |
Sextante.addErrorToLog(e); |
138 |
} |
139 |
|
140 |
return !m_Task.isCanceled();
|
141 |
|
142 |
} |
143 |
|
144 |
} |