Answer the question
In order to leave comments, you need to log in
How to make such a selection of mongodb geological data?
Hello everyone, how can I make a selection of all circles in the radius of which a point falls?
While I have such a solution, but it seems to me that it can be done much easier and maybe I'm missing something?
This is how the room model aka circle looks like.
var schema = new Schema ({
name: {
type: String,
required: true
},
inPrivate: {
type: Boolean,
required: false
},
location: {
type: { type: String, "enum": [
"Point",
"MultiPoint",
"LineString",
"MultiLineString",
"Polygon",
"MultiPolygon"
] },
coordinates: { type: Array }
},
radius : Number,
created: {
type: Date,
default: Date.now
}
});
schema.index({'polygonConversion': "2dsphere"});
module.exports.Room = mongoose.model('Room', schema);
function generateGeoJSONCircle(center, radius, numSides) {
var points = [];
var earthRadius = 6371;
var halfsides = numSides / 2;
var d = parseFloat(radius / 1000.) / earthRadius;
var lat = (center[1] * Math.PI) / 180;
var lon = (center[0] * Math.PI) / 180;
for(var i = 0; i < numSides; i++) {
var gpos = {};
var bearing = i * Math.PI / halfsides; //rad
gpos.latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(bearing));
gpos.longitude = ((lon + Math.atan2(Math.sin(bearing) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(gpos.latitude))) * 180) / Math.PI;
gpos.latitude = (gpos.latitude * 180) / Math.PI;
points.push([gpos.longitude, gpos.latitude]);
};
points.push(points[0]);
return {
type: 'Polygon',
coordinates: [ points ]
};
}
coords[0] = req.params.latitude;
coords[1] = req.params.longitude;
Room.find({
location: {
$geoIntersects: {
$geometry: {
type: "Point",
coordinates: coords
}
}
}}).limit(limit).exec(function (err, locations) {
if (err) {
return res.json(500, err);
}
log.info(locations);
res.json(200, locations);
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question