项目作者: mongodb-js

项目描述 :
Geo coordinates detection based off a mongodb-schema type
高级语言: JavaScript
项目地址: git://github.com/mongodb-js/detect-coordinates.git
创建时间: 2016-06-08T07:04:41Z
项目社区:https://github.com/mongodb-js/detect-coordinates

开源协议:Apache License 2.0

下载


detect-coordinates travis npm

Geo coordinates detection based on a mongodb-schema type.

This module exports a function which detects whether a mongodb-schema
type object represents geo coordinates, either as legacy coordinate pairs
(2-element array of long/lat coordinates) or a GeoJSON object.

It returns a zipped array of [[lng, lat], [lng, lat], ...] coordinates, or
false if no coordinates could be found.

Example

For this example, you need the mongodb node driver, mongodb-schema and
lodash modules installed in addition to this module:

  1. npm install mongodb mongodb-schema detect-coordinates lodash

This code connects to a MongoDB server, gets the schema of a collection
and looks at the first type of the first field to determine if it has
coordinates.

  1. var parseSchema = require('mongodb-schema');
  2. var connect = require('mongodb');
  3. var detect = require('detect-coordinates');
  4. var _ = require('lodash');
  5. // connect to a MongoDB instance
  6. connect('mongodb://localhost:27017/test', function(err, db){
  7. if(err) return console.error(err);
  8. // get the schema of the collection `test.users` (only first 100 docs)
  9. parseSchema('test.users', db.collection('users').find().limit(100), function(err, schema){
  10. if(err) return console.error(err);
  11. // serialize the schema and extract first type of field "last_position"
  12. var plainSchema = schema.serialize();
  13. var type = _.find(plainSchema.fields, ['name', 'last_position']).types[0];
  14. // check if that type represents geo coordinates
  15. var coordinates = detect(type);
  16. if (coordinates) {
  17. console.log('type contains coordinates, here they are:\n', coordinates);
  18. } else {
  19. console.log('type does not contain coordinates!');
  20. }
  21. db.close();
  22. });
  23. });

This outputs:

  1. type contains coordinates, here they are:
  2. [ [ -73.98808416, 40.74854862 ],
  3. [ -74.015756, 40.711512 ],
  4. [ -74.0026376103, 40.7390169121 ],
  5. [ -74.015756, 40.711512 ],
  6. [ -73.96525063, 40.71044554 ],
  7. ...

License

Apache 2.0