root/tools/routingservice/trunk/src/jp/co/orkney/restlet/geo/ClosestEdge.java

Revision 276, 5.8 KB (checked in by anton, 21 months ago)

Big cleaning at IOHelper class

Line 
1/**
2 * Copyright (c) 2007 Orkney, Inc. <http://www.orkney.co.jp/>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18package jp.co.orkney.restlet.geo;
19
20import java.sql.ResultSet;
21import java.sql.SQLException;
22
23import jp.co.orkney.restlet.util.IOHelper;
24import jp.co.orkney.restlet.util.Log;
25import jp.co.orkney.restlet.util.Point;
26import jp.co.orkney.restlet.util.Configuration;
27import jp.co.orkney.restlet.util.DatabaseConnection;
28
29import org.antlr.stringtemplate.StringTemplate;
30import org.json.JSONException;
31
32/**
33 * <b>ClosestEdge is a class which represents a "closest edge" algorithm.</b>
34 * <p>
35 * "closest edge" algorithm follows these steps:
36 * <ul>
37 * <li>Extract X and Y</li>
38 * <li>Start the database connection</li>
39 * <li>Make the query</li>
40 * <li>Send the query to the database</li>
41 * <li>Transform the result in the specified format</li>
42 * <li>Close the database connection</li>
43 * </ul>
44 * To do these steps, ClosestEdge class calls some method in IOHelper and
45 * DatabaseConnection
46 * </p>
47 *
48 * @author Matthieu Bilbille - Orkney Inc.
49 * @version 1.0
50 * @see IOHelper
51 * @see DatabaseConnection
52 */
53public class ClosestEdge extends GeoAction
54{
55        /**
56         * Constructor ClosestEdge
57         * <p>
58         * Creates a new ClosestEdge object using the specified Configuration object
59         * and extracts X and Y from the data provides by GET or POST request into a
60         * new point.
61         * </p>
62         *
63         * @param configuration
64         *            the current configuration
65         */
66        public ClosestEdge(Configuration configuration, DatabaseConnection databaseConnection, IOHelper ioHelper, Log log)
67        {
68                super(configuration, databaseConnection, ioHelper, log);
69               
70                name = "Closest edge";
71
72                // extract method for Geojson format
73                if (configuration.getFormatIn().getName().equals("geojson"))
74                {
75                        point = this.ioHelper.extractGEOJSONPxPy(configuration.getService().getParameter("point").getValue());
76                }
77                // extract method for Gml format
78                else if (configuration.getFormatIn().getName().equals("gml"))
79                {
80                        point = this.ioHelper.extractGMLPxPy(configuration.getService().getParameter("point").getValue());
81                }
82                // extract method for lonlat format
83                else if (configuration.getFormatIn().getName().equals("lonlat"))
84                {
85                        point = this.ioHelper.extractLONLATPxPy(configuration.getService().getParameter("point").getValue());
86                }
87                // extract method for wkt format
88                else if (configuration.getFormatIn().getName().equals("wkt"))
89                {
90                        point = this.ioHelper.extractWKTPxPy(configuration.getService().getParameter("point").getValue());
91                }
92                // extract method for kml format
93                else if (configuration.getFormatIn().getName().equals("kml"))
94                {
95                        point = this.ioHelper.extractKMLPxPy(configuration.getService().getParameter("point").getValue());
96                }
97        }
98
99        public void start() throws SQLException, JSONException
100        {
101                StringTemplate query = this.ioHelper.getQuery();
102                float bboxSize = Float.parseFloat(configuration.getService().getParameter("bbox").getValue());
103
104                // Makes the SQL query
105                query.setAttribute("sonPx", point.getX());
106                query.setAttribute("sonPy", point.getY());
107                query.setAttribute("sonPxM", point.getX(-bboxSize));
108                query.setAttribute("sonPxP", point.getX(bboxSize));
109                query.setAttribute("sonPyM", point.getY(-bboxSize));
110                query.setAttribute("sonPyP", point.getY(bboxSize));
111                query.setAttribute("sridProvider", configuration.getService().getDataProjection());
112                query.setAttribute("sridIn", configuration.getService().getParameter("sridInput").getValue());
113                query.setAttribute("sridOut", configuration.getService().getParameter("sridOutput").getValue());
114
115                // System.out.println(query.toString());
116                log.write(query.toString(), 2);
117                ResultSet rs = this.databaseConnection.getResult(configuration.getService(), query.toString());
118               
119                String reqId = configuration.getService().getParameter("request_id").getValue();
120       
121                StringTemplate template = this.configuration.getFormatOut().getTemplate();
122       
123                result = this.ioHelper.fillTemplate(rs, this.configuration.getFormatOut().getName(),
124                        template, reqId);               
125
126                /*
127                // Converts the SQL Result in Geojson
128                if (configuration.getFormatOut().getName().equals("geojson"))
129                {
130                        result = this.ioHelper.closestEdgeGEOJSONResult(rs, reqId);
131                }
132                // Converts the SQL Result in HTML
133                else if (configuration.getFormatOut().getName().equals("html"))
134                {
135                        System.out.println("hjhh");
136                        result = this.ioHelper.closestEdgeHTMLResult(rs, point, reqId);
137                }
138                // Converts the SQL Result in XML
139                else if (configuration.getFormatOut().getName().equals("xml"))
140                {
141                        result = this.ioHelper.closestEdgeXMLResult(rs, reqId);
142                }
143                // Converts the SQL Result in ID (edge id)
144                else if (configuration.getFormatOut().getName().equals("id"))
145                {
146                        result = this.ioHelper.closestEdgeIDResult(rs);
147                }
148                // Converts the SQL Result in GML
149                else if (configuration.getFormatOut().getName().equals("gml"))
150                {
151                        result = this.ioHelper.closestEdgeGMLResult(rs, reqId);
152                }
153                // Converts the SQL Result in WKT
154                else if (configuration.getFormatOut().getName().equals("wkt"))
155                {
156                        result = this.ioHelper.closestEdgeWKTResult(rs);
157                }
158                // Converts the SQL Result in KML
159                else if (configuration.getFormatOut().getName().equals("kml"))
160                {
161                        result = this.ioHelper.closestEdgeKMLResult(rs);
162                }
163                */
164
165                /*
166                 * databaseConnection.close(); }
167                 */
168        }
169
170}
Note: See TracBrowser for help on using the browser.