Mongodb Point inside a Polygon not getting results - Mongoid + Rails - mongodb

I'm trying to check if a point is inside a polygon with mongodb (mongodb 2.6).
I'm inserting the data in de collection like this:
db.areas.insert({"polygons":
{"type":"Polygon",
coordinates:
[[[-23.0651232, -45.6374645],
[-23.0557255, -45.6435585],
[-23.0370072, -45.6383228],
[-23.0299772, -45.6351471],
[-23.0025649, -45.6480217],
[-22.9723022, -45.6554031],
[-22.9340493, -45.6032181],
[-22.9353140, -45.5925751],
[-22.9383177, -45.5855370],
[-22.9601320, -45.5560112],
[-22.9645577, -45.5597878],
[-22.9938740, -45.5675125],
[-22.9939530, -45.5690575],
[-23.0217620, -45.5712891],
[-23.0241319, -45.5719757],
[-23.0258697, -45.5711174],
[-23.0268966, -45.5721474],
[-23.0656365, -45.6372499],
[-23.0651232, -45.6374645]
]]
}
});
And querying like this:
db.areas.find({polygons:
{$geoIntersects:
{$geometry:{ "type" : "Point",
"coordinates" : [ -22.112, -45.56 ] }
}
}
});
But I'm getting no result. Any ideas?
I've looked this links:
mongodb check if point is in polygon
Search all polygons that contains a series of points in mongodb
Any help will be appreciated

I found the answer.
The problemas was that when I tried to save, the coordinates of the Polygon from a Rails form it was saved as String and not as Double.
I chaged that before save an the problem was solved.

Related

Mongodb $geoIntersects giving wrong query results

I'm facing a rather strange issue with mongodb when I try to get which of my stored polygons intersects with the given area and its throwing results when it should't.
This is what I have stored:
It is the only one element stored in mongo and looks like this in a map:
Now this is the query (among others) that is giving me trouble :
db.getCollection('collection').find({"geometry": {"$geoIntersects": {"$geometry":{"coordinates":[[
[
-25.13671875,
16.636191878397664
],
[
-21.796875,
16.636191878397664
],
[
-21.796875,
19.145168196205297
],
[
-25.13671875,
19.145168196205297
],
[
-25.13671875,
16.636191878397664
]]],"type":"Polygon"}}}})
which in a map looks like this (including the previous mentioned area):
As you can see in the map (or doing simple math calc with the coordinates) those areas do not intersect at all, but mongo insists they do as the query gives me results.
I've tried using the following in $geometry operator but the results are the same
crs: {
type: "name",
properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
}
Last but not least, if the area in the query is east/west/south of the stored one the results are the expected ones, the problem is in areas in close vicinity north of the main polygon.
What am I doing wrong?
Please let me know if more data is needed and thank you all for your time.

Did I need to make the coordinate conversion?

The spatial reference of my data is based on BaiDu Map,now I'd like to create a 2dsphere index.so Did I need to make the coordinate conversion from the spatial reference of BaiDu Map to WGS84? Thank you very much!
The 2dsphere index overwiew in MongoDB documentation seems to say so:
The default datum for an earth-like sphere is WGS84. Coordinate-axis order is longitude, latitude.
What is important to remember is that your location data need to include geometry data.
You can't create a 2dsphere index on:
location : { coordinates: [ -73.97, 40.77 ] }
But you can on:
location : { type: "Point", coordinates: [ -73.97, 40.77 ] }

How to pull data from mongodb?

I built a location-aware web application and used the W3C Geolocation API for detecting the location of a visitor and used the Google Maps API to display the map. How can I get data stored in mongodb that represent information about places near me to display above a place when click it on the map?
for example:
db.places.insert({name:"",kind:"restaurant",serves:["Fast food","Sea food"],price:{fast food:200$,Sea food:175$}});
I want these information displayed when I click a restaurant on map.
When I click a restaurant's name on the map, I want to display these information above the restaurant's name.
Well, to start, you're gonna want to associate the restaurants with geo location data. If you have lat lon stored, then you can query using mongodb Geospatial queries:
http://docs.mongodb.org/manual/reference/operator/query-geospatial/
Here is a sample from the docs:
db.places.find( { loc : { $near :
{ $geometry :
{ type : "Point" ,
coordinates: [ 40 , 5 ] } },
$maxDistance : 500
} } )

mongodb and geospatial schema

im breaking my head with mongo and geospatial,
so maybe someone has some idea or solution how to solve this:
my object schema is like this sample for geoJSON taken from http://geojson.org/geojson-spec.html.
{
"name":"name",
"geoJSON":{
"type":"FeatureCollection",
"features":[
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]},"properties":{}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]},"properties":{}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]},"properties":{}}
]}}
additional info: I'm using spring data but that shouldn't influence the answer.
main problem is how/where to put indexes in this schema. I need to make a query to find all documents for given Point if some polygon intersects.
thanks in advance.
By creating a 2d or 2dsphere index on geoJSON.features.geometry you should be able to create an index covering all of the geoJSON-objects.
To get all documents where at least one of the sub-object in the features array covers a certain point, you can use the $geoIntersects operator with a geoJSON Point:
db.yourcollection.find(
{ `geoJSON.features.geometry` :
{ $geoIntersects :
{ $geometry :
{ type : "Point" ,
coordinates: [ 100.5 , 0.5 ]
}
}
}
}
)

Use property for calculation in mongodb

specialists,
I have a document with several properties, one bein an lon/lat-array with a 2d index on it.
Another property is a radius property.
What I want:
{
geo:{
$within: { $center: [[ 9.078597000000036,50.580947], 1+radius]}
}
}
Is this available with mongodb? No matter what I am searching in google I am always directed to the mongodb documentation about geospatial indexes but my question is not getting answered.
Not sure if i understand you, but the docs page about geospatial indexes give a an example of a query like yours:
db.places.find( { geo: { $centerSphere: [ [long,lat ] ,
radius ] } } )
searches places collection for everything withing radius distance (in radians) from the point [long, lat].