root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / polylinesToPolygons / PolylinesToPolygonsAlgorithm.java @ 59
History | View | Annotate | Download (3.58 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.vectorTools.polylinesToPolygons; |
4 |
|
5 |
import java.util.ArrayList; |
6 |
|
7 |
import com.vividsolutions.jts.geom.Coordinate; |
8 |
import com.vividsolutions.jts.geom.Geometry; |
9 |
import com.vividsolutions.jts.geom.GeometryFactory; |
10 |
import com.vividsolutions.jts.geom.LinearRing; |
11 |
import com.vividsolutions.jts.geom.Polygon; |
12 |
|
13 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
14 |
import es.unex.sextante.core.GeoAlgorithm; |
15 |
import es.unex.sextante.core.Sextante; |
16 |
import es.unex.sextante.dataObjects.IFeature; |
17 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
18 |
import es.unex.sextante.dataObjects.IVectorLayer; |
19 |
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
20 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
21 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
22 |
import es.unex.sextante.outputs.OutputVectorLayer; |
23 |
|
24 |
|
25 |
public class PolylinesToPolygonsAlgorithm |
26 |
extends
|
27 |
GeoAlgorithm { |
28 |
|
29 |
public static final String LAYER = "LAYER"; |
30 |
public static final String RESULT = "RESULT"; |
31 |
|
32 |
private IVectorLayer m_Layer;
|
33 |
private IVectorLayer m_Output;
|
34 |
|
35 |
|
36 |
@Override
|
37 |
public void defineCharacteristics() { |
38 |
|
39 |
setName(Sextante.getText("Polylines_to_polygons"));
|
40 |
setGroup(Sextante.getText("Tools_for_line_layers"));
|
41 |
setUserCanDefineAnalysisExtent(true);
|
42 |
|
43 |
try {
|
44 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Polylines"), AdditionalInfoVectorLayer.SHAPE_TYPE_LINE, true); |
45 |
addOutputVectorLayer(RESULT, Sextante.getText("Polygons"), OutputVectorLayer.SHAPE_TYPE_POLYGON);
|
46 |
} |
47 |
catch (final RepeatedParameterNameException e) { |
48 |
Sextante.addErrorToLog(e); |
49 |
} |
50 |
|
51 |
} |
52 |
|
53 |
|
54 |
@Override
|
55 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
56 |
|
57 |
int i;
|
58 |
int iShapeCount;
|
59 |
|
60 |
m_Layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
61 |
if (!m_bIsAutoExtent) {
|
62 |
m_Layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
63 |
} |
64 |
|
65 |
m_Output = getNewVectorLayer(RESULT, m_Layer.getName(), IVectorLayer.SHAPE_TYPE_POLYGON, m_Layer.getFieldTypes(), |
66 |
m_Layer.getFieldNames()); |
67 |
|
68 |
|
69 |
final ArrayList array = new ArrayList(); |
70 |
final GeometryFactory gf = new GeometryFactory(); |
71 |
iShapeCount = m_Layer.getShapesCount(); |
72 |
final IFeatureIterator iter = m_Layer.iterator();
|
73 |
i = 0;
|
74 |
while (iter.hasNext() && setProgress(i, iShapeCount)) {
|
75 |
array.clear(); |
76 |
final IFeature feature = iter.next();
|
77 |
final Geometry geom = feature.getGeometry();
|
78 |
for (int j = 0; j < geom.getNumGeometries(); j++) { |
79 |
final Geometry subgeom = geom.getGeometryN(j);
|
80 |
final Coordinate[] lineCoords = subgeom.getCoordinates(); |
81 |
final Coordinate[] ringCoords = new Coordinate[lineCoords.length + 1]; |
82 |
System.arraycopy(lineCoords, 0, ringCoords, 0, lineCoords.length); |
83 |
ringCoords[lineCoords.length] = new Coordinate(lineCoords[0].x, lineCoords[0].y); |
84 |
final LinearRing ring = gf.createLinearRing(ringCoords);
|
85 |
final Polygon polygon = gf.createPolygon(ring, null); |
86 |
array.add(polygon); |
87 |
} |
88 |
final Polygon[] polygons = new Polygon[array.size()]; |
89 |
for (int j = 0; j < array.size(); j++) { |
90 |
polygons[j] = (Polygon) array.get(j);
|
91 |
} |
92 |
m_Output.addFeature(gf.createMultiPolygon(polygons), feature.getRecord().getValues()); |
93 |
i++; |
94 |
} |
95 |
|
96 |
return !m_Task.isCanceled();
|
97 |
|
98 |
} |
99 |
|
100 |
|
101 |
} |