2

Ok, I have a database with many location records there (storing latitude & longitude).
When new potential record comes, I need to check - if it is closer than 50 meters to any of existing records - I should not include it in database.

I know there is a possibility to calculate distance between two locations.
But in order to do that I need extract all records from my database and then in loop compare potential record to every existing. I suppose it will take some time.

I would like to have some delta, on which I can increase/decrease latitude & longitude of potential record - and that delta would give me 50 meters distance radius.

For example, I'd like to have something like that:

    public boolean needsToBeIncluded(double newLat, double newLng) {
        int delta = 0;
        Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM myLocations WHERE latitude between " + (newLat - delta) + " and " + (newLat + delta) +
                " AND longitude between "  + (newLng - delta) + " and " + (newLng + delta), null); 
        return c.getCount() > 0;
    }

Is it possible or I have wrong imagination about latitude & longitude? Thx.

Goltsev Eugene
  • 3,325
  • 6
  • 25
  • 48
  • 1
    You can definitely get a delta depending on the area/region of your locations, which gives you a grid of roughly 50x50 meters, then you only need to check those points within the grid. – Xiongbing Jin Apr 07 '16 at 19:24
  • Please give me more info - how calculate such delta depending on region? Thanks. – Goltsev Eugene Apr 07 '16 at 19:30
  • 1
    You need to know the min and max `lat` value of all your existing points, because each degree of `lat` translate to roughly 111km regardless of `lon`, bu the distance for each degree of `lon` depends on `lat` (i.e. it's longer at the equator and zero at the arctic point). Use http://www.nhc.noaa.gov/gccalc.shtml to look up what one degree of `lon` would translate into at your min and max latitudes. Then you can get the `lon` deltas for 50 meters at the min and max latitudes. Use the larger of the two values. – Xiongbing Jin Apr 07 '16 at 19:44
  • I got the total idea. But it's still hard for me to implement this. If you would be so kind to make short example for this - I would be very greatful and mark it as correct. of course. Also, is there some table indicating how much distance for each lon degree is there for each lat degree? – Goltsev Eugene Apr 08 '16 at 11:43

1 Answers1

1

You could compute the geohash value of each of your location records. Comparing the prefix portions of location records' geohash values enables determining their nearness. This GIS Stack Exchange entry helps explain geohashes while this Stack Exchange entry explains the accuracy of adjacent geohash cells - the point where two geohash sequences start to differ determines their delta.

jwd630
  • 4,529
  • 1
  • 20
  • 22
  • Great answer, thanks, but not actually what I wanted. It would be almost the method I described in question - firstly get all records, and then in loop get distance to new point. But your information is very useful to know. – Goltsev Eugene Apr 13 '16 at 09:22
  • Also, it would not allow me to determing distance to 50 meters, for example (in 3rd link distances are 19 and 188 meters approx). – Goltsev Eugene Apr 13 '16 at 09:23