All files / geonet-geohash/src decode.js

100% Statements 10/10
100% Branches 3/3
100% Functions 1/1
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                1x                                 26x     26x 26x   26x 14x                   12x 12x   12x       1x  
/** ****************************************************************************************************
 * File: decode.js
 * Project: geohash
 * @author Nick Soggin <iSkore@users.noreply.github.com> on 19-Feb-2019
 *******************************************************************************************************/
'use strict';
 
const
	geohashToBBox = require( './geohashToBBox' );
 
/**
 * decode
 * Decode geohash to latitude/longitude (location is approximate centre of geohash cell, to reasonable precision).
 * Will round to close to centre without excessive precision: ⌊2-log10(Δ°)⌋ decimal places.
 *
 * @param   {string} geohash - Geohash string to be converted to latitude/longitude.
 * @param   {boolean} [calculateErrorOffset=false] - calculate the natural amount of geospatial inaccuracy
 * @returns {{lng,lat}|{lng,lat,error:{lng,lat}}} - Center of geohashed location.
 * @throws  Invalid geohash.
 *
 * @example
 *     const latlng = decode( 'dqbvhgk' ); // latlng: { lng: -77.4804, lat: 38.8662 }
 */
function decode( geohash, calculateErrorOffset = false ) {
	const
		bbox = geohashToBBox( geohash );
 
	let
		lng = ( bbox[ 0 ] + bbox[ 2 ] ) / 2,
		lat = ( bbox[ 1 ] + bbox[ 3 ] ) / 2;
 
	if ( calculateErrorOffset ) {
		return {
			lng, lat,
			error: {
				lng: bbox[ 2 ] - lng,
				lat: bbox[ 3 ] - lat
			}
		};
	}
	else {
		// round to close to centre without excessive precision: ⌊2-log10(Δ°)⌋ decimal places
		lng = +lng.toFixed( ~~( 2 - Math.log( bbox[ 2 ] - bbox[ 0 ] ) / Math.LN10 ) );
		lat = +lat.toFixed( ~~( 2 - Math.log( bbox[ 3 ] - bbox[ 1 ] ) / Math.LN10 ) );
 
		return { lng, lat };
	}
}
 
module.exports = decode;