index.cjs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. var earthRadius = 63710088e-1;
  3. var factors = {
  4. centimeters: earthRadius * 100,
  5. centimetres: earthRadius * 100,
  6. degrees: 360 / (2 * Math.PI),
  7. feet: earthRadius * 3.28084,
  8. inches: earthRadius * 39.37,
  9. kilometers: earthRadius / 1e3,
  10. kilometres: earthRadius / 1e3,
  11. meters: earthRadius,
  12. metres: earthRadius,
  13. miles: earthRadius / 1609.344,
  14. millimeters: earthRadius * 1e3,
  15. millimetres: earthRadius * 1e3,
  16. nauticalmiles: earthRadius / 1852,
  17. radians: 1,
  18. yards: earthRadius * 1.0936
  19. };
  20. var areaFactors = {
  21. acres: 247105e-9,
  22. centimeters: 1e4,
  23. centimetres: 1e4,
  24. feet: 10.763910417,
  25. hectares: 1e-4,
  26. inches: 1550.003100006,
  27. kilometers: 1e-6,
  28. kilometres: 1e-6,
  29. meters: 1,
  30. metres: 1,
  31. miles: 386e-9,
  32. nauticalmiles: 29155334959812285e-23,
  33. millimeters: 1e6,
  34. millimetres: 1e6,
  35. yards: 1.195990046
  36. };
  37. function feature(geom, properties, options = {}) {
  38. const feat = { type: "Feature" };
  39. if (options.id === 0 || options.id) {
  40. feat.id = options.id;
  41. }
  42. if (options.bbox) {
  43. feat.bbox = options.bbox;
  44. }
  45. feat.properties = properties || {};
  46. feat.geometry = geom;
  47. return feat;
  48. }
  49. function geometry(type, coordinates, _options = {}) {
  50. switch (type) {
  51. case "Point":
  52. return point(coordinates).geometry;
  53. case "LineString":
  54. return lineString(coordinates).geometry;
  55. case "Polygon":
  56. return polygon(coordinates).geometry;
  57. case "MultiPoint":
  58. return multiPoint(coordinates).geometry;
  59. case "MultiLineString":
  60. return multiLineString(coordinates).geometry;
  61. case "MultiPolygon":
  62. return multiPolygon(coordinates).geometry;
  63. default:
  64. throw new Error(type + " is invalid");
  65. }
  66. }
  67. function point(coordinates, properties, options = {}) {
  68. if (!coordinates) {
  69. throw new Error("coordinates is required");
  70. }
  71. if (!Array.isArray(coordinates)) {
  72. throw new Error("coordinates must be an Array");
  73. }
  74. if (coordinates.length < 2) {
  75. throw new Error("coordinates must be at least 2 numbers long");
  76. }
  77. if (!isNumber(coordinates[0]) || !isNumber(coordinates[1])) {
  78. throw new Error("coordinates must contain numbers");
  79. }
  80. const geom = {
  81. type: "Point",
  82. coordinates
  83. };
  84. return feature(geom, properties, options);
  85. }
  86. function points(coordinates, properties, options = {}) {
  87. return featureCollection(
  88. coordinates.map((coords) => {
  89. return point(coords, properties);
  90. }),
  91. options
  92. );
  93. }
  94. function polygon(coordinates, properties, options = {}) {
  95. for (const ring of coordinates) {
  96. if (ring.length < 4) {
  97. throw new Error(
  98. "Each LinearRing of a Polygon must have 4 or more Positions."
  99. );
  100. }
  101. if (ring[ring.length - 1].length !== ring[0].length) {
  102. throw new Error("First and last Position are not equivalent.");
  103. }
  104. for (let j = 0; j < ring[ring.length - 1].length; j++) {
  105. if (ring[ring.length - 1][j] !== ring[0][j]) {
  106. throw new Error("First and last Position are not equivalent.");
  107. }
  108. }
  109. }
  110. const geom = {
  111. type: "Polygon",
  112. coordinates
  113. };
  114. return feature(geom, properties, options);
  115. }
  116. function polygons(coordinates, properties, options = {}) {
  117. return featureCollection(
  118. coordinates.map((coords) => {
  119. return polygon(coords, properties);
  120. }),
  121. options
  122. );
  123. }
  124. function lineString(coordinates, properties, options = {}) {
  125. if (coordinates.length < 2) {
  126. throw new Error("coordinates must be an array of two or more positions");
  127. }
  128. const geom = {
  129. type: "LineString",
  130. coordinates
  131. };
  132. return feature(geom, properties, options);
  133. }
  134. function lineStrings(coordinates, properties, options = {}) {
  135. return featureCollection(
  136. coordinates.map((coords) => {
  137. return lineString(coords, properties);
  138. }),
  139. options
  140. );
  141. }
  142. function featureCollection(features, options = {}) {
  143. const fc = { type: "FeatureCollection" };
  144. if (options.id) {
  145. fc.id = options.id;
  146. }
  147. if (options.bbox) {
  148. fc.bbox = options.bbox;
  149. }
  150. fc.features = features;
  151. return fc;
  152. }
  153. function multiLineString(coordinates, properties, options = {}) {
  154. const geom = {
  155. type: "MultiLineString",
  156. coordinates
  157. };
  158. return feature(geom, properties, options);
  159. }
  160. function multiPoint(coordinates, properties, options = {}) {
  161. const geom = {
  162. type: "MultiPoint",
  163. coordinates
  164. };
  165. return feature(geom, properties, options);
  166. }
  167. function multiPolygon(coordinates, properties, options = {}) {
  168. const geom = {
  169. type: "MultiPolygon",
  170. coordinates
  171. };
  172. return feature(geom, properties, options);
  173. }
  174. function geometryCollection(geometries, properties, options = {}) {
  175. const geom = {
  176. type: "GeometryCollection",
  177. geometries
  178. };
  179. return feature(geom, properties, options);
  180. }
  181. function round(num, precision = 0) {
  182. if (precision && !(precision >= 0)) {
  183. throw new Error("precision must be a positive number");
  184. }
  185. const multiplier = Math.pow(10, precision || 0);
  186. return Math.round(num * multiplier) / multiplier;
  187. }
  188. function radiansToLength(radians, units = "kilometers") {
  189. const factor = factors[units];
  190. if (!factor) {
  191. throw new Error(units + " units is invalid");
  192. }
  193. return radians * factor;
  194. }
  195. function lengthToRadians(distance, units = "kilometers") {
  196. const factor = factors[units];
  197. if (!factor) {
  198. throw new Error(units + " units is invalid");
  199. }
  200. return distance / factor;
  201. }
  202. function lengthToDegrees(distance, units) {
  203. return radiansToDegrees(lengthToRadians(distance, units));
  204. }
  205. function bearingToAzimuth(bearing) {
  206. let angle = bearing % 360;
  207. if (angle < 0) {
  208. angle += 360;
  209. }
  210. return angle;
  211. }
  212. function azimuthToBearing(angle) {
  213. angle = angle % 360;
  214. if (angle > 180) {
  215. return angle - 360;
  216. } else if (angle < -180) {
  217. return angle + 360;
  218. }
  219. return angle;
  220. }
  221. function radiansToDegrees(radians) {
  222. const normalisedRadians = radians % (2 * Math.PI);
  223. return normalisedRadians * 180 / Math.PI;
  224. }
  225. function degreesToRadians(degrees) {
  226. const normalisedDegrees = degrees % 360;
  227. return normalisedDegrees * Math.PI / 180;
  228. }
  229. function convertLength(length, originalUnit = "kilometers", finalUnit = "kilometers") {
  230. if (!(length >= 0)) {
  231. throw new Error("length must be a positive number");
  232. }
  233. return radiansToLength(lengthToRadians(length, originalUnit), finalUnit);
  234. }
  235. function convertArea(area, originalUnit = "meters", finalUnit = "kilometers") {
  236. if (!(area >= 0)) {
  237. throw new Error("area must be a positive number");
  238. }
  239. const startFactor = areaFactors[originalUnit];
  240. if (!startFactor) {
  241. throw new Error("invalid original units");
  242. }
  243. const finalFactor = areaFactors[finalUnit];
  244. if (!finalFactor) {
  245. throw new Error("invalid final units");
  246. }
  247. return area / startFactor * finalFactor;
  248. }
  249. function isNumber(num) {
  250. return !isNaN(num) && num !== null && !Array.isArray(num);
  251. }
  252. function isObject(input) {
  253. return input !== null && typeof input === "object" && !Array.isArray(input);
  254. }
  255. function validateBBox(bbox) {
  256. if (!bbox) {
  257. throw new Error("bbox is required");
  258. }
  259. if (!Array.isArray(bbox)) {
  260. throw new Error("bbox must be an Array");
  261. }
  262. if (bbox.length !== 4 && bbox.length !== 6) {
  263. throw new Error("bbox must be an Array of 4 or 6 numbers");
  264. }
  265. bbox.forEach((num) => {
  266. if (!isNumber(num)) {
  267. throw new Error("bbox must only contain numbers");
  268. }
  269. });
  270. }
  271. function validateId(id) {
  272. if (!id) {
  273. throw new Error("id is required");
  274. }
  275. if (["string", "number"].indexOf(typeof id) === -1) {
  276. throw new Error("id must be a number or a string");
  277. }
  278. }
  279. exports.areaFactors = areaFactors; exports.azimuthToBearing = azimuthToBearing; exports.bearingToAzimuth = bearingToAzimuth; exports.convertArea = convertArea; exports.convertLength = convertLength; exports.degreesToRadians = degreesToRadians; exports.earthRadius = earthRadius; exports.factors = factors; exports.feature = feature; exports.featureCollection = featureCollection; exports.geometry = geometry; exports.geometryCollection = geometryCollection; exports.isNumber = isNumber; exports.isObject = isObject; exports.lengthToDegrees = lengthToDegrees; exports.lengthToRadians = lengthToRadians; exports.lineString = lineString; exports.lineStrings = lineStrings; exports.multiLineString = multiLineString; exports.multiPoint = multiPoint; exports.multiPolygon = multiPolygon; exports.point = point; exports.points = points; exports.polygon = polygon; exports.polygons = polygons; exports.radiansToDegrees = radiansToDegrees; exports.radiansToLength = radiansToLength; exports.round = round; exports.validateBBox = validateBBox; exports.validateId = validateId;
  280. //# sourceMappingURL=index.cjs.map