9 |
import java.util.List; |
import java.util.List; |
10 |
|
|
11 |
public class StationDAO { |
public class StationDAO { |
12 |
|
final static int LOCATION_LIMIT = 5; |
13 |
|
|
14 |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
private StationBean convertSingleRow(ResultSet res) throws SQLException { |
15 |
StationBean station = new StationBean(); |
StationBean station = new StationBean(); |
16 |
|
|
103 |
return result; |
return result; |
104 |
} |
} |
105 |
|
|
106 |
//the "hack" with max 1.5 degrees latitude and 2.5 degrees longitude is only valid since we only service danish trains |
//the "hack" with max 0.5 degrees latitude and 0.9 degrees longitude is only valid since we only service danish trains, |
107 |
// in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km |
// in denmark 0.5dg latitude ~ 55km, 0.9dg longitude ~ 57km |
108 |
|
|
109 |
// the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area) |
// the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area) |
110 |
// is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to |
// is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to |
111 |
// calculate the distance: |
// calculate the distance: |
112 |
// sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist |
// sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist |
113 |
|
|
114 |
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
public List<StationBean> getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { |
115 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + |
|
116 |
" earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
String limitExpression = geolimit == true ? "AND abs(latitude-?)<0.5 AND abs(longitude-?)<0.9 " : ""; |
117 |
"FROM trainstations " + |
|
118 |
"WHERE enabled = true AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " + |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " + |
119 |
"ORDER BY calcdist ASC " + |
"earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
120 |
"LIMIT 4 "; |
"FROM trainstations " + |
121 |
|
"WHERE enabled = true " + limitExpression + |
122 |
|
"ORDER BY calcdist ASC " + |
123 |
|
"LIMIT " + LOCATION_LIMIT; |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
List<StationBean> result; |
List<StationBean> result; |
128 |
Connection conn = null; |
Connection conn = null; |
129 |
PreparedStatement stmt = null; |
PreparedStatement stmt = null; |
133 |
stmt = conn.prepareStatement(SQL); |
stmt = conn.prepareStatement(SQL); |
134 |
stmt.setDouble(1, latitude); |
stmt.setDouble(1, latitude); |
135 |
stmt.setDouble(2, longitude); |
stmt.setDouble(2, longitude); |
136 |
stmt.setDouble(3, latitude); |
if (geolimit == true) { |
137 |
stmt.setDouble(4, longitude); |
stmt.setDouble(3, latitude); |
138 |
|
stmt.setDouble(4, longitude); |
139 |
|
} |
140 |
res = stmt.executeQuery(); |
res = stmt.executeQuery(); |
141 |
result = convertResultset(res); |
result = convertResultset(res); |
142 |
|
|
143 |
} finally { |
} finally { |
144 |
if (res != null) |
if (res != null) |
145 |
res.close(); |
res.close(); |
150 |
} |
} |
151 |
return result; |
return result; |
152 |
} |
} |
153 |
|
|
154 |
|
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
155 |
|
List<StationBean> result = getByLocationWorker(latitude, longitude, true); |
156 |
|
|
157 |
|
if (result.size() < LOCATION_LIMIT) { //failover |
158 |
|
result = getByLocationWorker(latitude, longitude, false); |
159 |
|
} |
160 |
|
|
161 |
|
return result; |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
|
|
166 |
public List<StationBean> getByList(String list) throws SQLException { |
public List<StationBean> getByList(String list) throws SQLException { |
167 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " + |
214 |
return station; |
return station; |
215 |
} |
} |
216 |
|
|
217 |
public int getBySpecificName(String name) throws SQLException { |
public int getIdByName(String name) throws SQLException { |
218 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
219 |
"FROM trainstations " + |
"FROM trainstations " + |
220 |
"WHERE name = ? AND enabled = true " + |
"WHERE name = ? AND enabled = true " + |