geospatial.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*!
  2. * Module requirements.
  3. */
  4. 'use strict';
  5. const castArraysOfNumbers = require('./helpers').castArraysOfNumbers;
  6. const castToNumber = require('./helpers').castToNumber;
  7. /*!
  8. * ignore
  9. */
  10. exports.cast$geoIntersects = cast$geoIntersects;
  11. exports.cast$near = cast$near;
  12. exports.cast$within = cast$within;
  13. function cast$near(val) {
  14. const SchemaArray = require('../array');
  15. if (Array.isArray(val)) {
  16. castArraysOfNumbers(val, this);
  17. return val;
  18. }
  19. _castMinMaxDistance(this, val);
  20. if (val && val.$geometry) {
  21. return cast$geometry(val, this);
  22. }
  23. if (!Array.isArray(val)) {
  24. throw new TypeError('$near must be either an array or an object ' +
  25. 'with a $geometry property');
  26. }
  27. return SchemaArray.prototype.castForQuery.call(this, val);
  28. }
  29. function cast$geometry(val, self) {
  30. switch (val.$geometry.type) {
  31. case 'Polygon':
  32. case 'LineString':
  33. case 'Point':
  34. castArraysOfNumbers(val.$geometry.coordinates, self);
  35. break;
  36. default:
  37. // ignore unknowns
  38. break;
  39. }
  40. _castMinMaxDistance(self, val);
  41. return val;
  42. }
  43. function cast$within(val) {
  44. _castMinMaxDistance(this, val);
  45. if (val.$box || val.$polygon) {
  46. const type = val.$box ? '$box' : '$polygon';
  47. val[type].forEach(arr => {
  48. if (!Array.isArray(arr)) {
  49. const msg = 'Invalid $within $box argument. '
  50. + 'Expected an array, received ' + arr;
  51. throw new TypeError(msg);
  52. }
  53. arr.forEach((v, i) => {
  54. arr[i] = castToNumber.call(this, v);
  55. });
  56. });
  57. } else if (val.$center || val.$centerSphere) {
  58. const type = val.$center ? '$center' : '$centerSphere';
  59. val[type].forEach((item, i) => {
  60. if (Array.isArray(item)) {
  61. item.forEach((v, j) => {
  62. item[j] = castToNumber.call(this, v);
  63. });
  64. } else {
  65. val[type][i] = castToNumber.call(this, item);
  66. }
  67. });
  68. } else if (val.$geometry) {
  69. cast$geometry(val, this);
  70. }
  71. return val;
  72. }
  73. function cast$geoIntersects(val) {
  74. const geo = val.$geometry;
  75. if (!geo) {
  76. return;
  77. }
  78. cast$geometry(val, this);
  79. return val;
  80. }
  81. function _castMinMaxDistance(self, val) {
  82. if (val.$maxDistance) {
  83. val.$maxDistance = castToNumber.call(self, val.$maxDistance);
  84. }
  85. if (val.$minDistance) {
  86. val.$minDistance = castToNumber.call(self, val.$minDistance);
  87. }
  88. }