root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / centroids / CentroidsAlgorithm.java @ 59
History | View | Annotate | Download (2.45 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.vectorTools.centroids; |
4 |
|
5 |
import com.vividsolutions.jts.geom.Geometry; |
6 |
import com.vividsolutions.jts.geom.Point; |
7 |
|
8 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
9 |
import es.unex.sextante.core.GeoAlgorithm; |
10 |
import es.unex.sextante.core.Sextante; |
11 |
import es.unex.sextante.dataObjects.IFeature; |
12 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
13 |
import es.unex.sextante.dataObjects.IVectorLayer; |
14 |
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
15 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
16 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
17 |
import es.unex.sextante.outputs.OutputVectorLayer; |
18 |
|
19 |
|
20 |
public class CentroidsAlgorithm |
21 |
extends
|
22 |
GeoAlgorithm { |
23 |
|
24 |
public static final String LAYER = "LAYER"; |
25 |
public static final String RESULT = "RESULT"; |
26 |
|
27 |
|
28 |
@Override
|
29 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
30 |
|
31 |
int i = 0; |
32 |
|
33 |
final IVectorLayer layerIn = m_Parameters.getParameterValueAsVectorLayer(LAYER);
|
34 |
if (!m_bIsAutoExtent) {
|
35 |
layerIn.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
36 |
} |
37 |
final IVectorLayer result = getNewVectorLayer(RESULT, Sextante.getText("Centroids"), IVectorLayer.SHAPE_TYPE_POINT, |
38 |
layerIn.getFieldTypes(), layerIn.getFieldNames()); |
39 |
|
40 |
final IFeatureIterator iter = layerIn.iterator();
|
41 |
final int iTotal = layerIn.getShapesCount(); |
42 |
while (iter.hasNext() && setProgress(i, iTotal)) {
|
43 |
final IFeature feature = iter.next();
|
44 |
final Point centroid = getCentroid(feature.getGeometry()); |
45 |
result.addFeature(centroid, feature.getRecord().getValues()); |
46 |
i++; |
47 |
} |
48 |
iter.close(); |
49 |
|
50 |
return !m_Task.isCanceled();
|
51 |
} |
52 |
|
53 |
|
54 |
@Override
|
55 |
public void defineCharacteristics() { |
56 |
|
57 |
setName(Sextante.getText("Centroids"));
|
58 |
setGroup(Sextante.getText("Tools_for_vector_layers"));
|
59 |
|
60 |
try {
|
61 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Vector_layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true); |
62 |
addOutputVectorLayer(RESULT, Sextante.getText("Centroids"), OutputVectorLayer.SHAPE_TYPE_POINT);
|
63 |
} |
64 |
catch (final RepeatedParameterNameException e) { |
65 |
Sextante.addErrorToLog(e); |
66 |
} |
67 |
|
68 |
} |
69 |
|
70 |
|
71 |
private Point getCentroid(final Geometry geometry) { |
72 |
|
73 |
return geometry.getCentroid();
|
74 |
|
75 |
} |
76 |
|
77 |
} |