问题
Guys I am trying to query all the polygons inside by bounding box but it simply returns 0.. It should be returning many polygons!
Alternatively I try to query a much larger bounding box and nothing happens!
My query is:
{
geometry:
{ $geoWithin:
{ $box:
[ [-73.995762,40.764826], [-73.934034,40.802038] ]
}
}
}
Notice that the very same query returns a valid result for geometries of type Point
回答1:
to query all the polygons inside by bounding box but it simply returns 0
The $box operator for $geoWithin only supports documents based on grid coordinates and does not support GeoJSON shapes format.
the very same query returns a valid result for geometries of type Point
Depending on your documents structure, and how you are querying them, this is probably treated as being grid coordinates i.e. {geometry: [<long>, <lat>]}
This may not have worked for your polygons document because GeoJSON Polygons require an extra array wrapper. i.e. [[ [<long>, <lat>] ]]
invalidating the grid coordinates format.
If your documents are in GeoJSON format, and you would like to select an area, you could utilise $geometry instead.
db.places.find(
{
'geometry': {
$geoWithin: {
$geometry: {
type : "Polygon" ,
coordinates: [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ]
}
}
}
}
)
Worth noting that MongoDB Compass geospatial visualisation currently (v1.6) does not support GeoJSON yet.
回答2:
Here goes the answer:
$box do work with GeoJSON, but not with polygons! You need to generate a geometry with the $box instead.
Also, it always has to has the start point and point, so a 4-point polygon will have 5 coordinate tuples
BUT it gets trickier, it has to follow the right hand rule (anticlockwise).
TESTING GEOJSON WITH $BOX AND POINTS
my query {geometry: { $geoWithin: { $box: [ [-71.934034,38.764826], [-75.995762,43.802038] ] } }}
来源:https://stackoverflow.com/questions/42405589/query-polygons-geowithin-box-mongodb-doesnt-return-anything