java - Finding all values within a radius using geographical math -
how can find values (in case establishments) within radius of (x) miles using latitude/longitude coordinates?
the code:
public class geogen { static final geoposition user_position = new geoposition(39.410868, -107.102182); public static void main(string[] args) { new establishment("fries electronics", randomlocation(user_position, 40)).print(); new establishment("walmart supercenter", randomlocation(user_position, 40)).print(); new establishment("target", randomlocation(user_position, 40)).print(); new establishment("krogers", randomlocation(user_position, 40)).print(); new establishment("mcdonalds", randomlocation(user_position, 40)).print(); } public static geoposition randomlocation(geoposition location, double radius) { random random = new random(); // convert radius miles meters double meters = radius * 1609.34; // convert radius meters degrees double radiusindegrees = meters / 111000f; double u = random.nextdouble(); double v = random.nextdouble(); double w = radiusindegrees * math.sqrt(u); double t = 2 * math.pi * v; double x = w * math.cos(t); double y = w * math.sin(t); // adjust x-coordinate shrinking of east-west distances double new_x = x / math.cos(location.latitude()); double foundlongitude = new_x + location.longitude(); double foundlatitude = y + location.latitude(); return new geoposition(foundlongitude, foundlatitude); } public static double distancebetween(geoposition a, geoposition b) { double longdif = a.longitude() - b.longitude(); double distance = math.sin(deg2rad(a.latitude())) * math.sin(deg2rad(b.latitude())) + math.cos(deg2rad(a.latitude())) * math.cos(deg2rad(b.latitude())) * math.cos(deg2rad(longdif)); distance = math.acos(distance); distance = rad2deg(distance); distance = distance * 60 * 1.1515; // convert meters distance = distance * 0.8684; // convert miles. return distance; } private static double rad2deg(double rad) { return (rad * 180.0 / math.pi); } private static double deg2rad(double deg) { return (deg * math.pi / 180.0); } } /** * class representing establishment in world. * * @author christian */ class establishment { public static map<geoposition, string> establishments = new hashmap<>(); private final string name; private final geoposition geoposition; public establishment(string name, geoposition geoposition) { this.name = name; this.geoposition = geoposition; establishments.put(geoposition, name); } public void print() { system.out.print("establishment("+name+") created approx "); system.out.printf("%.2f", geogen.distancebetween(geoposition, geogen.user_position)); system.out.print(" miles specified lat/long \n"); } public final string name() { return name; } public final geoposition position() { return geoposition; } } /** * class representing geographical location using latitude/longitude. * * @author christian */ class geoposition { private final double longitude; private final double latitude; public geoposition(double longitude, double latitude) { this.longitude = longitude; this.latitude = latitude; } public double longitude() { return longitude; } public double latitude() { return latitude; } }
running application produce different results each time, it's randomized, here example of using geogen#distancebetween(geoposition, geoposition)
method.
establishment(fries electronics) created approx 19.26 miles specified lat/long establishment(walmart supercenter) created approx 9.79 miles specified lat/long establishment(target) created approx 28.83 miles specified lat/long establishment(krogers) created approx 10.61 miles specified lat/long establishment(mcdonalds) created approx 3.37 miles specified lat/long
however, i'm trying figure out how establishments within x miles. example this
geogen#findestablishmentswithinradius(geoposition, double) returns list<establishment>
which return list of establishments within x miles of specified geoposition.
you have method calculate distance? loop through establishments , check if within radius or not.
add of establishments list, , can use stream example:
public list<establishment> findestablishmentswithinradius(list<establishment> list, geoposition position, double radius) { return list.stream().filter(e -> distancebetween(position, e.position()) <= radius).collect(collectors.tolist()); }
Comments
Post a Comment