Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / vectorial / expressionevaluator / functions / GeomFromText.java @ 22244

History | View | Annotate | Download (2.88 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 IVER T.I. S.A.   {{Task}}
26
 */
27

    
28
package org.gvsig.fmap.data.vectorial.expressionevaluator.functions;
29

    
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.operation.GeometryOperationException;
32
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
33
import org.gvsig.fmap.geom.operation.fromwkt.FromWKT;
34
import org.gvsig.fmap.geom.operation.fromwkt.FromWKTGeometryOperationContext;
35
import org.gvsig.fmap.geom.primitive.NullGeometry;
36
import org.medfoster.sqljep.ASTFunNode;
37
import org.medfoster.sqljep.JepRuntime;
38
import org.medfoster.sqljep.ParseException;
39
import org.medfoster.sqljep.function.PostfixCommand;
40

    
41
public class GeomFromText extends PostfixCommand {
42

    
43
        private static Geometry nullGeometry = new NullGeometry();
44

    
45
        public static final String NAME = "GeomFromText";
46
        private static String lastTextGeometry="";
47
        private static String lastSRS="";
48
        private static Geometry lastGeometry=null;
49

    
50
        final public int getNumberOfParameters() {
51
                return 2;
52
        }
53

    
54
        /*
55
         * (non-Javadoc)
56
         *
57
         * @see org.medfoster.sqljep.function.PostfixCommand#evaluate(org.medfoster.sqljep.ASTFunNode,
58
         *      org.medfoster.sqljep.JepRuntime)
59
         */
60
        public void evaluate(ASTFunNode node, JepRuntime runtime)
61
                        throws ParseException {
62
                node.childrenAccept(runtime.ev, null);
63
                String srs = (String) runtime.stack.pop();
64
                String text = (String) runtime.stack.pop();
65
                runtime.stack.push(geometryFromText(text, srs));
66
        }
67

    
68
        private static Object geometryFromText(String text, String srs)
69
                        throws ParseException {
70
                if (!lastTextGeometry.equals(text) || !lastSRS.equals(srs)) {
71
                        try {
72
                                lastGeometry = (Geometry) nullGeometry.invokeOperation(
73
                                                FromWKT.CODE,
74
                                                new FromWKTGeometryOperationContext(text,srs)
75
                                        );
76
                        } catch (GeometryOperationNotSupportedException e) {
77
                                throw new ParseException(NAME, e);
78
                        } catch (GeometryOperationException e) {
79
                                throw new ParseException(NAME, e);
80
                        }
81
                        lastTextGeometry = text;
82
                        lastSRS = srs;
83
                }
84
                return lastGeometry;
85
        }
86

    
87
}