Statistics
| Revision:

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 / GeomFromText.java @ 41610

History | View | Annotate | Download (3.05 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 IVER T.I. S.A.   {{Task}}
27
 */
28

    
29
package org.gvsig.tools.evaluator.sqljep.function;
30

    
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.fmap.geom.GeometryException;
33
import org.gvsig.fmap.geom.GeometryLocator;
34
import org.gvsig.fmap.geom.GeometryManager;
35
import org.gvsig.fmap.geom.exception.CreateGeometryException;
36
import org.gvsig.tools.locator.LocatorException;
37
import org.medfoster.sqljep.ASTFunNode;
38
import org.medfoster.sqljep.JepRuntime;
39
import org.medfoster.sqljep.ParseException;
40
import org.medfoster.sqljep.function.PostfixCommand;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
public class GeomFromText extends PostfixCommand {
45
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
46
        private static final Logger logger = LoggerFactory.getLogger(GeomFromText.class);
47
        
48
        private static Object lock = new Object();
49

    
50
        public static final String NAME = "GeomFromText";
51
        private static String lastTextGeometry="";
52
        private static String lastSRS="";
53
        private static Geometry lastGeometry=null;
54

    
55
        final public int getNumberOfParameters() {
56
                return 2;
57
        }
58

    
59
        public GeomFromText() {
60
                super();
61
        }
62

    
63
        /*
64
         * (non-Javadoc)
65
         *
66
         * @see org.medfoster.sqljep.function.PostfixCommand#evaluate(org.medfoster.sqljep.ASTFunNode,
67
         *      org.medfoster.sqljep.JepRuntime)
68
         */
69
        public void evaluate(ASTFunNode node, JepRuntime runtime)
70
                        throws ParseException {
71
                node.childrenAccept(runtime.ev, null);
72
                String srs = (String) runtime.stack.pop();
73
                String text = (String) runtime.stack.pop();
74
                runtime.stack.push(geometryFromText(text, srs));
75
        }
76

    
77
        private static Geometry geometryFromText(String text, String srs)
78
                        throws ParseException {
79
                if (!lastTextGeometry.equals(text) || !lastSRS.equals(srs)) {
80
                        try {
81
                                lastGeometry = geomManager.createFrom(text, srs); 
82
                        } catch (CreateGeometryException e) {
83
                                throw new ParseException(NAME, e);
84
                        } catch (LocatorException e) {
85
                                throw new ParseException(NAME, e);
86
                        } catch (GeometryException e) {
87
                                throw new ParseException(NAME, e);
88
                        }
89
                        lastTextGeometry = text;
90
                        lastSRS = srs;
91
                }
92
                return lastGeometry;
93
        }
94
}