Giter Club home page Giter Club logo

dms-js's Introduction

dms-conversion

A JavaScript library for converting between decimal degrees and degrees, minutes, and seconds (DMS).

npm version npm license

Node.js CI GitHub issues GitHub

Installation

npm install dms-conversion

Example

TypeScript example (from Jasmine test)

import DmsCoordinates, { parseDms } from "dms-conversion";

describe("DmsCoordinates", () => {
  const long = -122.902336120571;
  const lat = 46.9845854731319;

  it(`(${lat}, ${long}) coordinates should be ~ 46°59′5″ N, 122°54′8″ W`, () => {
    const dmsCoords = new DmsCoordinates(lat, long);
    expect(dmsCoords instanceof DmsCoordinates).toBe(true);

    const { longitude, latitude } = dmsCoords.dmsArrays;

    let [d, m, s, nsew] = longitude;
    expect(d).toBe(122);
    expect(m).toBe(54);
    expect(Math.round(s)).toBe(8);
    expect(nsew).toBe("W");

    [d, m, s, nsew] = latitude;

    expect(d).toBe(46);
    expect(m).toBe(59);
    expect(Math.round(s)).toBe(5);
    expect(nsew).toBe("N");

    expect(dmsCoords.toString()).toMatch(/46°59′4.\d+″ N, 122°54′8.\d+″ W/i);
  });

  it("Regexp should work", () => {
    const v = ["46°59′5″ N", "122°54′8″ W"];
    v.forEach((s) => expect(s.match(DmsCoordinates.dmsRe)).toBeTruthy(true));
    const [x, y] = v.map(parseDms);
    expect(typeof x).toEqual("number");
    expect(typeof y).toEqual("number");
  });

  it("Invalid numbers should throw exception", () => {
    const x = parseDms("");
    expect(isNaN(x)).toBe(true);
    expect(() => {
      const dmsc = new DmsCoordinates(lat, x);
    }).toThrowError(RangeError);
    expect(() => {
      const dmsc = new DmsCoordinates("this should fail" as any, long);
    }).toThrowError(TypeError);
  });
});

Alternatives

nerik/formatcoords

dms-js's People

Contributors

dependabot[bot] avatar jeffjacobson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dms-js's Issues

switch to webpack breaks use of dms-js in node.js server

/Users/matthew/code/msf-reach/node_modules/dms-conversion/dms.js:1
(function (exports, require, module, __filename, __dirname) { !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.dms=t():e.dms=t()}(window,function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:n})},r.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){"use strict";r.r(t),r.d(t,"Dms",function(){return i}),r.d(t,"parseDms",function(){return s});var n=/^(-?\d+(?:\.\d+)?)[°:d]?\s?(?:(

ReferenceError: window is not defined
    at Object.<anonymous> (/Users/matthew/code/msf-reach/node_modules/dms-conversion/dms.js:1:250)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/matthew/code/msf-reach/dist/lib/tsr.js:15:22)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/matthew/code/msf-reach/dist/api/routes/hazards/index.js:15:12)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node dist`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/matthew/.npm/_logs/2018-04-30T01_28_42_163Z-debug.log

More flexible format support

Hy,

I just had a look at your parse functionality (since I'm only interested in that at the moment) and found out that you do not support the possibility of having the direction in front of the coordinate (ex.: N 39° 03' 51.420" W 145° 12' 15.780"). Would it somehow be possible to implement that?

also found a different library that uses a different regex that might help:
https://github.com/gmaclennan/parse-dms
but I'd rather use your library as I see the potential in changing more of my code to use more of your functionality.

Thanks

Unable to install in VS2017, parameter 'Path' because it is null

Hello,
I haven't developped for awhile, used to work with C# 2.0. I created a new TypeScript project in VS2017, added packages for JSQuery 3.3.1 and jquery.TypeScript.DefinitelyTyped 3.1.2, the project compiles and run.

I tried to install dms-conversion but I am not very confident with that command-line tool:

PM> install dms-conversion

This message appears:

Join-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\TFS\...\packages\jQuery.3.3.1\tools\install.ps1:3 char:14
+ . (Join-Path $toolsPath common.ps1)
+              ~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Join-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : 	ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand
No Scripts folder found

I tried in several new projects, with different versions of JQuery, I understand this is probably not linked to your component but would you have some time to help me ??

Thank you very much,
Claude VERNIER

Unable to use it in my angular 2 project

I get this error when I use it.

Uncaught Error: Cannot find module "."
    at webpackMissingModule (dms.js:7)
    at dmsRe (dms.js:7)
    at Object.<anonymous> (dms.js:12)
    at __webpack_require__ (bootstrap 58d5d4b…:19)
    at Object.<anonymous> (user.ts:17)
    at __webpack_require__ (bootstrap 58d5d4b…:19)
    at Object.<anonymous> (main.js:107510)
    at __webpack_require__ (bootstrap 58d5d4b…:19)
    at Object.<anonymous> (module.js:20)
    at __webpack_require__ (bootstrap 58d5d4b…:19)

Steps

npm install dms-conversion
import DmsCoordinates from "dms-conversion";

Westerley longitudes within 1 degree of Greenwich Meridian are incorrectly positive

Beyond 1 degree West is fine, but under 1 whole degree the output is positive and matches the equivilent Easterly.

var dmsConversion = require("dms-conversion")
var dmsStringsWest = ["0°0′0″ N", "0°59′59″ W"];
var dmsCoordsWest = dmsStringsWest.map(dmsConversion.parseDms);
var dmsStringsEast = ["0°0′0″ N", "0°59′59″ E"];
var dmsCoordsEast = dmsStringsEast.map(dmsConversion.parseDms);
console.log(dmsCoordsWest,dmsCoordsEast);
if (dmsCoordsWest[1] == dmsCoordsEast[1]) {
console.log("East and West co-ordinates have been converted to be equal but are not!");
}

=>
[0, 0.9997222222222222]
"East and West co-ordinates have bee…converted to be equal but are not!"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.