mongodb - How does the geospatial $nearSphere operator interact with LineString and Polygon -
according documentation, 2dsphere geospatial index can used complex geojson shapes linestring's , polygon's consist of multiple nodes.
when search field filled such complex shapes $nearsphere proximity geojson object (which can potentially complex shape), how distance of these shapes calculated?
will compare closest nodes of shapes? distant nodes? average of nodes? closest point on of edges of shape?
$nearsphere operator allows specify point (center of "sphere" can define providing maxdistance parameter). therefore meaningful answer can how shapes sorted nearest furthest.
the answer shapes have equidistant "nearest" point going sorted ahead of shapes have nearest point further. in other words, distance between point , shape distance between point , nearest point on shape.
we can see running variant of $nearsphere $geonear command returns calculated distance can examine.
our sample data:
> db.near.find() { "_id" : 10, "loc" : { "type" : "polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 5 ], [ 5, 5 ], [ 5, 0 ], [ 0, 0 ] ] ] } } { "_id" : 11, "loc" : { "type" : "polygon", "coordinates" : [ [ [ 1, 1 ], [ 1, 5 ], [ 5, 5 ], [ 5, 1 ], [ 1, 1 ] ] ] } } { "_id" : 12, "loc" : { "type" : "polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [ 0, 0 ] ] ] } } { "_id" : 13, "loc" : { "type" : "polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [0, 0 ] ], [ [ 1, 1 ], [ 1, 5 ], [ 5, 5 ], [ 5, 1 ], [ 1, 1 ] ] ] } } { "_id" : 14, "loc" : { "type" : "polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [ 0, 0 ] ], [ [ 0.1, 0.1 ], [ 0.1, 5 ], [ 5, 5 ], [ 5, 0.1 ], [ 0.1, 0.1 ] ] ] } } { "_id" : 15, "loc" : { "type" : "polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 1 ], [ 1, 1 ], [ 1, 0 ], [0, 0 ] ], [ [ 0.1, 0.1 ], [ 0.1, 0.5 ], [ 0.5, 0.5 ], [ 0.5, 0.1 ], [ 0.1, 0.1 ] ] ] } } { "_id" : 16, "loc" : { "type" : "linestring", "coordinates" : [ [ 0, 0 ], [ 0.1, 0.1 ] ] } } { "_id" : 17, "loc" : { "type" : "linestring", "coordinates" : [ [ 0, 0 ], [ 0, 0.1 ] ] } } { "_id" : 18, "loc" : { "type" : "linestring", "coordinates" : [ [ 0, 0 ], [ 0.1, 0 ] ] } } our results $nearsphere:
{ "_id" : 16, "loc" : { "type" : "linestring", "coordinates" : [ [ 0, 0 ], [ 0.1, 0.1 ] ] } } { "_id" : 18, "loc" : { "type" : "linestring", "coordinates" : [ [ 0, 0 ], [ 0.1, 0 ] ] } } { "_id" : 14, "loc" : { "type" : "polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [ 0, 0 ] ], [ [ 0.1, 0.1 ], [ 0.1, 5 ], [ 5, 5 ], [ 5, 0.1 ], [ 0.1, 0.1 ] ] ] } } { "_id" : 13, "loc" : { "type" : "polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [ 0, 0 ] ], [ [ 1, 1 ], [ 1, 5 ], [ 5, 5 ], [ 5, 1 ], [ 1, 1 ] ] ] } } { "_id" : 15, "loc" : { "type" : "polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 1 ], [ 1, 1 ], [ 1, 0 ], [ 0, 0 ] ], [ [ 0.1, 0.1 ], [ 0.1, 0.5 ], [ 0.5, 0.5 ], [ 0.5, 0.1 ], [ 0.1, 0.1 ] ] ] } } { "_id" : 12, "loc" : { "type" : "polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 6 ], [ 6, 6 ], [ 6, 0 ], [ 0, 0 ] ] ] } } { "_id" : 17, "loc" : { "type" : "linestring", "coordinates" : [ [ 0, 0 ], [ 0, 0.1 ] ] } } { "_id" : 10, "loc" : { "type" : "polygon", "coordinates" : [ [ [ 0, 0 ], [ 0, 5 ], [ 5, 5 ], [ 5, 0 ], [ 0, 0 ] ] ] } } { "_id" : 11, "loc" : { "type" : "polygon", "coordinates" : [ [ [ 1, 1 ], [ 1, 5 ], [ 5, 5 ], [ 5, 1 ], [ 1, 1 ] ] ] } } and running $geosphere command, can confirm distance point shapes (omitting coordinates space considerations):
> db.runcommand({geonear:"near", near: {type:"point", coordinates:[0,0]}, spherical:true}) [ { "dis" : 0, "obj" : { "_id" : 16, "loc" : { "type" : "linestring" } } }, { "dis" : 0, "obj" : { "_id" : 18, "loc" : { "type" : "linestring" } } }, { "dis" : 0, "obj" : { "_id" : 14, "loc" : { "type" : "polygon" } } }, { "dis" : 0, "obj" : { "_id" : 13, "loc" : { "type" : "polygon" } } }, { "dis" : 0, "obj" : { "_id" : 15, "loc" : { "type" : "polygon" } } }, { "dis" : 0, "obj" : { "_id" : 12, "loc" : { "type" : "polygon" } } }, { "dis" : 0, "obj" : { "_id" : 17, "loc" : { "type" : "linestring" } } }, { "dis" : 0, "obj" : { "_id" : 10, "loc" : { "type" : "polygon" } } }, { "dis" : 157424.6238723255, "obj" : { "_id" : 11, "loc" : { "type" : "polygon" } } } ] as can see, distance of every shape (except last 1 started @ [1,1]) point [0,0] 0, though shapes differed in size , shape. within documents have same distance specified point, order not defined. means if seems deterministic, not guaranteed same.
Comments
Post a Comment