4 |
import java.util.ArrayList; |
import java.util.ArrayList; |
5 |
import java.util.Collections; |
import java.util.Collections; |
6 |
import java.util.Comparator; |
import java.util.Comparator; |
7 |
|
import java.util.Iterator; |
8 |
import java.util.List; |
import java.util.List; |
9 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
10 |
|
|
68 |
pm.close(); |
pm.close(); |
69 |
} |
} |
70 |
} |
} |
71 |
|
|
72 |
|
|
73 |
|
//String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
74 |
public StationBean getByLocation(double latitude, double longitude) { |
public List<JdoStationBean> getByLocationList(double latitude, double longitude, boolean geolimit) { |
75 |
|
|
|
|
|
|
|
|
76 |
PersistenceManager pm = null; |
PersistenceManager pm = null; |
77 |
|
final double LAT = 0.4; |
78 |
|
final double LNG = 0.75; |
79 |
|
|
80 |
try { |
try { |
81 |
pm = PMF.get().getPersistenceManager(); |
pm = PMF.get().getPersistenceManager(); |
82 |
List<JdoStationBean> beanList = (List<JdoStationBean>) pm.newQuery(JdoStationBean.class).execute(); |
Query q = pm.newQuery(JdoStationBean.class); |
83 |
|
|
84 |
|
|
|
StationBean stationBean = new StationBean(); |
|
85 |
|
|
86 |
|
if (geolimit == true) { |
87 |
|
double minLat = latitude - LAT; |
88 |
|
double maxLat = latitude + LAT; |
89 |
|
|
90 |
|
//DAMN JDO implementation only allows us to compare on one parameter |
91 |
|
|
92 |
for(JdoStationBean bean : beanList) { |
String filter = String.format("latitude > %f && latitude < %f", minLat, maxLat); |
|
double meter = Geo.distanceKM(latitude, longitude, bean.getLatitude(), bean.getLongitude()) * 1000.0; |
|
93 |
|
|
94 |
bean.distance = (int) meter; |
q.setFilter( filter ); |
95 |
} |
} |
96 |
|
|
97 |
|
List<JdoStationBean> beanList = (List<JdoStationBean>) q.execute(); |
98 |
|
|
99 |
|
logger.info("beanList size " + beanList.size()); |
100 |
|
|
101 |
|
return beanList; |
102 |
|
} finally { |
103 |
|
pm.close(); |
104 |
|
} |
105 |
|
} |
106 |
|
|
107 |
Collections.sort(beanList, new Comparator<JdoStationBean>() { |
|
108 |
@Override |
public StationBean getByLocation(double latitude, double longitude) { |
109 |
public int compare(JdoStationBean o1, JdoStationBean o2) { |
|
110 |
if (o1.distance < o2.distance) { |
List<JdoStationBean> beanList = getByLocationList(latitude,longitude,true); |
111 |
return -1; |
|
112 |
} else if (o1.distance > o2.distance) { |
if (beanList.size() < LOCATION_LIMIT ) { |
113 |
return 1; |
logger.info("getByLocation failover: " +latitude + "," + longitude); |
114 |
} else { |
beanList = getByLocationList(latitude,longitude, false); |
115 |
return 0; |
} |
116 |
} |
|
117 |
|
StationBean stationBean = new StationBean(); |
118 |
|
|
119 |
|
|
120 |
|
Geo location = new Geo(latitude,longitude); |
121 |
|
for(JdoStationBean bean : beanList) { |
122 |
|
double meter = Geo.distanceKM( location, new Geo(bean.getLatitude(), bean.getLongitude() )) * 1000.0; |
123 |
|
|
124 |
|
bean.distance = (int) meter; |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
|
Collections.sort(beanList, new Comparator<JdoStationBean>() { |
129 |
|
@Override |
130 |
|
public int compare(JdoStationBean o1, JdoStationBean o2) { |
131 |
|
if (o1.distance < o2.distance) { |
132 |
|
return -1; |
133 |
|
} else if (o1.distance > o2.distance) { |
134 |
|
return 1; |
135 |
|
} else { |
136 |
|
return 0; |
137 |
} |
} |
|
}); |
|
|
|
|
|
for (int i=0; i<LOCATION_LIMIT && i<beanList.size(); i++) { |
|
|
stationBean.entries.add( beanList.get(i).toStationEntry() ); |
|
138 |
} |
} |
139 |
|
}); |
140 |
|
|
141 |
return stationBean; |
for (int i=0; i<LOCATION_LIMIT && i<beanList.size(); i++) { |
142 |
} finally { |
stationBean.entries.add( beanList.get(i).toStationEntry() ); |
|
pm.close(); |
|
143 |
} |
} |
144 |
|
|
145 |
|
return stationBean; |
146 |
} |
} |
147 |
|
|
148 |
|
|