Java源码示例:ch.hsr.geohash.WGS84Point
示例1
@Override
public Object apply(List<Object> args, Context context) throws ParseException {
if(args.size() < 1) {
return null;
}
String hash = (String)args.get(0);
if(hash == null) {
return null;
}
Optional<WGS84Point> point = GeoHashUtil.INSTANCE.toPoint(hash);
if(point.isPresent()) {
Map<String, Object> ret = new HashMap<>();
ret.put(GeoLiteCityDatabase.GeoProps.LONGITUDE.getSimpleName(), point.get().getLongitude());
ret.put(GeoLiteCityDatabase.GeoProps.LATITUDE.getSimpleName(), point.get().getLatitude());
return ret;
}
return null;
}
示例2
@Override
public Object apply(List<Object> args, Context context) throws ParseException {
if(args.size() < 2) {
return null;
}
String hash1 = (String)args.get(0);
if(hash1 == null) {
return null;
}
Optional<WGS84Point> pt1 = GeoHashUtil.INSTANCE.toPoint(hash1);
String hash2 = (String)args.get(1);
if(hash2 == null) {
return null;
}
Optional<WGS84Point> pt2 = GeoHashUtil.INSTANCE.toPoint(hash2);
DistanceStrategy strat = DistanceStrategies.HAVERSINE;
if(args.size() > 2) {
strat = DistanceStrategies.valueOf((String) args.get(2));
}
if(pt1.isPresent() && pt2.isPresent()) {
return GeoHashUtil.INSTANCE.distance(pt1.get(), pt2.get(), strat);
}
return Double.NaN;
}
示例3
@Override
public Object apply(List<Object> args, Context context) throws ParseException {
if(args.size() < 1) {
return null;
}
Object o1 = args.get(0);
if(o1 == null) {
return null;
}
WGS84Point centroid = null;
if(o1 instanceof Map) {
centroid = GeoHashUtil.INSTANCE.centroidOfWeightedPoints((Map<String, Number>)o1);
}
else if(o1 instanceof Iterable) {
centroid = GeoHashUtil.INSTANCE.centroidOfHashes((Iterable<String>)o1);
}
if(centroid == null) {
return null;
}
Integer precision = 12;
if(args.size() > 1) {
precision = (Integer)args.get(1);
}
return GeoHashUtil.INSTANCE.computeHash(centroid, precision).orElse(null);
}
示例4
public static boolean hasValidCoordinates(TripEvent trip) {
try {
WGS84Point pickup = new WGS84Point(trip.pickupLatitude, trip.pickupLongitude);
WGS84Point dropoff = new WGS84Point(trip.dropoffLatitude, trip.dropoffLongitude);
return NYC.contains(pickup) && NYC.contains(dropoff);
} catch (IllegalArgumentException e) {
LOG.debug("cannot parse coordinates for event {}", trip, e);
return false;
}
}
示例5
public static boolean nearJFK(double latitude, double longitude) {
try {
return JFK.contains(new WGS84Point(latitude, longitude));
} catch (IllegalArgumentException e) {
return false;
}
}
示例6
public static boolean nearLGA(double latitude, double longitude) {
try {
return LGA.contains(new WGS84Point(latitude, longitude));
} catch (IllegalArgumentException e) {
return false;
}
}
示例7
private Cursor searchGeofencesRange(WGS84Point center, int radius) throws SQLException {
GeoHashCircleQuery query = new GeoHashCircleQuery(center, radius);
List<GeoHash> geoHashes = truncateToBase32(query.getSearchHashes());
String sql = "SELECT " + DBHelper.TG_FENCE + " FROM " + DBHelper.TABLE_GEOFENCES + " WHERE " + DBHelper.TG_FENCE + " LIKE ?";
String[] args = new String[geoHashes.size()];
for (int i = 0; i < geoHashes.size(); i++) {
args[i] = geoHashes.get(i).toBase32()+"%";
if (i > 0) {
sql += " OR " + DBHelper.TG_FENCE + " LIKE ?";
}
}
sql += " LIMIT " + (HIGH+1);
return db.rawQuery(sql, args);
}
示例8
public static boolean nearNYC(double latitude, double longitude) {
return NYC.stream().anyMatch(hash -> hash.contains(new WGS84Point(latitude, longitude)));
}
示例9
public static boolean nearJFK(double latitude, double longitude) {
return JFK.stream().anyMatch(hash -> hash.contains(new WGS84Point(latitude, longitude)));
}
示例10
public static boolean nearLGA(double latitude, double longitude) {
return LGA.stream().anyMatch(hash -> hash.contains(new WGS84Point(latitude, longitude)));
}
示例11
/**
* @param center Center point of search.
* @param radius Initial radius of search. Final value of radius will be stored.
* @param current Initial cursor. Will be closed if not needed anymore.
* It should have > HIGH rows, otherwise it will be returned back.
* @return Found cursor, possibly having less than HIGH geofences. If this is not possible,
* then it might have more than HIGH rows. It can also be original cursor.
*/
private Cursor searchAndReduce(WGS84Point center, int radius, Cursor current) {
Cursor result = current, previous = null;
int radiusPrev = radius;
boolean done = false;
while (!done) {
//Decide whether found is acceptable.
if (current.getCount() <= HIGH) {
if (current.getCount() > 0) {
//Good enough / perfect case.
result = current;
close(previous);
} else {
//It's zero, use previous.
radius = radiusPrev;
if (previous != null) {
//If the previous value was > 0 then we take it.
result = previous;
close(current);
} else {
//Previous is null, invalid invocation. (Called with current = 0)
result = current;
close(previous);
}
}
Logger.log.geofence("Found " + result.getCount() + " by reducing radius to " + radius + " m");
preferences.edit().putInt(
Constants.SharedPreferencesKeys.Location.INITIAL_GEOFENCES_SEARCH_RADIUS, radius).apply();
this.radius = radius;
break;
}
//Loop things
close(previous);
radiusPrev = radius;
radius /= 2;
if (radius < MIN_RADIUS) {
//So we don't overshoot or loop forever.
radius = MIN_RADIUS;
done = true;
Logger.log.geofence("Minimal radius reached: " + radius);
}
previous = current;
current = searchGeofencesRange(center, radius);
}
return result;
}
示例12
/**
* @param center Center point of search.
* @param radius Initial radius of search. Final value of radius will be stored.
* @param current Initial cursor. Will be closed if not needed anymore.
* It should have <= LOW rows, otherwise it will be returned back.
* @return Found cursor, possibly having more than LOW and less than HIGH geofences.
* If this is not possible then it might have more or less, but always at least 1,
* closest to center point, provided there are geofences at all in MAX_RADIUS.
*/
private Cursor searchAndExtend(WGS84Point center, int radius, Cursor current) {
Cursor result = current, previous = null;
int radiusPrev = radius;
boolean done = false;
while (!done) {
//Decide whether found is acceptable.
if (current.getCount() > LOW) {
if (current.getCount() <= HIGH) {
//Good enough / perfect case.
result = current;
close(previous);
} else {
//More than HIGH, check previous cursor.
radius = radiusPrev;
if (previous != null && previous.getCount() != 0) {
//If the previous value was > 0 then we take it.
result = previous;
close(current);
} else {
//Else we take current result (which is above HIGH) and then limit it to HIGH.
result = current;
close(previous);
}
}
Logger.log.geofence("Found: " + result.getCount() + " by extending radius to " + radius + " m");
preferences.edit().putInt(
Constants.SharedPreferencesKeys.Location.INITIAL_GEOFENCES_SEARCH_RADIUS, radius).apply();
this.radius = radius;
break;
}
//Loop things
close(previous);
radiusPrev = radius;
radius *= 2;
if (radius > MAX_RADIUS) {
//So we don't overshoot or loop forever.
radius = MAX_RADIUS;
done = true;
Logger.log.geofence("Maximum radius reached: " + radius);
}
previous = current;
current = searchGeofencesRange(center, radius);
}
return result;
}
示例13
public double distance(WGS84Point point1, WGS84Point point2);