mongodb geospacial query : $centerSphere not giving good result - mongodb

I have a db with geodata (lng,lat) from Europe.
I'm trying to do a geospatial query to get all element within a circle.
So, i create a GeoJSON field, and a 2dsphere index on it.
This look like that :
{
"_id" : ObjectId("56c3484612aeb853a83ec336"),
"defaultLabel" : "Zoo de Mulhouse",
"weight" : NumberInt(1),
"country" : "FR",
"loc" : {
"type" : "Point",
"coordinates" : [
47.731673,
7.347819
]
}
}
and db.myCollection.createIndex({"loc":"2dsphere"});
So far, so good.
But then i tried some queries to check.
I took a point at ~1,12 km (1120m) from the data, and did a request with a radius of 1200m.
db.poi.find({loc:
{$geoWithin:
{$centerSphere:[
[47.728593,7.333486],
1200 / 6378100] // radius of earth ~6378.1Km
}
}
})
I got no result.
With the same request, i have to put about 1650m to get the result.
Obviously, this is not acceptable.
So, what did I do wrong?

Found the problem : coordinates were stored as [lat,lgn] in my legacy, and i didn't check before create my GeoJSON, so it was in the wrong order.

Related

GeoLocation query in mongo db

How Could i get longitude and lattitude using mongodb query from mongo collection
based on disatnce and certain coordinates.
{
"_id" : ObjectId("5a559b13ae201d0c05cb6f6a"),
"id" : "99623",
"city" : "Wasilla",
"cnty" : "Matanuska Susitna",
"cntyFips" : null,
"st" : "AK",
"stName" : "Alaska",
"areaCode" : "907",
"lat" : 61.5816,
"long" : -149.4393,
"rgn" : "West",
"__v" : 0
}
in this document "lat" is for latitude and "long" is for longitude.
so basically i have to get all ids based on present id lat and long which comes in a particular miles distance range.
Mongo supports GeoJSON which should allow you to perform basic queries easily on these kind of data. Examples include $geoWithin for your particular use-case.
We need a little more information on the structure of the documents that you are querying, but here is a simple example of the query available in the Mongo documentation here.
It's worth noting that you require a 2dsphere index for location data defined as GeoJSON points in order for this to be effective.
{
$nearSphere: {
$geometry: {
type : "Point",
coordinates : [ <longitude>, <latitude> ]
},
$minDistance: <distance in meters>,
$maxDistance: <distance in meters>
}
}

How do I calculate the total distance traveled with MongoDB

I need to calculate the total distance traveled between a collection of points. They all have a timestamp to get them in the right order. I'm only allowed to use MongoDB.
Sample collection:
{
"_id" : ObjectId("596cd354241aa3174056fb98"),
"spelerID" : 1,
"timestamp" : ISODate("2017-02-01T19:00:00.000Z"),
"coordinates" : {
"type" : "Point",
"coordinates" : [
4.29870386367084,
50.8357637566422
]
}
}
/* 2 */
{
"_id" : ObjectId("596cd354241aa3174056fbb4"),
"spelerID" : 1,
"timestamp" : ISODate("2017-02-01T19:00:01.000Z"),
"coordinates" : {
"type" : "Point",
"coordinates" : [
4.29868458167181,
50.8357575419868
]
}
}
/* 3 */
{
"_id" : ObjectId("596cd354241aa3174056fbce"),
"spelerID" : 1,
"timestamp" : ISODate("2017-02-01T19:00:02.000Z"),
"coordinates" : {
"type" : "Point",
"coordinates" : [
4.29867536067721,
50.8357376028214
]
}
}
I have no idea how to calculate the distance between those points in the right order.
MongoDB can performance the distance between two points by using $near or $geoNear.
Returns documents in order of proximity to a specified point, from the nearest to farthest. geoNear requires a geospatial index.
https://docs.mongodb.com/manual/reference/command/geoNear/
In your case I don't think this will resolve the issue since you want to perform multiple distance calculation between multiple points.
MongoDB can be instruct to create very complex algorithm by using MapReduce. Even if I'm not expert in GIS application you could try to create a MapReduce routine with a Function that compute the distance between multiple points ordered as you like.
MongoDB - MapReduce
This link can help you to write a JS function to be used in Map Reduce:
Calculate distance between two latitude-longitude points? (Haversine formula)
Good Luck!

spring mongo support for geoJSOn for 2dsphere index

