root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tables / vectorFieldCalculator / VectorFieldCalculatorAlgorithm.java @ 59
History | View | Annotate | Download (6.38 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.tables.vectorFieldCalculator; |
4 |
|
5 |
import java.util.ArrayList; |
6 |
import java.util.HashMap; |
7 |
import java.util.Iterator; |
8 |
import java.util.Set; |
9 |
|
10 |
import org.nfunk.jep.JEP; |
11 |
|
12 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
13 |
import es.unex.sextante.core.GeoAlgorithm; |
14 |
import es.unex.sextante.core.Sextante; |
15 |
import es.unex.sextante.dataObjects.IFeature; |
16 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
17 |
import es.unex.sextante.dataObjects.IVectorLayer; |
18 |
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
19 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
20 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
21 |
import es.unex.sextante.gui.modeler.ModelAlgorithm; |
22 |
import es.unex.sextante.modeler.elements.ModelElementNumericalValue; |
23 |
import es.unex.sextante.outputs.IOutputChannel; |
24 |
import es.unex.sextante.outputs.OutputVectorLayer; |
25 |
import es.unex.sextante.shapesTools.ShapesTools; |
26 |
|
27 |
|
28 |
public class VectorFieldCalculatorAlgorithm |
29 |
extends
|
30 |
GeoAlgorithm { |
31 |
|
32 |
private static final Object NO_DATA = new Double(-99999.); |
33 |
|
34 |
public static final String FORMULA = "FORMULA"; |
35 |
public static final String LAYER = "LAYER"; |
36 |
public static final String RESULT = "RESULT"; |
37 |
|
38 |
|
39 |
@Override
|
40 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
41 |
|
42 |
int i, j;
|
43 |
double dValue;
|
44 |
boolean bOK;
|
45 |
String sVariable;
|
46 |
|
47 |
final Class[] types = { Double.class }; |
48 |
final String[] sFields = { Sextante.getText("Result") }; |
49 |
final IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
|
50 |
if (!m_bIsAutoExtent) {
|
51 |
layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
52 |
} |
53 |
|
54 |
final JEP jep = new JEP(); |
55 |
jep.addStandardConstants(); |
56 |
jep.addStandardFunctions(); |
57 |
|
58 |
String sFormula = m_Parameters.getParameterValueAsString(FORMULA).toLowerCase();
|
59 |
sFormula = sFormula.replaceAll(" ", ""); |
60 |
sFormula = replaceDots(sFormula); |
61 |
|
62 |
final String[] fieldNames = layer.getFieldNames(); |
63 |
final ArrayList variables = new ArrayList(); |
64 |
final ArrayList variableIndex = new ArrayList(); |
65 |
for (i = 0; i < fieldNames.length; i++) { |
66 |
sVariable = fieldNames[i].toLowerCase(); |
67 |
sVariable = sVariable.replaceAll(" ", ""); |
68 |
sVariable = replaceDots(sVariable); |
69 |
if (sFormula.lastIndexOf(sVariable) != -1) { |
70 |
jep.addVariable(sVariable, 0.0);
|
71 |
variables.add(sVariable); |
72 |
variableIndex.add(new Integer(i)); |
73 |
} |
74 |
} |
75 |
final int[] iVariables = new int[variables.size()]; |
76 |
final String[] sVariables = new String[variables.size()]; |
77 |
for (i = 0; i < variableIndex.size(); i++) { |
78 |
iVariables[i] = ((Integer) variableIndex.get(i)).intValue();
|
79 |
sVariables[i] = (String) variables.get(i);
|
80 |
} |
81 |
|
82 |
jep.parseExpression(sFormula); |
83 |
|
84 |
final int iTotalCount = layer.getShapesCount(); |
85 |
final Object[][] values = new Object[1][iTotalCount]; |
86 |
if (!jep.hasError()) {
|
87 |
final IFeatureIterator iter = layer.iterator();
|
88 |
IFeature feat; |
89 |
int iCount = 0; |
90 |
while (iter.hasNext() && setProgress(iCount, iTotalCount)) {
|
91 |
feat = iter.next(); |
92 |
bOK = true;
|
93 |
for (j = 0; (j < iVariables.length) && bOK; j++) { |
94 |
try {
|
95 |
dValue = Double.parseDouble(feat.getRecord().getValue(iVariables[j]).toString());
|
96 |
jep.addVariable(sVariables[j], dValue); |
97 |
} |
98 |
catch (final NumberFormatException nfe) { |
99 |
bOK = false;
|
100 |
} |
101 |
|
102 |
} |
103 |
if (bOK) {
|
104 |
dValue = jep.getValue(); |
105 |
values[0][iCount] = new Double(dValue); |
106 |
} |
107 |
else {
|
108 |
values[0][iCount] = NO_DATA;
|
109 |
} |
110 |
iCount++; |
111 |
} |
112 |
|
113 |
final IOutputChannel channel = getOutputChannel(RESULT);
|
114 |
final OutputVectorLayer out = new OutputVectorLayer(); |
115 |
out.setName(RESULT); |
116 |
out.setOutputChannel(channel); |
117 |
out.setDescription(Sextante.getText("Result"));
|
118 |
out.setOutputObject(ShapesTools.addFields(m_OutputFactory, layer, channel, sFields, values, types)); |
119 |
addOutputObject(out); |
120 |
|
121 |
} |
122 |
else {
|
123 |
throw new GeoAlgorithmExecutionException(jep.getErrorInfo()); |
124 |
} |
125 |
|
126 |
return !m_Task.isCanceled();
|
127 |
|
128 |
} |
129 |
|
130 |
|
131 |
@Override
|
132 |
public void defineCharacteristics() { |
133 |
|
134 |
setName(Sextante.getText("Field_calculator"));
|
135 |
setGroup(Sextante.getText("Tools_for_vector_layers"));
|
136 |
setUserCanDefineAnalysisExtent(true);
|
137 |
|
138 |
try {
|
139 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true); |
140 |
m_Parameters.addString(FORMULA, Sextante.getText("Formula"));
|
141 |
addOutputVectorLayer(RESULT, Sextante.getText("Result"));
|
142 |
} |
143 |
catch (final RepeatedParameterNameException e) { |
144 |
Sextante.addErrorToLog(e); |
145 |
} |
146 |
|
147 |
} |
148 |
|
149 |
|
150 |
private String replaceDots(final String s) { |
151 |
|
152 |
char c, c2;
|
153 |
StringBuffer sb = new StringBuffer(s); |
154 |
for (int i = 0; i < sb.length() - 1; i++) { |
155 |
c = sb.charAt(i); |
156 |
c2 = sb.charAt(i + 1);
|
157 |
if ((c == '.') && !Character.isDigit(c2)) { |
158 |
sb = sb.deleteCharAt(i); |
159 |
} |
160 |
} |
161 |
|
162 |
return sb.toString();
|
163 |
|
164 |
} |
165 |
|
166 |
|
167 |
@Override
|
168 |
public boolean preprocessForModeller(final Object obj) throws GeoAlgorithmExecutionException { |
169 |
|
170 |
final ModelAlgorithm model = (ModelAlgorithm) obj;
|
171 |
|
172 |
String sFormula = m_Parameters.getParameterValueAsString(FORMULA);
|
173 |
|
174 |
final HashMap inputs = model.getInputs(); |
175 |
final Set set = inputs.keySet(); |
176 |
final Iterator iter = set.iterator(); |
177 |
while (iter.hasNext()) {
|
178 |
final Object key = iter.next(); |
179 |
final Object input = inputs.get(key); |
180 |
if (input instanceof ModelElementNumericalValue) { |
181 |
if (sFormula.contains(((String) key))) { |
182 |
return false; |
183 |
} |
184 |
} |
185 |
if (sFormula.contains((String) key)) { |
186 |
sFormula = sFormula.replace(key.toString(), input.toString()); |
187 |
} |
188 |
} |
189 |
|
190 |
m_Parameters.getParameter(FORMULA).setParameterValue(sFormula); |
191 |
|
192 |
return true; |
193 |
|
194 |
} |
195 |
|
196 |
} |