root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / perturbatePointsLayer / PerturbatePointsLayerAlgorithm.java @ 59
History | View | Annotate | Download (4.31 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.vectorTools.perturbatePointsLayer; |
4 |
|
5 |
import com.vividsolutions.jts.geom.Coordinate; |
6 |
import com.vividsolutions.jts.geom.Geometry; |
7 |
import com.vividsolutions.jts.geom.GeometryFactory; |
8 |
import com.vividsolutions.jts.geom.Point; |
9 |
|
10 |
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue; |
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 PerturbatePointsLayerAlgorithm |
24 |
extends
|
25 |
GeoAlgorithm { |
26 |
|
27 |
public static final String LAYER = "LAYER"; |
28 |
public static final String MEAN = "MEAN"; |
29 |
public static final String STDDEV = "STDDEV"; |
30 |
public static final String RESULT = "RESULT"; |
31 |
|
32 |
private IVectorLayer m_Layer;
|
33 |
private IVectorLayer m_Output;
|
34 |
private double m_dMean; |
35 |
private double m_dStdDev; |
36 |
|
37 |
|
38 |
@Override
|
39 |
public void defineCharacteristics() { |
40 |
|
41 |
setName(Sextante.getText("Perturbate_points_layer"));
|
42 |
setGroup(Sextante.getText("Tools_for_point_layers"));
|
43 |
setUserCanDefineAnalysisExtent(true);
|
44 |
|
45 |
try {
|
46 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Points_layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_POINT,
|
47 |
true);
|
48 |
m_Parameters.addNumericalValue(MEAN, Sextante.getText("Mean"), AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 10, |
49 |
0, Double.MAX_VALUE); |
50 |
m_Parameters.addNumericalValue(STDDEV, Sextante.getText("Standard_deviation"),
|
51 |
AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 1, 0, Double.MAX_VALUE); |
52 |
|
53 |
addOutputVectorLayer(RESULT, Sextante.getText("Result"), OutputVectorLayer.SHAPE_TYPE_POINT);
|
54 |
} |
55 |
catch (final RepeatedParameterNameException e) { |
56 |
Sextante.addErrorToLog(e); |
57 |
} |
58 |
|
59 |
} |
60 |
|
61 |
|
62 |
@Override
|
63 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
64 |
|
65 |
int i;
|
66 |
int iShapeCount;
|
67 |
|
68 |
m_Layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
69 |
m_dMean = m_Parameters.getParameterValueAsDouble(MEAN); |
70 |
m_dStdDev = m_Parameters.getParameterValueAsDouble(STDDEV); |
71 |
|
72 |
if (!m_bIsAutoExtent) {
|
73 |
m_Layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
74 |
} |
75 |
|
76 |
m_Output = getNewVectorLayer(RESULT, m_Layer.getName(), m_Layer.getShapeType(), m_Layer.getFieldTypes(), |
77 |
m_Layer.getFieldNames()); |
78 |
|
79 |
i = 0;
|
80 |
iShapeCount = m_Layer.getShapesCount(); |
81 |
final IFeatureIterator iter = m_Layer.iterator();
|
82 |
while (iter.hasNext() && setProgress(i, iShapeCount)) {
|
83 |
final IFeature feature = iter.next();
|
84 |
final Geometry geom = feature.getGeometry();
|
85 |
final Coordinate coord = geom.getCoordinate();
|
86 |
addPoint(coord.x, coord.y, feature.getRecord().getValues()); |
87 |
i++; |
88 |
} |
89 |
|
90 |
iter.close(); |
91 |
|
92 |
return !m_Task.isCanceled();
|
93 |
|
94 |
} |
95 |
|
96 |
|
97 |
private void addPoint(final double x, |
98 |
final double y, |
99 |
final Object[] values) { |
100 |
|
101 |
final Point pt = getAlteredPoint(x, y); |
102 |
m_Output.addFeature(pt, values); |
103 |
|
104 |
} |
105 |
|
106 |
|
107 |
private Point getAlteredPoint(final double x, |
108 |
final double y) { |
109 |
|
110 |
final GeometryFactory gf = new GeometryFactory(); |
111 |
|
112 |
final double dDist = getDist(); |
113 |
final double dAngle = Math.random() * Math.PI * 2.; |
114 |
|
115 |
return gf.createPoint(new Coordinate(x + Math.cos(dAngle) * dDist, y + Math.sin(dAngle) * dDist)); |
116 |
|
117 |
} |
118 |
|
119 |
|
120 |
double getDist() {
|
121 |
|
122 |
|
123 |
double x1, x2, w, y1;
|
124 |
|
125 |
do {
|
126 |
x1 = 2.0 * Math.random() - 1.0; |
127 |
x2 = 2.0 * Math.random() - 1.0; |
128 |
|
129 |
w = x1 * x1 + x2 * x2; |
130 |
} |
131 |
while (w >= 1.0); |
132 |
|
133 |
w = Math.sqrt((-2.0 * Math.log(w)) / w); |
134 |
|
135 |
y1 = x1 * w; |
136 |
|
137 |
return (m_dMean + m_dStdDev * y1);
|
138 |
|
139 |
} |
140 |
|
141 |
} |