Skip to content
Browse files

Add an error term for distance comparisons in geo search. Part of SER…

…VER-848, SERVER-1191
  • Loading branch information...
1 parent 21bb8b4 commit 93661510e975f9c8a62f315c6a602fecbf50cbfb @kreuter kreuter committed Jun 8, 2010
Showing with 38 additions and 2 deletions.
  1. +10 −2 db/index_geo2d.cpp
  2. +28 −0 jstests/geo_circle3.js
View
12 db/index_geo2d.cpp
@@ -369,6 +369,11 @@ namespace mongo {
_scaling = (1024*1024*1024*4.0)/(_max-_min);
_order = orderBuilder.obj();
+
+ GeoHash a(0, 0, _bits);
+ GeoHash b = a;
+ b.move(1, 1);
+ _error = distance(a, b);
}
int _configval( const IndexSpec* spec , const string& name , int def ){
@@ -542,6 +547,7 @@ namespace mongo {
double _scaling;
BSONObj _order;
+ double _error;
};
class Point {
@@ -918,7 +924,8 @@ namespace mongo {
if ( ! checkDistance( GeoHash( node.key.firstElement() ) , d ) ){
GEODEBUG( "\t\t\t\t bad distance : " << node.recordLoc.obj() << "\t" << d );
return;
- }
+ }
+ GEODEBUG( "\t\t\t\t good distance : " << node.recordLoc.obj() << "\t" << d );
// matcher
MatchDetails details;
@@ -1356,6 +1363,7 @@ namespace mongo {
_prefix = _start;
_maxDistance = i.next().numberDouble();
uassert( 13061 , "need a max distance > 0 " , _maxDistance > 0 );
+ _maxDistance += g->_error;
_state = START;
_found = 0;
@@ -1411,7 +1419,7 @@ namespace mongo {
virtual bool checkDistance( const GeoHash& h , double& d ){
d = _g->distance( _start , h );
GEODEBUG( "\t " << h << "\t" << d );
- return d <= ( _maxDistance + .01 );
+ return d <= _maxDistance;
}
GeoHash _start;
View
28 jstests/geo_circle3.js
@@ -0,0 +1,28 @@
+// SERVER-848 and SERVER-1191.
+db.places.drop()
+
+n = 0;
+db.places.save({ "_id": n++, "loc" : { "x" : 4.9999, "y" : 52 } })
+db.places.save({ "_id": n++, "loc" : { "x" : 5, "y" : 52 } })
+db.places.save({ "_id": n++, "loc" : { "x" : 5.0001, "y" : 52 } })
+db.places.save({ "_id": n++, "loc" : { "x" : 5, "y" : 52.0001 } })
+db.places.save({ "_id": n++, "loc" : { "x" : 5, "y" : 51.9999 } })
+db.places.save({ "_id": n++, "loc" : { "x" : 4.9999, "y" : 52.0001 } })
+db.places.save({ "_id": n++, "loc" : { "x" : 5.0001, "y" : 52.0001 } })
+db.places.save({ "_id": n++, "loc" : { "x" : 4.9999, "y" : 51.9999 } })
+db.places.save({ "_id": n++, "loc" : { "x" : 5.0001, "y" : 51.9999 } })
+db.places.ensureIndex( { loc : "2d" } )
+radius=0.0001
+center=[5,52]
+//print(db.places.find({"loc" : {"$within" : {"$center" : [center, radius]}}}).count())
+// FIXME: we want an assert, e.g., that there be 5 answers in the find().
+db.places.find({"loc" : {"$within" : {"$center" : [center, radius]}}}).forEach(printjson);
+
+
+// the result:
+// { "_id" : ObjectId("4bb1f2f088df513435bcb4e1"), "loc" : { "x" : 5, "y" : 52 } }
+// { "_id" : ObjectId("4bb1f54383459c40223a8ae7"), "loc" : { "x" : 5, "y" : 51.9999 } }
+// { "_id" : ObjectId("4bb1f54583459c40223a8aeb"), "loc" : { "x" : 5.0001, "y" : 51.9999 } }
+// { "_id" : ObjectId("4bb1f2e588df513435bcb4e0"), "loc" : { "x" : 4.9999, "y" : 52 } }
+// { "_id" : ObjectId("4bb1f30888df513435bcb4e2"), "loc" : { "x" : 5.0001, "y" : 52 } }
+// { "_id" : ObjectId("4bb1f54383459c40223a8ae8"), "loc" : { "x" : 4.9999, "y" : 52.0001 } }

0 comments on commit 9366151

Please sign in to comment.
Something went wrong with that request. Please try again.