Java – what is the best way to find all addresses at a specific distance from the selected point?

I'm developing an application that should display addresses in specific locations I know how to find the distance between two points, but the problem is that I don't know what is the best way in terms of performance

One way is to retrieve all addresses and check them one by one to the selected address on the back end, but is there a way to minimize the number of items retrieved from the database instead of using memory? What is the best practice?

Imagine that I have 300000 records. I have to retrieve them and calculate their distance to the selected point? As James suggested, I can record records in different regions and calculate the distance. So which method can track well and calculate the distance through query or Java?

public class Address{
    long Id;
    Double latitude;
    Double longitude;
    ..
  }

Calculation

public static double distFrom(double lat1,double lng1,double lat2,double lng2) {
  double earthRadius = 3958.75;
  double dLat = Math.toradians(lat2-lat1);
  double dLng = Math.toradians(lng2-lng1);
  double sindLat = Math.sin(dLat / 2);
  double sindLng = Math.sin(dLng / 2);
  double a = Math.pow(sindLat,2) + Math.pow(sindLng,2)
        * Math.cos(Math.toradians(lat1)) *     Math.cos(Math.toradians(lat2));
  double c = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
  double dist = earthRadius * c;

  return dist;
}

This question and this one provide a method to calculate the distance through mysql, but I'm confused about which method is better Java or mysql

Solution

When I implement this in MySQL (where it is stored on a flat sphere, what is it basically (I assume you're talking about the earth!), I have stored as much pre - calculated information as possible in the database Therefore, for rows that store latitude and longitude, I also calculate the following fields when inserting:

>Radian length (math. Toradians (longitude)) > sinradianslatitude (math. Sin (math. Toradians (latitude)) > cosradianslatitude (math. Cos (math. Toradians (latitude))

Then when I search for places in latitude / longitude x units, my preparation statement is as follows:

from Location l where
    acos(
        sin(:latitude) * sinradiansLatitude + 
        cos(:latitude) * cosradiansLatitude * 
        cos(radiansLongitude - :longitude) 
        ) * YYYY < :distance
    and l.latitude>:minimumSearchLatitude
    and l.latitude<:maximumSearchLatitude 
    and l.longitude>:minimumSearchLongitude 
    and l.longitude<:maximumSearchLongitude 
    order by acos(
                sin(:latitude) * sinradiansLatitude + 
                cos(:latitude) * cosradiansLatitude * 
                cos(radiansLongitude - :longitude)  
        ) * YYYY asc

Where yyyy = 3965 can give the distance in miles or yyyy = 6367 can be used for the distance in kilometers

Finally, I've used the maximumsearchlatitude / maximumsearchlongitude / minimumsearchlongitude / maximumsearchlongitude parameters to exclude most points in the result set before the database performs any calculations You may or may not need this If you do, it will depend on what value you choose for these parameters, because it will depend on what you are searching for

Obviously, the wise application of indexes in database is necessary

The advantage of using this method is that the information that does not need to be changed each time can only be calculated once, and the radian length of each row is calculated each time the search is performed. The values of sinradianslatitude and cosradians latitude will be calculated very quickly

Another option is to use geospatial index, which means that all this is handled by the database for you I don't know how hibernate integrates

Disclaimer: since I've seen it for so long, I'm not a GIS expert!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>