--- android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/StationDAO.java 2010/09/22 21:09:39 1105 +++ android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/StationDAO.java 2010/09/22 22:46:26 1106 @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.Iterator; import java.util.List; import java.util.logging.Logger; @@ -67,49 +68,81 @@ pm.close(); } } + - - public StationBean getByLocation(double latitude, double longitude) { - - - + //String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; + public List getByLocationList(double latitude, double longitude, boolean geolimit) { + PersistenceManager pm = null; + final double LAT = 0.4; + final double LNG = 0.75; try { pm = PMF.get().getPersistenceManager(); - List beanList = (List) pm.newQuery(JdoStationBean.class).execute(); + Query q = pm.newQuery(JdoStationBean.class); + - StationBean stationBean = new StationBean(); + if (geolimit == true) { + double minLat = latitude - LAT; + double maxLat = latitude + LAT; + + //DAMN JDO implementation only allows us to compare on one parameter - for(JdoStationBean bean : beanList) { - double meter = Geo.distanceKM(latitude, longitude, bean.getLatitude(), bean.getLongitude()) * 1000.0; + String filter = String.format("latitude > %f && latitude < %f", minLat, maxLat); - bean.distance = (int) meter; + q.setFilter( filter ); } + List beanList = (List) q.execute(); + + logger.info("beanList size " + beanList.size()); + + return beanList; + } finally { + pm.close(); + } + } - Collections.sort(beanList, new Comparator() { - @Override - public int compare(JdoStationBean o1, JdoStationBean o2) { - if (o1.distance < o2.distance) { - return -1; - } else if (o1.distance > o2.distance) { - return 1; - } else { - return 0; - } + + public StationBean getByLocation(double latitude, double longitude) { + + List beanList = getByLocationList(latitude,longitude,true); + + if (beanList.size() < LOCATION_LIMIT ) { + logger.info("getByLocation failover: " +latitude + "," + longitude); + beanList = getByLocationList(latitude,longitude, false); + } + + StationBean stationBean = new StationBean(); + + + Geo location = new Geo(latitude,longitude); + for(JdoStationBean bean : beanList) { + double meter = Geo.distanceKM( location, new Geo(bean.getLatitude(), bean.getLongitude() )) * 1000.0; + + bean.distance = (int) meter; + } + + + Collections.sort(beanList, new Comparator() { + @Override + public int compare(JdoStationBean o1, JdoStationBean o2) { + if (o1.distance < o2.distance) { + return -1; + } else if (o1.distance > o2.distance) { + return 1; + } else { + return 0; } - }); - - for (int i=0; i