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

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

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

revision 1505 by torben, Wed Jun 8 15:45:41 2011 UTC revision 1512 by torben, Wed Jun 8 19:11:36 2011 UTC
# Line 105  public class StationDAO { Line 105  public class StationDAO {
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 " +
# Line 121  public class StationDAO { Line 121  public class StationDAO {
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            public StationBean getByName(final String name) throws SQLException {
149                    StationBean stations = getByNameNormal(name);
150                    
151                    if (stations.entries.size() == 0) {
152                            logger.info("getByName failover: " + name);
153                            stations = getByNameFuzzy(name);
154                    }
155                    return stations;
156            }
157            
158            
159    
160          //Latitude (horizonal), longitude(vertical) so          //Latitude (horizonal), longitude(vertical) so
161          // 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
162          // 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.
163          // 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)
164            // and is 59.4km at northern point (skagen = 57.75,10.65)
165            // The "hack" with max 0.4 degrees latitude and 0.75 degrees longitude is only valid since we only service danish trains,
166          // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km          // in denmark 0.4dg latitude ~ 44km, 0.75dg longitude ~ 47km
167    
168          // 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)

Legend:
Removed from v.1505  
changed lines
  Added in v.1512

  ViewVC Help
Powered by ViewVC 1.1.20