1

I have a collection of lat+long values. In a given moment a new point (lat+long) is passed to application. At this moment, application should return the distances from this point to those values stored in the collection. Application should also be able to filter all those values that are within a given distance. Something like: Return all values that are within 10 miles (or in Kilometers) away from the point. Could you help to build a java algorithm to perform that task ? Thanks a lot

j0k
  • 22,600
  • 28
  • 79
  • 90
blackjack
  • 1,081
  • 2
  • 13
  • 30

1 Answers1

3

If you have a large amount of data:

Look at: http://jsi.sourceforge.net/ . This is an in-memory geospatial database. It uses an RTree (http://en.wikipedia.org/wiki/R-tree) to efficiently do the type of calculations you want.

If the total number of points is small:

Then you could use the Haverine formulary (as mentioned in a comment) on each point. Here is an implementation I found: http://bigdatanerd.wordpress.com/2011/11/03/java-implementation-of-haversine-formula-for-distance-calculation-between-two-points/.

You would do something like:

for( Point p : pointCollection ){
  distance = haversine(p, newPoint);
  if( distance < targetDistance ){
     .. add new results ...
  }
}
Dave
  • 13,518
  • 7
  • 42
  • 51
  • Thanks for your answer. If my collection has about 500 points which of two approaches is the best suited? And what if it's 200 ? How much would be considered a large amount of data? Thousands ? Thanks again. – blackjack Oct 17 '13 at 04:36
  • 1
    It depends on how frequently this is called an how fast you need it to return. My guess is that under 1000 will be pretty fast either way. I would start with the simpler approach of just calculating it for each datapoint. If that appears to be too slow then move to using the library. – Dave Oct 18 '13 at 13:46