nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

formats.js (2639B)


      1 function truncToFixed(x, precision) {
      2     let s = x.toString();
      3     let point = s.indexOf('.');
      4     if (point === -1) {
      5         point = s.length;
      6         s += '.';
      7     }
      8     let sliceEnd = point + precision;
      9     if (precision > 0) {
     10         s = s.padEnd(point + precision + 1, '0');
     11         sliceEnd += 1;
     12     }
     13     s = s.slice(0, sliceEnd);
     14     return s;
     15 }
     16 
     17 function formatNumber(value, size, precision = 0) {
     18     if (value < 0) {
     19       throw new Error('Negative values not supported');
     20     }
     21 
     22     if (precision > 0) {
     23         size += 1;
     24     }
     25     return truncToFixed(value, precision).padStart(size + precision, '0');
     26 }
     27 
     28 function coordinatePresentations(coordinate, isLat) {
     29     const degrees = Math.abs(coordinate);
     30     const intDegrees = Math.floor(degrees);
     31     const minutes = (degrees - intDegrees) * 60;
     32     const intMinutes = Math.floor(minutes);
     33     const seconds = (minutes - intMinutes) * 60;
     34 
     35     let direction;
     36     if (isLat) {
     37         direction = (coordinate < 0) ? 'S' : 'N';
     38     } else {
     39         direction = (coordinate < 0) ? 'W' : 'E';
     40     }
     41 
     42     return {
     43         signedDegrees: coordinate.toFixed(5),
     44         degrees: formatNumber(degrees, 0, 5),
     45         intDegrees: formatNumber(intDegrees, 0),
     46         minutes: formatNumber(minutes, 2, 3),
     47         intMinutes: formatNumber(intMinutes, 2),
     48         seconds: formatNumber(seconds, 2, 2),
     49         direction
     50     };
     51 }
     52 
     53 function formatLatLng(latlng, format) {
     54     return {
     55         lat: format.formatter(coordinatePresentations(latlng.lat, true)),
     56         lng: format.formatter(coordinatePresentations(latlng.lng, false))
     57     };
     58 }
     59 
     60 const SIGNED_DEGREES = {
     61     code: 'd',
     62     label: '±ddd.ddddd',
     63     wrapperClass: 'leaflet-coordinates-wrapper-signed-degrees',
     64     formatter: ({signedDegrees}) => signedDegrees
     65 };
     66 
     67 const DEGREES = {
     68     code: 'D',
     69     label: 'ddd.ddddd°',
     70     wrapperClass: 'leaflet-coordinates-wrapper-degrees',
     71     formatter: ({degrees, direction}) => `${direction} ${degrees}°`
     72 };
     73 
     74 const DEGREES_AND_MINUTES = {
     75     code: 'DM',
     76     label: 'ddd°mm.mmm′',
     77     wrapperClass: 'leaflet-coordinates-wrapper-degrees-and-minutes',
     78     formatter: ({intDegrees, minutes, direction}) => `${direction} ${intDegrees}°${minutes}′`
     79 };
     80 
     81 const DEGREES_AND_MINUTES_AND_SECONDS = {
     82     code: 'DMS',
     83     label: 'ddd°mm′ss.s″',
     84     wrapperClass: 'leaflet-coordinates-wrapper-degrees-and-minutes-and-seconds',
     85     formatter: ({intDegrees, intMinutes, seconds, direction}) => `${direction} ${intDegrees}°${intMinutes}′${seconds}″`
     86 };
     87 
     88 export {
     89     SIGNED_DEGREES,
     90     DEGREES,
     91     DEGREES_AND_MINUTES,
     92     DEGREES_AND_MINUTES_AND_SECONDS,
     93     formatLatLng
     94 };