root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / voronoi / VoronoiAlgorithm.java @ 59
History | View | Annotate | Download (3.22 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.vectorTools.voronoi; |
4 |
|
5 |
import java.util.ArrayList; |
6 |
|
7 |
import com.vividsolutions.jts.geom.Geometry; |
8 |
import com.vividsolutions.jts.geom.GeometryFactory; |
9 |
import com.vividsolutions.jts.triangulate.VoronoiDiagramBuilder; |
10 |
|
11 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
12 |
import es.unex.sextante.core.GeoAlgorithm; |
13 |
import es.unex.sextante.core.Sextante; |
14 |
import es.unex.sextante.dataObjects.IFeature; |
15 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
16 |
import es.unex.sextante.dataObjects.IVectorLayer; |
17 |
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
18 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
19 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
20 |
import es.unex.sextante.outputs.OutputVectorLayer; |
21 |
|
22 |
|
23 |
public class VoronoiAlgorithm |
24 |
extends
|
25 |
GeoAlgorithm { |
26 |
|
27 |
public static final String POINTS = "POINTS"; |
28 |
public static final String TRIANGLES = "TRIANGLES"; |
29 |
|
30 |
private IVectorLayer m_Points;
|
31 |
private IVectorLayer m_Voronoi;
|
32 |
|
33 |
|
34 |
@Override
|
35 |
public void defineCharacteristics() { |
36 |
|
37 |
setName(Sextante.getText("Voronoi_polygons"));
|
38 |
setGroup(Sextante.getText("Tools_for_point_layers"));
|
39 |
setUserCanDefineAnalysisExtent(true);
|
40 |
|
41 |
try {
|
42 |
m_Parameters.addInputVectorLayer(POINTS, Sextante.getText("Points_layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_POINT,
|
43 |
true);
|
44 |
|
45 |
addOutputVectorLayer(TRIANGLES, Sextante.getText("Voronoi_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 |
final int iShapeCount; |
59 |
|
60 |
m_Points = m_Parameters.getParameterValueAsVectorLayer(POINTS); |
61 |
if (!m_bIsAutoExtent) {
|
62 |
m_Points.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
63 |
} |
64 |
|
65 |
final Class types[] = { Integer.class }; |
66 |
final String sNames[] = { "ID" }; |
67 |
m_Voronoi = getNewVectorLayer(TRIANGLES, Sextante.getText("Voronoi_polygons"), IVectorLayer.SHAPE_TYPE_POLYGON, types,
|
68 |
sNames); |
69 |
|
70 |
i = 0;
|
71 |
final ArrayList<Geometry> list = new ArrayList<Geometry>(); |
72 |
iShapeCount = m_Points.getShapesCount(); |
73 |
final IFeatureIterator iter = m_Points.iterator();
|
74 |
while (iter.hasNext() && setProgress(i, iShapeCount)) {
|
75 |
final IFeature feature = iter.next();
|
76 |
list.add(feature.getGeometry()); |
77 |
i++; |
78 |
} |
79 |
iter.close(); |
80 |
|
81 |
m_Task.setDeterminate(false);
|
82 |
final GeometryFactory gf = new GeometryFactory(); |
83 |
final Geometry geomcol = gf.createGeometryCollection(list.toArray(new Geometry[0])); |
84 |
final VoronoiDiagramBuilder voronoi = new VoronoiDiagramBuilder(); |
85 |
voronoi.setSites(geomcol); |
86 |
final Geometry polygons = voronoi.getDiagram(gf);
|
87 |
|
88 |
final int iPolygons = polygons.getNumGeometries(); |
89 |
for (int iPolygon = 0; iPolygon < iPolygons; iPolygon++) { |
90 |
final Geometry triangle = polygons.getGeometryN(iPolygon);
|
91 |
final Object[] record = { new Integer(iPolygon) }; |
92 |
m_Voronoi.addFeature(triangle, record); |
93 |
} |
94 |
|
95 |
return !m_Task.isCanceled();
|
96 |
|
97 |
} |
98 |
|
99 |
} |