Revision 44006
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/spi/AbstractFunction.java | ||
---|---|---|
1 | 1 |
package org.gvsig.expressionevaluator.spi; |
2 | 2 |
|
3 |
import java.io.InputStream; |
|
4 |
import java.net.URL; |
|
3 | 5 |
import java.util.ArrayList; |
4 | 6 |
import java.util.List; |
7 |
import java.util.Locale; |
|
5 | 8 |
import java.util.Objects; |
9 |
import org.apache.commons.io.IOUtils; |
|
6 | 10 |
import org.apache.commons.lang3.BooleanUtils; |
7 | 11 |
import org.apache.commons.lang3.Range; |
12 |
import org.apache.commons.lang3.StringUtils; |
|
8 | 13 |
import org.apache.commons.math.util.MathUtils; |
9 | 14 |
import org.gvsig.expressionevaluator.Code; |
10 | 15 |
import org.gvsig.expressionevaluator.Code.Caller.Arguments; |
... | ... | |
12 | 17 |
import org.gvsig.expressionevaluator.Interpreter; |
13 | 18 |
import org.gvsig.fmap.geom.Geometry; |
14 | 19 |
import org.gvsig.fmap.geom.primitive.Point; |
20 |
import org.json.JSONArray; |
|
21 |
import org.json.JSONObject; |
|
15 | 22 |
|
23 |
@SuppressWarnings("UseSpecificCatch") |
|
16 | 24 |
public abstract class AbstractFunction implements Function { |
17 | 25 |
|
18 | 26 |
private final String name; |
19 |
private final String group;
|
|
20 |
private final Range argc;
|
|
21 |
private final String description;
|
|
22 |
private final String[] descriptionArgs;
|
|
27 |
private String group; |
|
28 |
private Range argc; |
|
29 |
private String description; |
|
30 |
private String[] descriptionArgs; |
|
23 | 31 |
private List<String> alias; |
24 | 32 |
private String template; |
25 | 33 |
private String returnType; |
... | ... | |
34 | 42 |
this.descriptionArgs = descriptionArgs; |
35 | 43 |
this.returnType = returnType; |
36 | 44 |
this.sqlCompatible = sqlCompatible; |
45 |
load_from_resource(); |
|
37 | 46 |
} |
38 | 47 |
protected AbstractFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs, String returnType) { |
39 | 48 |
this(group, name, argc, description, template, descriptionArgs, returnType, false); |
... | ... | |
83 | 92 |
|
84 | 93 |
@Override |
85 | 94 |
public void addAlias(String name) { |
95 |
if( StringUtils.isBlank(name) ) { |
|
96 |
return; |
|
97 |
} |
|
86 | 98 |
if( this.alias == null ) { |
87 | 99 |
this.alias = new ArrayList<>(); |
88 | 100 |
} |
101 |
if( this.alias.contains(name) ) { |
|
102 |
return; |
|
103 |
} |
|
89 | 104 |
this.alias.add(name); |
90 | 105 |
} |
91 | 106 |
|
... | ... | |
226 | 241 |
} |
227 | 242 |
return BooleanUtils.toBoolean(value.toString()); |
228 | 243 |
} |
244 |
|
|
245 |
private void load_from_resource() { |
|
246 |
String lang = Locale.getDefault().getLanguage(); |
|
247 |
URL url = this.getClass().getResource("/org/gvsig/expressionevaluator/functions/"+lang+"/"+this.name()+".json"); |
|
248 |
if( url == null ) { |
|
249 |
url = this.getClass().getResource("/org/gvsig/expressionevaluator/functions/en/"+this.name()+".json"); |
|
250 |
if( url == null ) { |
|
251 |
return; |
|
252 |
} |
|
253 |
} |
|
254 |
InputStream is = null; |
|
255 |
JSONObject json; |
|
256 |
try { |
|
257 |
is = url.openStream(); |
|
258 |
List<String> lines = IOUtils.readLines(is); |
|
259 |
json = new JSONObject(StringUtils.join(lines, "\n")); |
|
260 |
} catch (Exception ex) { |
|
261 |
return; |
|
262 |
} finally { |
|
263 |
IOUtils.closeQuietly(is); |
|
264 |
} |
|
265 |
|
|
266 |
if( json.has("group") ) { |
|
267 |
this.group = json.getString("group"); |
|
268 |
} |
|
269 |
if( json.has("description") ) { |
|
270 |
Object x = json.get("description"); |
|
271 |
if( x instanceof String ) { |
|
272 |
this.description = (String) x; |
|
273 |
} else if( x instanceof JSONArray ) { |
|
274 |
StringBuilder builder = new StringBuilder(); |
|
275 |
for (int i = 0; i < ((JSONArray)x).length(); i++) { |
|
276 |
if( i>0 ) { |
|
277 |
builder.append(" "); |
|
278 |
} |
|
279 |
builder.append(((JSONArray)x).getString(i)); |
|
280 |
} |
|
281 |
this.description = builder.toString(); |
|
282 |
} else { |
|
283 |
this.description = x.toString(); |
|
284 |
} |
|
285 |
this.description = StringUtils.replace( |
|
286 |
this.description, |
|
287 |
"@@@", |
|
288 |
url.toString() |
|
289 |
); |
|
290 |
} |
|
291 |
if( json.has("template") ) { |
|
292 |
this.template = json.getString("template"); |
|
293 |
} |
|
294 |
if( json.has("returnType") ) { |
|
295 |
this.returnType = json.getString("returnType"); |
|
296 |
} |
|
297 |
if( json.has("sqlCompatible") ) { |
|
298 |
this.sqlCompatible = json.getBoolean("sqlCompatible"); |
|
299 |
} |
|
300 |
if( json.has("args") ) { |
|
301 |
JSONArray x = json.getJSONArray("args"); |
|
302 |
String[] args = new String[x.length()]; |
|
303 |
for (int i = 0; i < x.length(); i++) { |
|
304 |
args[i] = x.getString(i); |
|
305 |
} |
|
306 |
this.descriptionArgs = args; |
|
307 |
} |
|
308 |
if( json.has("alias") ) { |
|
309 |
JSONArray x = json.getJSONArray("alias"); |
|
310 |
for (int i = 0; i < x.length(); i++) { |
|
311 |
this.addAlias(x.getString(i)); |
|
312 |
} |
|
313 |
} |
|
314 |
} |
|
229 | 315 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/ExpressionEvaluatorManager.java | ||
---|---|---|
39 | 39 |
public void setAccuracy(Double accuracy); |
40 | 40 |
|
41 | 41 |
public Expression createExpression(); |
42 |
|
|
43 |
public ExpressionBuilder createExpressionBuilder(); |
|
42 | 44 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/ExpressionBuilder.java | ||
---|---|---|
1 |
package org.gvsig.expressionevaluator; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
import org.cresques.cts.IProjection; |
|
5 |
import org.gvsig.fmap.geom.Geometry; |
|
6 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
7 |
|
|
8 |
public interface ExpressionBuilder { |
|
9 |
|
|
10 |
public enum GeometrySupportType { |
|
11 |
WKT, |
|
12 |
WKB, |
|
13 |
EWKB, |
|
14 |
NATIVE |
|
15 |
} |
|
16 |
|
|
17 |
public enum ParameterType { |
|
18 |
Variable, |
|
19 |
Constant, |
|
20 |
Geometry |
|
21 |
} |
|
22 |
|
|
23 |
public interface VisitorFilter { |
|
24 |
boolean accept(Visitable visitable); |
|
25 |
} |
|
26 |
|
|
27 |
public interface Visitor { |
|
28 |
public void visit(Visitable value); |
|
29 |
} |
|
30 |
|
|
31 |
public interface Visitable { |
|
32 |
public void accept(Visitor visitor, VisitorFilter filter); |
|
33 |
} |
|
34 |
|
|
35 |
public interface Config { |
|
36 |
public final static String has_spatial_functions = "has_spatial_functions"; |
|
37 |
public final static String support_schemas = "support_schemas"; |
|
38 |
public final static String quote_for_identifiers = "quote_for_identifiers"; |
|
39 |
public final static String quote_for_strings = "quote_for_strings"; |
|
40 |
public final static String geometry_type_support = "geometry_type_support"; |
|
41 |
public final static String constant_true = "constant_true"; |
|
42 |
public final static String constant_false = "constant_false"; |
|
43 |
public final static String group = "group"; |
|
44 |
public final static String attribute_crs = "crs"; |
|
45 |
|
|
46 |
public final static String ST_SRID = "ST_SRID"; |
|
47 |
public final static String ST_AsText = "ST_AsText"; |
|
48 |
public final static String ST_AsBinary = "ST_AsBinary"; |
|
49 |
public final static String ST_AsEWKB = "ST_AsEWKB"; |
|
50 |
public final static String ST_Contains = "ST_Contains"; |
|
51 |
public final static String ST_Crosses = "ST_Crosses"; |
|
52 |
public final static String ST_Disjoint = "ST_Disjoint"; |
|
53 |
public final static String ST_Equals = "ST_Equals"; |
|
54 |
public final static String ST_IsClosed = "ST_IsClosed"; |
|
55 |
public final static String ST_Overlaps = "ST_Overlaps"; |
|
56 |
public final static String ST_Touches = "ST_Touches"; |
|
57 |
public final static String ST_Within = "ST_Within"; |
|
58 |
public final static String ST_Intersects = "ST_Intersects"; |
|
59 |
public final static String ST_Envelope = "ST_Envelope"; |
|
60 |
public final static String ST_GeomFromText = "ST_GeomFromText"; |
|
61 |
public final static String ST_GeomFromWKB = "ST_GeomFromWKB"; |
|
62 |
public final static String ST_GeomFromEWKB = "ST_GeomFromEWKB"; |
|
63 |
public final static String ST_Simplify = "ST_Simplify"; |
|
64 |
public final static String lcase = "lcase"; |
|
65 |
public final static String ucase = "ucase"; |
|
66 |
public final static String isNull = "isNull"; |
|
67 |
public final static String notIsNull = "notIsNull"; |
|
68 |
public final static String operator_not = "operator_not"; |
|
69 |
|
|
70 |
public final static String operator_AND = "operator_AND"; |
|
71 |
public final static String operator_OR = "operator_OR"; |
|
72 |
public final static String operator_EQ = "operator_EQ"; |
|
73 |
public final static String operator_NE = "operator_NE"; |
|
74 |
public final static String operator_GT = "operator_GT"; |
|
75 |
public final static String operator_GE = "operator_GE"; |
|
76 |
public final static String operator_LT = "operator_LT"; |
|
77 |
public final static String operator_LE = "operator_LE"; |
|
78 |
public final static String operator_LIKE = "operator_LIKE"; |
|
79 |
public final static String operator_ILIKE = "operator_ILIKE"; |
|
80 |
|
|
81 |
public final static String operator_add = "operator_add"; |
|
82 |
public final static String operator_subst = "operator_subst"; |
|
83 |
public final static String operator_mult = "operator_mult"; |
|
84 |
public final static String operator_div = "operator_div"; |
|
85 |
public final static String operator_concat = "operator_concat"; |
|
86 |
|
|
87 |
|
|
88 |
public GeometrySupportType getGeometryTypeSupport(); |
|
89 |
public boolean has_spatial_functions(); |
|
90 |
public boolean has_functionality(String functionality); |
|
91 |
public void remove_functionality(String functionality); |
|
92 |
|
|
93 |
public boolean getBoolean(String name); |
|
94 |
public String getString(String name); |
|
95 |
public Object get(String name); |
|
96 |
public void set(String name, Object value); |
|
97 |
} |
|
98 |
|
|
99 |
public interface Value extends Visitable { |
|
100 |
|
|
101 |
} |
|
102 |
|
|
103 |
public interface Group extends Value { |
|
104 |
public Value getValue(); |
|
105 |
} |
|
106 |
|
|
107 |
public interface Constant extends Value { |
|
108 |
public Object getValue(); |
|
109 |
} |
|
110 |
|
|
111 |
public interface Custom extends Value { |
|
112 |
public Object getValue(); |
|
113 |
public Custom add(Variable variable); |
|
114 |
public Custom add(Parameter parameter); |
|
115 |
} |
|
116 |
|
|
117 |
public interface GeometryValue extends Value { |
|
118 |
public Geometry getGeometry(); |
|
119 |
public IProjection getSRS(); |
|
120 |
} |
|
121 |
|
|
122 |
public interface ColumnDescriptor { |
|
123 |
public String getName(); |
|
124 |
public int getType(); |
|
125 |
public int getSize(); |
|
126 |
public int getPrecision(); |
|
127 |
public boolean isPrimaryKey(); |
|
128 |
public boolean isIndexed(); |
|
129 |
public boolean isAutomatic(); |
|
130 |
boolean allowNulls(); |
|
131 |
public Object getDefaultValue(); |
|
132 |
public int getGeometryType(); |
|
133 |
public int getGeometrySubtype(); |
|
134 |
public Object getGeometrySRSId(); |
|
135 |
public boolean isGeometry(); |
|
136 |
|
|
137 |
public void setName(String name); |
|
138 |
public void setType(int type); |
|
139 |
public void setSize(int size); |
|
140 |
public void setPrecision(int precision); |
|
141 |
public void setIsPrimaryKey(boolean isPk); |
|
142 |
public void setIsAutomatic(boolean isAutomatic); |
|
143 |
public void setAllowNulls(boolean allowNulls); |
|
144 |
public void setDefaultValue(Object defaultValue); |
|
145 |
public void setGeometryType(int geom_type); |
|
146 |
public void setGeometrySubtype(int geom_subtype); |
|
147 |
public void setGeometrySRSId(Object geom_srsid); |
|
148 |
} |
|
149 |
|
|
150 |
public interface Variable extends Value, Comparable<Variable> { |
|
151 |
public String getName(); |
|
152 |
public ColumnDescriptor getDescriptor(); |
|
153 |
} |
|
154 |
|
|
155 |
public interface Parameter extends Value { |
|
156 |
public Parameter as_constant(); |
|
157 |
public Parameter as_variable(); |
|
158 |
public Parameter as_geometry_variable(); |
|
159 |
public Parameter srs(Value srs); |
|
160 |
public Parameter srs(IProjection srs); |
|
161 |
public String getName(); |
|
162 |
public Object getValue(); |
|
163 |
public Parameter value(Object value); |
|
164 |
public Parameter name(String value); |
|
165 |
public ParameterType getType(); |
|
166 |
public Value getSRS(); |
|
167 |
public boolean is_constant(); |
|
168 |
public boolean is_variable(); |
|
169 |
public boolean is_geometry_variable(); |
|
170 |
} |
|
171 |
|
|
172 |
public interface Parameters extends List<Parameter> { |
|
173 |
public Parameter get(String name); |
|
174 |
} |
|
175 |
|
|
176 |
public interface Function extends Value { |
|
177 |
public String getName(); |
|
178 |
public List<Value> parameters(); |
|
179 |
public Function parameter(Value parameter); |
|
180 |
} |
|
181 |
|
|
182 |
public interface BinaryOperator extends Value { |
|
183 |
public String getName(); |
|
184 |
public Value getLeft(); |
|
185 |
public Value getRight(); |
|
186 |
public BinaryOperator setLeft(Value operand); |
|
187 |
public BinaryOperator setRight(Value operand); |
|
188 |
} |
|
189 |
|
|
190 |
public void accept(Visitor visitor, VisitorFilter filter); |
|
191 |
|
|
192 |
public List<Variable> getVariables(); |
|
193 |
|
|
194 |
public Parameters getParameters(); |
|
195 |
|
|
196 |
public boolean has_spatial_functions(); |
|
197 |
|
|
198 |
public GeometrySupportType geometry_support_type(); |
|
199 |
|
|
200 |
public String identifier(String name); |
|
201 |
|
|
202 |
public String bytearray(byte[] data); |
|
203 |
|
|
204 |
public String bytearray_hex(byte[] data); |
|
205 |
|
|
206 |
public String bytearray_0x(byte[] data); |
|
207 |
|
|
208 |
public String bytearray_x(byte[] data); |
|
209 |
|
|
210 |
public String string(String s); |
|
211 |
|
|
212 |
public Object getSRSId(IProjection projection); |
|
213 |
|
|
214 |
public Config getConfig(); |
|
215 |
|
|
216 |
public Value getValue(); |
|
217 |
|
|
218 |
public ExpressionBuilder setValue(Value value); |
|
219 |
|
|
220 |
public Variable variable(String name); |
|
221 |
|
|
222 |
public Variable column(String name); // Alias for variable(name) |
|
223 |
|
|
224 |
public Parameter parameter(String name); |
|
225 |
|
|
226 |
public Parameter parameter(); |
|
227 |
|
|
228 |
public Constant constant(Object value); |
|
229 |
|
|
230 |
public Group group(Value group); |
|
231 |
|
|
232 |
public GeometryValue geometry(Geometry geom, IProjection projection); |
|
233 |
|
|
234 |
public GeometryValue geometry(Geometry geom); |
|
235 |
|
|
236 |
public GeometryValue envelope(Envelope envelope, IProjection projection); |
|
237 |
|
|
238 |
public GeometryValue envelope(Envelope envelope); |
|
239 |
|
|
240 |
public Custom custom(Object value); |
|
241 |
|
|
242 |
public Constant srs(IProjection projection); |
|
243 |
|
|
244 |
public Function getAsGeometry(Value value); |
|
245 |
|
|
246 |
|
|
247 |
public Function not(Value value); |
|
248 |
|
|
249 |
public Function notIsNull(Value value); |
|
250 |
|
|
251 |
public Function isNull(Value value); |
|
252 |
|
|
253 |
public Function ST_SRID(Value geom); |
|
254 |
|
|
255 |
public Function ST_AsText(Value geom); |
|
256 |
|
|
257 |
public Function ST_AsBinary(Value geom); |
|
258 |
|
|
259 |
public Function ST_AsEWKB(Value geom); |
|
260 |
|
|
261 |
public Function ST_Envelope(Value geom); |
|
262 |
|
|
263 |
public Function ST_GeomFromText(Value geom, Value crs); |
|
264 |
|
|
265 |
public Function ST_GeomFromWKB(Value geom, Value crs); |
|
266 |
|
|
267 |
public Function ST_GeomFromEWKB(Value geom, Value crs); |
|
268 |
|
|
269 |
public Function ST_Simplify(Value geom, Value tolerance); |
|
270 |
|
|
271 |
public Function ST_Equals(Value geom1, Value geom2); |
|
272 |
|
|
273 |
public Function ST_Intersects(Value geom1, Value geom2); |
|
274 |
|
|
275 |
public Function ST_Contains(Value geom1, Value geom2); |
|
276 |
|
|
277 |
public Function ST_Disjoint(Value geom1, Value geom2); |
|
278 |
|
|
279 |
public Function ST_Crosses(Value geom1, Value geom2); |
|
280 |
|
|
281 |
public Function ST_IsClosed(Value geom1); |
|
282 |
|
|
283 |
public Function ST_Overlaps(Value geom1, Value geom2); |
|
284 |
|
|
285 |
public Function ST_Touches(Value geom1, Value geom2); |
|
286 |
|
|
287 |
public Function ST_Within(Value geom1, Value geom2); |
|
288 |
|
|
289 |
public Function lcase(Value s); |
|
290 |
|
|
291 |
public Function ucase(Value s); |
|
292 |
|
|
293 |
public BinaryOperator and(Value op1, Value op2); |
|
294 |
|
|
295 |
public BinaryOperator or(Value op1, Value op2); |
|
296 |
|
|
297 |
public BinaryOperator eq(Value op1, Value op2); |
|
298 |
|
|
299 |
public BinaryOperator ne(Value op1, Value op2); |
|
300 |
|
|
301 |
public BinaryOperator gt(Value op1, Value op2); |
|
302 |
|
|
303 |
public BinaryOperator ge(Value op1, Value op2); |
|
304 |
|
|
305 |
public BinaryOperator lt(Value op1, Value op2); |
|
306 |
|
|
307 |
public BinaryOperator le(Value op1, Value op2); |
|
308 |
|
|
309 |
public BinaryOperator like(Value op1, Value op2); |
|
310 |
|
|
311 |
public BinaryOperator ilike(Value op1, Value op2); |
|
312 |
|
|
313 |
public BinaryOperator add(Value op1, Value op2); |
|
314 |
|
|
315 |
public BinaryOperator subst(Value op1, Value op2); |
|
316 |
|
|
317 |
public BinaryOperator mult(Value op1, Value op2); |
|
318 |
|
|
319 |
public BinaryOperator div(Value op1, Value op2); |
|
320 |
|
|
321 |
public BinaryOperator concat(Value op1, Value op2); |
|
322 |
|
|
323 |
public ExpressionBuilder set(Value value); |
|
324 |
|
|
325 |
public ExpressionBuilder and(Value value); |
|
326 |
|
|
327 |
public ExpressionBuilder or(Value value); |
|
328 |
|
|
329 |
public ExpressionBuilder createExpressionBuilder(); |
|
330 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/pom.xml | ||
---|---|---|
49 | 49 |
<artifactId>commons-math</artifactId> |
50 | 50 |
<scope>compile</scope> |
51 | 51 |
</dependency> |
52 |
<dependency> |
|
53 |
<groupId>org.json</groupId> |
|
54 |
<artifactId>json</artifactId> |
|
55 |
<scope>compile</scope> |
|
56 |
</dependency> |
|
52 | 57 |
</dependencies> |
53 | 58 |
</project> |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/java/org/gvsig/expresionevaluator/impl/TestExpressionBuilder.java | ||
---|---|---|
1 |
package org.gvsig.expresionevaluator.impl; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.List; |
|
5 |
import junit.framework.TestCase; |
|
6 |
import org.apache.commons.lang3.ArrayUtils; |
|
7 |
import org.cresques.cts.IProjection; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
9 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Config; |
|
10 |
import org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType; |
|
11 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Parameter; |
|
12 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Variable; |
|
13 |
import org.gvsig.expressionevaluator.impl.DefaultExpressionBuilder; |
|
14 |
import org.gvsig.fmap.crs.CRSFactory; |
|
15 |
import org.gvsig.fmap.geom.Geometry; |
|
16 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
17 |
import org.gvsig.fmap.geom.GeometryManager; |
|
18 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
|
19 |
import org.gvsig.fmap.geom.primitive.Point; |
|
20 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
|
21 |
|
|
22 |
public class TestExpressionBuilder extends TestCase { |
|
23 |
|
|
24 |
public TestExpressionBuilder(String testName) { |
|
25 |
super(testName); |
|
26 |
} |
|
27 |
|
|
28 |
@Override |
|
29 |
protected void setUp() throws Exception { |
|
30 |
super.setUp(); |
|
31 |
new DefaultLibrariesInitializer().fullInitialize(); |
|
32 |
// new ExpressionEvaluatorImplLibrary().initialize(); |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
protected void tearDown() throws Exception { |
|
37 |
super.tearDown(); |
|
38 |
} |
|
39 |
|
|
40 |
// TODO add test methods here. The name must begin with 'test'. For example: |
|
41 |
// public void testHello() {} |
|
42 |
|
|
43 |
List<String> getVariableNames(ExpressionBuilder builder) { |
|
44 |
List<String> vars = new ArrayList<>(); |
|
45 |
for (Variable var : builder.getVariables()) { |
|
46 |
vars.add(var.getName()); |
|
47 |
} |
|
48 |
return vars; |
|
49 |
} |
|
50 |
|
|
51 |
List<String> getParameterNames(ExpressionBuilder builder) { |
|
52 |
List<String> params = new ArrayList<>(); |
|
53 |
for (Parameter param : builder.getParameters()) { |
|
54 |
String s; |
|
55 |
switch(param.getType()) { |
|
56 |
case Constant: |
|
57 |
Object value = param.getValue(); |
|
58 |
if( value==null ) { |
|
59 |
s = "null"; |
|
60 |
} else if( value instanceof String ) { |
|
61 |
s = "'" + (String)value + "'"; |
|
62 |
} else { |
|
63 |
s = value.toString(); |
|
64 |
} |
|
65 |
break; |
|
66 |
case Geometry: |
|
67 |
case Variable: |
|
68 |
default: |
|
69 |
s = "\"" + param.getName() + "\""; |
|
70 |
} |
|
71 |
params.add(s); |
|
72 |
} |
|
73 |
return params; |
|
74 |
} |
|
75 |
|
|
76 |
public void test2() { |
|
77 |
ExpressionBuilder builder = new DefaultExpressionBuilder(); |
|
78 |
|
|
79 |
builder.and( |
|
80 |
builder.eq( |
|
81 |
builder.lcase(builder.variable("colum_name_c")), |
|
82 |
builder.parameter("colum_name_p") |
|
83 |
) |
|
84 |
); |
|
85 |
builder.and( |
|
86 |
builder.group( |
|
87 |
builder.or( |
|
88 |
builder.like( |
|
89 |
builder.lcase( builder.variable("uno")), |
|
90 |
builder.constant("%10") |
|
91 |
), |
|
92 |
builder.lt( |
|
93 |
builder.variable("dos"), |
|
94 |
builder.constant(-3.5) |
|
95 |
) |
|
96 |
) |
|
97 |
) |
|
98 |
); |
|
99 |
builder.and( |
|
100 |
builder.ST_Intersects( |
|
101 |
builder.variable("geom1"), |
|
102 |
builder.ST_Envelope( |
|
103 |
builder.ST_GeomFromWKB( |
|
104 |
builder.parameter("geom2"), |
|
105 |
builder.parameter().value(4326).as_constant() |
|
106 |
) |
|
107 |
) |
|
108 |
) |
|
109 |
); |
|
110 |
builder.and( |
|
111 |
builder.gt( |
|
112 |
builder.variable("tres"), |
|
113 |
builder.constant(123456789) |
|
114 |
) |
|
115 |
); |
|
116 |
assertEquals( |
|
117 |
"( (LCASE(\"colum_name_c\")) = (?) ) AND ( ( (LCASE(\"uno\")) LIKE ('%10') ) OR ( (\"dos\") < (-3.5) ) ) AND ST_Intersects((\"geom1\"), (ST_Envelope(ST_GeomFromWKB((?), (?))))) AND ( (\"tres\") > (123456789) )", |
|
118 |
builder.toString() |
|
119 |
); |
|
120 |
assertEquals( |
|
121 |
"[colum_name_c, dos, geom1, tres, uno]", |
|
122 |
ArrayUtils.toString(getVariableNames(builder)) |
|
123 |
); |
|
124 |
assertEquals( |
|
125 |
"[\"colum_name_p\", \"geom2\", 4326]", |
|
126 |
ArrayUtils.toString(getParameterNames(builder)) |
|
127 |
); |
|
128 |
} |
|
129 |
|
|
130 |
public void test3() throws CreateGeometryException { |
|
131 |
ExpressionBuilder builder = new DefaultExpressionBuilder(); |
|
132 |
|
|
133 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
|
134 |
IProjection proj = CRSFactory.getCRS("EPSG:4326"); |
|
135 |
|
|
136 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
|
137 |
builder.set( |
|
138 |
builder.ST_Intersects( |
|
139 |
builder.geometry(point,proj), |
|
140 |
builder.variable("the_geom") |
|
141 |
) |
|
142 |
); |
|
143 |
builder.getConfig().set(Config.geometry_type_support, GeometrySupportType.WKT); |
|
144 |
System.out.println(builder.toString()); |
|
145 |
assertEquals( |
|
146 |
"ST_Intersects((ST_GeomFromText('POINT (10 20)', (4326))), (\"the_geom\"))", |
|
147 |
builder.toString() |
|
148 |
); |
|
149 |
builder.getConfig().set(Config.geometry_type_support, GeometrySupportType.WKB); |
|
150 |
System.out.println(builder.toString()); |
|
151 |
assertEquals( |
|
152 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (\"the_geom\"))", |
|
153 |
builder.toString() |
|
154 |
); |
|
155 |
assertEquals( |
|
156 |
"[the_geom]", |
|
157 |
ArrayUtils.toString(getVariableNames(builder)) |
|
158 |
); |
|
159 |
assertEquals( |
|
160 |
"[]", |
|
161 |
ArrayUtils.toString(getParameterNames(builder)) |
|
162 |
); |
|
163 |
} |
|
164 |
|
|
165 |
public void test4() throws CreateGeometryException { |
|
166 |
ExpressionBuilder builder = new DefaultExpressionBuilder(); |
|
167 |
|
|
168 |
IProjection proj = CRSFactory.getCRS("EPSG:4326"); |
|
169 |
|
|
170 |
builder.and( |
|
171 |
builder.ST_Intersects( |
|
172 |
builder.variable("geom1"), |
|
173 |
builder.ST_Envelope( |
|
174 |
builder.ST_GeomFromWKB( |
|
175 |
builder.parameter("geom2"), |
|
176 |
builder.parameter().value(proj).as_constant() |
|
177 |
) |
|
178 |
) |
|
179 |
) |
|
180 |
); |
|
181 |
|
|
182 |
System.out.println(builder.toString()); |
|
183 |
assertEquals( |
|
184 |
"ST_Intersects((\"geom1\"), (ST_Envelope(ST_GeomFromWKB((?), (?)))))", |
|
185 |
builder.toString() |
|
186 |
); |
|
187 |
assertEquals( |
|
188 |
"[geom1]", |
|
189 |
ArrayUtils.toString(getVariableNames(builder)) |
|
190 |
); |
|
191 |
assertEquals( |
|
192 |
"[\"geom2\", 4326]", |
|
193 |
ArrayUtils.toString(getParameterNames(builder)) |
|
194 |
); |
|
195 |
} |
|
196 |
|
|
197 |
public void test5() throws CreateGeometryException { |
|
198 |
ExpressionBuilder builder = new DefaultExpressionBuilder(); |
|
199 |
|
|
200 |
IProjection proj = CRSFactory.getCRS("EPSG:4326"); |
|
201 |
|
|
202 |
builder.and( |
|
203 |
builder.eq( |
|
204 |
builder.ST_SRID( builder.variable("geom") ), |
|
205 |
builder.srs(proj) |
|
206 |
) |
|
207 |
); |
|
208 |
|
|
209 |
System.out.println(builder.toString()); |
|
210 |
assertEquals( |
|
211 |
"( (ST_SRID(\"geom\")) = (4326) )", |
|
212 |
builder.toString() |
|
213 |
); |
|
214 |
assertEquals( |
|
215 |
"[geom]", |
|
216 |
ArrayUtils.toString(getVariableNames(builder)) |
|
217 |
); |
|
218 |
assertEquals( |
|
219 |
"[]", |
|
220 |
ArrayUtils.toString(getParameterNames(builder)) |
|
221 |
); |
|
222 |
} |
|
223 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/java/org/gvsig/expresionevaluator/impl/TestLoadResource.java | ||
---|---|---|
1 |
package org.gvsig.expresionevaluator.impl; |
|
2 |
|
|
3 |
import static junit.framework.Assert.assertEquals; |
|
4 |
import junit.framework.TestCase; |
|
5 |
import org.gvsig.expressionevaluator.impl.ExpressionEvaluatorImplLibrary; |
|
6 |
import org.gvsig.expressionevaluator.Function; |
|
7 |
import org.gvsig.expressionevaluator.impl.function.numeric.ACosFunction; |
|
8 |
import org.gvsig.expressionevaluator.impl.function.spatial.STAreaFunction; |
|
9 |
|
|
10 |
/** |
|
11 |
* |
|
12 |
* @author jjdelcerro |
|
13 |
*/ |
|
14 |
public class TestLoadResource extends TestCase { |
|
15 |
|
|
16 |
public TestLoadResource(String testName) { |
|
17 |
super(testName); |
|
18 |
} |
|
19 |
|
|
20 |
@Override |
|
21 |
protected void setUp() throws Exception { |
|
22 |
super.setUp(); |
|
23 |
new ExpressionEvaluatorImplLibrary().initialize(); |
|
24 |
} |
|
25 |
|
|
26 |
@Override |
|
27 |
protected void tearDown() throws Exception { |
|
28 |
super.tearDown(); |
|
29 |
} |
|
30 |
|
|
31 |
// TODO add test methods here. The name must begin with 'test'. For example: |
|
32 |
// public void testHello() {} |
|
33 |
|
|
34 |
public void testFunctionExpression1() { |
|
35 |
Function fn = new STAreaFunction(); |
|
36 |
assertEquals("ST_Area({{geometry}})", fn.template()); |
|
37 |
} |
|
38 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/java/org/gvsig/expresionevaluator/impl/TestCompiler.java | ||
---|---|---|
47 | 47 |
|
48 | 48 |
Compiler compiler = createCompiler(); |
49 | 49 |
Code code = compiler.compileExpression(source); |
50 |
assertEquals("[precio]", code.toString());
|
|
50 |
assertEquals("\"precio\"", code.toString());
|
|
51 | 51 |
} |
52 | 52 |
|
53 | 53 |
public void testIdentifier2() { |
... | ... | |
55 | 55 |
|
56 | 56 |
Compiler compiler = createCompiler(); |
57 | 57 |
Code code = compiler.compileExpression(source); |
58 |
assertEquals("[precio]", code.toString());
|
|
58 |
assertEquals("\"precio\"", code.toString());
|
|
59 | 59 |
} |
60 | 60 |
|
61 | 61 |
public void testIdentifier3() { |
... | ... | |
63 | 63 |
|
64 | 64 |
Compiler compiler = createCompiler(); |
65 | 65 |
Code code = compiler.compileExpression(source); |
66 |
assertEquals("[precio]", code.toString());
|
|
66 |
assertEquals("\"precio\"", code.toString());
|
|
67 | 67 |
} |
68 | 68 |
|
69 | 69 |
|
... | ... | |
112 | 112 |
|
113 | 113 |
Compiler compiler = createCompiler(); |
114 | 114 |
Code code = compiler.compileExpression(source); |
115 |
assertEquals(operatorName+"([precio], 23)", code.toString());
|
|
115 |
assertEquals(operatorName+"(\"precio\", 23)", code.toString());
|
|
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
public void testOperators() { |
... | ... | |
141 | 141 |
Compiler compiler = createCompiler(); |
142 | 142 |
|
143 | 143 |
Code code = compiler.compileExpression(source); |
144 |
assertEquals("+(+(+([precio], *(10, 2)), 20), 30)", code.toString());
|
|
144 |
assertEquals("+(+(+(\"precio\", *(10, 2)), 20), 30)", code.toString());
|
|
145 | 145 |
} |
146 | 146 |
|
147 | 147 |
public void testAddMulPar() { |
... | ... | |
150 | 150 |
Compiler compiler = createCompiler(); |
151 | 151 |
|
152 | 152 |
Code code = compiler.compileExpression(source); |
153 |
assertEquals("+(+(*(+([precio], 10), 2), 20), 30)", code.toString());
|
|
153 |
assertEquals("+(+(*(+(\"precio\", 10), 2), 20), 30)", code.toString());
|
|
154 | 154 |
} |
155 | 155 |
|
156 | 156 |
public void testAbs() { |
... | ... | |
159 | 159 |
Compiler compiler = createCompiler(); |
160 | 160 |
|
161 | 161 |
Code code = compiler.compileExpression(source); |
162 |
assertEquals("+([precio], abs(10))", code.toString());
|
|
162 |
assertEquals("+(\"precio\", abs(10))", code.toString());
|
|
163 | 163 |
} |
164 | 164 |
|
165 | 165 |
public void testAbs2() { |
... | ... | |
168 | 168 |
Compiler compiler = createCompiler(); |
169 | 169 |
|
170 | 170 |
Code code = compiler.compileExpression(source); |
171 |
assertEquals("+([precio], abs(-10))", code.toString());
|
|
171 |
assertEquals("+(\"precio\", abs(-10))", code.toString());
|
|
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
public void testPI() { |
... | ... | |
177 | 177 |
Compiler compiler = createCompiler(); |
178 | 178 |
|
179 | 179 |
Code code = compiler.compileExpression(source); |
180 |
assertEquals("+([precio], PI())", code.toString());
|
|
180 |
assertEquals("+(\"precio\", PI())", code.toString());
|
|
181 | 181 |
} |
182 | 182 |
|
183 | 183 |
public void testCeil() { |
... | ... | |
186 | 186 |
Compiler compiler = createCompiler(); |
187 | 187 |
|
188 | 188 |
Code code = compiler.compileExpression(source); |
189 |
assertEquals("+([precio], CEIL(PI()))", code.toString());
|
|
189 |
assertEquals("+(\"precio\", CEIL(PI()))", code.toString());
|
|
190 | 190 |
} |
191 | 191 |
|
192 | 192 |
public void testConcat() { |
... | ... | |
195 | 195 |
Compiler compiler = createCompiler(); |
196 | 196 |
|
197 | 197 |
Code code = compiler.compileExpression(source); |
198 |
assertEquals("CONCAT([precio], ' euros')", code.toString());
|
|
198 |
assertEquals("CONCAT(\"precio\", ' euros')", code.toString());
|
|
199 | 199 |
} |
200 | 200 |
|
201 | 201 |
public void test1() { |
... | ... | |
204 | 204 |
Compiler compiler = createCompiler(); |
205 | 205 |
|
206 | 206 |
Code code = compiler.compileExpression(source); |
207 |
assertEquals("=([NOMBRE03], 'Torre d'En Besora (la)')", code.toString());
|
|
207 |
assertEquals("=(\"NOMBRE03\", 'Torre d'En Besora (la)')", code.toString());
|
|
208 | 208 |
} |
209 | 209 |
|
210 | 210 |
public void test2() { |
... | ... | |
213 | 213 |
Compiler compiler = createCompiler(); |
214 | 214 |
|
215 | 215 |
Code code = compiler.compileExpression(source); |
216 |
assertEquals("=([1990], 0.168873933773767)", code.toString());
|
|
216 |
assertEquals("=(\"1990\", 0.168873933773767)", code.toString());
|
|
217 | 217 |
} |
218 | 218 |
|
219 | 219 |
public void test2fields() { |
... | ... | |
222 | 222 |
Compiler compiler = createCompiler(); |
223 | 223 |
|
224 | 224 |
Code code = compiler.compileExpression(source); |
225 |
assertEquals("=([1990], [precio])", code.toString());
|
|
225 |
assertEquals("=(\"1990\", \"precio\")", code.toString());
|
|
226 | 226 |
} |
227 | 227 |
|
228 | 228 |
|
... | ... | |
232 | 232 |
Compiler compiler = createCompiler(); |
233 | 233 |
|
234 | 234 |
Code code = compiler.compileExpression(source); |
235 |
assertEquals("ST_Area([GEOMETRY])", code.toString());
|
|
235 |
assertEquals("ST_Area(\"GEOMETRY\")", code.toString());
|
|
236 | 236 |
} |
237 | 237 |
|
238 | 238 |
public void testInvokeMethod1() { |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/java/org/gvsig/expresionevaluator/impl/TestCase1.java | ||
---|---|---|
1 |
package org.gvsig.expresionevaluator.impl; |
|
2 |
|
|
3 |
import static junit.framework.Assert.assertEquals; |
|
4 |
import junit.framework.TestCase; |
|
5 |
import org.cresques.cts.IProjection; |
|
6 |
import org.gvsig.expressionevaluator.Code; |
|
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Config; |
|
9 |
import org.gvsig.expressionevaluator.ExpressionBuilder.GeometrySupportType; |
|
10 |
import org.gvsig.expressionevaluator.LexicalAnalyzer; |
|
11 |
import org.gvsig.expressionevaluator.impl.DefaultCompiler; |
|
12 |
import org.gvsig.expressionevaluator.impl.DefaultExpressionBuilder; |
|
13 |
import org.gvsig.expressionevaluator.impl.SQLLexicalAnalyzer; |
|
14 |
import org.gvsig.fmap.crs.CRSFactory; |
|
15 |
import org.gvsig.fmap.geom.Geometry; |
|
16 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
17 |
import org.gvsig.fmap.geom.GeometryManager; |
|
18 |
import org.gvsig.fmap.geom.primitive.Point; |
|
19 |
import org.gvsig.fmap.geom.primitive.Polygon; |
|
20 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
|
21 |
import org.gvsig.expressionevaluator.Compiler; |
|
22 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
23 |
import org.gvsig.expressionevaluator.Interpreter; |
|
24 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
|
25 |
import org.gvsig.expressionevaluator.SymbolTable; |
|
26 |
import org.gvsig.expressionevaluator.impl.DefaultInterpreter; |
|
27 |
import org.gvsig.expressionevaluator.impl.DefaultSymbolTable; |
|
28 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
|
29 |
|
|
30 |
public class TestCase1 extends TestCase { |
|
31 |
|
|
32 |
public TestCase1(String testName) { |
|
33 |
super(testName); |
|
34 |
} |
|
35 |
|
|
36 |
@Override |
|
37 |
protected void setUp() throws Exception { |
|
38 |
super.setUp(); |
|
39 |
new DefaultLibrariesInitializer().fullInitialize(); |
|
40 |
// new ExpressionEvaluatorImplLibrary().initialize(); |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
44 |
protected void tearDown() throws Exception { |
|
45 |
super.tearDown(); |
|
46 |
} |
|
47 |
|
|
48 |
// TODO add test methods here. The name must begin with 'test'. For example: |
|
49 |
// public void testHello() {} |
|
50 |
|
|
51 |
protected LexicalAnalyzer createLexicalAnalyzer() { |
|
52 |
SQLLexicalAnalyzer lexer = new SQLLexicalAnalyzer(); |
|
53 |
return lexer; |
|
54 |
} |
|
55 |
|
|
56 |
protected Compiler createCompiler() { |
|
57 |
Compiler compiler = new DefaultCompiler(); |
|
58 |
compiler.setLexicalAnalyzer(createLexicalAnalyzer()); |
|
59 |
return compiler; |
|
60 |
} |
|
61 |
protected MutableSymbolTable createSymbolTable() { |
|
62 |
DefaultSymbolTable symbolTable = new DefaultSymbolTable(); |
|
63 |
return symbolTable; |
|
64 |
} |
|
65 |
|
|
66 |
protected Interpreter createInterpreter(SymbolTable symbolTable) { |
|
67 |
Interpreter interpreter = new DefaultInterpreter(); |
|
68 |
interpreter.setSymbolTable(symbolTable); |
|
69 |
return interpreter; |
|
70 |
} |
|
71 |
|
|
72 |
private String createExpr1() { |
|
73 |
ExpressionBuilder builder = new DefaultExpressionBuilder(); |
|
74 |
|
|
75 |
Value expr1 = builder.gt( |
|
76 |
builder.column("campo1"), |
|
77 |
builder.constant(10) |
|
78 |
); |
|
79 |
System.out.println(expr1.toString()); |
|
80 |
assertEquals( |
|
81 |
"( (\"campo1\") > (10) )", |
|
82 |
expr1.toString() |
|
83 |
); |
|
84 |
return expr1.toString(); |
|
85 |
} |
|
86 |
|
|
87 |
private Polygon createPolygon() throws CreateGeometryException { |
|
88 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
|
89 |
IProjection proj = CRSFactory.getCRS("EPSG:4326"); |
|
90 |
|
|
91 |
Polygon polygon = geometryManager.createPolygon(Geometry.SUBTYPES.GEOM2D); |
|
92 |
polygon.addVertex(0, 0); |
|
93 |
polygon.addVertex(0, 100); |
|
94 |
polygon.addVertex(100, 100); |
|
95 |
polygon.addVertex(100, 0); |
|
96 |
polygon.addVertex(0, 0); |
|
97 |
|
|
98 |
polygon.setProjection(proj); |
|
99 |
return polygon; |
|
100 |
} |
|
101 |
|
|
102 |
private Geometry createPoint(double x, double y) throws CreateGeometryException { |
|
103 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
|
104 |
IProjection proj = CRSFactory.getCRS("EPSG:4326"); |
|
105 |
Point point = geometryManager.createPoint(x, y, Geometry.SUBTYPES.GEOM2D); |
|
106 |
point.setProjection(proj); |
|
107 |
return point; |
|
108 |
} |
|
109 |
|
|
110 |
public void testBuildExpression() throws Exception { |
|
111 |
ExpressionBuilder builder = new DefaultExpressionBuilder(); |
|
112 |
|
|
113 |
Value expr2 = builder.and( |
|
114 |
builder.custom(this.createExpr1()), |
|
115 |
builder.ST_Intersects( |
|
116 |
builder.geometry(createPolygon()), |
|
117 |
builder.column("GEOMETRY") |
|
118 |
) |
|
119 |
); |
|
120 |
builder.getConfig().set(Config.geometry_type_support, GeometrySupportType.WKT); |
|
121 |
System.out.println(expr2.toString()); |
|
122 |
assertEquals( |
|
123 |
"( (\"campo1\") > (10) ) AND ST_Intersects((ST_GeomFromText('POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0))', (4326))), (\"GEOMETRY\"))", |
|
124 |
expr2.toString() |
|
125 |
); |
|
126 |
builder.getConfig().set(Config.geometry_type_support, GeometrySupportType.WKB); |
|
127 |
System.out.println(expr2.toString()); |
|
128 |
assertEquals( |
|
129 |
"( (\"campo1\") > (10) ) AND ST_Intersects((ST_GeomFromWKB((DECODE('000000000300000001000000050000000000000000000000000000000000000000000000004059000000000000405900000000000040590000000000004059000000000000000000000000000000000000000000000000000000000000','hex')), (4326))), (\"GEOMETRY\"))", |
|
130 |
expr2.toString() |
|
131 |
); |
|
132 |
} |
|
133 |
|
|
134 |
public void testCompileExpression() throws Exception { |
|
135 |
IProjection proj = CRSFactory.getCRS("EPSG:4326"); |
|
136 |
ExpressionBuilder builder = new DefaultExpressionBuilder(); |
|
137 |
|
|
138 |
Value expr2 = builder.and( |
|
139 |
builder.custom(this.createExpr1()), |
|
140 |
builder.ST_Intersects( |
|
141 |
builder.geometry(createPolygon()), |
|
142 |
builder.column("GEOMETRY") |
|
143 |
) |
|
144 |
); |
|
145 |
|
|
146 |
Compiler compiler = createCompiler(); |
|
147 |
Code code; |
|
148 |
|
|
149 |
builder.getConfig().set(Config.geometry_type_support, GeometrySupportType.WKT); |
|
150 |
code = compiler.compileExpression(expr2.toString()); |
|
151 |
System.out.println(code.toString()); |
|
152 |
assertEquals( |
|
153 |
"and(>(\"campo1\", 10), ST_Intersects(ST_GeomFromText('POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0))', 4326), \"GEOMETRY\"))", |
|
154 |
code.toString() |
|
155 |
); |
|
156 |
|
|
157 |
builder.getConfig().set(Config.geometry_type_support, GeometrySupportType.WKB); |
|
158 |
code = compiler.compileExpression(expr2.toString()); |
|
159 |
System.out.println(code.toString()); |
|
160 |
assertEquals( |
|
161 |
"and(>(\"campo1\", 10), ST_Intersects(ST_GeomFromWKB(DECODE('000000000300000001000000050000000000000000000000000000000000000000000000004059000000000000405900000000000040590000000000004059000000000000000000000000000000000000000000000000000000000000', 'hex'), 4326), \"GEOMETRY\"))", |
|
162 |
code.toString() |
|
163 |
); |
|
164 |
|
|
165 |
} |
|
166 |
|
|
167 |
public void testRunExpression() throws Exception { |
|
168 |
|
|
169 |
ExpressionBuilder builder = new DefaultExpressionBuilder(); |
|
170 |
builder.getConfig().set(Config.geometry_type_support, GeometrySupportType.WKB); |
|
171 |
|
|
172 |
String expr2 = builder.and( |
|
173 |
builder.custom(this.createExpr1()), |
|
174 |
builder.ST_Intersects( |
|
175 |
builder.geometry(createPolygon()), |
|
176 |
builder.column("GEOMETRY") |
|
177 |
) |
|
178 |
).toString(); |
|
179 |
|
|
180 |
Compiler compiler = createCompiler(); |
|
181 |
Code code = compiler.compileExpression(expr2); |
|
182 |
|
|
183 |
MutableSymbolTable symbolTable = createSymbolTable(); |
|
184 |
Interpreter interpreter = createInterpreter(symbolTable); |
|
185 |
Object v; |
|
186 |
|
|
187 |
symbolTable.setVar("campo1", 11); |
|
188 |
symbolTable.setVar("GEOMETRY", createPoint(50, 50)); |
|
189 |
v = interpreter.run(code); |
|
190 |
assertEquals(true, ((Boolean)v).booleanValue()); |
|
191 |
|
|
192 |
symbolTable.setVar("campo1", 9); |
|
193 |
symbolTable.setVar("GEOMETRY", createPoint(50, 50)); |
|
194 |
v = interpreter.run(code); |
|
195 |
assertEquals(false, ((Boolean)v).booleanValue()); |
|
196 |
|
|
197 |
symbolTable.setVar("campo1", 11); |
|
198 |
symbolTable.setVar("GEOMETRY", createPoint(150, 50)); |
|
199 |
v = interpreter.run(code); |
|
200 |
assertEquals(false, ((Boolean)v).booleanValue()); |
|
201 |
} |
|
202 |
|
|
203 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/resources/org/gvsig/expressionevaluator/functions/en/ST_Contains.json | ||
---|---|---|
1 |
{ |
|
2 |
"name": "ST_Contains", |
|
3 |
"group": "OGC", |
|
4 |
"template": "ST_Contains({{geometryA}}, geometryB)", |
|
5 |
"args": [ |
|
6 |
"geometryA - A geometry", |
|
7 |
"geometryB - A geometry" |
|
8 |
], |
|
9 |
"description": "Geometry A contains Geometry B if and only if no points of B lie in the exterior of A, and at least one point of the interior of B lies in the interior of A. An important subtlety of this definition is that A does not contain its boundary, but A does contain itself.\nReturns TRUE if geometry B is completely inside geometry A. For this function to make sense, the source geometries must both be of the same coordinate projection, having the same SRID. ST_Contains is the inverse of ST_Within. So ST_Contains(A,B) implies ST_Within(B,A) except in the case of invalid geometries where the result is always false regardless or not defined.", |
|
10 |
"returnType": "Boolean" |
|
11 |
} |
|
12 |
|
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/resources/org/gvsig/expressionevaluator/functions/en/ACOS.json | ||
---|---|---|
1 |
{ |
|
2 |
"name": "ACOS", |
|
3 |
"group": "Numeric", |
|
4 |
"template": "ACOS({{a}})", |
|
5 |
"args": [ |
|
6 |
"value whose arc tangent is to be returned" |
|
7 |
], |
|
8 |
"description": "Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.", |
|
9 |
"returnType": "Double" |
|
10 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/resources/org/gvsig/expressionevaluator/functions/en/ST_Covers.json | ||
---|---|---|
1 |
{ |
|
2 |
"name": "ST_Covers", |
|
3 |
"group": "OGC", |
|
4 |
"template": "ST_Covers({{geometryA}}, geometryB)", |
|
5 |
"args": [ |
|
6 |
"geometryA - A geometry", |
|
7 |
"geometryB - A geometry" |
|
8 |
], |
|
9 |
"description": "Returns TRUE if no point in Geometry B is outside Geometry A", |
|
10 |
"returnType": "Boolean" |
|
11 |
} |
|
12 |
|
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/resources/org/gvsig/expressionevaluator/functions/en/ST_Crosses.json | ||
---|---|---|
1 |
{ |
|
2 |
"name": "ST_Crosses", |
|
3 |
"group": "OGC", |
|
4 |
"template": "ST_Crosses({{geometryA}}, geometryB)", |
|
5 |
"args": [ |
|
6 |
"geometryA - A geometry", |
|
7 |
"geometryB - A geometry" |
|
8 |
], |
|
9 |
"description": [ |
|
10 |
"ST_Crosses takes two geometry objects and returns TRUE if their", |
|
11 |
"intersection 'spatially cross', that is, the geometries have some,", |
|
12 |
"but not all interior points in common. The intersection of the interiors", |
|
13 |
"of the geometries must not be the empty set and must have a dimensionality", |
|
14 |
"less than the maximum dimension of the two input geometries.", |
|
15 |
"Additionally, the intersection of the two geometries must not equal either", |
|
16 |
"of the source geometries. Otherwise, it returns FALSE.\n", |
|
17 |
"The following illustrations all return TRUE.\n", |
|
18 |
"<img src=\"@@@.d/st_crosses01.png\">\n", |
|
19 |
"MULTIPOINT/LINESTRING\n", |
|
20 |
"\n", |
|
21 |
"<img src=\"@@@.d/st_crosses02.png\">\n", |
|
22 |
"MULTIPOINT/POLYGON\n", |
|
23 |
"\n", |
|
24 |
"<img src=\"@@@.d/st_crosses03.png\">\n", |
|
25 |
"LINESTRING/POLYGON\n", |
|
26 |
"\n", |
|
27 |
"<img src=\"@@@.d/st_crosses04.png\">\n", |
|
28 |
"LINESTRING/LINESTRING\n" |
|
29 |
], |
|
30 |
"returnType": "Boolean" |
|
31 |
} |
|
32 |
|
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/resources/org/gvsig/expressionevaluator/functions/en/ST_CoveredBy.json | ||
---|---|---|
1 |
{ |
|
2 |
"name": "ST_CoveredBy", |
|
3 |
"group": "OGC", |
|
4 |
"template": "ST_CoveredBy({{geometryA}}, geometryB)", |
|
5 |
"args": [ |
|
6 |
"geometryA - A geometry", |
|
7 |
"geometryB - A geometry" |
|
8 |
], |
|
9 |
"description": "Returns TRUE if no point in Geometry A is outside Geometry B", |
|
10 |
"returnType": "Boolean" |
|
11 |
} |
|
12 |
|
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/resources/org/gvsig/expressionevaluator/functions/en/ST_Centroid.json | ||
---|---|---|
1 |
{ |
|
2 |
"name": "ST_Centroid", |
|
3 |
"group": "OGC", |
|
4 |
"template": "ST_Centroid({{geometry}})", |
|
5 |
"args": [ |
|
6 |
"geometry - Geometry from which you have to calculate your centroid" |
|
7 |
], |
|
8 |
"description": "Computes the geometric center of a geometry, or equivalently, the center of mass of the geometry as a POINT", |
|
9 |
"returnType": "Geometry" |
|
10 |
} |
|
11 |
|
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/resources/org/gvsig/expressionevaluator/functions/en/ST_ConvexHull.json | ||
---|---|---|
1 |
{ |
|
2 |
"name": "ST_ConvexHull", |
|
3 |
"group": "OGC", |
|
4 |
"template": "ST_ConvexHull({{geometry}})", |
|
5 |
"args": [ |
|
6 |
"geometry - A geometry" |
|
7 |
], |
|
8 |
"description": "The convex hull of a geometry represents the minimum convex geometry that encloses all geometries within the set.\nOne can think of the convex hull as the geometry you get by wrapping an elastic band around a set of geometries. This is different from a concave hull which is analogous to shrink-wrapping your geometries.", |
|
9 |
"returnType": "Geometry" |
|
10 |
} |
|
11 |
|
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/resources/org/gvsig/expressionevaluator/functions/en/ST_Area.json | ||
---|---|---|
1 |
{ |
|
2 |
"name": "ST_Area", |
|
3 |
"group": "OGC", |
|
4 |
"template": "ST_Area({{geometry}})", |
|
5 |
"args": [ |
|
6 |
"geometry - Geometry from which you have to calculate your area" |
|
7 |
], |
|
8 |
"description": "Returns the area of the geometry if it is a Polygon or MultiPolygon. Return the area measurement of an ST_Surface or ST_MultiSurface value. For geometry, a 2D Cartesian area is determined with units specified by the SRID.", |
|
9 |
"returnType": "Double" |
|
10 |
} |
|
11 |
|
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/resources/org/gvsig/expressionevaluator/functions/en/ST_AsText.json | ||
---|---|---|
1 |
{ |
|
2 |
"name": "ST_AsText", |
|
3 |
"group": "OGC", |
|
4 |
"template": "ST_AsText({{geometry}})", |
|
5 |
"args": [ |
|
6 |
"geometry - Geometry from which you have to calculate your WKT" |
|
7 |
], |
|
8 |
"description": "Returns the Well-Known Text representation of the geometry.\nWKT format does not maintain precision so to prevent floating truncation, use ST_AsBinary or ST_AsEWKB format for transport.", |
|
9 |
"returnType": "String" |
|
10 |
} |
|
11 |
|
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/resources/org/gvsig/expressionevaluator/functions/en/ST_Buffer.json | ||
---|---|---|
1 |
{ |
|
2 |
"name": "ST_Buffer", |
|
3 |
"group": "OGC", |
|
4 |
"template": "ST_Buffer({{geometry}}, radius)", |
|
5 |
"args": [ |
|
6 |
"geometry - Geometry from which you have to calculate your buffer" |
|
7 |
"radius - radius of the buffer" |
|
8 |
], |
|
9 |
"description": "Returns a geometry that represents all points whose distance from this Geometry is less than or equal to distance.", |
|
10 |
"returnType": "Geometry" |
|
11 |
} |
|
12 |
|
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/resources/org/gvsig/expressionevaluator/functions/en/ST_Difference.json | ||
---|---|---|
1 |
{ |
|
2 |
"name": "ST_Difference", |
|
3 |
"group": "OGC", |
|
4 |
"template": "ST_Difference({{geometryA}}, geometryB)", |
|
5 |
"args": [ |
|
6 |
"geometryA - A geometry", |
|
7 |
"geometryB - A geometry" |
|
8 |
], |
|
9 |
"description": "Returns a geometry that represents that part of geometry A that does not intersect with geometry B. One can think of this as GeometryA - ST_Intersection(A,B). If A is completely contained in B then an empty geometry collection is returned.", |
|
10 |
"returnType": "Geometry" |
|
11 |
} |
|
12 |
|
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/string/DecodeFunction.java | ||
---|---|---|
1 |
package org.gvsig.expressionevaluator.impl.function.string; |
|
2 |
|
|
3 |
import org.apache.commons.codec.binary.Base64; |
|
4 |
import org.apache.commons.codec.binary.Hex; |
|
5 |
import org.apache.commons.lang3.Range; |
|
6 |
import org.gvsig.expressionevaluator.Interpreter; |
|
7 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
|
8 |
|
|
9 |
public class DecodeFunction extends AbstractFunction { |
|
10 |
|
|
11 |
public DecodeFunction() { |
|
12 |
super( |
|
13 |
"String", "DECODE", Range.is(2), |
|
14 |
"Decode binary data from textual representation in string.\nSupported formats are: base64, hex.", |
|
15 |
"DECODE({{data}}, 'hex')", |
|
16 |
new String[]{ |
|
17 |
"data - A string value with the data to be converted", |
|
18 |
"format - A string value indicating the format of the input data. The default value is 'hex'." |
|
19 |
}, |
|
20 |
"Byte array", |
|
21 |
true |
|
22 |
); |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
|
27 |
String data_s = getStr(args,0); |
|
28 |
String type = getStr(args,1); |
|
29 |
if( "hex".equalsIgnoreCase(type) ) { |
|
30 |
byte[] data = Hex.decodeHex(data_s.toCharArray()); |
|
31 |
return data; |
|
32 |
} else if( "base64".equalsIgnoreCase(type) ) { |
|
33 |
byte[] data = Base64.decodeBase64(data_s); |
|
34 |
return data; |
|
35 |
} else if( "escape".equalsIgnoreCase(type) ) { |
|
36 |
// Not supported |
|
37 |
} |
|
38 |
throw new IllegalArgumentException("Unsupported format type '"+type+"'."); |
|
39 |
} |
|
40 |
|
|
41 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/spatial/STSRIDFunction.java | ||
---|---|---|
1 |
package org.gvsig.expressionevaluator.impl.function.spatial; |
|
2 |
|
|
3 |
import org.apache.commons.lang3.Range; |
|
4 |
import org.cresques.cts.IProjection; |
|
5 |
import org.gvsig.expressionevaluator.Interpreter; |
|
6 |
import org.gvsig.expressionevaluator.impl.ProjectionUtils; |
|
7 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
|
8 |
import org.gvsig.fmap.geom.Geometry; |
|
9 |
|
|
10 |
public class STSRIDFunction extends AbstractFunction { |
|
11 |
|
|
12 |
public STSRIDFunction() { |
|
13 |
super("OGC", "ST_SRID", Range.is(1)); |
|
14 |
} |
|
15 |
|
|
16 |
@Override |
|
17 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
|
18 |
Geometry geom = getGeom(args, 0); |
|
19 |
IProjection proj = geom.getProjection(); |
|
20 |
return ProjectionUtils.getCode(proj); |
|
21 |
} |
|
22 |
|
|
23 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/spatial/STGeomFromTextFunction.java | ||
---|---|---|
1 | 1 |
package org.gvsig.expressionevaluator.impl.function.spatial; |
2 | 2 |
|
3 | 3 |
import org.apache.commons.lang3.Range; |
4 |
import org.cresques.cts.IProjection; |
|
4 | 5 |
import org.gvsig.expressionevaluator.Interpreter; |
5 | 6 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
7 |
import org.gvsig.fmap.crs.CRSFactory; |
|
6 | 8 |
import org.gvsig.fmap.geom.Geometry; |
7 | 9 |
import org.gvsig.fmap.geom.GeometryLocator; |
8 | 10 |
import org.gvsig.fmap.geom.GeometryManager; |
... | ... | |
10 | 12 |
public class STGeomFromTextFunction extends AbstractFunction { |
11 | 13 |
|
12 | 14 |
public STGeomFromTextFunction() { |
13 |
super("OGC", "ST_GeomFromText", Range.is(1));
|
|
15 |
super("OGC", "ST_GeomFromText", Range.between(1,2));
|
|
14 | 16 |
} |
15 | 17 |
|
16 | 18 |
@Override |
... | ... | |
18 | 20 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
19 | 21 |
String wkt = getStr(args, 0); |
20 | 22 |
Geometry r = geomManager.createFrom(wkt); |
23 |
if( args.length==2 ) { |
|
24 |
int code = getInt(args, 1); |
|
25 |
IProjection proj = CRSFactory.getCRS("EPSG:"+code); |
|
26 |
r.setProjection(proj); |
|
27 |
} |
|
21 | 28 |
return r; |
22 | 29 |
} |
23 | 30 |
|
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/spatial/STGeomFromWKBFunction.java | ||
---|---|---|
1 |
package org.gvsig.expressionevaluator.impl.function.spatial; |
|
2 |
|
|
3 |
import org.apache.commons.lang3.Range; |
|
4 |
import org.cresques.cts.IProjection; |
|
5 |
import org.gvsig.expressionevaluator.Interpreter; |
|
6 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
|
7 |
import org.gvsig.fmap.crs.CRSFactory; |
|
8 |
import org.gvsig.fmap.geom.Geometry; |
|
9 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
10 |
import org.gvsig.fmap.geom.GeometryManager; |
|
11 |
|
|
12 |
public class STGeomFromWKBFunction extends AbstractFunction { |
|
13 |
|
|
14 |
public STGeomFromWKBFunction() { |
|
15 |
super("OGC", "ST_GeomFromWKB", Range.between(1,2)); |
|
16 |
} |
|
17 |
|
|
18 |
@Override |
|
19 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
|
20 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
|
21 |
byte[] wkb = (byte[]) getObject(args, 0); |
|
22 |
Geometry r = geomManager.createFrom(wkb); |
|
23 |
if( args.length==2 ) { |
Also available in: Unified diff