[SERVER-12725] $box should be able to use a 2dsphere query if available and valid coords Created: 14/Feb/14  Updated: 17/Jun/16  Resolved: 16/Apr/14

Status: Closed
Project: Core Server
Component/s: Geo
Affects Version/s: 2.5.5
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Ross Lawley Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Participants:

 Description   

In the following test case - I'd expect the $box query to be able to use the 2dsphere index rather than just a basic cursor.

db.geo.drop()
db.geo.insert( { type: "house", loc : { type : "Point", coordinates : [ 71.0603, 42.3583 ] } })
db.geo.insert( { type: "flat",  loc : { type : "Point", coordinates : [ 87.6500, 41.8500 ] } })
 
db.geo.ensureIndex({loc: "2dsphere", type: 1});
 
// Query using $box
db.geo.find({loc: {
    $geoWithin: {
        $box: [[-180, -90], [180, 90]]
    }
}, type: "house"}).explain()

Essentially the query is the same as - which will use the 2dsphere index

db.geo.find( { loc :
  { $geoWithin :
    { $geometry :
      { type : "Polygon" ,
        coordinates : [[ [-180, -90], [180, -90], [180, 90], [-180, -90] ] ]
  } } },
  type: "house" } ).explain()



 Comments   
Comment by Greg Studer [ 16/Apr/14 ]

This isn't the same query - $box queries over a projected (flat) range, while a GeoJSON Polygon (by default) is a shape over the geographic (spheroidal) globe (WGS84 coordinate reference system (CRS)). This basically means the line segments connecting each vertex follow different paths - a horizontal line of latitude is not the shortest distance between two points on the globe (except at the equator).

Basically extended shapes (lines, polygons) have nontrivial projections between the "2d" indexed space and "2dsphere" indexed space (points are special, since they're equal by convention and more-or-less define the projection).

We're working on making more of the geo interface uniform though, ideally you'd want to be able to query flat shapes using GeoJSON syntax. This requires supporting more than just the default CRS.

Comment by Daniel Pasette (Inactive) [ 18/Feb/14 ]

There is a planned effort to clean up and unify much of the geo user interface. This could be part of that effort.

Generated at Thu Feb 08 03:29:25 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.