Click Below to subscribe

How to find all polygons that contains a specific point in mongoose 2022

In this article, we gonna learn how to find all polygons that contains a specific point.

We need to use GeoJSON objects for geospatial queries.

Ref:- https://mongoosejs.com/docs/geojson.html

Point Schema:- The most simple structure in GeoJSON is a point. 

{
    "type" : "Point",
    "coordinates" : [-122.5, 37.7]
}

Note:- that longitude comes first in a GeoJSON coordinate array, not latitude.

models/region.model.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const regionSchema = new Schema(
    {
        name: String,
        location: {
            type: {
                type: String,
                enum: ['Polygon'],
                required: true
            },
            coordinates: {
                type: [[[Number]]],
                required: true
            }
        }
    },
    {
        timestamps: true
    }
);

regionSchema.index({ location: '2dsphere' }); 

const Region = mongoose.model('Region', regionSchema);

module.exports = Region;

Insert Dummy Data

await Region.insertMany([
    {
        name: 'Region 1',
        location: {
            type: 'Polygon',
            coordinates: [
                [79.9490441, 28.710228],
                [79.929818, 28.6439618],
                [79.9806297, 28.5788596],
                [80.0767601, 28.70541],
                [79.9490441, 28.710228]
            ]
        }
    },
    {
        name: 'Region 2',
        location: {
            type: 'Polygon',
            coordinates: [
                [79.6620262, 28.7620069],
                [79.6592796, 28.6668585],
                [79.8048485, 28.6511928],
                [79.8487938, 28.7475596],
                [79.6620262, 28.7620069]
            ]
        }
    }
]);

Here is an example for finding all polygons that contains a specific point

 

Example using find()

const latitude = 28.71143;
const longitude = 79.74579; 

const regions = await Region.find({
    location: {
        $geoIntersects: {
            $geometry: {
                type: 'Point',
                coordinates: [longitude, latitude]
            }
        }
    }
});

 

Example using static function

We can also make it reusable by defining this as a static function inside the model.

regionSchema.static('findPolygon', function (longitude, latitude) {
    return this.find({
        location: {
            $geoIntersects: {
                $geometry: {
                    type: 'Point',
                    coordinates: [longitude, latitude]
                }
            }
        }
    });
});

Now in the controller, you can call this function.

const regions = await Region.findPolygon(longitude, latitude);

 

Example using aggregate()

const regions = await Region.aggregate([
    {
        $match: {
            location: {
                $geoIntersects: {
                    $geometry: {
                        type: 'Point',
                        coordinates: [longitude, latitude]
                    }
                }
            }
        }
    }
]);

Checkout my full mongoose-geointersects example.

https://github.com/ultimateakash/mongoose-geointersects

Leave Your Comment