index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var helpers_1 = require("@turf/helpers");
  4. /**
  5. * Unwrap a coordinate from a Point Feature, Geometry or a single coordinate.
  6. *
  7. * @name getCoord
  8. * @param {Array<number>|Geometry<Point>|Feature<Point>} coord GeoJSON Point or an Array of numbers
  9. * @returns {Array<number>} coordinates
  10. * @example
  11. * var pt = turf.point([10, 10]);
  12. *
  13. * var coord = turf.getCoord(pt);
  14. * //= [10, 10]
  15. */
  16. function getCoord(coord) {
  17. if (!coord) {
  18. throw new Error("coord is required");
  19. }
  20. if (!Array.isArray(coord)) {
  21. if (coord.type === "Feature" &&
  22. coord.geometry !== null &&
  23. coord.geometry.type === "Point") {
  24. return coord.geometry.coordinates;
  25. }
  26. if (coord.type === "Point") {
  27. return coord.coordinates;
  28. }
  29. }
  30. if (Array.isArray(coord) &&
  31. coord.length >= 2 &&
  32. !Array.isArray(coord[0]) &&
  33. !Array.isArray(coord[1])) {
  34. return coord;
  35. }
  36. throw new Error("coord must be GeoJSON Point or an Array of numbers");
  37. }
  38. exports.getCoord = getCoord;
  39. /**
  40. * Unwrap coordinates from a Feature, Geometry Object or an Array
  41. *
  42. * @name getCoords
  43. * @param {Array<any>|Geometry|Feature} coords Feature, Geometry Object or an Array
  44. * @returns {Array<any>} coordinates
  45. * @example
  46. * var poly = turf.polygon([[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]);
  47. *
  48. * var coords = turf.getCoords(poly);
  49. * //= [[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]
  50. */
  51. function getCoords(coords) {
  52. if (Array.isArray(coords)) {
  53. return coords;
  54. }
  55. // Feature
  56. if (coords.type === "Feature") {
  57. if (coords.geometry !== null) {
  58. return coords.geometry.coordinates;
  59. }
  60. }
  61. else {
  62. // Geometry
  63. if (coords.coordinates) {
  64. return coords.coordinates;
  65. }
  66. }
  67. throw new Error("coords must be GeoJSON Feature, Geometry Object or an Array");
  68. }
  69. exports.getCoords = getCoords;
  70. /**
  71. * Checks if coordinates contains a number
  72. *
  73. * @name containsNumber
  74. * @param {Array<any>} coordinates GeoJSON Coordinates
  75. * @returns {boolean} true if Array contains a number
  76. */
  77. function containsNumber(coordinates) {
  78. if (coordinates.length > 1 &&
  79. helpers_1.isNumber(coordinates[0]) &&
  80. helpers_1.isNumber(coordinates[1])) {
  81. return true;
  82. }
  83. if (Array.isArray(coordinates[0]) && coordinates[0].length) {
  84. return containsNumber(coordinates[0]);
  85. }
  86. throw new Error("coordinates must only contain numbers");
  87. }
  88. exports.containsNumber = containsNumber;
  89. /**
  90. * Enforce expectations about types of GeoJSON objects for Turf.
  91. *
  92. * @name geojsonType
  93. * @param {GeoJSON} value any GeoJSON object
  94. * @param {string} type expected GeoJSON type
  95. * @param {string} name name of calling function
  96. * @throws {Error} if value is not the expected type.
  97. */
  98. function geojsonType(value, type, name) {
  99. if (!type || !name) {
  100. throw new Error("type and name required");
  101. }
  102. if (!value || value.type !== type) {
  103. throw new Error("Invalid input to " +
  104. name +
  105. ": must be a " +
  106. type +
  107. ", given " +
  108. value.type);
  109. }
  110. }
  111. exports.geojsonType = geojsonType;
  112. /**
  113. * Enforce expectations about types of {@link Feature} inputs for Turf.
  114. * Internally this uses {@link geojsonType} to judge geometry types.
  115. *
  116. * @name featureOf
  117. * @param {Feature} feature a feature with an expected geometry type
  118. * @param {string} type expected GeoJSON type
  119. * @param {string} name name of calling function
  120. * @throws {Error} error if value is not the expected type.
  121. */
  122. function featureOf(feature, type, name) {
  123. if (!feature) {
  124. throw new Error("No feature passed");
  125. }
  126. if (!name) {
  127. throw new Error(".featureOf() requires a name");
  128. }
  129. if (!feature || feature.type !== "Feature" || !feature.geometry) {
  130. throw new Error("Invalid input to " + name + ", Feature with geometry required");
  131. }
  132. if (!feature.geometry || feature.geometry.type !== type) {
  133. throw new Error("Invalid input to " +
  134. name +
  135. ": must be a " +
  136. type +
  137. ", given " +
  138. feature.geometry.type);
  139. }
  140. }
  141. exports.featureOf = featureOf;
  142. /**
  143. * Enforce expectations about types of {@link FeatureCollection} inputs for Turf.
  144. * Internally this uses {@link geojsonType} to judge geometry types.
  145. *
  146. * @name collectionOf
  147. * @param {FeatureCollection} featureCollection a FeatureCollection for which features will be judged
  148. * @param {string} type expected GeoJSON type
  149. * @param {string} name name of calling function
  150. * @throws {Error} if value is not the expected type.
  151. */
  152. function collectionOf(featureCollection, type, name) {
  153. if (!featureCollection) {
  154. throw new Error("No featureCollection passed");
  155. }
  156. if (!name) {
  157. throw new Error(".collectionOf() requires a name");
  158. }
  159. if (!featureCollection || featureCollection.type !== "FeatureCollection") {
  160. throw new Error("Invalid input to " + name + ", FeatureCollection required");
  161. }
  162. for (var _i = 0, _a = featureCollection.features; _i < _a.length; _i++) {
  163. var feature = _a[_i];
  164. if (!feature || feature.type !== "Feature" || !feature.geometry) {
  165. throw new Error("Invalid input to " + name + ", Feature with geometry required");
  166. }
  167. if (!feature.geometry || feature.geometry.type !== type) {
  168. throw new Error("Invalid input to " +
  169. name +
  170. ": must be a " +
  171. type +
  172. ", given " +
  173. feature.geometry.type);
  174. }
  175. }
  176. }
  177. exports.collectionOf = collectionOf;
  178. /**
  179. * Get Geometry from Feature or Geometry Object
  180. *
  181. * @param {Feature|Geometry} geojson GeoJSON Feature or Geometry Object
  182. * @returns {Geometry|null} GeoJSON Geometry Object
  183. * @throws {Error} if geojson is not a Feature or Geometry Object
  184. * @example
  185. * var point = {
  186. * "type": "Feature",
  187. * "properties": {},
  188. * "geometry": {
  189. * "type": "Point",
  190. * "coordinates": [110, 40]
  191. * }
  192. * }
  193. * var geom = turf.getGeom(point)
  194. * //={"type": "Point", "coordinates": [110, 40]}
  195. */
  196. function getGeom(geojson) {
  197. if (geojson.type === "Feature") {
  198. return geojson.geometry;
  199. }
  200. return geojson;
  201. }
  202. exports.getGeom = getGeom;
  203. /**
  204. * Get GeoJSON object's type, Geometry type is prioritize.
  205. *
  206. * @param {GeoJSON} geojson GeoJSON object
  207. * @param {string} [name="geojson"] name of the variable to display in error message (unused)
  208. * @returns {string} GeoJSON type
  209. * @example
  210. * var point = {
  211. * "type": "Feature",
  212. * "properties": {},
  213. * "geometry": {
  214. * "type": "Point",
  215. * "coordinates": [110, 40]
  216. * }
  217. * }
  218. * var geom = turf.getType(point)
  219. * //="Point"
  220. */
  221. function getType(geojson, _name) {
  222. if (geojson.type === "FeatureCollection") {
  223. return "FeatureCollection";
  224. }
  225. if (geojson.type === "GeometryCollection") {
  226. return "GeometryCollection";
  227. }
  228. if (geojson.type === "Feature" && geojson.geometry !== null) {
  229. return geojson.geometry.type;
  230. }
  231. return geojson.type;
  232. }
  233. exports.getType = getType;