svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.geometry / org.gvsig.expressionevaluator.geometry.lib / org.gvsig.expressionevaluator.geometry.lib.impl / src / test / java / org / gvsig / expresionevaluator / impl / TestExpressionBuilder.java @ 45985
History | View | Annotate | Download (18.6 KB)
1 |
package org.gvsig.expresionevaluator.impl; |
---|---|
2 |
|
3 |
import junit.framework.TestCase; |
4 |
import org.apache.commons.lang3.ArrayUtils; |
5 |
import org.cresques.cts.IProjection; |
6 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
7 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilderHelper.GeometrySupportType; |
8 |
import org.gvsig.expressionevaluator.GeometryExpressionUtils; |
9 |
import org.gvsig.fmap.crs.CRSFactory; |
10 |
import org.gvsig.fmap.geom.Geometry; |
11 |
import org.gvsig.fmap.geom.GeometryLocator; |
12 |
import org.gvsig.fmap.geom.GeometryManager; |
13 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
14 |
import org.gvsig.fmap.geom.primitive.Point; |
15 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
16 |
|
17 |
public class TestExpressionBuilder extends TestCase { |
18 |
|
19 |
public TestExpressionBuilder(String testName) { |
20 |
super(testName);
|
21 |
} |
22 |
|
23 |
@Override
|
24 |
protected void setUp() throws Exception { |
25 |
super.setUp();
|
26 |
new DefaultLibrariesInitializer().fullInitialize();
|
27 |
} |
28 |
|
29 |
@Override
|
30 |
protected void tearDown() throws Exception { |
31 |
super.tearDown();
|
32 |
} |
33 |
|
34 |
protected GeometryExpressionBuilder createExpressionBuilder() {
|
35 |
GeometryExpressionBuilder builder = GeometryExpressionUtils.createExpressionBuilder(); |
36 |
return builder;
|
37 |
} |
38 |
|
39 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
40 |
// public void testHello() {}
|
41 |
|
42 |
public void test2() { |
43 |
GeometryExpressionBuilder builder = createExpressionBuilder(); |
44 |
|
45 |
builder.and( |
46 |
builder.eq( |
47 |
builder.lower(builder.variable("colum_name_c")),
|
48 |
builder.parameter("colum_name_p")
|
49 |
) |
50 |
); |
51 |
builder.and( |
52 |
builder.group( |
53 |
builder.or( |
54 |
builder.like( |
55 |
builder.lower( builder.variable("uno")),
|
56 |
builder.constant("%10")
|
57 |
), |
58 |
builder.lt( |
59 |
builder.variable("dos"),
|
60 |
builder.constant(-3.5)
|
61 |
) |
62 |
) |
63 |
) |
64 |
); |
65 |
builder.and( |
66 |
builder.ST_Intersects( |
67 |
builder.variable("geom1"),
|
68 |
builder.ST_Envelope( |
69 |
builder.ST_GeomFromWKB( |
70 |
builder.parameter("geom2"),
|
71 |
builder.parameter().value(4326).as_constant()
|
72 |
) |
73 |
) |
74 |
) |
75 |
); |
76 |
builder.and( |
77 |
builder.gt( |
78 |
builder.variable("tres"),
|
79 |
builder.constant(123456789)
|
80 |
) |
81 |
); |
82 |
assertEquals( |
83 |
"(((( (LOWER(\"colum_name_c\")) = (?) ) AND ( (( (LOWER(\"uno\")) LIKE ('%10') ) OR ( (\"dos\") < (-3.5) )) )) AND ST_Intersects((\"geom1\"), (ST_Envelope(ST_GeomFromWKB((?), (?)))))) AND ( (\"tres\") > (123456789) ))",
|
84 |
builder.toString() |
85 |
); |
86 |
assertEquals( |
87 |
"[colum_name_c, dos, geom1, tres, uno]",
|
88 |
ArrayUtils.toString(builder.variables_names()) |
89 |
); |
90 |
assertEquals( |
91 |
"[\"colum_name_p\", \"geom2\", 4326]",
|
92 |
ArrayUtils.toString(builder.parameters_names()) |
93 |
); |
94 |
} |
95 |
|
96 |
public void test3() throws CreateGeometryException { |
97 |
GeometryExpressionBuilder builder = createExpressionBuilder(); |
98 |
|
99 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
100 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
101 |
|
102 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
103 |
builder.set( |
104 |
builder.ST_Intersects( |
105 |
builder.geometry(point,proj), |
106 |
builder.variable("the_geom")
|
107 |
) |
108 |
); |
109 |
builder.geometry_support_type(GeometrySupportType.WKT); |
110 |
System.out.println(builder.toString());
|
111 |
assertEquals( |
112 |
"ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (\"the_geom\"))",
|
113 |
builder.toString() |
114 |
); |
115 |
builder.geometry_support_type(GeometrySupportType.WKB); |
116 |
System.out.println(builder.toString());
|
117 |
assertEquals( |
118 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (\"the_geom\"))",
|
119 |
builder.toString() |
120 |
); |
121 |
assertEquals( |
122 |
"[the_geom]",
|
123 |
ArrayUtils.toString(builder.variables_names()) |
124 |
); |
125 |
assertEquals( |
126 |
"[]",
|
127 |
ArrayUtils.toString(builder.parameters_names()) |
128 |
); |
129 |
} |
130 |
|
131 |
public void testParameter() throws CreateGeometryException { |
132 |
GeometryExpressionBuilder builder = createExpressionBuilder(); |
133 |
|
134 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
135 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
136 |
|
137 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
138 |
builder.set( |
139 |
builder.ST_Intersects( |
140 |
builder.geometry(point,proj), |
141 |
builder.parameter("the_geom")
|
142 |
) |
143 |
); |
144 |
builder.geometry_support_type(GeometrySupportType.WKT); |
145 |
System.out.println(builder.toString());
|
146 |
assertEquals( |
147 |
"ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (?))",
|
148 |
builder.toString() |
149 |
); |
150 |
builder.geometry_support_type(GeometrySupportType.WKB); |
151 |
System.out.println(builder.toString());
|
152 |
assertEquals( |
153 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (?))",
|
154 |
builder.toString() |
155 |
); |
156 |
assertEquals( |
157 |
"[]",
|
158 |
ArrayUtils.toString(builder.variables_names()) |
159 |
); |
160 |
assertEquals( |
161 |
"[\"the_geom\"]",
|
162 |
ArrayUtils.toString(builder.parameters_names()) |
163 |
); |
164 |
} |
165 |
|
166 |
public void testParameterGeomWhitoutSRS() throws CreateGeometryException { |
167 |
GeometryExpressionBuilder builder = createExpressionBuilder(); |
168 |
|
169 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
170 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
171 |
|
172 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
173 |
builder.set( |
174 |
builder.ST_Intersects( |
175 |
builder.geometry(point,proj), |
176 |
builder.parameter("the_geom").as_geometry_variable()
|
177 |
) |
178 |
); |
179 |
try {
|
180 |
builder.toString(); |
181 |
fail("Geometry parameter without SRS not detected.");
|
182 |
} catch(IllegalArgumentException ex) { |
183 |
// Do nothing, it's ok that fail if not SRS is specified.
|
184 |
} |
185 |
} |
186 |
|
187 |
public void testPatameterGeomWithSRS1() throws CreateGeometryException { |
188 |
GeometryExpressionBuilder builder = createExpressionBuilder(); |
189 |
|
190 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
191 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
192 |
|
193 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
194 |
builder.set( |
195 |
builder.ST_Intersects( |
196 |
builder.geometry(point,proj), |
197 |
builder.parameter("the_geom").srs(proj)
|
198 |
) |
199 |
); |
200 |
builder.geometry_support_type(GeometrySupportType.WKT); |
201 |
System.out.println(builder.toString());
|
202 |
assertEquals( |
203 |
"ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (ST_GeomFromText((?), (4326))))",
|
204 |
builder.toString() |
205 |
); |
206 |
builder.geometry_support_type(GeometrySupportType.WKB); |
207 |
System.out.println(builder.toString());
|
208 |
assertEquals( |
209 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (ST_GeomFromWKB((?), (4326))))",
|
210 |
builder.toString() |
211 |
); |
212 |
assertEquals( |
213 |
"[]",
|
214 |
ArrayUtils.toString(builder.variables_names()) |
215 |
); |
216 |
assertEquals( |
217 |
"[\"the_geom\"]",
|
218 |
ArrayUtils.toString(builder.parameters_names()) |
219 |
); |
220 |
} |
221 |
|
222 |
public void testParameterGeomWithSRS2() throws CreateGeometryException { |
223 |
GeometryExpressionBuilder builder = createExpressionBuilder(); |
224 |
|
225 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
226 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
227 |
|
228 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
229 |
builder.set( |
230 |
builder.ST_Intersects( |
231 |
builder.geometry(point,proj), |
232 |
builder.parameter("the_geom").srs(builder.constant(4326)) |
233 |
) |
234 |
); |
235 |
builder.geometry_support_type(GeometrySupportType.WKT); |
236 |
System.out.println(builder.toString());
|
237 |
assertEquals( |
238 |
"ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (ST_GeomFromText((?), (4326))))",
|
239 |
builder.toString() |
240 |
); |
241 |
builder.geometry_support_type(GeometrySupportType.WKB); |
242 |
System.out.println(builder.toString());
|
243 |
assertEquals( |
244 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (ST_GeomFromWKB((?), (4326))))",
|
245 |
builder.toString() |
246 |
); |
247 |
assertEquals( |
248 |
"[]",
|
249 |
ArrayUtils.toString(builder.variables_names()) |
250 |
); |
251 |
assertEquals( |
252 |
"[\"the_geom\"]",
|
253 |
ArrayUtils.toString(builder.parameters_names()) |
254 |
); |
255 |
} |
256 |
|
257 |
public void testParameterGeomWithParameterSRS() throws CreateGeometryException { |
258 |
GeometryExpressionBuilder builder = createExpressionBuilder(); |
259 |
|
260 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
261 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
262 |
|
263 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
264 |
builder.set( |
265 |
builder.ST_Intersects( |
266 |
builder.geometry(point,proj), |
267 |
builder.parameter("the_geom").srs(
|
268 |
builder.parameter(proj) |
269 |
) |
270 |
) |
271 |
); |
272 |
builder.geometry_support_type(GeometrySupportType.WKT); |
273 |
System.out.println(builder.toString());
|
274 |
assertEquals( |
275 |
"ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (ST_GeomFromText((?), (?))))",
|
276 |
builder.toString() |
277 |
); |
278 |
builder.geometry_support_type(GeometrySupportType.WKB); |
279 |
System.out.println(builder.toString());
|
280 |
assertEquals( |
281 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (ST_GeomFromWKB((?), (?))))",
|
282 |
builder.toString() |
283 |
); |
284 |
assertEquals( |
285 |
"[]",
|
286 |
ArrayUtils.toString(builder.variables_names()) |
287 |
); |
288 |
assertEquals( |
289 |
"[\"the_geom\", 4326]",
|
290 |
ArrayUtils.toString(builder.parameters_names()) |
291 |
); |
292 |
} |
293 |
|
294 |
public void testParameterGeomConstant() throws CreateGeometryException { |
295 |
GeometryExpressionBuilder builder = createExpressionBuilder(); |
296 |
|
297 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
298 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
299 |
|
300 |
Point point = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
301 |
Point point2 = geometryManager.createPoint(10, 20, Geometry.SUBTYPES.GEOM2D); |
302 |
point2.setProjection(proj); |
303 |
builder.set( |
304 |
builder.ST_Intersects( |
305 |
builder.geometry(point,proj), |
306 |
builder.parameter().value(point2) |
307 |
) |
308 |
); |
309 |
builder.geometry_support_type(GeometrySupportType.WKT); |
310 |
System.out.println(builder.toString());
|
311 |
assertEquals( |
312 |
"ST_Intersects((ST_GeomFromText(('POINT (10 20)'), (4326))), (ST_GeomFromText((?), (?))))",
|
313 |
builder.toString() |
314 |
); |
315 |
builder.geometry_support_type(GeometrySupportType.WKB); |
316 |
System.out.println(builder.toString());
|
317 |
assertEquals( |
318 |
"ST_Intersects((ST_GeomFromWKB((DECODE('000000000140240000000000004034000000000000','hex')), (4326))), (ST_GeomFromWKB((?), (?))))",
|
319 |
builder.toString() |
320 |
); |
321 |
assertEquals( |
322 |
"[]",
|
323 |
ArrayUtils.toString(builder.variables_names()) |
324 |
); |
325 |
assertEquals( |
326 |
"[0x000000000140240000000000004034000000000000, 4326]",
|
327 |
ArrayUtils.toString(builder.parameters_names()) |
328 |
); |
329 |
} |
330 |
|
331 |
public void test4() throws CreateGeometryException { |
332 |
GeometryExpressionBuilder builder = createExpressionBuilder(); |
333 |
|
334 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
335 |
|
336 |
builder.and( |
337 |
builder.ST_Intersects( |
338 |
builder.variable("geom1"),
|
339 |
builder.ST_Envelope( |
340 |
builder.ST_GeomFromWKB( |
341 |
builder.parameter("geom2"),
|
342 |
builder.parameter().value(proj).as_constant() |
343 |
) |
344 |
) |
345 |
) |
346 |
); |
347 |
|
348 |
System.out.println(builder.toString());
|
349 |
assertEquals( |
350 |
"ST_Intersects((\"geom1\"), (ST_Envelope(ST_GeomFromWKB((?), (?)))))",
|
351 |
builder.toString() |
352 |
); |
353 |
assertEquals( |
354 |
"[geom1]",
|
355 |
ArrayUtils.toString(builder.variables_names()) |
356 |
); |
357 |
assertEquals( |
358 |
"[\"geom2\", 4326]",
|
359 |
ArrayUtils.toString(builder.parameters_names()) |
360 |
); |
361 |
} |
362 |
|
363 |
public void test5() throws CreateGeometryException { |
364 |
GeometryExpressionBuilder builder = createExpressionBuilder(); |
365 |
|
366 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
367 |
|
368 |
builder.and( |
369 |
builder.eq( |
370 |
builder.ST_SRID( builder.variable("geom") ),
|
371 |
builder.srs(proj) |
372 |
) |
373 |
); |
374 |
|
375 |
System.out.println(builder.toString());
|
376 |
assertEquals( |
377 |
"( (ST_SRID(\"geom\")) = (4326) )",
|
378 |
builder.toString() |
379 |
); |
380 |
assertEquals( |
381 |
"[geom]",
|
382 |
ArrayUtils.toString(builder.variables_names()) |
383 |
); |
384 |
assertEquals( |
385 |
"[]",
|
386 |
ArrayUtils.toString(builder.parameters_names()) |
387 |
); |
388 |
} |
389 |
|
390 |
public void test6() throws Exception { |
391 |
GeometryExpressionBuilder builder = createExpressionBuilder(); |
392 |
|
393 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
394 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
395 |
|
396 |
Point point = geometryManager.createPoint(-0.1779813, 51.6122315, Geometry.SUBTYPES.GEOM2D); |
397 |
Geometry point_buffer = point.buffer(0.001);
|
398 |
|
399 |
builder.or( |
400 |
builder.ST_Intersects( |
401 |
builder.ST_StartPoint(builder.column("geometry")),
|
402 |
builder.geometry(point_buffer, proj) |
403 |
) |
404 |
); |
405 |
builder.or( |
406 |
builder.ST_Intersects( |
407 |
builder.ST_EndPoint(builder.column("geometry")),
|
408 |
builder.geometry(point_buffer, proj) |
409 |
) |
410 |
); |
411 |
|
412 |
System.out.println(builder.toString());
|
413 |
assertEquals( |
414 |
"(ST_Intersects((ST_StartPoint(\"geometry\")), (ST_GeomFromWKB((DECODE('00000000030000000100000021bfc6a752bfc074d04049ce5d9a0f0a5fbfc6a7f3ef0c44504049ce573585c39cbfc6a9d14b36039c4049ce510fe0a08dbfc6acd87c00e8e74049ce4b65990cbfbfc6b0ebb72658ed4049ce466e6aca60bfc6b5e2e568b7fa4049ce425b2fa4f0bfc6bb8d2cfc85e24049ce3f53feda0abfc6c1b2d21f95294049ce3d76a2b04bbfc6c8175b6658244049ce3cd573647cbfc6ce7be4ad1b1f4049ce3d76a2b04bbfc6d4a189d02a664049ce3f53feda0abfc6da4bd163f84e4049ce425b2fa4f0bfc6df42ffa6575b4049ce466e6aca60bfc6e3563acbc7614049ce4b65990cbfbfc6e65d6b96acac4049ce510fe0a08dbfc6e83ac7c06bf84049ce573585c39cbfc6e8dbf70c3b784049ce5d9a0f0a5fbfc6e83ac7c06bf84049ce63fe985122bfc6e65d6b96acac4049ce6a243d7431bfc6e3563acbc7614049ce6fce8507ffbfc6df42ffa6575b4049ce74c5b34a5ebfc6da4bd163f84e4049ce78d8ee6fcebfc6d4a189d02a664049ce7be01f3ab4bfc6ce7be4ad1b1f4049ce7dbd7b6473bfc6c8175b6658244049ce7e5eaab042bfc6c1b2d21f95294049ce7dbd7b6473bfc6bb8d2cfc85e24049ce7be01f3ab4bfc6b5e2e568b7fa4049ce78d8ee6fcebfc6b0ebb72658ed4049ce74c5b34a5ebfc6acd87c00e8e74049ce6fce8507ffbfc6a9d14b36039b4049ce6a243d7431bfc6a7f3ef0c44504049ce63fe985122bfc6a752bfc074d04049ce5d9a0f0a5f','hex')), (4326)))) OR ST_Intersects((ST_EndPoint(\"geometry\")), (ST_GeomFromWKB((DECODE('00000000030000000100000021bfc6a752bfc074d04049ce5d9a0f0a5fbfc6a7f3ef0c44504049ce573585c39cbfc6a9d14b36039c4049ce510fe0a08dbfc6acd87c00e8e74049ce4b65990cbfbfc6b0ebb72658ed4049ce466e6aca60bfc6b5e2e568b7fa4049ce425b2fa4f0bfc6bb8d2cfc85e24049ce3f53feda0abfc6c1b2d21f95294049ce3d76a2b04bbfc6c8175b6658244049ce3cd573647cbfc6ce7be4ad1b1f4049ce3d76a2b04bbfc6d4a189d02a664049ce3f53feda0abfc6da4bd163f84e4049ce425b2fa4f0bfc6df42ffa6575b4049ce466e6aca60bfc6e3563acbc7614049ce4b65990cbfbfc6e65d6b96acac4049ce510fe0a08dbfc6e83ac7c06bf84049ce573585c39cbfc6e8dbf70c3b784049ce5d9a0f0a5fbfc6e83ac7c06bf84049ce63fe985122bfc6e65d6b96acac4049ce6a243d7431bfc6e3563acbc7614049ce6fce8507ffbfc6df42ffa6575b4049ce74c5b34a5ebfc6da4bd163f84e4049ce78d8ee6fcebfc6d4a189d02a664049ce7be01f3ab4bfc6ce7be4ad1b1f4049ce7dbd7b6473bfc6c8175b6658244049ce7e5eaab042bfc6c1b2d21f95294049ce7dbd7b6473bfc6bb8d2cfc85e24049ce7be01f3ab4bfc6b5e2e568b7fa4049ce78d8ee6fcebfc6b0ebb72658ed4049ce74c5b34a5ebfc6acd87c00e8e74049ce6fce8507ffbfc6a9d14b36039b4049ce6a243d7431bfc6a7f3ef0c44504049ce63fe985122bfc6a752bfc074d04049ce5d9a0f0a5f','hex')), (4326)))))",
|
415 |
builder.toString() |
416 |
); |
417 |
assertEquals( |
418 |
"[geometry]",
|
419 |
ArrayUtils.toString(builder.variables_names()) |
420 |
); |
421 |
assertEquals( |
422 |
"[]",
|
423 |
ArrayUtils.toString(builder.parameters_names()) |
424 |
); |
425 |
} |
426 |
} |