--- android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/06/11 20:13:18 841 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/06/27 17:07:27 929 @@ -7,9 +7,12 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.List; +import java.util.logging.Logger; public class StationDAO { final static int LOCATION_LIMIT = 5; + static final Logger logger = Logger.getLogger(StationDAO.class.getName()); + private StationBean convertSingleRow(ResultSet res) throws SQLException { StationBean station = new StationBean(); @@ -103,8 +106,8 @@ return result; } - //the "hack" with max 1.5 degrees latitude and 2.5 degrees longitude is only valid since we only service danish trains - // in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km + //the "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains, + // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km // the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area) // is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to @@ -113,7 +116,7 @@ public List getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { - String limitExpression = geolimit == true ? "AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " : ""; + String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, " + "earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + @@ -121,6 +124,8 @@ "WHERE enabled = true " + limitExpression + "ORDER BY calcdist ASC " + "LIMIT " + LOCATION_LIMIT; + + List result; Connection conn = null; @@ -137,7 +142,7 @@ } res = stmt.executeQuery(); result = convertResultset(res); - + } finally { if (res != null) res.close(); @@ -153,6 +158,8 @@ List result = getByLocationWorker(latitude, longitude, true); if (result.size() < LOCATION_LIMIT) { //failover + logger.info("getByLocation failover: " +latitude + "," + longitude); + result = getByLocationWorker(latitude, longitude, false); } @@ -212,7 +219,7 @@ return station; } - public int getBySpecificName(String name) throws SQLException { + public int getIdByName(String name) throws SQLException { String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + "FROM trainstations " + "WHERE name = ? AND enabled = true " +