svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.tools.evaluator.sqljep / src / main / java / org / gvsig / tools / evaluator / sqljep / function / OpenGISFunctions.java @ 43777
History | View | Annotate | Download (15.8 KB)
1 |
package org.gvsig.tools.evaluator.sqljep.function; |
---|---|
2 |
|
3 |
import org.gvsig.fmap.geom.Geometry; |
4 |
import org.gvsig.fmap.geom.GeometryLocator; |
5 |
import org.gvsig.fmap.geom.GeometryManager; |
6 |
import org.gvsig.fmap.geom.aggregate.Aggregate; |
7 |
import org.gvsig.fmap.geom.primitive.OrientablePrimitive; |
8 |
import org.gvsig.fmap.geom.primitive.Point; |
9 |
import org.gvsig.fmap.geom.type.GeometryType; |
10 |
import org.gvsig.tools.ToolsLocator; |
11 |
import org.gvsig.tools.dataTypes.CoercionException; |
12 |
import org.gvsig.tools.dataTypes.DataTypes; |
13 |
import org.medfoster.sqljep.ASTFunNode; |
14 |
import org.medfoster.sqljep.BaseJEP; |
15 |
import org.medfoster.sqljep.JepRuntime; |
16 |
import org.medfoster.sqljep.ParseException; |
17 |
import org.medfoster.sqljep.function.PostfixCommand; |
18 |
|
19 |
public class OpenGISFunctions { |
20 |
|
21 |
public static BaseFunction[] functions = { |
22 |
new ST_Distance(),
|
23 |
new ST_Disjoint(),
|
24 |
new ST_Touches(),
|
25 |
new ST_Crosses(),
|
26 |
new ST_Within(),
|
27 |
new ST_Overlaps(),
|
28 |
new ST_Contains(),
|
29 |
new ST_Covers(),
|
30 |
new ST_CoveredBy(),
|
31 |
new ST_Intersects(),
|
32 |
new ST_Centroid(),
|
33 |
new ST_Area(),
|
34 |
// new ST_Length(),
|
35 |
// new ST_Boundary(),
|
36 |
new ST_Buffer(),
|
37 |
new ST_ConvexHull(),
|
38 |
new ST_Intersection(),
|
39 |
new ST_Difference(),
|
40 |
new ST_Union(),
|
41 |
new ST_AsText(),
|
42 |
// new ST_AsBinary(),
|
43 |
// new ST_SRID(),
|
44 |
new ST_Dimension(),
|
45 |
new ST_Envelope(),
|
46 |
new ST_IsSimple(),
|
47 |
// new ST_IsRing(),
|
48 |
new ST_NumGeometries(),
|
49 |
new ST_GeometryN(),
|
50 |
new ST_NumPoints(),
|
51 |
new ST_PointN(),
|
52 |
new ST_StartPoint(),
|
53 |
new ST_EndPoint(),
|
54 |
new ST_X(),
|
55 |
new ST_Y(),
|
56 |
new ST_Z(),
|
57 |
new ST_GeomFromText(),
|
58 |
new ST_isValid()
|
59 |
}; |
60 |
|
61 |
public static void registerFunctions() { |
62 |
for( int i=0; i<functions.length; i++ ) { |
63 |
BaseFunction f = functions[i]; |
64 |
BaseJEP.addFunction(f.getName(),f ); |
65 |
} |
66 |
} |
67 |
|
68 |
|
69 |
public static abstract class BaseFunction extends PostfixCommand { |
70 |
|
71 |
public String getName() { |
72 |
String name = this.getClass().getName(); |
73 |
int index = name.lastIndexOf("."); |
74 |
if ( index < 1 ) { |
75 |
return name;
|
76 |
} |
77 |
return name.substring(index + 1); |
78 |
} |
79 |
|
80 |
public Geometry pop_geometry(JepRuntime runtime)
|
81 |
throws ParseException { |
82 |
return (Geometry) runtime.stack.pop();
|
83 |
} |
84 |
|
85 |
public double pop_double(JepRuntime runtime) |
86 |
throws ParseException { |
87 |
Double value = null; |
88 |
try {
|
89 |
value = (Double) ToolsLocator.getDataTypesManager().coerce(
|
90 |
DataTypes.DOUBLE, runtime.stack.pop() |
91 |
); |
92 |
} catch (CoercionException ex) {
|
93 |
throw new ParseException(this.getName(), ex); |
94 |
} |
95 |
return value.doubleValue();
|
96 |
} |
97 |
|
98 |
public int pop_int(JepRuntime runtime) |
99 |
throws ParseException { |
100 |
Integer value = null; |
101 |
try {
|
102 |
value = (Integer) ToolsLocator.getDataTypesManager().coerce(
|
103 |
DataTypes.INT, runtime.stack.pop() |
104 |
); |
105 |
} catch (CoercionException ex) {
|
106 |
throw new ParseException(this.getName(), ex); |
107 |
} |
108 |
return value.intValue();
|
109 |
} |
110 |
|
111 |
public String pop_string(JepRuntime runtime) |
112 |
throws ParseException { |
113 |
String value = null; |
114 |
try {
|
115 |
value = (String) ToolsLocator.getDataTypesManager().coerce(
|
116 |
DataTypes.STRING, runtime.stack.pop() |
117 |
); |
118 |
} catch (CoercionException ex) {
|
119 |
throw new ParseException(this.getName(), ex); |
120 |
} |
121 |
return value;
|
122 |
} |
123 |
|
124 |
public void evaluate(ASTFunNode node, JepRuntime runtime) |
125 |
throws ParseException { |
126 |
try {
|
127 |
Comparable value = this.evaluate(runtime); |
128 |
runtime.stack.push(value); |
129 |
} catch (Exception ex) { |
130 |
throw new ParseException(getName(), ex); |
131 |
} |
132 |
} |
133 |
|
134 |
public abstract Comparable evaluate(JepRuntime runtime) throws Exception; |
135 |
} |
136 |
|
137 |
public static abstract class Function_geom1_geom2 extends BaseFunction { |
138 |
|
139 |
final public int getNumberOfParameters() { |
140 |
return 2; |
141 |
} |
142 |
|
143 |
public Comparable evaluate(JepRuntime runtime) |
144 |
throws Exception { |
145 |
Geometry geom1 = pop_geometry(runtime); |
146 |
Geometry geom2 = pop_geometry(runtime); |
147 |
return evaluate(geom1, geom2);
|
148 |
} |
149 |
|
150 |
public abstract Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception; |
151 |
} |
152 |
|
153 |
public static abstract class Function_geom1 extends BaseFunction { |
154 |
|
155 |
final public int getNumberOfParameters() { |
156 |
return 1; |
157 |
} |
158 |
|
159 |
public Comparable evaluate(JepRuntime runtime) |
160 |
throws Exception { |
161 |
Geometry geom1 = pop_geometry(runtime); |
162 |
return evaluate(geom1);
|
163 |
} |
164 |
|
165 |
public abstract Comparable evaluate(Geometry geom1) throws Exception; |
166 |
} |
167 |
|
168 |
public static abstract class Function_geom1_double extends BaseFunction { |
169 |
|
170 |
final public int getNumberOfParameters() { |
171 |
return 2; |
172 |
} |
173 |
|
174 |
public Comparable evaluate(JepRuntime runtime) |
175 |
throws Exception { |
176 |
Geometry geom1 = pop_geometry(runtime); |
177 |
double value = pop_double(runtime);
|
178 |
return evaluate(geom1, value);
|
179 |
} |
180 |
|
181 |
public abstract Comparable evaluate(Geometry geom1, double value) throws Exception; |
182 |
} |
183 |
|
184 |
public static abstract class Function_geom1_int extends BaseFunction { |
185 |
|
186 |
final public int getNumberOfParameters() { |
187 |
return 2; |
188 |
} |
189 |
|
190 |
public Comparable evaluate(JepRuntime runtime) |
191 |
throws Exception { |
192 |
Geometry geom1 = pop_geometry(runtime); |
193 |
int value = pop_int(runtime);
|
194 |
return evaluate(geom1, value);
|
195 |
} |
196 |
|
197 |
public abstract Comparable evaluate(Geometry geom1, int value) throws Exception; |
198 |
} |
199 |
|
200 |
public static abstract class Function_string extends BaseFunction { |
201 |
|
202 |
final public int getNumberOfParameters() { |
203 |
return 1; |
204 |
} |
205 |
|
206 |
public Comparable evaluate(JepRuntime runtime) |
207 |
throws Exception { |
208 |
String value = pop_string(runtime);
|
209 |
return evaluate(value);
|
210 |
} |
211 |
|
212 |
public abstract Comparable evaluate(String value) throws Exception; |
213 |
} |
214 |
|
215 |
public static class ST_Distance extends Function_geom1_geom2 { |
216 |
|
217 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
218 |
return Double.valueOf(geom1.distance(geom2)); |
219 |
} |
220 |
} |
221 |
|
222 |
public static class ST_Disjoint extends Function_geom1_geom2 { |
223 |
|
224 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
225 |
return Boolean.valueOf(geom1.disjoint(geom2)); |
226 |
} |
227 |
} |
228 |
|
229 |
public static class ST_Touches extends Function_geom1_geom2 { |
230 |
|
231 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
232 |
return Boolean.valueOf(geom1.touches(geom2)); |
233 |
} |
234 |
} |
235 |
|
236 |
public static class ST_Crosses extends Function_geom1_geom2 { |
237 |
|
238 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
239 |
return Boolean.valueOf(geom1.crosses(geom2)); |
240 |
} |
241 |
} |
242 |
|
243 |
public static class ST_Within extends Function_geom1_geom2 { |
244 |
|
245 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
246 |
return Boolean.valueOf(geom1.within(geom2)); |
247 |
} |
248 |
} |
249 |
|
250 |
public static class ST_Overlaps extends Function_geom1_geom2 { |
251 |
|
252 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
253 |
return Boolean.valueOf(geom1.overlaps(geom2)); |
254 |
} |
255 |
} |
256 |
|
257 |
public static class ST_Contains extends Function_geom1_geom2 { |
258 |
|
259 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
260 |
return Boolean.valueOf(geom1.contains(geom2)); |
261 |
} |
262 |
} |
263 |
|
264 |
public static class ST_Covers extends Function_geom1_geom2 { |
265 |
|
266 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
267 |
return Boolean.valueOf(geom1.covers(geom2)); |
268 |
} |
269 |
} |
270 |
|
271 |
public static class ST_CoveredBy extends Function_geom1_geom2 { |
272 |
|
273 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
274 |
return Boolean.valueOf(geom1.coveredBy(geom2)); |
275 |
} |
276 |
} |
277 |
|
278 |
public static class ST_Intersects extends Function_geom1_geom2 { |
279 |
|
280 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
281 |
return Boolean.valueOf(geom1.intersects(geom2)); |
282 |
} |
283 |
} |
284 |
|
285 |
public static class ST_Centroid extends Function_geom1 { |
286 |
|
287 |
public Comparable evaluate(Geometry geom1) throws Exception { |
288 |
return geom1.centroid();
|
289 |
} |
290 |
} |
291 |
|
292 |
public static class ST_Area extends Function_geom1 { |
293 |
|
294 |
public Comparable evaluate(Geometry geom1) throws Exception { |
295 |
return Double.valueOf(geom1.area()); |
296 |
} |
297 |
} |
298 |
|
299 |
// public static class ST_Length extends Function_geom1 {
|
300 |
//
|
301 |
// public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception {
|
302 |
// return Double.valueOf(geom1.length());
|
303 |
// }
|
304 |
// }
|
305 |
// public static class ST_Boundary extends Function_geom1 {
|
306 |
//
|
307 |
// public Comparable evaluate(Geometry geom1) throws Exception {
|
308 |
// return geom1.boundary();
|
309 |
// }
|
310 |
// }
|
311 |
public static class ST_Buffer extends Function_geom1_double { |
312 |
|
313 |
public Comparable evaluate(Geometry geom1, double value) throws Exception { |
314 |
return geom1.buffer(value);
|
315 |
} |
316 |
} |
317 |
|
318 |
public static class ST_ConvexHull extends Function_geom1 { |
319 |
|
320 |
public Comparable evaluate(Geometry geom1) throws Exception { |
321 |
return geom1.convexHull();
|
322 |
} |
323 |
} |
324 |
|
325 |
public static class ST_Intersection extends Function_geom1_geom2 { |
326 |
|
327 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
328 |
return geom1.intersection(geom2);
|
329 |
} |
330 |
} |
331 |
|
332 |
public static class ST_Difference extends Function_geom1_geom2 { |
333 |
|
334 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
335 |
return geom1.difference(geom2);
|
336 |
} |
337 |
} |
338 |
|
339 |
public static class ST_Union extends Function_geom1_geom2 { |
340 |
|
341 |
public Comparable evaluate(Geometry geom1, Geometry geom2) throws Exception { |
342 |
return geom1.union(geom2);
|
343 |
} |
344 |
} |
345 |
|
346 |
public static class ST_AsText extends Function_geom1 { |
347 |
|
348 |
public Comparable evaluate(Geometry geom1) throws Exception { |
349 |
return geom1.convertToWKT();
|
350 |
} |
351 |
} |
352 |
|
353 |
// public static class ST_AsBinary extends Function_geom1 {
|
354 |
//
|
355 |
// public Comparable evaluate(Geometry geom1) throws Exception {
|
356 |
// byte[] bytes = geom1.convertToWKB();
|
357 |
// return bytes;
|
358 |
// }
|
359 |
// }
|
360 |
// public static class ST_SRID extends Function_geom1 {
|
361 |
//
|
362 |
// public Comparable evaluate(Geometry geom1) throws Exception {
|
363 |
// return geom1.getCRS();
|
364 |
// }
|
365 |
// }
|
366 |
public static class ST_Dimension extends Function_geom1 { |
367 |
|
368 |
public Comparable evaluate(Geometry geom1) throws Exception { |
369 |
GeometryType type = geom1.getGeometryType(); |
370 |
if ( type.isSubTypeOf(Geometry.TYPES.POINT)
|
371 |
|| type.isSubTypeOf(Geometry.TYPES.MULTIPOINT) ) { |
372 |
return 0; |
373 |
} |
374 |
if ( type.isSubTypeOf(Geometry.TYPES.CURVE)
|
375 |
|| type.isSubTypeOf(Geometry.TYPES.MULTICURVE) ) { |
376 |
return 1; |
377 |
} |
378 |
if ( type.isSubTypeOf(Geometry.TYPES.SURFACE)
|
379 |
|| type.isSubTypeOf(Geometry.TYPES.MULTISURFACE) ) { |
380 |
return 2; |
381 |
} |
382 |
if ( type.isSubTypeOf(Geometry.TYPES.SOLID) ) {
|
383 |
return 3; |
384 |
} |
385 |
return -1; |
386 |
} |
387 |
} |
388 |
|
389 |
public static class ST_Envelope extends Function_geom1 { |
390 |
|
391 |
public Comparable evaluate(Geometry geom1) throws Exception { |
392 |
return geom1.getEnvelope().getGeometry();
|
393 |
} |
394 |
} |
395 |
|
396 |
public static class ST_IsSimple extends Function_geom1 { |
397 |
|
398 |
public Comparable evaluate(Geometry geom1) throws Exception { |
399 |
return geom1.isSimple();
|
400 |
} |
401 |
} |
402 |
|
403 |
// public static class ST_IsRing extends Function_geom1 {
|
404 |
// public Comparable evaluate(Geometry geom1) throws Exception {
|
405 |
// return geom1.isClosed();
|
406 |
// }
|
407 |
// }
|
408 |
public static class ST_NumGeometries extends Function_geom1 { |
409 |
|
410 |
public Comparable evaluate(Geometry geom1) throws Exception { |
411 |
if ( geom1 instanceof Aggregate ) { |
412 |
int n = ((Aggregate) geom1).getPrimitivesNumber();
|
413 |
return Integer.valueOf(n); |
414 |
} |
415 |
return null; |
416 |
} |
417 |
} |
418 |
|
419 |
public static class ST_GeometryN extends Function_geom1_int { |
420 |
|
421 |
public Comparable evaluate(Geometry geom1, int value) throws Exception { |
422 |
if ( geom1 instanceof Aggregate ) { |
423 |
return ((Aggregate) geom1).getPrimitiveAt(value);
|
424 |
} |
425 |
return null; |
426 |
} |
427 |
} |
428 |
|
429 |
public static class ST_NumPoints extends Function_geom1 { |
430 |
|
431 |
public Comparable evaluate(Geometry geom1) throws Exception { |
432 |
if ( geom1 instanceof OrientablePrimitive ) { |
433 |
int n = ((OrientablePrimitive) geom1).getNumVertices();
|
434 |
return Integer.valueOf(n); |
435 |
} |
436 |
return null; |
437 |
} |
438 |
} |
439 |
|
440 |
public static class ST_PointN extends Function_geom1_int { |
441 |
|
442 |
public Comparable evaluate(Geometry geom1, int value) throws Exception { |
443 |
if ( geom1 instanceof OrientablePrimitive ) { |
444 |
return ((OrientablePrimitive) geom1).getVertex(value);
|
445 |
} |
446 |
return null; |
447 |
} |
448 |
} |
449 |
|
450 |
public static class ST_StartPoint extends Function_geom1 { |
451 |
|
452 |
public Comparable evaluate(Geometry geom1) throws Exception { |
453 |
if ( geom1 instanceof OrientablePrimitive ) { |
454 |
return ((OrientablePrimitive) geom1).getVertex(0); |
455 |
} |
456 |
return null; |
457 |
} |
458 |
} |
459 |
|
460 |
public static class ST_EndPoint extends Function_geom1 { |
461 |
|
462 |
public Comparable evaluate(Geometry geom1) throws Exception { |
463 |
if ( geom1 instanceof OrientablePrimitive ) { |
464 |
int n = ((OrientablePrimitive) geom1).getNumVertices();
|
465 |
return ((OrientablePrimitive) geom1).getVertex(n - 1); |
466 |
} |
467 |
return null; |
468 |
} |
469 |
} |
470 |
|
471 |
public static class ST_X extends Function_geom1 { |
472 |
|
473 |
public Comparable evaluate(Geometry geom1) throws Exception { |
474 |
return ((Point) geom1).getX(); |
475 |
} |
476 |
} |
477 |
|
478 |
public static class ST_Y extends Function_geom1 { |
479 |
|
480 |
public Comparable evaluate(Geometry geom1) throws Exception { |
481 |
return ((Point) geom1).getY(); |
482 |
} |
483 |
} |
484 |
|
485 |
public static class ST_Z extends Function_geom1 { |
486 |
|
487 |
public Comparable evaluate(Geometry geom1) throws Exception { |
488 |
Point p = (Point) geom1; |
489 |
if ( p.getGeometryType().getSubType() == Geometry.SUBTYPES.GEOM3D ) {
|
490 |
return Double.valueOf(p.getCoordinateAt(2)); |
491 |
} |
492 |
return null; |
493 |
} |
494 |
} |
495 |
|
496 |
public static class ST_GeomFromText extends Function_string { |
497 |
|
498 |
public Comparable evaluate(String value) throws Exception { |
499 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
500 |
return manager.createFrom(value);
|
501 |
} |
502 |
} |
503 |
|
504 |
public static class ST_isValid extends Function_geom1 { |
505 |
|
506 |
public Comparable evaluate(Geometry geom1) throws Exception { |
507 |
return Boolean.valueOf(geom1.isValid()); |
508 |
} |
509 |
} |
510 |
|
511 |
} |