Revision 56
org.gvsig.toolbox/trunk/org.gvsig.toolbox/org.gvsig.toolbox.algorithm/src/es/unex/sextante/gridCalculus/gridCalculator/FormulaParser.java | ||
---|---|---|
1 | 1 |
package es.unex.sextante.gridCalculus.gridCalculator; |
2 | 2 |
|
3 | 3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
|
4 | 5 |
|
5 | 6 |
import org.nfunk.jep.JEP; |
6 | 7 |
|
... | ... | |
15 | 16 |
|
16 | 17 |
|
17 | 18 |
public static String prepareFormula(final String sFormula, |
18 |
final ArrayList names) {
|
|
19 |
final List<String> names) {
|
|
19 | 20 |
|
20 | 21 |
int i; |
21 | 22 |
int iIndex; |
... | ... | |
53 | 54 |
|
54 | 55 |
} |
55 | 56 |
|
57 |
/** |
|
58 |
* Converts the name of a layer in a normalized name. |
|
59 |
* @param name |
|
60 |
* @return |
|
61 |
*/ |
|
62 |
public static String getNormalizedName(String name) { |
|
63 |
//If the name starts with a number adds a prefix |
|
64 |
String pattern = "^[\\d].*"; |
|
65 |
if(name != null && name.matches(pattern)) { |
|
66 |
name = "prefix_" + name; |
|
67 |
} |
|
68 |
return name; |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* Replace all variables in a formula by a normalized name |
|
73 |
* @param formula |
|
74 |
* @param layerName |
|
75 |
* @param normalizedName |
|
76 |
* @return |
|
77 |
*/ |
|
78 |
public static String replaceVariables(String formula, String layerName, String normalizedName) { |
|
79 |
if(!normalizedName.equals(layerName)) { |
|
80 |
return formula.replaceAll(layerName, normalizedName); |
|
81 |
} |
|
82 |
return formula; |
|
83 |
} |
|
56 | 84 |
|
57 |
public static ArrayList getBandsFromFormula(String sFormula,
|
|
58 |
final ArrayList layers) {
|
|
85 |
public static List<RasterLayerAndBand> getBandsFromFormula(String sFormula,
|
|
86 |
final List<IRasterLayer> layers) {
|
|
59 | 87 |
|
60 | 88 |
int i, j; |
61 | 89 |
|
62 | 90 |
IRasterLayer layer; |
63 | 91 |
String sLayerName; |
64 |
final ArrayList array = new ArrayList();
|
|
65 |
final ArrayList names = new ArrayList();
|
|
92 |
final List<RasterLayerAndBand> array = new ArrayList<RasterLayerAndBand>();
|
|
93 |
final List<String> names = new ArrayList<String>();
|
|
66 | 94 |
final JEP jep = new JEP(); |
67 | 95 |
jep.addStandardConstants(); |
68 | 96 |
jep.addStandardFunctions(); |
69 |
|
|
97 |
|
|
70 | 98 |
for (i = 0; i < layers.size(); i++) { |
71 | 99 |
layer = (IRasterLayer) layers.get(i); |
72 |
names.add(layer.getName()); |
|
100 |
String layerName = layer.getName(); |
|
101 |
String normalizedLayerName = getNormalizedName(layerName); |
|
102 |
sFormula = replaceVariables(sFormula, layerName, normalizedLayerName); |
|
103 |
names.add(normalizedLayerName); |
|
73 | 104 |
} |
74 |
|
|
105 |
|
|
75 | 106 |
sFormula = FormulaParser.prepareFormula(sFormula.toLowerCase(), names); |
76 | 107 |
sFormula = sFormula.toLowerCase().replaceAll(" ", ""); |
77 | 108 |
sFormula = sFormula.replaceAll("\\[", "_"); |
... | ... | |
86 | 117 |
sLayerName = sLayerName.replaceAll("\\[", "_"); |
87 | 118 |
sLayerName = sLayerName.replaceAll("\\]", "_"); |
88 | 119 |
sLayerName = FormulaParser.replaceDots(sLayerName); |
120 |
sLayerName = getNormalizedName(sLayerName); |
|
89 | 121 |
if (sFormula.lastIndexOf(sLayerName) != -1) { |
90 | 122 |
array.add(new RasterLayerAndBand(layer, j)); |
91 | 123 |
jep.addVariable(sLayerName, 0.0); |
... | ... | |
94 | 126 |
|
95 | 127 |
} |
96 | 128 |
|
97 |
|
|
98 | 129 |
jep.parseExpression(sFormula); |
99 | 130 |
|
100 | 131 |
if (jep.hasError()) { |
... | ... | |
107 | 138 |
} |
108 | 139 |
|
109 | 140 |
return array; |
110 |
|
|
111 | 141 |
} |
112 | 142 |
|
113 | 143 |
|
114 |
public static ArrayList getBandsFromFormulaForModeler(String sFormula,
|
|
144 |
public static List<String> getBandsFromFormulaForModeler(String sFormula,
|
|
115 | 145 |
final ObjectAndDescription[] layers, |
116 | 146 |
final ObjectAndDescription[] numerical) { |
117 |
|
|
118 | 147 |
int i, j; |
119 | 148 |
|
120 | 149 |
ObjectAndDescription oad; |
121 | 150 |
String sLayerName, sName; |
122 |
final ArrayList<String> array = new ArrayList<String>();
|
|
123 |
final ArrayList<String> names = new ArrayList<String>();
|
|
151 |
final List<String> array = new ArrayList<String>(); |
|
152 |
final List<String> names = new ArrayList<String>(); |
|
124 | 153 |
final JEP jep = new JEP(); |
125 | 154 |
jep.addStandardConstants(); |
126 | 155 |
jep.addStandardFunctions(); |
127 | 156 |
|
128 | 157 |
for (i = 0; i < layers.length; i++) { |
129 | 158 |
oad = layers[i]; |
130 |
names.add((String) oad.getObject()); |
|
159 |
String layerName = (String) oad.getObject(); |
|
160 |
String normalizedLayerName = getNormalizedName(layerName); |
|
161 |
sFormula = replaceVariables(sFormula, layerName, normalizedLayerName); |
|
162 |
names.add(layerName); |
|
131 | 163 |
} |
132 | 164 |
|
133 | 165 |
for (i = 0; i < numerical.length; i++) { |
... | ... | |
152 | 184 |
sLayerName = sLayerName.replaceAll("\\:", "_"); |
153 | 185 |
sLayerName = sLayerName.replaceAll("\"", "_"); |
154 | 186 |
sLayerName = FormulaParser.replaceDots(sLayerName); |
187 |
sLayerName = getNormalizedName(sLayerName); |
|
155 | 188 |
if (sFormula.lastIndexOf(sLayerName) != -1) { |
156 | 189 |
array.add((String) layers[i].getObject()); |
157 | 190 |
jep.addVariable(sLayerName, 0.0); |
org.gvsig.toolbox/trunk/org.gvsig.toolbox/org.gvsig.toolbox.algorithm/src/es/unex/sextante/gridCalculus/gridCalculator/GridCalculatorParametersPanel.java | ||
---|---|---|
10 | 10 |
import java.awt.event.MouseListener; |
11 | 11 |
import java.util.ArrayList; |
12 | 12 |
import java.util.Iterator; |
13 |
import java.util.List; |
|
13 | 14 |
import java.util.Set; |
14 | 15 |
import java.util.TreeMap; |
15 | 16 |
|
... | ... | |
235 | 236 |
for (final IRasterLayer element : layers) { |
236 | 237 |
layersList.add(element); |
237 | 238 |
} |
238 |
final ArrayList array = FormulaParser.getBandsFromFormula(jTextExpression.getText(), layersList);
|
|
239 |
final List<RasterLayerAndBand> array = FormulaParser.getBandsFromFormula(jTextExpression.getText(), layersList);
|
|
239 | 240 |
|
240 | 241 |
if (array == null) { |
241 | 242 |
throw new WrongInputException(); |
org.gvsig.toolbox/trunk/org.gvsig.toolbox/org.gvsig.toolbox.algorithm/src/es/unex/sextante/gridCalculus/gridCalculator/GridCalculatorAlgorithm.java | ||
---|---|---|
4 | 4 |
import java.util.ArrayList; |
5 | 5 |
import java.util.HashMap; |
6 | 6 |
import java.util.Iterator; |
7 |
import java.util.List; |
|
7 | 8 |
import java.util.Set; |
8 | 9 |
|
9 | 10 |
import org.nfunk.jep.JEP; |
... | ... | |
39 | 40 |
|
40 | 41 |
IRasterLayer layer; |
41 | 42 |
RasterLayerAndBand layerAndBand; |
42 |
final ArrayList names = new ArrayList();
|
|
43 |
final List<String> names = new ArrayList<String>();
|
|
43 | 44 |
final ArrayList layers = m_Parameters.getParameterValueAsArrayList(LAYERS); |
44 | 45 |
String sFormula = m_Parameters.getParameterValueAsString(FORMULA).toLowerCase(); |
45 |
final ArrayList bands = FormulaParser.getBandsFromFormula(sFormula, layers);
|
|
46 |
final List<RasterLayerAndBand> bands = FormulaParser.getBandsFromFormula(sFormula, layers);
|
|
46 | 47 |
|
47 | 48 |
if (bands == null) { |
48 | 49 |
throw new GeoAlgorithmExecutionException(Sextante.getText("Syntax_error")); |
... | ... | |
65 | 66 |
layerAndBand = (RasterLayerAndBand) bands.get(i); |
66 | 67 |
layer = layerAndBand.getRasterLayer(); |
67 | 68 |
iBands[i] = layerAndBand.getBand(); |
68 |
names.add(layer.getName()); |
|
69 |
String layerName = layer.getName(); |
|
70 |
String normalizedName = FormulaParser.getNormalizedName(layerName); |
|
71 |
sFormula = FormulaParser.replaceVariables(sFormula, layerName, normalizedName); |
|
72 |
names.add(normalizedName); |
|
69 | 73 |
window[i] = layer; |
70 | 74 |
window[i].setWindowExtent(extent); |
71 | 75 |
if ((layer.getDataType() == DataBuffer.TYPE_FLOAT) || (layer.getDataType() == DataBuffer.TYPE_DOUBLE)) { |
... | ... | |
81 | 85 |
sVariable[i] = sVariable[i].replaceAll("\\[", "_"); |
82 | 86 |
sVariable[i] = sVariable[i].replaceAll("\\]", "_"); |
83 | 87 |
sVariable[i] = FormulaParser.replaceDots(sVariable[i]); |
84 |
|
|
88 |
sVariable[i] = FormulaParser.getNormalizedName(sVariable[i]); |
|
89 |
|
|
85 | 90 |
jep.addVariable(sVariable[i], 0.0); |
86 | 91 |
|
87 | 92 |
} |
org.gvsig.toolbox/trunk/org.gvsig.toolbox/org.gvsig.toolbox.algorithm/src/es/unex/sextante/gridCalculus/gridCalculator/GridCalculatorModelerParametersPanel.java | ||
---|---|---|
11 | 11 |
import java.util.ArrayList; |
12 | 12 |
import java.util.HashMap; |
13 | 13 |
import java.util.Iterator; |
14 |
import java.util.List; |
|
14 | 15 |
import java.util.Set; |
15 | 16 |
|
16 | 17 |
import javax.swing.BorderFactory; |
... | ... | |
282 | 283 |
final ArrayList layersList = new ArrayList(); |
283 | 284 |
|
284 | 285 |
final String sFormula = jTextExpression.getText(); |
285 |
final ArrayList array = FormulaParser.getBandsFromFormulaForModeler(sFormula, layers, numerical);
|
|
286 |
final List<String> array = FormulaParser.getBandsFromFormulaForModeler(sFormula, layers, numerical);
|
|
286 | 287 |
|
287 | 288 |
if (array == null) { |
288 | 289 |
return false; |
Also available in: Unified diff