I want to get point within a polygon and I am using below query for getting data
loc: {
$geoWithin: {
$geometry: {
type: 'Polygon',
coordinates: [
[
[
-117.83736,
33.73838
],
[
-117.83588,
33.73837
],
[
-117.83369,
33.73839
]...
]
]
}
}
}
But I am getting an error like
Edges 1 and 3 cross. Edge locations in degrees:
How can I solve this?
As i stated in the comments your coordinates list is an invalid polygon.
You need to validate each edge with every other edge using a simple check as described here.
Related
I am creating NodeJS backend for uber like app for android and iOS. locations of users will be taken from phone and coordinates i.e. latitude and longitude will be sent to server. There are 2 groups of users. lets say drivers and passengers. once passenger press search for driver, I need to send notifications to all the drivers with in 5 km of the passenger radius. Any ideas how to implement this logic .??.
On frontend geo fences will be created around the passengers. what I cant figure out is that how to know on the server side if drivers are in those geo fences or not.
Frontend is also being developed by me using react native so any suggestions from frontend point of view are also welcome. Thanks
MongoDB support Geospatial queries, with that you can query document in the specified geolocation bounds.
Assume that we have drivers collection as below
db.drivers.insertMany( [
{
name:"Patrick Konrab",
location: { type: "Point", coordinates: [ -73.97, 40.77 ] },
},
{
name: "James Webb",
location: { type: "Point", coordinates: [ -73.9928, 40.7193 ] },
category: "Parks"
},
{
name: "Kyle Grill",
location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
}
] )
The following operation creates a 2dsphere index on the location field:
db.drivers.createIndex( { location: "2dsphere" } )
The drivers collection above has a 2dsphere index. The following query uses the $near operator to return documents that are at least 1000 meters from and at most 5000 meters from the specified GeoJSON point, sorted in order from nearest to farthest:
// Passenger coordinates should be returned user device
//https://github.com/react-native-geolocation/react-native-geolocation
const passengerCoordinate = [ -73.9667, 40.78 ]
db.drivers.find(
{
location:
{ $near:
{
$geometry: { type: "Point", coordinates: passengerCoordinate },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
It's been now 8 hours I'm trying to deal with a MongoDB Geojson $geoIntersects issue :
It's working very well when my polygon is a square, or a rectangle,
but I'm unable to get some results from a $geoIntersects request when my Polygon has crossing vertices, like this example :
(Image from (https://geoman.io/geojson-editor))
The polygon data looks like this :
{
"type": "Feature",
"properties": {
"shape": "Polygon"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-1.565584, 47.226352],
[-1.564704, 47.226927],
[-1.564093, 47.225616],
[-1.563138, 47.226374],
[-1.565584, 47.226352]
]
]
},
"id": "dda54a42-090b-46ea-9dd0-fdda6d240f90"
}
For this example, I need to know if the Polygon includes my Point coordinates.
This is my simple query :
db.geojsondatas.find({
geometry: {
$geoIntersects: {
$geometry: {
type: "Point",
coordinates: [ -1.555638, 47.216245 ]
}
}
}
});
Anyone knows if there is a way to do this ?
Thanks by advance.
Maybe try with JTS Topology Suite and investigate your polygon:
I don't think $geoIntersects supports Self-intersecting polygon.
I have a collection with a nested array of polygons stored in the below given format:
_id: ObjectId("...."),
"attributes": {
"zones": [
{
"zoneName": "...",
"zoneLocs" : [
{
"type" : "Polygon",
"coordinates" : [[...]]
}
....
]
}
....
]
}
I want to perform a geojson search on this collection, where I pass a point and a max-distance value, and pull all the documents which have a polygon in the enclosed area.
I tried to do this by using $elemMatch, $geoWithin and $center. My final query looked like this, but did not fetch any results, even though there were polygons in the enclosing area:
{
"attributes.zones": {
"$elemMatch":{
"zoneLocs": {
"$elemMatch" : {
"$geoWithin": {
"$center" : [
[
-104.84127910390623,
39.820705065855044
],
100000
]
}
}
}
}
}
}
I have created a 2dsphere index on the path 'attributes.zones.zoneLocs', but no luck so far. Any help would be greatly appreciated.
I am working on mongodb Geospatial feature.
Following is my db schema
{
_id:5c6fea5aa7f54038cc63db92,
location:{
coordinates:[-96.81916,33.07516],
type:"Point"
},
userId:1
}
Indexing on location field with 2dsphere
I have 2 point and each one have latitude and longitude and its example is below
sw_latitude: 32.76286484066411 sw_longitude: -96.91870791625979
ne_latitude: 32.789133220783015 ne_longitude: -96.67529208374026
base on these 2 points i need to get all userId which are cover in these 2 points square
Can you please suggest me mongo query which give me result?
I tried many way and finally i got one solution
Step for solution 1:
Step 1: find boundary from that 2 point
latitude:[32.740776 TO 32.8112099] AND longitude:[-96.910983 TO -96.683016]
Step 2: use $box from Geospatial (ref: $box from mongo) and pass this boundary point in that..
{ location: {
$geoWithin: {
$box: [ [ 32.740776, 32.8112099 ], [ -96.910983, -96.683016 ] ]
}
}
}
Step for solution 2:
Step 1: setup existing point proper way as following
latitude:[32.76286484066411 TO 32.789133220783015] AND longitude:[-96.91870791625979 TO -96.67529208374026]
Step 2: use $box from Geospatial (ref: $box from mongo) and pass this boundary point in that..
{ location: {
$geoWithin: {
$box: [ [ 32.76286484066411, 32.789133220783015 ], [ -96.91870791625979, -96.67529208374026 ] ]
}
}
}
Hope will be helpful for you
I have a document in the database that contains a "polygon" property that, as far as I can tell, is a valid GeoJSON object. I want to search the database using a GeoJSON Point object to find documents where the polygon in the polygon property contains the Point. To do so, I am using the $geoIntersects operator, however, whenever I preform the find, MongoDB returns the error: [Error: Can't use $geoIntersects].
The only object in the database:
{
"_id": ObjectId("581540795fd2da1b188eb09c"),
"name":"String",
"polygon":{
"coordinates":[
[ -90, -180 ],
[ 90, -180 ],
[ 90, 180 ],
[ -90, 180 ],
[ -90, -180 ]
],
"_id": ObjectId("581540795fd2da1b188eb09d"),
"name": "String",
"type": "Polygon"
},
"__v":0
}
I am using mongoose to search the database. The query that is used to preform the search:
{
polygon: {
$geoIntersects: {
$geometry: {
type: 'Point',
coordinates: [<long>, <lat>]
}
}
}
}
If I set the latitude and longitude to something simple, say (0, 0) or (1, 1). It returns the error. According to what I read elsewhere, the only reason this error should be returned is because the documents in the database are not valid GeoJSON objects, but I cannot see anything wrong with the only object in the database.
I think you're missing the outer array on your coordinates for a geoJSON polygon. Your document should look like this:
{
"_id": ObjectId("581540795fd2da1b188eb09c"),
"name":"String",
"polygon":{
"coordinates":[[
[ -90, -180 ],
[ 90, -180 ],
[ 90, 180 ],
[ -90, 180 ],
[ -90, -180 ]
]],
"_id": ObjectId("581540795fd2da1b188eb09d"),
"name": "String",
"type": "Polygon"
},
"__v":0
}
From the geoJSON spec, http://geojson.org/geojson-spec.html#id4
Coordinates of a Polygon are an array of LinearRing coordinate arrays. The first element in the array represents the exterior ring. Any subsequent elements represent interior rings (or holes).