N
N
Nick932016-06-08 11:51:45
JavaScript
Nick93, 2016-06-08 11:51:45

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);

My solution is that I convert the latitude longitude and radius into a circle polygon.
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 ]
    };
}

And in this way, I find all the polygons for the circumference of the circle in the radius of which the transmitted latitude and longitude fall.
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);
    });

But the solution seems to me not very correct, since in order to then draw the circle polygon on the client's map, you need to store 360 ​​points for the polygon in the database. I also thought about making a selection of circles that fall into a certain radius for the user, and already on the client to consider whether the point falls into these circles or not? Has anyone else had a similar experience and can point you in the right direction?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question