| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- // index.ts
- var earthRadius = 63710088e-1;
- var factors = {
- centimeters: earthRadius * 100,
- centimetres: earthRadius * 100,
- degrees: 360 / (2 * Math.PI),
- feet: earthRadius * 3.28084,
- inches: earthRadius * 39.37,
- kilometers: earthRadius / 1e3,
- kilometres: earthRadius / 1e3,
- meters: earthRadius,
- metres: earthRadius,
- miles: earthRadius / 1609.344,
- millimeters: earthRadius * 1e3,
- millimetres: earthRadius * 1e3,
- nauticalmiles: earthRadius / 1852,
- radians: 1,
- yards: earthRadius * 1.0936
- };
- var areaFactors = {
- acres: 247105e-9,
- centimeters: 1e4,
- centimetres: 1e4,
- feet: 10.763910417,
- hectares: 1e-4,
- inches: 1550.003100006,
- kilometers: 1e-6,
- kilometres: 1e-6,
- meters: 1,
- metres: 1,
- miles: 386e-9,
- nauticalmiles: 29155334959812285e-23,
- millimeters: 1e6,
- millimetres: 1e6,
- yards: 1.195990046
- };
- function feature(geom, properties, options = {}) {
- const feat = { type: "Feature" };
- if (options.id === 0 || options.id) {
- feat.id = options.id;
- }
- if (options.bbox) {
- feat.bbox = options.bbox;
- }
- feat.properties = properties || {};
- feat.geometry = geom;
- return feat;
- }
- function geometry(type, coordinates, _options = {}) {
- switch (type) {
- case "Point":
- return point(coordinates).geometry;
- case "LineString":
- return lineString(coordinates).geometry;
- case "Polygon":
- return polygon(coordinates).geometry;
- case "MultiPoint":
- return multiPoint(coordinates).geometry;
- case "MultiLineString":
- return multiLineString(coordinates).geometry;
- case "MultiPolygon":
- return multiPolygon(coordinates).geometry;
- default:
- throw new Error(type + " is invalid");
- }
- }
- function point(coordinates, properties, options = {}) {
- if (!coordinates) {
- throw new Error("coordinates is required");
- }
- if (!Array.isArray(coordinates)) {
- throw new Error("coordinates must be an Array");
- }
- if (coordinates.length < 2) {
- throw new Error("coordinates must be at least 2 numbers long");
- }
- if (!isNumber(coordinates[0]) || !isNumber(coordinates[1])) {
- throw new Error("coordinates must contain numbers");
- }
- const geom = {
- type: "Point",
- coordinates
- };
- return feature(geom, properties, options);
- }
- function points(coordinates, properties, options = {}) {
- return featureCollection(
- coordinates.map((coords) => {
- return point(coords, properties);
- }),
- options
- );
- }
- function polygon(coordinates, properties, options = {}) {
- for (const ring of coordinates) {
- if (ring.length < 4) {
- throw new Error(
- "Each LinearRing of a Polygon must have 4 or more Positions."
- );
- }
- if (ring[ring.length - 1].length !== ring[0].length) {
- throw new Error("First and last Position are not equivalent.");
- }
- for (let j = 0; j < ring[ring.length - 1].length; j++) {
- if (ring[ring.length - 1][j] !== ring[0][j]) {
- throw new Error("First and last Position are not equivalent.");
- }
- }
- }
- const geom = {
- type: "Polygon",
- coordinates
- };
- return feature(geom, properties, options);
- }
- function polygons(coordinates, properties, options = {}) {
- return featureCollection(
- coordinates.map((coords) => {
- return polygon(coords, properties);
- }),
- options
- );
- }
- function lineString(coordinates, properties, options = {}) {
- if (coordinates.length < 2) {
- throw new Error("coordinates must be an array of two or more positions");
- }
- const geom = {
- type: "LineString",
- coordinates
- };
- return feature(geom, properties, options);
- }
- function lineStrings(coordinates, properties, options = {}) {
- return featureCollection(
- coordinates.map((coords) => {
- return lineString(coords, properties);
- }),
- options
- );
- }
- function featureCollection(features, options = {}) {
- const fc = { type: "FeatureCollection" };
- if (options.id) {
- fc.id = options.id;
- }
- if (options.bbox) {
- fc.bbox = options.bbox;
- }
- fc.features = features;
- return fc;
- }
- function multiLineString(coordinates, properties, options = {}) {
- const geom = {
- type: "MultiLineString",
- coordinates
- };
- return feature(geom, properties, options);
- }
- function multiPoint(coordinates, properties, options = {}) {
- const geom = {
- type: "MultiPoint",
- coordinates
- };
- return feature(geom, properties, options);
- }
- function multiPolygon(coordinates, properties, options = {}) {
- const geom = {
- type: "MultiPolygon",
- coordinates
- };
- return feature(geom, properties, options);
- }
- function geometryCollection(geometries, properties, options = {}) {
- const geom = {
- type: "GeometryCollection",
- geometries
- };
- return feature(geom, properties, options);
- }
- function round(num, precision = 0) {
- if (precision && !(precision >= 0)) {
- throw new Error("precision must be a positive number");
- }
- const multiplier = Math.pow(10, precision || 0);
- return Math.round(num * multiplier) / multiplier;
- }
- function radiansToLength(radians, units = "kilometers") {
- const factor = factors[units];
- if (!factor) {
- throw new Error(units + " units is invalid");
- }
- return radians * factor;
- }
- function lengthToRadians(distance, units = "kilometers") {
- const factor = factors[units];
- if (!factor) {
- throw new Error(units + " units is invalid");
- }
- return distance / factor;
- }
- function lengthToDegrees(distance, units) {
- return radiansToDegrees(lengthToRadians(distance, units));
- }
- function bearingToAzimuth(bearing) {
- let angle = bearing % 360;
- if (angle < 0) {
- angle += 360;
- }
- return angle;
- }
- function azimuthToBearing(angle) {
- angle = angle % 360;
- if (angle > 180) {
- return angle - 360;
- } else if (angle < -180) {
- return angle + 360;
- }
- return angle;
- }
- function radiansToDegrees(radians) {
- const normalisedRadians = radians % (2 * Math.PI);
- return normalisedRadians * 180 / Math.PI;
- }
- function degreesToRadians(degrees) {
- const normalisedDegrees = degrees % 360;
- return normalisedDegrees * Math.PI / 180;
- }
- function convertLength(length, originalUnit = "kilometers", finalUnit = "kilometers") {
- if (!(length >= 0)) {
- throw new Error("length must be a positive number");
- }
- return radiansToLength(lengthToRadians(length, originalUnit), finalUnit);
- }
- function convertArea(area, originalUnit = "meters", finalUnit = "kilometers") {
- if (!(area >= 0)) {
- throw new Error("area must be a positive number");
- }
- const startFactor = areaFactors[originalUnit];
- if (!startFactor) {
- throw new Error("invalid original units");
- }
- const finalFactor = areaFactors[finalUnit];
- if (!finalFactor) {
- throw new Error("invalid final units");
- }
- return area / startFactor * finalFactor;
- }
- function isNumber(num) {
- return !isNaN(num) && num !== null && !Array.isArray(num);
- }
- function isObject(input) {
- return input !== null && typeof input === "object" && !Array.isArray(input);
- }
- function validateBBox(bbox) {
- if (!bbox) {
- throw new Error("bbox is required");
- }
- if (!Array.isArray(bbox)) {
- throw new Error("bbox must be an Array");
- }
- if (bbox.length !== 4 && bbox.length !== 6) {
- throw new Error("bbox must be an Array of 4 or 6 numbers");
- }
- bbox.forEach((num) => {
- if (!isNumber(num)) {
- throw new Error("bbox must only contain numbers");
- }
- });
- }
- function validateId(id) {
- if (!id) {
- throw new Error("id is required");
- }
- if (["string", "number"].indexOf(typeof id) === -1) {
- throw new Error("id must be a number or a string");
- }
- }
- export {
- areaFactors,
- azimuthToBearing,
- bearingToAzimuth,
- convertArea,
- convertLength,
- degreesToRadians,
- earthRadius,
- factors,
- feature,
- featureCollection,
- geometry,
- geometryCollection,
- isNumber,
- isObject,
- lengthToDegrees,
- lengthToRadians,
- lineString,
- lineStrings,
- multiLineString,
- multiPoint,
- multiPolygon,
- point,
- points,
- polygon,
- polygons,
- radiansToDegrees,
- radiansToLength,
- round,
- validateBBox,
- validateId
- };
- //# sourceMappingURL=index.js.map
|