Revision 46468

View differences:

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/main/java/org/gvsig/expressionevaluator/impl/symboltable/OGCSymbolTable.java
1 1
package org.gvsig.expressionevaluator.impl.symboltable;
2 2

  
3 3
import org.gvsig.expressionevaluator.impl.function.spatial.AndAndOperator;
4
import org.gvsig.expressionevaluator.impl.function.spatial.ConvexTrapezoidLengthFunction;
4 5
import org.gvsig.expressionevaluator.impl.function.spatial.STAreaFunction;
5 6
import org.gvsig.expressionevaluator.impl.function.spatial.STAsTextFunction;
6 7
import org.gvsig.expressionevaluator.impl.function.spatial.STBufferFunction;
......
102 103
        this.addFunction(new STMakePointFunction());
103 104
        this.addFunction(new STForce2DFunction());
104 105
        this.addFunction(new STTransformFunction());
105
        this.addFunction(new PointByAngleFunction());
106 106
        this.addFunction(new STBoundaryFunction());
107 107
        this.addFunction(new STAsBinaryFunction());
108

  
108 109
        
110
        this.addFunction(new PointByAngleFunction());
111
        this.addFunction(new ConvexTrapezoidLengthFunction());
112

  
113
        
109 114
        this.addFunction(new AndAndOperator());
110 115
    }    
111 116
}
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/main/java/org/gvsig/expressionevaluator/impl/function/spatial/ConvexTrapezoidLengthFunction.java
1
package org.gvsig.expressionevaluator.impl.function.spatial;
2

  
3
import org.apache.commons.lang3.Range;
4
import org.gvsig.euclidean.EuclideanManager;
5
import org.gvsig.expressionevaluator.Interpreter;
6
import org.gvsig.expressionevaluator.spi.AbstractGeometryFunction;
7
import org.gvsig.fmap.geom.Geometry;
8
import org.gvsig.fmap.geom.primitive.Polygon;
9
import org.gvsig.tools.util.ToolsUtilLocator;
10

  
11
public class ConvexTrapezoidLengthFunction extends AbstractGeometryFunction {
12

  
13
    public ConvexTrapezoidLengthFunction() {
14
        super("Spatial", "ConvexTrapezoidLength", Range.is(1));
15
    }
16

  
17
    @Override
18
    public boolean allowConstantFolding() {
19
        return true;
20
    }
21

  
22
    @Override
23
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
24
        Geometry arg1 = (Geometry) getObject(args, 0);
25
        if( arg1==null ) {
26
            return null;
27
        }
28
        try {
29
            return convexTrapezoidLength((Polygon) arg1);
30
        } catch(Exception ex) {
31
            return null;
32
        }
33
    }
34

  
35
    public static double convexTrapezoidLength(Polygon geom) {
36
        EuclideanManager manager = ToolsUtilLocator.getEuclideanManager();
37
        
38
        if( geom.getNumInteriorRings()!=0 ) {
39
            throw new IllegalArgumentException("Can't calculate convex trapezoid length if the polygon has interior rings");
40
        }
41
        if( geom.isClosed() ) {
42
            if( geom.getNumVertices()!=5 ) {
43
                throw new IllegalArgumentException("Wrong number of sides, a convex trapezoid is required");
44
            }
45
        } else {
46
            if( geom.getNumVertices()!=4 ) {
47
                throw new IllegalArgumentException("Wrong number of sides, a convex trapezoid is required");
48
            }
49
        }
50
        double l[] = new double[4];
51
        l[0] = manager.distance(
52
                geom.getCoordinateAt(0, Geometry.DIMENSIONS.X),
53
                geom.getCoordinateAt(0, Geometry.DIMENSIONS.Y),
54
                geom.getCoordinateAt(1, Geometry.DIMENSIONS.X),
55
                geom.getCoordinateAt(1, Geometry.DIMENSIONS.Y)
56
        );
57
        l[1] = manager.distance(
58
                geom.getCoordinateAt(1, Geometry.DIMENSIONS.X),
59
                geom.getCoordinateAt(1, Geometry.DIMENSIONS.Y),
60
                geom.getCoordinateAt(2, Geometry.DIMENSIONS.X),
61
                geom.getCoordinateAt(2, Geometry.DIMENSIONS.Y)
62
        );
63
        l[2] = manager.distance(
64
                geom.getCoordinateAt(2, Geometry.DIMENSIONS.X),
65
                geom.getCoordinateAt(2, Geometry.DIMENSIONS.Y),
66
                geom.getCoordinateAt(3, Geometry.DIMENSIONS.X),
67
                geom.getCoordinateAt(3, Geometry.DIMENSIONS.Y)
68
        );
69
        l[3] = manager.distance(
70
                geom.getCoordinateAt(3, Geometry.DIMENSIONS.X),
71
                geom.getCoordinateAt(3, Geometry.DIMENSIONS.Y),
72
                geom.getCoordinateAt(4, Geometry.DIMENSIONS.X),
73
                geom.getCoordinateAt(4, Geometry.DIMENSIONS.Y)
74
        );
75
        
76
        // Comprobamos si se cruzan los lados del trapezoide.
77
        if( !geom.isSimple() ) {
78
            throw new IllegalArgumentException("Wrong trapezoid, a convex trapezoid is required");
79
        }
80
        
81
        // Falta comprobar que no es un trapezoide concavo, que no tiene algun
82
        // angulo interior concavo.
83
        // Si es concavo probablemente este dando un calculo de longitud incorrecto.
84
        
85
        int max = 0;
86
        for (int i = 0; i < l.length; i++) {
87
            if( l[max] < l[i] ) {
88
                max = i;
89
            }
90
        }
91
        int opposed = max +2;
92
        if( opposed>3 ) {
93
            opposed = 1;
94
        }
95
        double n = (l[max]+l[opposed]) / 2;
96
        return n;
97
    }
