Query to exclude the polygon which lying on given coordinate - mongodb

I want to try to use geoIntersects query and the result returns all the polygon which intersect with the given coordinate. Here my requirement is that I don't want to include that polygon which edge are coinciding with the given coordinates .
{ "geometry": {
"$geoIntersects": {
"$geometry": {
"type": "Polygon",
"coordinates": coordinate
}
}
}}

You can use the $not operator to select the documents that do not intersect with the given coordinate.
{ "geometry": {
"$not": {
"$geoIntersects": {
"$geometry": {
"type": "Polygon",
"coordinates": coordinate
}
}
}
}}

Related

$geoWithin query fail to retrieve point included in the queried polygon - v2

I've already ask the same question in this post. My example was subject to earth curvature issue so lets have a look to another one.
This time I have two nested polygons. A large one and a smaller one. Performing a $geoWithin query with the larger one return no document while the document is found using the smaller polygon. Any thoughts?
https://mongoplayground.net/p/V_3-s-itngA
The document I'm trying to query which is located on an island on France west coast.
{
"_id": {
"$oid": "625fec0f6476793a4581d172"
},
"featureOfInterest": {
"samplingFeature": {
"geometry": {
"type": "Point",
"coordinates": [
-2.3433781679840138,
46.713764788942484
]
},
"name": [
{
"lang": "en",
"text": "France"
}
],
"type": "Feature"
}
}
}
And the query
db.collection.aggregate([
{
$match: {
"$or": [
//Largest polygon - return no document
{
"samplingFeature.geometry": {
"$geoWithin": {
"$geometry": {
"type": "Polygon",
"coordinates": [
[[-55.722656,34.161818],[58.007813,34.161818],[ 58.007813,53.540307],[ -55.722656, 53.540307], [-55.722656,34.161818]]]
}
}
}
},
//Smallest polygon - return the document
//{
// "samplingFeature.geometry": {
// "$geoWithin": {
// "$geometry": {
// "type": "Polygon",
// "coordinates": [[[-5.800781, 42.682435], [9.316406, 42.682435], [9.316406, 50.625073], [-5.800781, 50.625073], [-5.800781, 42.682435]]]
// }
// }
//}
//}
]
}
}
])
You've bent the planet other way round. The coordinates in your post make these boxes:
the marker is clearly outside the the larger box.
Please use the link to jsfddle in my answer to your previous question instead of globes, or wherever you get your image from. The shortest path between 2 points far away does not necessarily follow longitudes and latitudes. Please read about the math of Geodesic calculations used in mongo geospatial queries.

Mongodb aggregate on two collections based on geopoint

I have two collections
person
{
"name": "Mike",
"age": 42,
"address": {
"location": {"type": "Point", "coordinates": [12.3456, 78.9101]}
}
}
restaurant
{
"name": "Bistro",
"type": ["Maxican", "Italian"],
"location": {"type": "Point", "coordinates": [12.3555, 78.9333]}
}
I've created spherical indexes on both collections
db.person.createIndex({"address.location": "2dsphere"})
db.restaurant.createIndex({"location": "2dsphere"})
Now I want to find all the restaurants near to a person using lat/long in both collections.
for e.g find all the restaurants within 10KM of a person. result will look like below
{
"name": "Mike",
"age": 42,
"address": {
"location": {"type": "Point", "coordinates": [12.3456, 78.9101]}
},
"nearby_restaurants": [{"name": "Bistro", "distance": 5.3}] // this means bistro is 5.3 KM away from this person
}
Notice that, I'm only using name field from restaurant collection and an additional field distance which will be calculated after the query.
I want to do this for all person. so basically for each person, i have to scan the whole restaurant collection every time.
To achieve this I thought of using lookup with aggregate, something like below
db.person.aggregate([
{
$lookup: {
from: "restaurant",
let: {"personPoint": "$address.location"},
as: "nearby_restaurants",
pipeline: [
{
$geoNear: {
near: "$$personPoint",
spherical: true,
distanceField: "distance",
maxDistance: 10 * 1000, // within 10 KM
distanceMultiplier: 0.001 // get the result in KM
}
},
{
$unwind: "$location"
},
]
}
},
{
$unwind: {
path: "$nearby_restaurants",
preserveNullAndEmptyArrays: true
}
}
])
This doesnt work at all. I get this error: $geoNear requires a 'near' option as an Array"
I tried to look at different sources but couldnt understand and fix the error.
PS: If currently, it's not possible through lookups/aggregate, how can we achieve the same through looping over each person's document?