Mongo though provides 2dsphere index on legacy co-ordinates, the query requires to present to Point/Shapes in geoJSON format. For e.g., I have inserted the following records to address collection.
{ "city" : "First", "geo" : [ 13.45, 23.46 ] }
{ "city" : "Second", "geo" : [ 13.45, 20.46 ] }
Then I added 2dsphere index using following command as mongodb still allows 2dsphere index on legacy co-ordinates.
db.address.ensureIndex({"geo":"2dsphere"})
Then if I do $near query using legacy format, but got an exception.
> db.address.find({"geo":{$near:{"x":13.45,"y":23.45}}})
error: {
"$err" : "can't parse query (2dsphere): { $near: { x: 13.45, y: 23.45 } }",
"code" : 16535
}
But If do same query with geoJSON format, then I get result.
> db.address.find({"geo":{$near:{"type":"Point",coordinates:[13.45,23.45]}}})
{ "_id" : ObjectId("537306b4b8ac1f134d9efe89"), "city" : "First", "geo" : [ 13.45, 23.46 ] }
{ "_id" : ObjectId("537306c3b8ac1f134d9efe8a"), "city" : "Second", "geo" : [ 13.45, 20.46 ] }
My question is, GeoConverters has all conversion made to legacy format. So, obviously they wont' work if I use 2dsphere index. Are there any converts available for geoJSON format. Is there any workaround?
Currently spring-data-mongo doesn't support the new mongo (> 2.4) 2dsphere indexes. There is a open issue on Jira about it:
https://jira.spring.io/browse/DATAMONGO-1113?jql=project%20%3D%20DATAMONGO%20AND%20text%20~%20%22%24geometry%22
In the link you can find a gist link to example of how create such converters. You can use it or you can overcome this limitation creating a #Query with the query that you want that spring-data-mongo execute.
Regards.
avaz

Mongodb Exception "point not in interval of [ -180, 180 ]

I have a collection 'place' ,one documentis as below
{
"_id" : ObjectId("52401a7267778834a23a54a2"),
"userid" : "123",
"loc" : {
"lng" : 77.6166685,
"lat" : 12.9361732
},
"t" : ISODate("2013-04-23T10:39:46.540Z")
}
I want to find out locations of user with userid 234 and loc near to [77.6166685,12.9361732] within 1km
but the below query is not working
db.place.find({{"userid":"234","loc":{"$near":{"$geometry":{"type":"point","coordinates":[77.6166685,12.9361732]},"$maxDistance":1000}}})
it showing error as given below
error: {
"$err" : "point not in interval of [ -180, 180 ] :: caused by :: { 0: 0.0, 1: 250.0 }",
"code" : 16433
}
what is this error and how can I correct it?
I think that the problem is the following:
You document has a form which is applicable for "2d" indexes, (you have not told what kind of indexes do you have) and you are trying to query it with a query which is applicable for "2dsphere" indexes. So you have two options:
convert the document to GeoJSON format (here you are not specifying geometry), put 2dsphere index and to query it in this way
do nothing and to query it with a correct way with "2d" indexes
I have done in the second way:
db.d.find({
"loc":{
"$near": [77.6166685,12.9361732],
"$maxDistance":1000
}
})
and got a correct result.

select twitter near a specific coordinates from mongodb

I've stored some twitter from streaming api to MongoDB. They are all in jason format and has attribute like below:
"geo" : { "type" : "Point", "coordinates" : [ 42.03759247, -71.82113956 ] }
I'm just wondering what is the correct query to select tweet near a specific coordinates. I did it like this:
db.tweets.find( { coordinates : { $near : [151.1955562233925,-33.87107475181752] , $maxDistance : 1000} } )
but it didn't work. Thank you for your answer!
According to this page, you must first create a geo-spatial index to be able to query geographically:
Now, inorder to query by geo co-ordinates, we need to create an index
over the “loc” field of our dealership documents.
db.dealerships.ensureIndex({loc:"2d"})
So try something like this:
db.tweets.ensureIndex({"coordinates":"2d"})
You might also need to change the structure of your "geo" property to match the following (I'm not sure about this part):
"geo" : { "type" : "Point", "coordinates" : { "lon":42.03759247, "lat":-71.82113956 } }
This works for me:
db.tweets.ensureIndex({"coordinates.coordinates":"2d"})
Query:
db.tweets.find({"coordinates.coordinates": {$near:[-1,-53]}}).limit(2)