98
    
99
//    public static void main(String[] args) {
100
//        // https://www.juntadeandalucia.es/averroes/centros-tic/21003232/helvia/sitio/upload/apuntes4____trapezoides.pdf
101
//        
102
//        new DefaultLibrariesInitializer().fullInitialize();
103
//        String wktGeom;
104
//        Polygon geom;
105
//        double l;
106
//        
107
//        // Trapezoide rectangulo
108
//        wktGeom = "POLYGON ((200 300, 700 300, 700 200, 200 200, 200 300))";
109
//        try {
110
//            geom = (Polygon) GeometryUtils.createFrom(wktGeom);
111
//            l = convexTrapezoidLength(geom);
112
//            System.err.println("Trapezoide rectangulo [OK]: "+l);
113
//        } catch(Exception ex) {
114
//            System.err.println("Trapezoide rectangulo [ER]: "+ex.getMessage());
115
//        }
116
//        
117
//        // No es un trapezoide
118
//        wktGeom = "POLYGON ((290 120, 440 60, 560 260, 820 320, 870 340, 840 380, 694 345, 410 260, 360 200, 290 120))";
119
//        try {
120
//            geom = (Polygon) GeometryUtils.createFrom(wktGeom);
121
//            l = convexTrapezoidLength(geom);
122
//            System.err.println("No es un trapezoide [ER]: "+l);
123
//        } catch(Exception ex) {
124
//            System.err.println("No es un trapezoide [OK]: "+ex.getMessage());
125
//        }
126
//
127
//        // Trapezoide convexo
128
//        wktGeom = "POLYGON ((200 300, 700 300, 533.3333333333334 75.55555555555556, 200 200, 200 300))";
129
//        try {
130
//            geom = (Polygon) GeometryUtils.createFrom(wktGeom);
131
//            l = convexTrapezoidLength(geom);
132
//            System.err.println("Trapezoide convexo [OK]: "+l);
133
//        } catch(Exception ex) {
134
//            System.err.println("Trapezoide convexo [ER]: "+ex.getMessage());
135
//        }
136
//        
137
//        // Trapezoide cruzado
138
//        wktGeom = "POLYGON ((200 300, 700 300, 400 500, 200 200, 200 300))";
139
//        try {
140
//            geom = (Polygon) GeometryUtils.createFrom(wktGeom);
141
//            l = convexTrapezoidLength(geom);
142
//            System.err.println("Trapezoide cruzado [ER]: "+l);
143
//        } catch(Exception ex) {
144
//            System.err.println("Trapezoide cruzado [OK]: "+ex.getMessage());
145
//        }
146
//        
147
//        // Trapezoide concavo
148
//        wktGeom = "POLYGON ((450 470, 530 220, 390 280, 200 200, 450 470))";
149
//        try {
150
//            geom = (Polygon) GeometryUtils.createFrom(wktGeom);
151
//            l = convexTrapezoidLength(geom);
152
//            System.err.println("Trapezoide concavo [ER]: "+l);
153
//        } catch(Exception ex) {
154
//            System.err.println("Trapezoide concavo [OK]: "+ex.getMessage());
155
//        }
156
//    }
157
    
158
}
0 159

  
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/main/java/org/gvsig/expressionevaluator/impl/function/spatial/PointByAngleFunction.java
12 12
public class PointByAngleFunction extends AbstractGeometryFunction {
13 13

  
14 14
    public PointByAngleFunction() {
15
        super("OGC", "PointByAngle", Range.between(3, 5));
15
        super("Spatial", "PointByAngle", Range.between(3, 5));
16 16
    }
17 17

  
18 18
    @Override

Also available in: Unified diff