How to define a circle using GeoJson?

狂风中的少年 提交于 2019-12-24 01:25:27

问题


I want to use geometry in Mongodb.

But circle is not supported in geojson according to the geojson.org


回答1:


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



回答2:


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




回答3:


{
   <location field>: {
      $geoWithin: { $centerSphere: [ [ <x>, <y> ], <radius> ] }
   }
}

https://docs.mongodb.com/manual/reference/operator/query/centerSphere/

Since v1.8




回答4:


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

See full example in action



来源:https://stackoverflow.com/questions/17691956/how-to-define-a-circle-using-geojson

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!