MongoDB: GeoJSON object type index, Can't extract geo keys

I have made many the below GeoJSON Point objects in MongoDB Compass as per the docs
{
"_id": {
"$oid": "5e86d275a3d7fd05e4f022a8"
},
"location": {
"type": "point",
"coordinates": ["-110.85458435", "39.68476146"]
},
"website": "carbonmedicalservice.com",
"address": "125 S Main St",
"state": "UT",
"zip": "84526",
"name": "Carbon Medical"
}
I want to be able te search the collection to return all records within an reactangle, I believe I need to add a Geospatial Indexe to the location but I get the error shown below...
This is how I entered the data:
In a GeoJSON type, coordinates should be float/double not string.
Also, the type should be Point, not point in your case. So, the GeoJSON in your case should be:
{
"type": "Point",
"coordinates": [-110.85458435, 39.68476146]
}
instead of
{
"type": "point",
"coordinates": ["-110.85458435", "39.68476146"]
}

MongoDB: how to store GPS track including timestamps

I'm interested in your input on what's the most efficient (or most intelligent) way to store a GPS track consisting of GPS coordinates (Longitude & Latitude) and timestamps.
I'm thinking of those two ideas:
gpsTrack = {
"gps_track" : {
"type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [104.0, 0.3]},
"properties": {
"absolute_timestamp" : "2011-12-31T23:50:59Z"
}
},
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [128.0, 0.5]},
"properties": {
"absolute_timestamp" : "2011-12-31T23:59:59Z"
}
}
] }
}
Or using the GeoJSON MultiPoint and the timestamps as an array.
gpsTrack = {
"geometry": {"type": "MultiPoint", "coordinates": [[104.0, 0.3], [128.0, 0.5]]},
"properties": {
"timestamps" : ["2011-12-31T23:50:59Z", "2011-12-31T23:59:59Z"]
}
The total amount of way points is assumed to be 4.000 to 10.000 but for sure below 100.000. For the first idea I'm afraid that the collection will become quite long since every points and its timestamp is stored separately. While the second idea is more compact and utilizing the MultiPoint GeoJSON type.
Thanks for your input.
May be you can use coordinates array for timestamp too. Most likely mongodb will just interested in first 2 objects of coordinates. You can put timestamp as third object.
gpsTrack = {
"geometry": {
"type": "MultiPoint",
"coordinates": [
[104.0, 0.3, "2011-12-31T23:50:59Z"],
[128.0, 0.5, "2011-12-31T23:59:59Z"]]
}
}
like I said, I didn't try if this violates mongodb to make geo query over coordinates.

How to convert geojson to polygon in Leaflet?

I have this json data:
var states = [{
"type": "Feature",
"properties": {"party": "Republican"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-104.05, 48.99],
[-97.22, 48.98],
[-96.58, 45.94],
[-104.03, 45.94],
[-104.05, 48.99]
]]
}
}
}];
It'is easy to visualize this on leaflet map by doing this:
L.geoJSON(states, {
style: function(feature) {
switch (feature.properties.party) {
case 'Republican': return {color: "#ff0000"};
}
}
}).addTo(map);
But i need the "type" of states is "polygon" not "geojson", so the result will be like this:
var states_polygon = L.polygon([[
[48.99,-104.05],
[48.98,-97.22],
[45.94,-96.58],
[45.94,-104.03],
[48.99,-104.05]
]]).addTo(map);
I knew that both variable will be give the same visual on leaflet map but all i need is the object type of that variable as polygon.. How can i do this?