/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java
ViewVC logotype

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 650 by torben, Mon Apr 19 19:04:34 2010 UTC revision 741 by torben, Wed May 19 12:56:10 2010 UTC
# Line 63  public class StationDAO { Line 63  public class StationDAO {
63                  return result;                  return result;
64          }          }
65                    
66                    /*
67             * 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) )
68             *     create function rlike(text,text) returns bool as
69             *     'select $2 ilike $1' language sql strict immutable;
70         *     create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);
71             */
72          public List<StationBean> getByName(String name) throws SQLException {          public List<StationBean> getByName(String name) throws SQLException {
73                  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 " +
74                  "FROM trainstations " +                  "FROM trainstations " +
75                  "WHERE name ILIKE ? AND enabled = true " +                  "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
76                  "ORDER BY name ";                  "ORDER BY name ";
77    
78                                    
# Line 80  public class StationDAO { Line 85  public class StationDAO {
85                          stmt = conn.prepareStatement(SQL);                          stmt = conn.prepareStatement(SQL);
86                                                    
87                          stmt.setString(1, name + "%");                          stmt.setString(1, name + "%");
88                            stmt.setString(2, name + "%");
89                                                    
90                          res = stmt.executeQuery();                          res = stmt.executeQuery();
91                          result = convertResultset(res);                          result = convertResultset(res);
# Line 95  public class StationDAO { Line 101  public class StationDAO {
101                  return result;                  return result;
102          }          }
103                    
104            //the "hack" with max 1.5 degrees latitude and 2.5 degrees longitude is only valid since we only service danish trains
105            // in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km
106            
107            // the ultra fast method  (and only slightly inaccurate as long as we only cover a limited geographically area)
108            // is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to
109            // calculate the distance:
110        //     sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist
111            
112          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {
113                  String SQL = "SELECT * FROM ( "+                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +
114                  "               SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +                                           "       earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
115                  "                     earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +                                           "FROM trainstations " +
116                  "               FROM trainstations " +                                           "WHERE enabled = true AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " +
117                  "               WHERE enabled = true " +                                           "ORDER BY calcdist ASC " +
118                  "       ) AS trainstations2 " +                                           "LIMIT 4 ";
                 "ORDER BY calcdist ASC " +  
                 "LIMIT 4 ";  
119                  List<StationBean> result;                  List<StationBean> result;
120                  Connection conn = null;                  Connection conn = null;
121                  PreparedStatement stmt = null;                  PreparedStatement stmt = null;
# Line 113  public class StationDAO { Line 125  public class StationDAO {
125                          stmt = conn.prepareStatement(SQL);                          stmt = conn.prepareStatement(SQL);
126                          stmt.setDouble(1, latitude);                          stmt.setDouble(1, latitude);
127                          stmt.setDouble(2, longitude);                          stmt.setDouble(2, longitude);
128                            stmt.setDouble(3, latitude);
129                            stmt.setDouble(4, longitude);                  
130                          res = stmt.executeQuery();                          res = stmt.executeQuery();
131                          result = convertResultset(res);                          result = convertResultset(res);
132                                                    

Legend:
Removed from v.650  
changed lines
  Added in v.741

  ViewVC Help
Powered by ViewVC 1.1.20