97 |
} |
} |
98 |
} |
} |
99 |
|
|
|
public StationBean dumpAll() throws SQLException { |
|
|
|
|
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " + |
|
|
"FROM trainstations WHERE enabled = true ORDER BY id"; |
|
|
|
|
|
Connection conn = null; |
|
|
Statement stmt = null; |
|
|
ResultSet res = null; |
|
100 |
|
|
|
|
|
|
try { |
|
|
conn = DBConnection.getConnection(); |
|
|
|
|
|
stmt = conn.createStatement(); |
|
|
res = stmt.executeQuery(SQL); |
|
|
|
|
|
// Does mostly the same as convertResultset() |
|
|
StationBean stations = new StationBean(); |
|
|
while (res.next()) { |
|
|
StationEntry entry = convertSingleRow(res); |
|
|
|
|
|
Array arr = res.getArray(10); |
|
|
if (arr != null) { |
|
|
String[] aliases = (String[]) arr.getArray(); |
|
|
entry.setAliases(aliases); |
|
|
} |
|
|
|
|
|
stations.entries.add( entry ); |
|
|
|
|
|
} |
|
|
return stations; |
|
|
|
|
|
|
|
|
} finally { |
|
|
if (res != null) |
|
|
res.close(); |
|
|
if (stmt != null) |
|
|
stmt.close(); |
|
|
if (conn != null) |
|
|
conn.close(); |
|
|
} |
|
|
|
|
|
} |
|
101 |
|
|
102 |
/* |
/* |
103 |
* this code requires theses statements are run on database in order to do ILIKE searches against aliases (which is defines as array of varchar(64) ) |
* this code requires theses statements are run on database in order to do ILIKE searches against aliases (which is defines as array of varchar(64) ) |
105 |
* 'select $2 ilike $1' language sql strict immutable; |
* 'select $2 ilike $1' language sql strict immutable; |
106 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
107 |
*/ |
*/ |
108 |
public StationBean getByName(final String name) throws SQLException { |
public StationBean getByNameNormal(final String name) throws SQLException { |
109 |
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 " + |
110 |
"FROM trainstations " + |
"FROM trainstations " + |
111 |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
121 |
|
|
122 |
return fetchStations(SQL, new NameSetter() ); |
return fetchStations(SQL, new NameSetter() ); |
123 |
} |
} |
124 |
|
|
125 |
|
|
126 |
|
public StationBean getByNameFuzzy(final String name) throws SQLException { |
127 |
|
String SQL = "SELECT * FROM (" + |
128 |
|
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0, " + |
129 |
|
" levenshtein(lower(name),lower(?) ) as leven " + |
130 |
|
" FROM trainstations " + |
131 |
|
" WHERE enabled = true ) as lev2 " + |
132 |
|
"WHERE (leven <= 3) " + |
133 |
|
"ORDER BY leven " + |
134 |
|
"LIMIT 1"; |
135 |
|
|
136 |
|
class NameSetter implements StatementParamSetter { |
137 |
|
@Override |
138 |
|
public void setParams(PreparedStatement stmt) throws SQLException { |
139 |
|
stmt.setString(1, name ); |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
|
StationBean stations = fetchStations(SQL, new NameSetter() ); |
144 |
|
stations.fuzzystrmatch = true; |
145 |
|
return stations; |
146 |
|
} |
147 |
|
|
148 |
|
private String removeSuffix(String str, String suffix) { |
149 |
|
if (str.endsWith(suffix)) { |
150 |
|
return str.substring(0, str.length() - suffix.length() ); |
151 |
|
} else { |
152 |
|
return str; |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
public StationBean getByName(String name) throws SQLException { |
157 |
|
name = removeSuffix(name, " st."); |
158 |
|
name = removeSuffix(name, " st"); |
159 |
|
name = removeSuffix(name, " station"); |
160 |
|
|
161 |
|
StationBean stations = getByNameNormal(name); |
162 |
|
|
163 |
|
if (stations.entries.size() == 0) { |
164 |
|
stations = getByNameFuzzy(name); |
165 |
|
|
166 |
|
logger.info("getByName failover: " + name + "(" + (stations.entries.size() >0) + ")" ); |
167 |
|
} |
168 |
|
return stations; |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
|
|
173 |
//Latitude (horizonal), longitude(vertical) so |
//Latitude (horizonal), longitude(vertical) so |
174 |
// 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same |
// 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same |
175 |
// 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles. |
// 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles. |
176 |
// the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains, |
// so 1 degree longitude is 64.5 km at denmarks southern point (gedser=54.55,11.95) |
177 |
|
// and is 59.4km at northern point (skagen = 57.75,10.65) |
178 |
|
// The "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains, |
179 |
// in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km |
// in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km |
180 |
|
|
181 |
// 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) |
256 |
return null; |
return null; |
257 |
} |
} |
258 |
} |
} |
259 |
|
|
260 |
|
//used to create full dump in order to populate Google Appengine DB |
261 |
|
//after 1.1.0 also used to populate client-side station list |
262 |
|
public StationBean dumpAll() throws SQLException { |
263 |
|
|
264 |
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0,aliases " + |
265 |
|
"FROM trainstations WHERE enabled = true ORDER BY id"; |
266 |
|
|
267 |
|
Connection conn = null; |
268 |
|
Statement stmt = null; |
269 |
|
ResultSet res = null; |
270 |
|
|
271 |
|
|
272 |
|
try { |
273 |
|
conn = DBConnection.getConnection(); |
274 |
|
|
275 |
|
stmt = conn.createStatement(); |
276 |
|
res = stmt.executeQuery(SQL); |
277 |
|
|
278 |
|
// Does mostly the same as convertResultset() |
279 |
|
StationBean stations = new StationBean(); |
280 |
|
while (res.next()) { |
281 |
|
StationEntry entry = convertSingleRow(res); |
282 |
|
|
283 |
|
Array arr = res.getArray(10); |
284 |
|
if (arr != null) { |
285 |
|
String[] aliases = (String[]) arr.getArray(); |
286 |
|
entry.setAliases(aliases); |
287 |
|
} |
288 |
|
|
289 |
|
stations.entries.add( entry ); |
290 |
|
|
291 |
|
} |
292 |
|
return stations; |
293 |
|
|
294 |
|
|
295 |
|
} finally { |
296 |
|
if (res != null) |
297 |
|
res.close(); |
298 |
|
if (stmt != null) |
299 |
|
stmt.close(); |
300 |
|
if (conn != null) |
301 |
|
conn.close(); |
302 |
|
} |
303 |
|
|
304 |
|
} |
305 |
|
|
306 |
@Deprecated |
@Deprecated |
307 |
public static String getStationName(int stationID) { |
public static String getStationName(int stationID) { |