index.js 6.8 KB

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