index.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * @module helpers
  5. */
  6. /**
  7. * Earth Radius used with the Harvesine formula and approximates using a spherical (non-ellipsoid) Earth.
  8. *
  9. * @memberof helpers
  10. * @type {number}
  11. */
  12. exports.earthRadius = 6371008.8;
  13. /**
  14. * Unit of measurement factors using a spherical (non-ellipsoid) earth radius.
  15. *
  16. * @memberof helpers
  17. * @type {Object}
  18. */
  19. exports.factors = {
  20. centimeters: exports.earthRadius * 100,
  21. centimetres: exports.earthRadius * 100,
  22. degrees: exports.earthRadius / 111325,
  23. feet: exports.earthRadius * 3.28084,
  24. inches: exports.earthRadius * 39.37,
  25. kilometers: exports.earthRadius / 1000,
  26. kilometres: exports.earthRadius / 1000,
  27. meters: exports.earthRadius,
  28. metres: exports.earthRadius,
  29. miles: exports.earthRadius / 1609.344,
  30. millimeters: exports.earthRadius * 1000,
  31. millimetres: exports.earthRadius * 1000,
  32. nauticalmiles: exports.earthRadius / 1852,
  33. radians: 1,
  34. yards: exports.earthRadius * 1.0936,
  35. };
  36. /**
  37. * Units of measurement factors based on 1 meter.
  38. *
  39. * @memberof helpers
  40. * @type {Object}
  41. */
  42. exports.unitsFactors = {
  43. centimeters: 100,
  44. centimetres: 100,
  45. degrees: 1 / 111325,
  46. feet: 3.28084,
  47. inches: 39.37,
  48. kilometers: 1 / 1000,
  49. kilometres: 1 / 1000,
  50. meters: 1,
  51. metres: 1,
  52. miles: 1 / 1609.344,
  53. millimeters: 1000,
  54. millimetres: 1000,
  55. nauticalmiles: 1 / 1852,
  56. radians: 1 / exports.earthRadius,
  57. yards: 1.0936133,
  58. };
  59. /**
  60. * Area of measurement factors based on 1 square meter.
  61. *
  62. * @memberof helpers
  63. * @type {Object}
  64. */
  65. exports.areaFactors = {
  66. acres: 0.000247105,
  67. centimeters: 10000,
  68. centimetres: 10000,
  69. feet: 10.763910417,
  70. hectares: 0.0001,
  71. inches: 1550.003100006,
  72. kilometers: 0.000001,
  73. kilometres: 0.000001,
  74. meters: 1,
  75. metres: 1,
  76. miles: 3.86e-7,
  77. millimeters: 1000000,
  78. millimetres: 1000000,
  79. yards: 1.195990046,
  80. };
  81. /**
  82. * Wraps a GeoJSON {@link Geometry} in a GeoJSON {@link Feature}.
  83. *
  84. * @name feature
  85. * @param {Geometry} geometry input geometry
  86. * @param {Object} [properties={}] an Object of key-value pairs to add as properties
  87. * @param {Object} [options={}] Optional Parameters
  88. * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
  89. * @param {string|number} [options.id] Identifier associated with the Feature
  90. * @returns {Feature} a GeoJSON Feature
  91. * @example
  92. * var geometry = {
  93. * "type": "Point",
  94. * "coordinates": [110, 50]
  95. * };
  96. *
  97. * var feature = turf.feature(geometry);
  98. *
  99. * //=feature
  100. */
  101. function feature(geom, properties, options) {
  102. if (options === void 0) { options = {}; }
  103. var feat = { type: "Feature" };
  104. if (options.id === 0 || options.id) {
  105. feat.id = options.id;
  106. }
  107. if (options.bbox) {
  108. feat.bbox = options.bbox;
  109. }
  110. feat.properties = properties || {};
  111. feat.geometry = geom;
  112. return feat;
  113. }
  114. exports.feature = feature;
  115. /**
  116. * Creates a GeoJSON {@link Geometry} from a Geometry string type & coordinates.
  117. * For GeometryCollection type use `helpers.geometryCollection`
  118. *
  119. * @name geometry
  120. * @param {string} type Geometry Type
  121. * @param {Array<any>} coordinates Coordinates
  122. * @param {Object} [options={}] Optional Parameters
  123. * @returns {Geometry} a GeoJSON Geometry
  124. * @example
  125. * var type = "Point";
  126. * var coordinates = [110, 50];
  127. * var geometry = turf.geometry(type, coordinates);
  128. * // => geometry
  129. */
  130. function geometry(type, coordinates, _options) {
  131. if (_options === void 0) { _options = {}; }
  132. switch (type) {
  133. case "Point":
  134. return point(coordinates).geometry;
  135. case "LineString":
  136. return lineString(coordinates).geometry;
  137. case "Polygon":
  138. return polygon(coordinates).geometry;
  139. case "MultiPoint":
  140. return multiPoint(coordinates).geometry;
  141. case "MultiLineString":
  142. return multiLineString(coordinates).geometry;
  143. case "MultiPolygon":
  144. return multiPolygon(coordinates).geometry;
  145. default:
  146. throw new Error(type + " is invalid");
  147. }
  148. }
  149. exports.geometry = geometry;
  150. /**
  151. * Creates a {@link Point} {@link Feature} from a Position.
  152. *
  153. * @name point
  154. * @param {Array<number>} coordinates longitude, latitude position (each in decimal degrees)
  155. * @param {Object} [properties={}] an Object of key-value pairs to add as properties
  156. * @param {Object} [options={}] Optional Parameters
  157. * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
  158. * @param {string|number} [options.id] Identifier associated with the Feature
  159. * @returns {Feature<Point>} a Point feature
  160. * @example
  161. * var point = turf.point([-75.343, 39.984]);
  162. *
  163. * //=point
  164. */
  165. function point(coordinates, properties, options) {
  166. if (options === void 0) { options = {}; }
  167. if (!coordinates) {
  168. throw new Error("coordinates is required");
  169. }
  170. if (!Array.isArray(coordinates)) {
  171. throw new Error("coordinates must be an Array");
  172. }
  173. if (coordinates.length < 2) {
  174. throw new Error("coordinates must be at least 2 numbers long");
  175. }
  176. if (!isNumber(coordinates[0]) || !isNumber(coordinates[1])) {
  177. throw new Error("coordinates must contain numbers");
  178. }
  179. var geom = {
  180. type: "Point",
  181. coordinates: coordinates,
  182. };
  183. return feature(geom, properties, options);
  184. }
  185. exports.point = point;
  186. /**
  187. * Creates a {@link Point} {@link FeatureCollection} from an Array of Point coordinates.
  188. *
  189. * @name points
  190. * @param {Array<Array<number>>} coordinates an array of Points
  191. * @param {Object} [properties={}] Translate these properties to each Feature
  192. * @param {Object} [options={}] Optional Parameters
  193. * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north]
  194. * associated with the FeatureCollection
  195. * @param {string|number} [options.id] Identifier associated with the FeatureCollection
  196. * @returns {FeatureCollection<Point>} Point Feature
  197. * @example
  198. * var points = turf.points([
  199. * [-75, 39],
  200. * [-80, 45],
  201. * [-78, 50]
  202. * ]);
  203. *
  204. * //=points
  205. */
  206. function points(coordinates, properties, options) {
  207. if (options === void 0) { options = {}; }
  208. return featureCollection(coordinates.map(function (coords) {
  209. return point(coords, properties);
  210. }), options);
  211. }
  212. exports.points = points;
  213. /**
  214. * Creates a {@link Polygon} {@link Feature} from an Array of LinearRings.
  215. *
  216. * @name polygon
  217. * @param {Array<Array<Array<number>>>} coordinates an array of LinearRings
  218. * @param {Object} [properties={}] an Object of key-value pairs to add as properties
  219. * @param {Object} [options={}] Optional Parameters
  220. * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
  221. * @param {string|number} [options.id] Identifier associated with the Feature
  222. * @returns {Feature<Polygon>} Polygon Feature
  223. * @example
  224. * var polygon = turf.polygon([[[-5, 52], [-4, 56], [-2, 51], [-7, 54], [-5, 52]]], { name: 'poly1' });
  225. *
  226. * //=polygon
  227. */
  228. function polygon(coordinates, properties, options) {
  229. if (options === void 0) { options = {}; }
  230. for (var _i = 0, coordinates_1 = coordinates; _i < coordinates_1.length; _i++) {
  231. var ring = coordinates_1[_i];
  232. if (ring.length < 4) {
  233. throw new Error("Each LinearRing of a Polygon must have 4 or more Positions.");
  234. }
  235. for (var j = 0; j < ring[ring.length - 1].length; j++) {
  236. // Check if first point of Polygon contains two numbers
  237. if (ring[ring.length - 1][j] !== ring[0][j]) {
  238. throw new Error("First and last Position are not equivalent.");
  239. }
  240. }
  241. }
  242. var geom = {
  243. type: "Polygon",
  244. coordinates: coordinates,
  245. };
  246. return feature(geom, properties, options);
  247. }
  248. exports.polygon = polygon;
  249. /**
  250. * Creates a {@link Polygon} {@link FeatureCollection} from an Array of Polygon coordinates.
  251. *
  252. * @name polygons
  253. * @param {Array<Array<Array<Array<number>>>>} coordinates an array of Polygon coordinates
  254. * @param {Object} [properties={}] an Object of key-value pairs to add as properties
  255. * @param {Object} [options={}] Optional Parameters
  256. * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
  257. * @param {string|number} [options.id] Identifier associated with the FeatureCollection
  258. * @returns {FeatureCollection<Polygon>} Polygon FeatureCollection
  259. * @example
  260. * var polygons = turf.polygons([
  261. * [[[-5, 52], [-4, 56], [-2, 51], [-7, 54], [-5, 52]]],
  262. * [[[-15, 42], [-14, 46], [-12, 41], [-17, 44], [-15, 42]]],
  263. * ]);
  264. *
  265. * //=polygons
  266. */
  267. function polygons(coordinates, properties, options) {
  268. if (options === void 0) { options = {}; }
  269. return featureCollection(coordinates.map(function (coords) {
  270. return polygon(coords, properties);
  271. }), options);
  272. }
  273. exports.polygons = polygons;
  274. /**
  275. * Creates a {@link LineString} {@link Feature} from an Array of Positions.
  276. *
  277. * @name lineString
  278. * @param {Array<Array<number>>} coordinates an array of Positions
  279. * @param {Object} [properties={}] an Object of key-value pairs to add as properties
  280. * @param {Object} [options={}] Optional Parameters
  281. * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
  282. * @param {string|number} [options.id] Identifier associated with the Feature
  283. * @returns {Feature<LineString>} LineString Feature
  284. * @example
  285. * var linestring1 = turf.lineString([[-24, 63], [-23, 60], [-25, 65], [-20, 69]], {name: 'line 1'});
  286. * var linestring2 = turf.lineString([[-14, 43], [-13, 40], [-15, 45], [-10, 49]], {name: 'line 2'});
  287. *
  288. * //=linestring1
  289. * //=linestring2
  290. */
  291. function lineString(coordinates, properties, options) {
  292. if (options === void 0) { options = {}; }
  293. if (coordinates.length < 2) {
  294. throw new Error("coordinates must be an array of two or more positions");
  295. }
  296. var geom = {
  297. type: "LineString",
  298. coordinates: coordinates,
  299. };
  300. return feature(geom, properties, options);
  301. }
  302. exports.lineString = lineString;
  303. /**
  304. * Creates a {@link LineString} {@link FeatureCollection} from an Array of LineString coordinates.
  305. *
  306. * @name lineStrings
  307. * @param {Array<Array<Array<number>>>} coordinates an array of LinearRings
  308. * @param {Object} [properties={}] an Object of key-value pairs to add as properties
  309. * @param {Object} [options={}] Optional Parameters
  310. * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north]
  311. * associated with the FeatureCollection
  312. * @param {string|number} [options.id] Identifier associated with the FeatureCollection
  313. * @returns {FeatureCollection<LineString>} LineString FeatureCollection
  314. * @example
  315. * var linestrings = turf.lineStrings([
  316. * [[-24, 63], [-23, 60], [-25, 65], [-20, 69]],
  317. * [[-14, 43], [-13, 40], [-15, 45], [-10, 49]]
  318. * ]);
  319. *
  320. * //=linestrings
  321. */
  322. function lineStrings(coordinates, properties, options) {
  323. if (options === void 0) { options = {}; }
  324. return featureCollection(coordinates.map(function (coords) {
  325. return lineString(coords, properties);
  326. }), options);
  327. }
  328. exports.lineStrings = lineStrings;
  329. /**
  330. * Takes one or more {@link Feature|Features} and creates a {@link FeatureCollection}.
  331. *
  332. * @name featureCollection
  333. * @param {Feature[]} features input features
  334. * @param {Object} [options={}] Optional Parameters
  335. * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
  336. * @param {string|number} [options.id] Identifier associated with the Feature
  337. * @returns {FeatureCollection} FeatureCollection of Features
  338. * @example
  339. * var locationA = turf.point([-75.343, 39.984], {name: 'Location A'});
  340. * var locationB = turf.point([-75.833, 39.284], {name: 'Location B'});
  341. * var locationC = turf.point([-75.534, 39.123], {name: 'Location C'});
  342. *
  343. * var collection = turf.featureCollection([
  344. * locationA,
  345. * locationB,
  346. * locationC
  347. * ]);
  348. *
  349. * //=collection
  350. */
  351. function featureCollection(features, options) {
  352. if (options === void 0) { options = {}; }
  353. var fc = { type: "FeatureCollection" };
  354. if (options.id) {
  355. fc.id = options.id;
  356. }
  357. if (options.bbox) {
  358. fc.bbox = options.bbox;
  359. }
  360. fc.features = features;
  361. return fc;
  362. }
  363. exports.featureCollection = featureCollection;
  364. /**
  365. * Creates a {@link Feature<MultiLineString>} based on a
  366. * coordinate array. Properties can be added optionally.
  367. *
  368. * @name multiLineString
  369. * @param {Array<Array<Array<number>>>} coordinates an array of LineStrings
  370. * @param {Object} [properties={}] an Object of key-value pairs to add as properties
  371. * @param {Object} [options={}] Optional Parameters
  372. * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
  373. * @param {string|number} [options.id] Identifier associated with the Feature
  374. * @returns {Feature<MultiLineString>} a MultiLineString feature
  375. * @throws {Error} if no coordinates are passed
  376. * @example
  377. * var multiLine = turf.multiLineString([[[0,0],[10,10]]]);
  378. *
  379. * //=multiLine
  380. */
  381. function multiLineString(coordinates, properties, options) {
  382. if (options === void 0) { options = {}; }
  383. var geom = {
  384. type: "MultiLineString",
  385. coordinates: coordinates,
  386. };
  387. return feature(geom, properties, options);
  388. }
  389. exports.multiLineString = multiLineString;
  390. /**
  391. * Creates a {@link Feature<MultiPoint>} based on a
  392. * coordinate array. Properties can be added optionally.
  393. *
  394. * @name multiPoint
  395. * @param {Array<Array<number>>} coordinates an array of Positions
  396. * @param {Object} [properties={}] an Object of key-value pairs to add as properties
  397. * @param {Object} [options={}] Optional Parameters
  398. * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
  399. * @param {string|number} [options.id] Identifier associated with the Feature
  400. * @returns {Feature<MultiPoint>} a MultiPoint feature
  401. * @throws {Error} if no coordinates are passed
  402. * @example
  403. * var multiPt = turf.multiPoint([[0,0],[10,10]]);
  404. *
  405. * //=multiPt
  406. */
  407. function multiPoint(coordinates, properties, options) {
  408. if (options === void 0) { options = {}; }
  409. var geom = {
  410. type: "MultiPoint",
  411. coordinates: coordinates,
  412. };
  413. return feature(geom, properties, options);
  414. }
  415. exports.multiPoint = multiPoint;
  416. /**
  417. * Creates a {@link Feature<MultiPolygon>} based on a
  418. * coordinate array. Properties can be added optionally.
  419. *
  420. * @name multiPolygon
  421. * @param {Array<Array<Array<Array<number>>>>} coordinates an array of Polygons
  422. * @param {Object} [properties={}] an Object of key-value pairs to add as properties
  423. * @param {Object} [options={}] Optional Parameters
  424. * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
  425. * @param {string|number} [options.id] Identifier associated with the Feature
  426. * @returns {Feature<MultiPolygon>} a multipolygon feature
  427. * @throws {Error} if no coordinates are passed
  428. * @example
  429. * var multiPoly = turf.multiPolygon([[[[0,0],[0,10],[10,10],[10,0],[0,0]]]]);
  430. *
  431. * //=multiPoly
  432. *
  433. */
  434. function multiPolygon(coordinates, properties, options) {
  435. if (options === void 0) { options = {}; }
  436. var geom = {
  437. type: "MultiPolygon",
  438. coordinates: coordinates,
  439. };
  440. return feature(geom, properties, options);
  441. }
  442. exports.multiPolygon = multiPolygon;
  443. /**
  444. * Creates a {@link Feature<GeometryCollection>} based on a
  445. * coordinate array. Properties can be added optionally.
  446. *
  447. * @name geometryCollection
  448. * @param {Array<Geometry>} geometries an array of GeoJSON Geometries
  449. * @param {Object} [properties={}] an Object of key-value pairs to add as properties
  450. * @param {Object} [options={}] Optional Parameters
  451. * @param {Array<number>} [options.bbox] Bounding Box Array [west, south, east, north] associated with the Feature
  452. * @param {string|number} [options.id] Identifier associated with the Feature
  453. * @returns {Feature<GeometryCollection>} a GeoJSON GeometryCollection Feature
  454. * @example
  455. * var pt = turf.geometry("Point", [100, 0]);
  456. * var line = turf.geometry("LineString", [[101, 0], [102, 1]]);
  457. * var collection = turf.geometryCollection([pt, line]);
  458. *
  459. * // => collection
  460. */
  461. function geometryCollection(geometries, properties, options) {
  462. if (options === void 0) { options = {}; }
  463. var geom = {
  464. type: "GeometryCollection",
  465. geometries: geometries,
  466. };
  467. return feature(geom, properties, options);
  468. }
  469. exports.geometryCollection = geometryCollection;
  470. /**
  471. * Round number to precision
  472. *
  473. * @param {number} num Number
  474. * @param {number} [precision=0] Precision
  475. * @returns {number} rounded number
  476. * @example
  477. * turf.round(120.4321)
  478. * //=120
  479. *
  480. * turf.round(120.4321, 2)
  481. * //=120.43
  482. */
  483. function round(num, precision) {
  484. if (precision === void 0) { precision = 0; }
  485. if (precision && !(precision >= 0)) {
  486. throw new Error("precision must be a positive number");
  487. }
  488. var multiplier = Math.pow(10, precision || 0);
  489. return Math.round(num * multiplier) / multiplier;
  490. }
  491. exports.round = round;
  492. /**
  493. * Convert a distance measurement (assuming a spherical Earth) from radians to a more friendly unit.
  494. * Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
  495. *
  496. * @name radiansToLength
  497. * @param {number} radians in radians across the sphere
  498. * @param {string} [units="kilometers"] can be degrees, radians, miles, inches, yards, metres,
  499. * meters, kilometres, kilometers.
  500. * @returns {number} distance
  501. */
  502. function radiansToLength(radians, units) {
  503. if (units === void 0) { units = "kilometers"; }
  504. var factor = exports.factors[units];
  505. if (!factor) {
  506. throw new Error(units + " units is invalid");
  507. }
  508. return radians * factor;
  509. }
  510. exports.radiansToLength = radiansToLength;
  511. /**
  512. * Convert a distance measurement (assuming a spherical Earth) from a real-world unit into radians
  513. * Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
  514. *
  515. * @name lengthToRadians
  516. * @param {number} distance in real units
  517. * @param {string} [units="kilometers"] can be degrees, radians, miles, inches, yards, metres,
  518. * meters, kilometres, kilometers.
  519. * @returns {number} radians
  520. */
  521. function lengthToRadians(distance, units) {
  522. if (units === void 0) { units = "kilometers"; }
  523. var factor = exports.factors[units];
  524. if (!factor) {
  525. throw new Error(units + " units is invalid");
  526. }
  527. return distance / factor;
  528. }
  529. exports.lengthToRadians = lengthToRadians;
  530. /**
  531. * Convert a distance measurement (assuming a spherical Earth) from a real-world unit into degrees
  532. * Valid units: miles, nauticalmiles, inches, yards, meters, metres, centimeters, kilometres, feet
  533. *
  534. * @name lengthToDegrees
  535. * @param {number} distance in real units
  536. * @param {string} [units="kilometers"] can be degrees, radians, miles, inches, yards, metres,
  537. * meters, kilometres, kilometers.
  538. * @returns {number} degrees
  539. */
  540. function lengthToDegrees(distance, units) {
  541. return radiansToDegrees(lengthToRadians(distance, units));
  542. }
  543. exports.lengthToDegrees = lengthToDegrees;
  544. /**
  545. * Converts any bearing angle from the north line direction (positive clockwise)
  546. * and returns an angle between 0-360 degrees (positive clockwise), 0 being the north line
  547. *
  548. * @name bearingToAzimuth
  549. * @param {number} bearing angle, between -180 and +180 degrees
  550. * @returns {number} angle between 0 and 360 degrees
  551. */
  552. function bearingToAzimuth(bearing) {
  553. var angle = bearing % 360;
  554. if (angle < 0) {
  555. angle += 360;
  556. }
  557. return angle;
  558. }
  559. exports.bearingToAzimuth = bearingToAzimuth;
  560. /**
  561. * Converts an angle in radians to degrees
  562. *
  563. * @name radiansToDegrees
  564. * @param {number} radians angle in radians
  565. * @returns {number} degrees between 0 and 360 degrees
  566. */
  567. function radiansToDegrees(radians) {
  568. var degrees = radians % (2 * Math.PI);
  569. return (degrees * 180) / Math.PI;
  570. }
  571. exports.radiansToDegrees = radiansToDegrees;
  572. /**
  573. * Converts an angle in degrees to radians
  574. *
  575. * @name degreesToRadians
  576. * @param {number} degrees angle between 0 and 360 degrees
  577. * @returns {number} angle in radians
  578. */
  579. function degreesToRadians(degrees) {
  580. var radians = degrees % 360;
  581. return (radians * Math.PI) / 180;
  582. }
  583. exports.degreesToRadians = degreesToRadians;
  584. /**
  585. * Converts a length to the requested unit.
  586. * Valid units: miles, nauticalmiles, inches, yards, meters, metres, kilometers, centimeters, feet
  587. *
  588. * @param {number} length to be converted
  589. * @param {Units} [originalUnit="kilometers"] of the length
  590. * @param {Units} [finalUnit="kilometers"] returned unit
  591. * @returns {number} the converted length
  592. */
  593. function convertLength(length, originalUnit, finalUnit) {
  594. if (originalUnit === void 0) { originalUnit = "kilometers"; }
  595. if (finalUnit === void 0) { finalUnit = "kilometers"; }
  596. if (!(length >= 0)) {
  597. throw new Error("length must be a positive number");
  598. }
  599. return radiansToLength(lengthToRadians(length, originalUnit), finalUnit);
  600. }
  601. exports.convertLength = convertLength;
  602. /**
  603. * Converts a area to the requested unit.
  604. * Valid units: kilometers, kilometres, meters, metres, centimetres, millimeters, acres, miles, yards, feet, inches, hectares
  605. * @param {number} area to be converted
  606. * @param {Units} [originalUnit="meters"] of the distance
  607. * @param {Units} [finalUnit="kilometers"] returned unit
  608. * @returns {number} the converted area
  609. */
  610. function convertArea(area, originalUnit, finalUnit) {
  611. if (originalUnit === void 0) { originalUnit = "meters"; }
  612. if (finalUnit === void 0) { finalUnit = "kilometers"; }
  613. if (!(area >= 0)) {
  614. throw new Error("area must be a positive number");
  615. }
  616. var startFactor = exports.areaFactors[originalUnit];
  617. if (!startFactor) {
  618. throw new Error("invalid original units");
  619. }
  620. var finalFactor = exports.areaFactors[finalUnit];
  621. if (!finalFactor) {
  622. throw new Error("invalid final units");
  623. }
  624. return (area / startFactor) * finalFactor;
  625. }
  626. exports.convertArea = convertArea;
  627. /**
  628. * isNumber
  629. *
  630. * @param {*} num Number to validate
  631. * @returns {boolean} true/false
  632. * @example
  633. * turf.isNumber(123)
  634. * //=true
  635. * turf.isNumber('foo')
  636. * //=false
  637. */
  638. function isNumber(num) {
  639. return !isNaN(num) && num !== null && !Array.isArray(num);
  640. }
  641. exports.isNumber = isNumber;
  642. /**
  643. * isObject
  644. *
  645. * @param {*} input variable to validate
  646. * @returns {boolean} true/false
  647. * @example
  648. * turf.isObject({elevation: 10})
  649. * //=true
  650. * turf.isObject('foo')
  651. * //=false
  652. */
  653. function isObject(input) {
  654. return !!input && input.constructor === Object;
  655. }
  656. exports.isObject = isObject;
  657. /**
  658. * Validate BBox
  659. *
  660. * @private
  661. * @param {Array<number>} bbox BBox to validate
  662. * @returns {void}
  663. * @throws Error if BBox is not valid
  664. * @example
  665. * validateBBox([-180, -40, 110, 50])
  666. * //=OK
  667. * validateBBox([-180, -40])
  668. * //=Error
  669. * validateBBox('Foo')
  670. * //=Error
  671. * validateBBox(5)
  672. * //=Error
  673. * validateBBox(null)
  674. * //=Error
  675. * validateBBox(undefined)
  676. * //=Error
  677. */
  678. function validateBBox(bbox) {
  679. if (!bbox) {
  680. throw new Error("bbox is required");
  681. }
  682. if (!Array.isArray(bbox)) {
  683. throw new Error("bbox must be an Array");
  684. }
  685. if (bbox.length !== 4 && bbox.length !== 6) {
  686. throw new Error("bbox must be an Array of 4 or 6 numbers");
  687. }
  688. bbox.forEach(function (num) {
  689. if (!isNumber(num)) {
  690. throw new Error("bbox must only contain numbers");
  691. }
  692. });
  693. }
  694. exports.validateBBox = validateBBox;
  695. /**
  696. * Validate Id
  697. *
  698. * @private
  699. * @param {string|number} id Id to validate
  700. * @returns {void}
  701. * @throws Error if Id is not valid
  702. * @example
  703. * validateId([-180, -40, 110, 50])
  704. * //=Error
  705. * validateId([-180, -40])
  706. * //=Error
  707. * validateId('Foo')
  708. * //=OK
  709. * validateId(5)
  710. * //=OK
  711. * validateId(null)
  712. * //=Error
  713. * validateId(undefined)
  714. * //=Error
  715. */
  716. function validateId(id) {
  717. if (!id) {
  718. throw new Error("id is required");
  719. }
  720. if (["string", "number"].indexOf(typeof id) === -1) {
  721. throw new Error("id must be a number or a string");
  722. }
  723. }
  724. exports.validateId = validateId;