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

Revision 167, 6.5 KB (checked in by anton, 3 years ago)

Some code cleaning with less logs

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.Configuration;
27import jp.co.orkney.restlet.util.DatabaseConnection;
28import jp.co.orkney.restlet.util.IOHelper;
29import jp.co.orkney.restlet.util.Log;
30import jp.co.orkney.restlet.util.Point;
31
32/**
33 * <b>DrivingDistance is a class which represents a "driving distance isoline"
34 * algorithm.</b>
35 * <p>
36 * "driving distance" algorithm follows these steps:
37 * <ul>
38 * <li>Extract X and Y</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, DrivingDistance 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 DrivingDistance extends GeoAction
55{
56        private float speed;
57        private float time;
58        private float length;
59
60
61        /**
62         * Constructor DrivingDistance
63         * <p>
64         * Creates a new DrivingDistance object using the specified Configuration
65         * object and extracts X and Y from the data provides by GET or POST request
66         * into a new point.
67         * </p>
68         *
69         * @param configuration
70         *            the current configuration
71         */
72        public DrivingDistance(Configuration configuration, DatabaseConnection databaseConnection, IOHelper ioHelper, Log log)
73        {
74                super(configuration, databaseConnection, ioHelper, log);
75               
76                name = "Driving distance";
77
78                // extract method for Geojson format
79                if (this.configuration.getFormatIn().getName().equals("geojson"))
80                {
81                        point = ioHelper.extractGEOJSONPxPy(this.configuration.getService().getParameter("point").getValue());
82                }
83                // extract method for Gml format
84                else if (this.configuration.getFormatIn().getName().equals("gml"))
85                {
86                        point = ioHelper.extractGMLPxPy(this.configuration.getService().getParameter("point").getValue());
87                }
88                // extract method for lonlat format
89                else if (this.configuration.getFormatIn().getName().equals("lonlat"))
90                {
91                        point = ioHelper.extractLONLATPxPy(this.configuration.getService().getParameter("point").getValue());
92                }
93                // extract method for wkt format
94                else if (this.configuration.getFormatIn().getName().equals("wkt"))
95                {
96                        point = ioHelper.extractWKTPxPy(this.configuration.getService().getParameter("point").getValue());
97                }
98                // extract method for kml format
99                else if (this.configuration.getFormatIn().getName().equals("kml"))
100                {
101                        point = ioHelper.extractKMLPxPy(this.configuration.getService().getParameter("point").getValue());
102                }
103
104                if (this.configuration.getService().getParameter("speed").getValue() != null)
105                {
106                        speed = Float.parseFloat(this.configuration.getService().getParameter("speed").getValue());
107                }
108                else
109                {
110                        speed = 0;
111                }
112
113                if (this.configuration.getService().getParameter("time").getValue() != null)
114                {
115                        time = Float.parseFloat(this.configuration.getService().getParameter("time").getValue());
116                }
117                else
118                {
119                        time = 0;
120                }
121
122                if (this.configuration.getService().getParameter("length").getValue() != null)
123                {
124                        length = Float.parseFloat(this.configuration.getService().getParameter("length").getValue());
125                }
126                else
127                {
128                        length = 0;
129                }
130        }
131
132        /**
133         * Gets the result of the driving distance algorithm
134         *
135         * @return this closest edge's result
136         */
137        public String get()
138        {
139                return result;
140        }
141
142        /**
143         * Starts the "driving distance" algorithm
144         *
145         * @throws SQLException
146         *             Error to send the query to the database
147         * @throws JSONException
148         *             Error to transform the result in Geojson format
149         */
150        public void start() throws SQLException, JSONException
151        {
152                StringTemplate query = ioHelper.getQuery();
153                float bboxSize = Float.parseFloat(configuration.getService().getParameter("bbox").getValue());
154                float distance = 0;
155                // I can not specify both TIME and LENGTH for driving distance
156                // algorithms
157                if (time > 0)
158                {
159                        distance = time * speed;
160                }
161                else
162                {
163                        distance = length;
164                }
165                // Makes the SQL query
166                query.setAttribute("sonPx", point.getX());
167                query.setAttribute("sonPy", point.getY());
168                query.setAttribute("bbox", bboxSize);
169                query.setAttribute("distance", distance);
170                query.setAttribute("sridProvider", configuration.getService().getDataProjection());
171                query.setAttribute("sridIn", configuration.getService().getParameter("sridInput").getValue());
172                query.setAttribute("sridOut", configuration.getService().getParameter("sridOutput").getValue());
173
174                // System.out.println(query.toString());
175                log.write(query.toString(), 2);
176                ResultSet rs = databaseConnection.getResult(configuration.getService(), query.toString());
177
178                String reqId = configuration.getService().getParameter("request_id").getValue();
179               
180                // Converts the SQL Result in Geojson
181                if (configuration.getFormatOut().getName().equals("geojson"))
182                {
183                        result = ioHelper.drivingDistanceGEOJSONResult(rs, reqId);
184                }
185                // Converts the SQL Result in HTML
186                else if (configuration.getFormatOut().getName().equals("html"))
187                {
188                        result = ioHelper.drivingDistanceHTMLResult(rs, point, reqId);
189                }
190                // Converts the SQL Result in XML
191                else if (configuration.getFormatOut().getName().equals("xml"))
192                {
193                        result = ioHelper.drivingDistanceXMLResult(rs, reqId);
194                }
195                // Converts the SQL Result in GML
196                else if (configuration.getFormatOut().getName().equals("gml"))
197                {
198                        result = ioHelper.drivingDistanceGMLResult(rs, reqId);
199                }
200                // Converts the SQL Result in WKT
201                else if (configuration.getFormatOut().getName().equals("wkt"))
202                {
203                        result = ioHelper.drivingDistanceWKTResult(rs);
204                }
205                // Converts the SQL Result in KML
206                else if (configuration.getFormatOut().getName().equals("kml"))
207                {
208                        result = ioHelper.drivingDistanceKMLResult(rs);
209                }
210        }
211}
Note: See TracBrowser for help on using the browser.