Revision 41539

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.tools.evaluator.sqljep/src/main/java/org/gvsig/tools/evaluator/sqljep/SQLJEPLibrary.java
53 53
import org.gvsig.tools.evaluator.sqljep.function.Equals;
54 54
import org.gvsig.tools.evaluator.sqljep.function.GeomFromText;
55 55
import org.gvsig.tools.evaluator.sqljep.function.Intersects;
56
import org.gvsig.tools.evaluator.sqljep.function.OpenGISFunctions;
56 57
import org.gvsig.tools.evaluator.sqljep.function.Overlaps;
57 58
import org.gvsig.tools.library.AbstractLibrary;
58 59
import org.gvsig.tools.library.LibraryException;
......
77 78
		BaseJEP.addFunction(Overlaps.NAME, new Overlaps());
78 79
		BaseJEP.addFunction(Boundary.NAME, new Boundary());
79 80
		BaseJEP.addFunction(SQLJEPEvaluator.TODATE_FUNCTION_NAME, new ToDate());
81
                OpenGISFunctions.registerFunctions();
80 82
	}
81 83
}
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.tools.evaluator.sqljep/src/main/java/org/gvsig/tools/evaluator/sqljep/function/Boundary.java
86 86

  
87 87
	private static Geometry boundary(Geometry g)
88 88
			throws ParseException {
89
                // Oppss esta implementacion parece que no es buena ya que el baundary
90
                // no es el envelope.
89 91
		return g.getEnvelope().getGeometry();
90 92
	}
91 93

  
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.tools.evaluator.sqljep/src/main/java/org/gvsig/tools/evaluator/sqljep/function/Intersects.java
10 10
 *
11 11
 * This program is distributed in the hope that it will be useful,
12 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14
 * GNU General Public License for more details.
15 15
 *
16 16
 * You should have received a copy of the GNU General Public License
17 17
 * along with this program; if not, write to the Free Software
18 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
19
 * MA 02110-1301, USA.
20 20
 *
21 21
 * For any additional information, do not hesitate to contact us
22 22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 23
 */
24 24
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2008 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
*
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
*
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
*
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
42
* MA  02110-1301, USA.
43
*
44
*/
25
 *
26
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
27
 * of the Valencian Government (CIT)
28
 *
29
 * This program is free software; you can redistribute it and/or
30
 * modify it under the terms of the GNU General Public License
31
 * as published by the Free Software Foundation; either version 2
32
 * of the License, or (at your option) any later version.
33
 *
34
 * This program is distributed in the hope that it will be useful,
35
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
 * GNU General Public License for more details.
38
 *
39
 * You should have received a copy of the GNU General Public License
40
 * along with this program; if not, write to the Free Software
41
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
42
 * MA  02110-1301, USA.
43
 *
44
 */
45 45

  
46 46
/*
47
* AUTHORS (In addition to CIT):
48
* 2008 IVER T.I. S.A.   {{Task}}
49
*/
50

  
47
 * AUTHORS (In addition to CIT):
48
 * 2008 IVER T.I. S.A.   {{Task}}
49
 */
51 50
package org.gvsig.tools.evaluator.sqljep.function;
52 51

  
53 52
import org.gvsig.fmap.geom.Geometry;
......
60 59

  
61 60
public class Intersects extends PostfixCommand {
62 61

  
63
	public static final String NAME = "intersects";
62
    public static final String NAME = "intersects";
64 63

  
65
	final public int getNumberOfParameters() {
66
		return 2;
67
	}
64
    final public int getNumberOfParameters() {
65
        return 2;
66
    }
68 67

  
69
	/* (non-Javadoc)
70
	 * @see org.medfoster.sqljep.function.PostfixCommand#evaluate(org.medfoster.sqljep.ASTFunNode, org.medfoster.sqljep.JepRuntime)
71
	 */
72
	public void evaluate(ASTFunNode node, JepRuntime runtime)
73
			throws ParseException {
74
		node.childrenAccept(runtime.ev, null);
75
		Geometry geom1 = (Geometry)runtime.stack.pop();
76
		Geometry geom2 = (Geometry)runtime.stack.pop();
77
		try {
78
			runtime.stack.push( Boolean.valueOf(geom1.intersects(geom2)) );
79
		} catch (GeometryOperationNotSupportedException e) {
80
			throw new ParseException(NAME, e);
81
		} catch (GeometryOperationException e) {
82
			throw new ParseException(NAME, e);
83
		}
84
	}
85

  
86

  
68
    public void evaluate(ASTFunNode node, JepRuntime runtime)
69
            throws ParseException {
70
        node.childrenAccept(runtime.ev, null);
71
        Geometry geom1 = (Geometry) runtime.stack.pop();
72
        Geometry geom2 = (Geometry) runtime.stack.pop();
73
        try {
74
            runtime.stack.push(Boolean.valueOf(geom1.intersects(geom2)));
75
        } catch (GeometryOperationNotSupportedException e) {
76
            throw new ParseException(NAME, e);
77
        } catch (GeometryOperationException e) {
78
            throw new ParseException(NAME, e);
79
        }
80
    }
87 81
}
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.tools.evaluator.sqljep/src/main/java/org/gvsig/tools/evaluator/sqljep/function/OpenGISFunctions.java
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).getX();
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
}

Also available in: Unified diff