root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tables / filterVector / FilterVectorAlgorithm.java @ 59
History | View | Annotate | Download (5.79 KB)
1 | 59 | nbrodin | |
---|---|---|---|
2 | |||
3 | package es.unex.sextante.tables.filterVector; |
||
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 | |||
24 | |||
25 | public class FilterVectorAlgorithm |
||
26 | extends
|
||
27 | GeoAlgorithm { |
||
28 | |||
29 | public static final String FORMULA = "FORMULA"; |
||
30 | public static final String LAYER = "LAYER"; |
||
31 | public static final String RESULT = "RESULT"; |
||
32 | |||
33 | |||
34 | @Override
|
||
35 | public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
||
36 | |||
37 | int i, j;
|
||
38 | double dValue;
|
||
39 | String sVariable;
|
||
40 | |||
41 | final IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
|
||
42 | if (!m_bIsAutoExtent) {
|
||
43 | layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
||
44 | } |
||
45 | final IVectorLayer outputLayer = getNewVectorLayer(RESULT, layer.getName(), layer.getShapeType(), layer.getFieldTypes(),
|
||
46 | layer.getFieldNames()); |
||
47 | |||
48 | final JEP jep = new JEP(); |
||
49 | jep.addStandardConstants(); |
||
50 | jep.addStandardFunctions(); |
||
51 | |||
52 | String sFormula = m_Parameters.getParameterValueAsString(FORMULA).toLowerCase();
|
||
53 | sFormula = sFormula.replaceAll(" ", ""); |
||
54 | sFormula = replaceDots(sFormula); |
||
55 | |||
56 | final String[] fieldNames = layer.getFieldNames(); |
||
57 | final ArrayList variables = new ArrayList(); |
||
58 | final ArrayList variableIndex = new ArrayList(); |
||
59 | for (i = 0; i < fieldNames.length; i++) { |
||
60 | sVariable = fieldNames[i].toLowerCase(); |
||
61 | sVariable = sVariable.replaceAll(" ", ""); |
||
62 | sVariable = replaceDots(sVariable); |
||
63 | if (sFormula.lastIndexOf(sVariable) != -1) { |
||
64 | jep.addVariable(sVariable, 0.0);
|
||
65 | variables.add(sVariable); |
||
66 | variableIndex.add(new Integer(i)); |
||
67 | } |
||
68 | } |
||
69 | final int[] iVariables = new int[variables.size()]; |
||
70 | final String[] sVariables = new String[variables.size()]; |
||
71 | for (i = 0; i < variableIndex.size(); i++) { |
||
72 | iVariables[i] = ((Integer) variableIndex.get(i)).intValue();
|
||
73 | sVariables[i] = (String) variables.get(i);
|
||
74 | } |
||
75 | |||
76 | sFormula = "if(" + sFormula + ",1,0)"; |
||
77 | jep.parseExpression(sFormula); |
||
78 | |||
79 | int iCount = 0; |
||
80 | final int iTotalCount = layer.getShapesCount(); |
||
81 | if (!jep.hasError()) {
|
||
82 | final IFeatureIterator iter = layer.iterator();
|
||
83 | IFeature feat; |
||
84 | while (iter.hasNext() && setProgress(iCount, iTotalCount)) {
|
||
85 | feat = iter.next(); |
||
86 | for (j = 0; (j < iVariables.length); j++) { |
||
87 | String sValue = null; |
||
88 | try {
|
||
89 | sValue = feat.getRecord().getValue(iVariables[j]).toString(); |
||
90 | dValue = Double.parseDouble(sValue);
|
||
91 | jep.addVariable(sVariables[j], dValue); |
||
92 | } |
||
93 | catch (final NumberFormatException nfe) { |
||
94 | jep.addVariable(sVariables[j], sValue.toLowerCase()); |
||
95 | } |
||
96 | } |
||
97 | dValue = jep.getValue(); |
||
98 | if ((dValue != 0) && !Double.isNaN(dValue)) { |
||
99 | outputLayer.addFeature(feat); |
||
100 | } |
||
101 | |||
102 | iCount++; |
||
103 | } |
||
104 | } |
||
105 | else {
|
||
106 | throw new GeoAlgorithmExecutionException(jep.getErrorInfo()); |
||
107 | } |
||
108 | |||
109 | return !m_Task.isCanceled();
|
||
110 | |||
111 | } |
||
112 | |||
113 | |||
114 | @Override
|
||
115 | public void defineCharacteristics() { |
||
116 | |||
117 | setName(Sextante.getText("Filter_vector"));
|
||
118 | setGroup(Sextante.getText("Tools_for_vector_layers"));
|
||
119 | setUserCanDefineAnalysisExtent(true);
|
||
120 | |||
121 | try {
|
||
122 | m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true); |
||
123 | m_Parameters.addString(FORMULA, Sextante.getText("Formula"));
|
||
124 | addOutputVectorLayer(RESULT, Sextante.getText("Result"));
|
||
125 | } |
||
126 | catch (final RepeatedParameterNameException e) { |
||
127 | Sextante.addErrorToLog(e); |
||
128 | } |
||
129 | |||
130 | } |
||
131 | |||
132 | |||
133 | private String replaceDots(final String s) { |
||
134 | |||
135 | char c, c2;
|
||
136 | StringBuffer sb = new StringBuffer(s); |
||
137 | for (int i = 0; i < sb.length() - 1; i++) { |
||
138 | c = sb.charAt(i); |
||
139 | c2 = sb.charAt(i + 1);
|
||
140 | if ((c == '.') && !Character.isDigit(c2)) { |
||
141 | sb = sb.deleteCharAt(i); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | return sb.toString();
|
||
146 | |||
147 | } |
||
148 | |||
149 | |||
150 | @Override
|
||
151 | public boolean preprocessForModeller(final Object obj) throws GeoAlgorithmExecutionException { |
||
152 | |||
153 | final ModelAlgorithm model = (ModelAlgorithm) obj;
|
||
154 | |||
155 | String sFormula = m_Parameters.getParameterValueAsString(FORMULA);
|
||
156 | |||
157 | final HashMap inputs = model.getInputs(); |
||
158 | final Set set = inputs.keySet(); |
||
159 | final Iterator iter = set.iterator(); |
||
160 | while (iter.hasNext()) {
|
||
161 | final Object key = iter.next(); |
||
162 | final Object input = inputs.get(key); |
||
163 | if (input instanceof ModelElementNumericalValue) { |
||
164 | if (sFormula.contains(((String) key))) { |
||
165 | return false; |
||
166 | } |
||
167 | } |
||
168 | if (sFormula.contains((String) key)) { |
||
169 | sFormula = sFormula.replace(key.toString(), input.toString()); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | m_Parameters.getParameter(FORMULA).setParameterValue(sFormula); |
||
174 | |||
175 | return true; |
||
176 | |||
177 | } |
||
178 | |||
179 | } |