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

Revision 276, 8.1 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 org.antlr.stringtemplate.StringTemplate;
24import org.json.JSONException;
25
26import jp.co.orkney.restlet.util.IOHelper;
27import jp.co.orkney.restlet.util.LimitsHelper;
28import jp.co.orkney.restlet.util.Log;
29import jp.co.orkney.restlet.util.Point;
30import jp.co.orkney.restlet.util.Configuration;
31import jp.co.orkney.restlet.util.DatabaseConnection;
32
33/**
34 * <b>ShortestPath is a class which represents a "shortest path" algorithm.</b>
35 * <p>
36 * "shortest path" algorithm follows these steps:
37 * <ul>
38 * <li>Extract X and Y for start and end point</li>
39 * <li>Start the database connection</li>
40 * <li>Make the query</li>
41 * <li>Send the query to the database</li>
42 * <li>Transform the result in the specified format</li>
43 * <li>Close the database connection</li>
44 * </ul>
45 * To do these steps, ShortestPath class calls some method in IOHelper and
46 * DatabaseConnection
47 * </p>
48 *
49 * @author Matthieu Bilbille - Orkney Inc.
50 * @version 1.0
51 * @see IOHelper
52 * @see DatabaseConnection
53 */
54public class ShortestPath extends GeoAction
55{
56    private Point startPoint;
57
58    private Point endPoint;
59
60    /**
61         * Constructor ShortestPath
62         * <p>
63         * Creates a new ShortestPath object using the specified Configuration
64         * object and extracts X and Y from the data provides by GET or POST
65         * request into a new point (for start and end point).
66         * </p>
67         *
68         * @param configuration
69         *                the current configuration
70         */
71    public ShortestPath(Configuration configuration,
72            DatabaseConnection databaseConnection, IOHelper ioHelper, Log log)
73    {
74        super(configuration, databaseConnection, ioHelper, log);
75
76        name = "Shortest path";
77
78        // extract method for Geojson format
79        if (configuration.getFormatIn().getName().equals("geojson"))
80        {
81            startPoint = ioHelper.extractGEOJSONPxPy(configuration.getService()
82                    .getParameter("point_start").getValue());
83            endPoint = ioHelper.extractGEOJSONPxPy(configuration.getService()
84                    .getParameter("point_end").getValue());
85        }
86        // extract method for Gml format
87        else if (configuration.getFormatIn().getName().equals("gml"))
88        {
89            startPoint = ioHelper.extractGMLPxPy(configuration.getService()
90                    .getParameter("point_start").getValue());
91            endPoint = ioHelper.extractGMLPxPy(configuration.getService()
92                    .getParameter("point_end").getValue());
93        }
94        // extract method for lonlat format
95        else if (configuration.getFormatIn().getName().equals("lonlat"))
96        {
97            startPoint = ioHelper.extractLONLATPxPy(configuration.getService()
98                    .getParameter("point_start").getValue());
99            endPoint = ioHelper.extractLONLATPxPy(configuration.getService()
100                    .getParameter("point_end").getValue());
101        }
102        // extract method for wkt format
103        else if (configuration.getFormatIn().getName().equals("wkt"))
104        {
105            startPoint = ioHelper.extractWKTPxPy(configuration.getService()
106                    .getParameter("point_start").getValue());
107            endPoint = ioHelper.extractWKTPxPy(configuration.getService()
108                    .getParameter("point_end").getValue());
109        }
110        // extract method for kml format
111        else if (configuration.getFormatIn().getName().equals("kml"))
112        {
113            startPoint = ioHelper.extractKMLPxPy(configuration.getService()
114                    .getParameter("point_start").getValue());
115            endPoint = ioHelper.extractKMLPxPy(configuration.getService()
116                    .getParameter("point_end").getValue());
117        }
118    }
119
120    /**
121         * Gets the result of the shortest path algorithm
122         *
123         * @return this shortest path's result
124         */
125    public String get()
126    {
127        return result;
128    }
129
130    /**
131         * Starts the "shortest path" algorithm
132         *
133         * @throws SQLException
134         *                 error to send the query to the database
135         * @throws JSONException
136         *                 Error to transform the result in Geojson format
137         */
138    public void start() throws SQLException, JSONException
139    {
140        StringTemplate query = this.ioHelper.getQuery();
141        ResultSet rs = null;
142
143        float bboxSize = Float.parseFloat(configuration.getService()
144                .getParameter("bbox").getValue());
145
146        // We need to define the input projection and reproject the data.
147        // This place is not beautiful yet and needs to be reconsidered.
148        Point sPoint = startPoint;
149        Point ePoint = endPoint;
150
151        if (configuration.getService().getParameter("sridInput").getValue() != null)
152        {
153            sPoint = databaseConnection.transform(configuration.getService(),
154                    startPoint, configuration.getService().getParameter(
155                            "sridInput").getValue(), configuration.getService()
156                            .getDataProjection());
157            ePoint = databaseConnection.transform(configuration.getService(),
158                    endPoint, configuration.getService().getParameter(
159                            "sridInput").getValue(), configuration.getService()
160                            .getDataProjection());
161        }
162
163        if (LimitsHelper.isDistanceAllowed(sPoint, ePoint, configuration
164                .getService().getLimit().getDistance()))
165        {
166
167            this.log.write("Distance limit is "+configuration.getService().getLimit().getDistance(), 2);
168            // Makes the SQL query
169            query.setAttribute("sonStartX", startPoint.getX());
170            query.setAttribute("sonStartY", startPoint.getY());
171            query.setAttribute("sonEndX", endPoint.getX());
172            query.setAttribute("sonEndY", endPoint.getY());
173            query.setAttribute("saBoxSize", bboxSize);
174            query.setAttribute("sridProvider", configuration.getService()
175                    .getDataProjection());
176            query.setAttribute("sridIn", configuration.getService()
177                    .getParameter("sridInput").getValue());
178            query.setAttribute("sridOut", configuration.getService()
179                    .getParameter("sridOutput").getValue());
180
181            // System.out.println(query.toString());
182            this.log.write(query.toString(), 2);
183            rs = databaseConnection.getResult(configuration.getService(), query
184                    .toString());
185
186        }
187        else
188        {
189            this.log.write("Distance exceedes limits", 1);
190            //There should be an exception in this case
191            return;
192        }
193
194        String reqId = configuration.getService().getParameter("request_id")
195                .getValue();
196
197        StringTemplate template = this.configuration.getFormatOut()
198                .getTemplate();
199
200        result = this.ioHelper.fillTemplate(rs,
201                this.configuration.getFormatOut().getName(), template, reqId);
202
203        /*
204         *  // Converts the SQL Result in Geojson if
205         * (this.configuration.getFormatOut().getName().equals("geojson")) {
206         * result = this.ioHelper.shortestPathGEOJSONResult(rs, reqId); } //
207         * Converts the SQL Result in HTML else if
208         * (this.configuration.getFormatOut().getName().equals("html")) { result =
209         * this.ioHelper.shortestPathHTMLResult(rs, startPoint, endPoint,
210         * reqId); } // Converts the SQL Result in XML else if
211         * (this.configuration.getFormatOut().getName().equals("xml")) { result =
212         * this.ioHelper.shortestPathXMLResult(rs, reqId); } // Converts the SQL
213         * Result in GML else if
214         * (this.configuration.getFormatOut().getName().equals("gml")) { result =
215         * this.ioHelper.shortestPathGMLResult(rs, reqId); } // Converts the SQL
216         * Result in WKT else if
217         * (this.configuration.getFormatOut().getName().equals("wkt")) { result =
218         * this.ioHelper.shortestPathWKTResult(rs); } // Converts the SQL Result
219         * in KML else if
220         * (this.configuration.getFormatOut().getName().equals("kml")) { result =
221         * this.ioHelper.shortestPathKMLResult(rs); }
222         */
223
224    }
225
226}
Note: See TracBrowser for help on using the browser.