IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Décimale en Degrés Minutes Secondes


Sujet :

JavaScript

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    320
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 320
    Points : 79
    Points
    79
    Par défaut Décimale en Degrés Minutes Secondes
    Bonsoir,
    Je rencontre un soucis avec ce code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
      var d_lat = 15.876868
      var NS = (d_lat>=0)?'N':'S'
    function dms(d_lat) {
      var d = Math.trunc(d_lat); 
      var x = (d_lat - d) * 60; // minutes
      var m = Math.trunc(x);
      var y = (x - m) * 60; // secondes
      s = Math.trunc(y);
      return d + '°' + NS + ' ' + m + "'" + s +'"'; 
    }
    dms(d_lat); // 15°N 52'36"
    Si d_lat est positive ( Latitude Nord ) aucun problème ça fait le job en revanche si d_lat est négative ( latitude Sud ) la j'ai des (-) partout ...

    Je souhaite que d_lat=15.876868 me donne 15°N 52'36" et que d_lat=-15.876868 me donne 15°S 52'36" ...

    Il existe une fonction qui fait le job d'un Math.trunc.abs ?

    Merci
    Cdlt

  2. #2
    Membre émérite
    Avatar de badaze
    Homme Profil pro
    Chef de projets info
    Inscrit en
    Septembre 2002
    Messages
    1 412
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Chef de projets info
    Secteur : Transports

    Informations forums :
    Inscription : Septembre 2002
    Messages : 1 412
    Points : 2 522
    Points
    2 522
    Par défaut
    Comme ça c'est bon ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    function dms(d_lat) {
      var NS = (d_lat>=0)?'N':'S'
      var d = Math.trunc(d_lat); 
      var x = (d_lat - d) * 60; // minutes
      var m = Math.trunc(x);
      var y = (x - m) * 60; // secondes
      s = Math.trunc(y);
      return Math.abs(d) + '°' + NS + ' ' + Math.abs(m) + "'" + Math.abs(s) +'"'; 
    }
    Cela ne sert à rien d'optimiser quelque chose qui ne fonctionne pas.

    Mon site : www.emmella.fr

    Je recherche le manuel de l'Olivetti Logos 80B.

  3. #3
    Rédacteur

    Avatar de danielhagnoul
    Homme Profil pro
    Étudiant perpétuel
    Inscrit en
    Février 2009
    Messages
    6 389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 73
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant perpétuel
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2009
    Messages : 6 389
    Points : 22 933
    Points
    22 933
    Billets dans le blog
    125
    Par défaut


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    let
      d_lat = -15.876868,
      NS = ( d_lat >= 0 ) ? 'N' : 'S',
      d, x, m, y, s;
     
    function dms( d_lat ){
      d = Math.trunc( d_lat );
      x = Math.abs( d_lat - d ) * 60; // minutes
      m = Math.trunc( x );
      y = ( x - m ) * 60; // secondes
      s = Math.trunc( y );
      return d + '°' + NS + ' ' + m + "'" + s +'"'; 
    }
     
    console.log( dms( d_lat ) ); // -15°N 52'36"
    Je vous conseille https://github.com/perfectline/geopoint

    Exemple :

    Code HTML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    <!DOCTYPE html>
    <html lang="fr" dir="ltr">
    <head>
      <meta http-equiv="cache-control" content="public, max-age=60">
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <meta name="author" content="Daniel Hagnoul">
      <title>Test</title>
      <style>
        
      </style>
      <script>
        'use strict';
        
        let GeoPoint = function(lon, lat) {
          switch (typeof(lon)) {
            case 'number':
              this.lonDeg = this.dec2deg(lon, this.MAX_LON);
              this.lonDec = lon;
              break;
            case 'string':
              if (this.decode(lon)) {
                this.lonDeg = lon;
              }
              this.lonDec = this.deg2dec(lon, this.MAX_LON);
              break;
          }
        
          switch (typeof(lat)) {
            case 'number':
              this.latDeg = this.dec2deg(lat, this.MAX_LAT);
              this.latDec = lat;
              break;
            case 'string':
              if (this.decode(lat)) {
                this.latDeg = lat;
              }
        
              this.latDec = this.deg2dec(lat, this.MAX_LAT);
              break;
          }
        };
        
        GeoPoint.prototype = {
            CHAR_DEG : "\u00B0",
            CHAR_MIN : "\u0027",
            CHAR_SEC : "\u0022",
            CHAR_SEP : "\u0020",
        
            MAX_LON: 180,
            MAX_LAT: 90,
        
            // decimal
            lonDec: NaN,
            latDec: NaN,
        
            // degrees
            lonDeg: NaN,
            latDeg: NaN,
        
            dec2deg: function(value, max) {
              var sign = value < 0 ? -1 : 1;
        
              var abs = Math.abs(Math.round(value * 1000000));
        
              if (abs > (max * 1000000)) {
                return NaN;
              }
        
              var dec = abs % 1000000 / 1000000;
              var deg = Math.floor(abs / 1000000) * sign;
              var min = Math.floor(dec * 60);
              var sec = (dec - min / 60) * 3600;
        
              var result = "";
        
              result += deg;
              result += this.CHAR_DEG;
              result += this.CHAR_SEP;
              result += min;
              result += this.CHAR_MIN;
              result += this.CHAR_SEP;
              result += sec.toFixed(2);
              result += this.CHAR_SEC;
        
              return result;
            },
        
            deg2dec: function(value) {
              var matches = this.decode(value);
        
              if (!matches) {
                return NaN;
              }
        
              var deg = parseFloat(matches[1]);
              var min = parseFloat(matches[2]);
              var sec = parseFloat(matches[3]);
        
              if (isNaN(deg) || isNaN(min) || isNaN(sec)) {
                return NaN;
              }
        
              return deg + (min / 60.0) + (sec / 3600);
            },
        
            decode: function(value) {
              var pattern = "";
        
              // deg
              pattern += "(-?\\d+)";
              pattern += this.CHAR_DEG;
              pattern += "\\s*";
        
              // min
              pattern += "(\\d+)";
              pattern += this.CHAR_MIN;
              pattern += "\\s*";
        
              // sec
              pattern += "(\\d+(?:\\.\\d+)?)";
              pattern += this.CHAR_SEC;
        
              return value.match(new RegExp(pattern));
            },
            getLonDec: function() {
              return this.lonDec;
            },
            getLatDec: function() {
              return this.latDec;
            },
            getLonDeg: function() {
              return this.lonDeg;
            },
            getLatDeg: function() {
              return this.latDeg;
            }
        };
     
        document.addEventListener( 'DOMContentLoaded', ev => {
          
        }, false );
        
        window.addEventListener( 'load', ev => {
          
          {
            var lon = 24.0;
            var lat = 15.876868;
            
            var point = new GeoPoint(lon, lat);
            
            console.log(point.getLonDeg()); // 24° 0' 0.00"
            console.log(point.getLatDeg()); // 15° 52' 36.72"
          }
          
          {
            var lon = 24.0;
            var lat = -15.876868;
            
            var point = new GeoPoint(lon, lat);
            
            console.log(point.getLonDeg()); // 24° 0' 0.00"
            console.log(point.getLatDeg()); // -15° 52' 36.72"
          }
     
     
        }, false );
      </script>
    </head>
    <body>
      <main>
     
     
      </main>
    </body>
    </html>

    Blog

    Sans l'analyse et la conception, la programmation est l'art d'ajouter des bogues à un fichier texte vide.
    (Louis Srygley : Without requirements or design, programming is the art of adding bugs to an empty text file.)

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    320
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 320
    Points : 79
    Points
    79
    Par défaut
    Citation Envoyé par badaze Voir le message
    Comme ça c'est bon ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    function dms(d_lat) {
      var NS = (d_lat>=0)?'N':'S'
      var d = Math.trunc(d_lat); 
      var x = (d_lat - d) * 60; // minutes
      var m = Math.trunc(x);
      var y = (x - m) * 60; // secondes
      s = Math.trunc(y);
      return Math.abs(d) + '°' + NS + ' ' + Math.abs(m) + "'" + Math.abs(s) +'"'; 
    }
    Merci badaze encore une fois c'est juste parfait ( et beaucoup plus clair que mon brouillon du dessus ... )

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    320
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 320
    Points : 79
    Points
    79
    Par défaut
    Citation Envoyé par danielhagnoul Voir le message


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    let
      d_lat = -15.876868,
      NS = ( d_lat >= 0 ) ? 'N' : 'S',
      d, x, m, y, s;
     
    function dms( d_lat ){
      d = Math.trunc( d_lat );
      x = Math.abs( d_lat - d ) * 60; // minutes
      m = Math.trunc( x );
      y = ( x - m ) * 60; // secondes
      s = Math.trunc( y );
      return d + '°' + NS + ' ' + m + "'" + s +'"'; 
    }
     
    console.log( dms( d_lat ) ); // -15°N 52'36"
    Je vous conseille https://github.com/perfectline/geopoint

    Exemple :

    Code HTML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    <!DOCTYPE html>
    <html lang="fr" dir="ltr">
    <head>
      <meta http-equiv="cache-control" content="public, max-age=60">
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <meta name="author" content="Daniel Hagnoul">
      <title>Test</title>
      <style>
        
      </style>
      <script>
        'use strict';
        
        let GeoPoint = function(lon, lat) {
          switch (typeof(lon)) {
            case 'number':
              this.lonDeg = this.dec2deg(lon, this.MAX_LON);
              this.lonDec = lon;
              break;
            case 'string':
              if (this.decode(lon)) {
                this.lonDeg = lon;
              }
              this.lonDec = this.deg2dec(lon, this.MAX_LON);
              break;
          }
        
          switch (typeof(lat)) {
            case 'number':
              this.latDeg = this.dec2deg(lat, this.MAX_LAT);
              this.latDec = lat;
              break;
            case 'string':
              if (this.decode(lat)) {
                this.latDeg = lat;
              }
        
              this.latDec = this.deg2dec(lat, this.MAX_LAT);
              break;
          }
        };
        
        GeoPoint.prototype = {
            CHAR_DEG : "\u00B0",
            CHAR_MIN : "\u0027",
            CHAR_SEC : "\u0022",
            CHAR_SEP : "\u0020",
        
            MAX_LON: 180,
            MAX_LAT: 90,
        
            // decimal
            lonDec: NaN,
            latDec: NaN,
        
            // degrees
            lonDeg: NaN,
            latDeg: NaN,
        
            dec2deg: function(value, max) {
              var sign = value < 0 ? -1 : 1;
        
              var abs = Math.abs(Math.round(value * 1000000));
        
              if (abs > (max * 1000000)) {
                return NaN;
              }
        
              var dec = abs % 1000000 / 1000000;
              var deg = Math.floor(abs / 1000000) * sign;
              var min = Math.floor(dec * 60);
              var sec = (dec - min / 60) * 3600;
        
              var result = "";
        
              result += deg;
              result += this.CHAR_DEG;
              result += this.CHAR_SEP;
              result += min;
              result += this.CHAR_MIN;
              result += this.CHAR_SEP;
              result += sec.toFixed(2);
              result += this.CHAR_SEC;
        
              return result;
            },
        
            deg2dec: function(value) {
              var matches = this.decode(value);
        
              if (!matches) {
                return NaN;
              }
        
              var deg = parseFloat(matches[1]);
              var min = parseFloat(matches[2]);
              var sec = parseFloat(matches[3]);
        
              if (isNaN(deg) || isNaN(min) || isNaN(sec)) {
                return NaN;
              }
        
              return deg + (min / 60.0) + (sec / 3600);
            },
        
            decode: function(value) {
              var pattern = "";
        
              // deg
              pattern += "(-?\\d+)";
              pattern += this.CHAR_DEG;
              pattern += "\\s*";
        
              // min
              pattern += "(\\d+)";
              pattern += this.CHAR_MIN;
              pattern += "\\s*";
        
              // sec
              pattern += "(\\d+(?:\\.\\d+)?)";
              pattern += this.CHAR_SEC;
        
              return value.match(new RegExp(pattern));
            },
            getLonDec: function() {
              return this.lonDec;
            },
            getLatDec: function() {
              return this.latDec;
            },
            getLonDeg: function() {
              return this.lonDeg;
            },
            getLatDeg: function() {
              return this.latDeg;
            }
        };
     
        document.addEventListener( 'DOMContentLoaded', ev => {
          
        }, false );
        
        window.addEventListener( 'load', ev => {
          
          {
            var lon = 24.0;
            var lat = 15.876868;
            
            var point = new GeoPoint(lon, lat);
            
            console.log(point.getLonDeg()); // 24° 0' 0.00"
            console.log(point.getLatDeg()); // 15° 52' 36.72"
          }
          
          {
            var lon = 24.0;
            var lat = -15.876868;
            
            var point = new GeoPoint(lon, lat);
            
            console.log(point.getLonDeg()); // 24° 0' 0.00"
            console.log(point.getLatDeg()); // -15° 52' 36.72"
          }
     
     
        }, false );
      </script>
    </head>
    <body>
      <main>
     
     
      </main>
    </body>
    </html>
    Merci danielhagnoul ( ça devient beaucoup trop compliquer pour mon petit niveau ...)

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [GPS] convertion de degré.décimal et degré.minute
    Par pierrot10 dans le forum Débuter
    Réponses: 1
    Dernier message: 02/10/2015, 00h55
  2. Fonction conversion Données décimales en Degré,Minutes,Secondes
    Par NeoGeekette dans le forum Général JavaScript
    Réponses: 7
    Dernier message: 16/08/2014, 13h40
  3. Minutes décimales -> minutes:secondes
    Par Jay-P dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 18/06/2010, 13h35
  4. [Mappy] Afficher le format des latitudes et longitudes WGS (decimal) en degré minute et seconde
    Par nakata77 dans le forum Bibliothèques & Frameworks
    Réponses: 0
    Dernier message: 28/05/2010, 16h59
  5. Formater une durée sous la forme Heure:Minute:Seconde
    Par marsupile dans le forum C++Builder
    Réponses: 2
    Dernier message: 31/01/2004, 23h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo