root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / pointAnalysis / meanCenter / MeanCenterAlgorithm.java @ 59
History | View | Annotate | Download (5.52 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.pointAnalysis.meanCenter; |
4 |
|
5 |
import com.vividsolutions.jts.geom.Coordinate; |
6 |
import com.vividsolutions.jts.geom.Geometry; |
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.OptionalParentParameterException; |
17 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
18 |
import es.unex.sextante.exceptions.UndefinedParentParameterNameException; |
19 |
import es.unex.sextante.outputs.OutputVectorLayer; |
20 |
import es.unex.sextante.shapesTools.ShapesTools; |
21 |
|
22 |
|
23 |
public class MeanCenterAlgorithm |
24 |
extends
|
25 |
GeoAlgorithm { |
26 |
|
27 |
private static final int METHOD_WEIGHTED = 1; |
28 |
private static final int METHOD_NOT_WEIGHTED = 0; |
29 |
|
30 |
private static final String POINTS = "POINTS"; |
31 |
private static final String FIELD = "FIELDS"; |
32 |
private static final String METHOD = "METHOD"; |
33 |
private static final String RESULT = "RESULT"; |
34 |
|
35 |
|
36 |
@Override
|
37 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
38 |
|
39 |
int i;
|
40 |
int iField;
|
41 |
int iMethod;
|
42 |
double x, y;
|
43 |
double dSumX = 0, dSumY = 0; |
44 |
double dWeight, dSumWeight = 0; |
45 |
double xCenter, yCenter;
|
46 |
double dDifX, dDifY, dDifDist = 0; |
47 |
int iCount;
|
48 |
|
49 |
final IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(POINTS);
|
50 |
if (!m_bIsAutoExtent) {
|
51 |
layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
52 |
} |
53 |
|
54 |
iMethod = m_Parameters.getParameterValueAsInt(METHOD); |
55 |
iField = m_Parameters.getParameterValueAsInt(FIELD); |
56 |
iCount = layer.getShapesCount(); |
57 |
if (iCount == 0) { |
58 |
throw new GeoAlgorithmExecutionException("0 points in layer"); |
59 |
} |
60 |
|
61 |
i = 0;
|
62 |
IFeatureIterator iter = layer.iterator(); |
63 |
while (iter.hasNext() && setProgress(i, iCount * 2)) { |
64 |
final IFeature feature = iter.next();
|
65 |
final Geometry geom = feature.getGeometry();
|
66 |
final Coordinate coord = geom.getCoordinate();
|
67 |
x = coord.x; |
68 |
y = coord.y; |
69 |
if (iMethod == METHOD_WEIGHTED) {
|
70 |
try {
|
71 |
dWeight = Double.parseDouble(feature.getRecord().getValue(iField).toString());
|
72 |
} |
73 |
catch (final Exception e) { |
74 |
dWeight = 1;
|
75 |
} |
76 |
} |
77 |
else {
|
78 |
dWeight = 1;
|
79 |
} |
80 |
dSumWeight += dWeight; |
81 |
dSumX += (dWeight * x); |
82 |
dSumY += (dWeight * y); |
83 |
i++; |
84 |
} |
85 |
iter.close(); |
86 |
|
87 |
xCenter = dSumX / dSumWeight; |
88 |
yCenter = dSumY / dSumWeight; |
89 |
dSumWeight = 0;
|
90 |
|
91 |
iter = layer.iterator(); |
92 |
while (iter.hasNext() && setProgress(i, iCount * 2)) { |
93 |
final IFeature feature = iter.next();
|
94 |
final Geometry geom = feature.getGeometry();
|
95 |
final Coordinate coord = geom.getCoordinate();
|
96 |
x = coord.x; |
97 |
y = coord.y; |
98 |
if (iMethod == METHOD_WEIGHTED) {
|
99 |
try {
|
100 |
dWeight = Double.parseDouble(feature.getRecord().getValue(iField).toString());
|
101 |
} |
102 |
catch (final Exception e) { |
103 |
dWeight = 1;
|
104 |
} |
105 |
} |
106 |
else {
|
107 |
dWeight = 1;
|
108 |
} |
109 |
dSumWeight += dWeight; |
110 |
dDifX = (x - xCenter); |
111 |
dDifY = (y - yCenter); |
112 |
dDifDist += ((dDifX * dDifX * dWeight) + (dDifY * dDifY * dWeight)); |
113 |
i++; |
114 |
} |
115 |
iter.close(); |
116 |
|
117 |
final double dStdDist = Math.sqrt(dDifDist / dSumWeight); |
118 |
final IVectorLayer output = getNewVectorLayer(RESULT, Sextante.getText("Standard_distance"), |
119 |
IVectorLayer.SHAPE_TYPE_POLYGON, new Class[] { Double.class }, |
120 |
new String[] { Sextante.getText("Radius__standard_deviation") }); |
121 |
final Object[] value = new Object[1]; |
122 |
value[0] = new Double(dStdDist); |
123 |
|
124 |
final Geometry circle = ShapesTools.createCircle(xCenter, yCenter, dStdDist);
|
125 |
output.addFeature(circle, value); |
126 |
|
127 |
return !m_Task.isCanceled();
|
128 |
|
129 |
} |
130 |
|
131 |
|
132 |
@Override
|
133 |
public void defineCharacteristics() { |
134 |
|
135 |
final String sOptions[] = { Sextante.getText("Mean_center"), Sextante.getText("Weighted_mean_center") }; |
136 |
|
137 |
setName(Sextante.getText("Mean_center_and_standard_distance"));
|
138 |
setGroup(Sextante.getText("Tools_for_point_layers"));
|
139 |
setUserCanDefineAnalysisExtent(true);
|
140 |
try {
|
141 |
m_Parameters.addInputVectorLayer(POINTS, Sextante.getText("Points"), AdditionalInfoVectorLayer.SHAPE_TYPE_POINT, true); |
142 |
m_Parameters.addTableField(FIELD, Sextante.getText("Weights"), "POINTS"); |
143 |
m_Parameters.addSelection(METHOD, Sextante.getText("Method"), sOptions);
|
144 |
addOutputVectorLayer(RESULT, Sextante.getText("Mean_center_and_standard_distance"), OutputVectorLayer.SHAPE_TYPE_POLYGON);
|
145 |
} |
146 |
catch (final RepeatedParameterNameException e) { |
147 |
Sextante.addErrorToLog(e); |
148 |
} |
149 |
catch (final UndefinedParentParameterNameException e) { |
150 |
Sextante.addErrorToLog(e); |
151 |
} |
152 |
catch (final OptionalParentParameterException e) { |
153 |
Sextante.addErrorToLog(e); |
154 |
} |
155 |
|
156 |
} |
157 |
|
158 |
} |