index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // index.ts
  2. import { isNumber } from "@turf/helpers";
  3. function getCoord(coord) {
  4. if (!coord) {
  5. throw new Error("coord is required");
  6. }
  7. if (!Array.isArray(coord)) {
  8. if (coord.type === "Feature" && coord.geometry !== null && coord.geometry.type === "Point") {
  9. return [...coord.geometry.coordinates];
  10. }
  11. if (coord.type === "Point") {
  12. return [...coord.coordinates];
  13. }
  14. }
  15. if (Array.isArray(coord) && coord.length >= 2 && !Array.isArray(coord[0]) && !Array.isArray(coord[1])) {
  16. return [...coord];
  17. }
  18. throw new Error("coord must be GeoJSON Point or an Array of numbers");
  19. }
  20. function getCoords(coords) {
  21. if (Array.isArray(coords)) {
  22. return coords;
  23. }
  24. if (coords.type === "Feature") {
  25. if (coords.geometry !== null) {
  26. return coords.geometry.coordinates;
  27. }
  28. } else {
  29. if (coords.coordinates) {
  30. return coords.coordinates;
  31. }
  32. }
  33. throw new Error(
  34. "coords must be GeoJSON Feature, Geometry Object or an Array"
  35. );
  36. }
  37. function containsNumber(coordinates) {
  38. if (coordinates.length > 1 && isNumber(coordinates[0]) && isNumber(coordinates[1])) {
  39. return true;
  40. }
  41. if (Array.isArray(coordinates[0]) && coordinates[0].length) {
  42. return containsNumber(coordinates[0]);
  43. }
  44. throw new Error("coordinates must only contain numbers");
  45. }
  46. function geojsonType(value, type, name) {
  47. if (!type || !name) {
  48. throw new Error("type and name required");
  49. }
  50. if (!value || value.type !== type) {
  51. throw new Error(
  52. "Invalid input to " + name + ": must be a " + type + ", given " + value.type
  53. );
  54. }
  55. }
  56. function featureOf(feature, type, name) {
  57. if (!feature) {
  58. throw new Error("No feature passed");
  59. }
  60. if (!name) {
  61. throw new Error(".featureOf() requires a name");
  62. }
  63. if (!feature || feature.type !== "Feature" || !feature.geometry) {
  64. throw new Error(
  65. "Invalid input to " + name + ", Feature with geometry required"
  66. );
  67. }
  68. if (!feature.geometry || feature.geometry.type !== type) {
  69. throw new Error(
  70. "Invalid input to " + name + ": must be a " + type + ", given " + feature.geometry.type
  71. );
  72. }
  73. }
  74. function collectionOf(featureCollection, type, name) {
  75. if (!featureCollection) {
  76. throw new Error("No featureCollection passed");
  77. }
  78. if (!name) {
  79. throw new Error(".collectionOf() requires a name");
  80. }
  81. if (!featureCollection || featureCollection.type !== "FeatureCollection") {
  82. throw new Error(
  83. "Invalid input to " + name + ", FeatureCollection required"
  84. );
  85. }
  86. for (const feature of featureCollection.features) {
  87. if (!feature || feature.type !== "Feature" || !feature.geometry) {
  88. throw new Error(
  89. "Invalid input to " + name + ", Feature with geometry required"
  90. );
  91. }
  92. if (!feature.geometry || feature.geometry.type !== type) {
  93. throw new Error(
  94. "Invalid input to " + name + ": must be a " + type + ", given " + feature.geometry.type
  95. );
  96. }
  97. }
  98. }
  99. function getGeom(geojson) {
  100. if (geojson.type === "Feature") {
  101. return geojson.geometry;
  102. }
  103. return geojson;
  104. }
  105. function getType(geojson, _name) {
  106. if (geojson.type === "FeatureCollection") {
  107. return "FeatureCollection";
  108. }
  109. if (geojson.type === "GeometryCollection") {
  110. return "GeometryCollection";
  111. }
  112. if (geojson.type === "Feature" && geojson.geometry !== null) {
  113. return geojson.geometry.type;
  114. }
  115. return geojson.type;
  116. }
  117. export {
  118. collectionOf,
  119. containsNumber,
  120. featureOf,
  121. geojsonType,
  122. getCoord,
  123. getCoords,
  124. getGeom,
  125. getType
  126. };
  127. //# sourceMappingURL=index.js.map