I want to use geometry in Mongodb.
But circle is not supported in geojson according to the geojson.org
I want to use geometry in Mongodb.
But circle is not supported in geojson according to the geojson.org
I had exactly the same problem, the solution is to create a polygon that roughly approximates a circle (imagine a polygon with 32+ edges).
I wrote a module that does this. You can use it like this:
const circleToPolygon = require('circle-to-polygon');
const coordinates = [-27.4575887, -58.99029]; //[lon, lat]
const radius = 100; // in meters
const numberOfEdges = 32; //optional that defaults to 32
let polygon = circleToPolygon(coordinates, radius, numberOfEdges);
You will need to model it as a point and then store the radius in another field. If you want to test whether or not something is inside of that circle, you will need to use the proximity spatial index as discussed here
{
<location field>: {
$geoWithin: { $centerSphere: [ [ <x>, <y> ], <radius> ] }
}
}
https://docs.mongodb.com/manual/reference/operator/query/centerSphere/
Since v1.8
Another approach to it. In this case, I have used mongoose one of the most popular distribution of MongoDB to add a circle to a map with a radius and then query using an external parameter and assessing if it's inside a circle or outside the circle. This example also has commented section for polygon, where if you have saved a polygon and you want to search if the point exists inside a polygon, you can do that too. Also, there is an upcoming section for a full integration of front end and backend for a complete geofence experience.
The code
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var assert = require('assert');
console.log('\n===========');
console.log(' mongoose version: %s', mongoose.version);
console.log('========\n\n');
var dbname = 'testing_geojsonPoint';
mongoose.connect('localhost', dbname);
mongoose.connection.on('error', function() {
console.error('connection error', arguments);
});
// schema
var schema = new Schema({
loc: {
type: {
type: String
},
coordinates: []
},
radius : {
type : 'Number'
}
});
schema.index({
loc: '2dsphere'
});
var A = mongoose.model('A', schema);
// mongoose.connection.on('open', function() {
// A.on('index', function(err) {
// if (err) return done(err);
// A.create({
// loc: {
// type: 'Polygon',
// coordinates: [
// [
// [77.69866, 13.025621],
// [77.69822, 13.024999, ],
// [77.699314, 13.025025, ],
// [77.69866, 13.025621]
// ]
// ]
// }
// }, function(err) {
// if (err) return done(err);
// A.find({
// loc: {
// $geoIntersects: {
// $geometry: {
// type: 'Point',
// coordinates: [77.69979,13.02593]
// }
// }
// }
// }, function(err, docs) {
// if (err) return done(err);
// console.log(docs);
// done();
// });
// });
// });
// });
mongoose.connection.on('open', function() {
A.on('index', function(err) {
if (err) return done(err);
A.create({
loc: {
type: 'Point',
coordinates: [77.698027,13.025292],
},
radius : 115.1735664276843
}, function(err, docs) {
if (err) return done(err);
A.find({
loc: {
$geoNear: {
$geometry: {
type: 'Point',
coordinates: [77.69735,13.02489]
},
$maxDistance :docs.radius
}
}
}, function(err, docs) {
if (err) return done(err);
console.log(docs);
done();
});
});
});
});
function done(err) {
if (err) console.error(err.stack);
mongoose.connection.db.dropDatabase(function() {
mongoose.connection.close();
});
}