All files / geonet-geohash/src getBBoxStartingPoint.js

100% Statements 15/15
100% Branches 1/1
100% Functions 1/1
100% Lines 15/15

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                1x 1x 1x 1x                                   14x 14x 14x 14x 14x 14x 14x 14x 14x   14x             1x  
/** ****************************************************************************************************
 * File: getBBoxStartingPoint.js
 * Project: geohash
 * @author Nick Soggin <iSkore@users.noreply.github.com> on 19-Feb-2019
 *******************************************************************************************************/
'use strict';
 
const
	decode          = require( './decode' ),
	encode          = require( './encode' ),
	geohashToBBox   = require( './geohashToBBox' ),
	{ ENCODE_AUTO } = require( './variables' );
 
/**
 * getBBoxStartingPoint
 *
 * Beginning operations for generating a geohash bbox.
 * Get the geohash for the southwest corner of a bbox and how many hash steps there are to the northeast corner.
 *
 * @param {number} minLng - minimum longitude
 * @param {number} minLat - minimum latitude
 * @param {number} maxLng - maximum longitude
 * @param {number} maxLat - maximum latitude
 * @param {number} [precision=ENCODE_AUTO] - geohash precision
 * @returns {{hashSouthWest: string, lngStep: number, latStep: number}}
 * southwest hash and how many lng/lat steps to the northeast hash
 */
function getBBoxStartingPoint( minLng, minLat, maxLng, maxLat, precision = ENCODE_AUTO ) {
	const
		hashSouthWest = encode( minLng, minLat, precision ),
		hashNorthEast = encode( maxLng, maxLat, precision ),
		latLng        = decode( hashSouthWest, true ),
		perLng        = latLng.error.lng * 2,
		perLat        = latLng.error.lat * 2,
		boxSouthWest  = geohashToBBox( hashSouthWest ),
		boxNorthEast  = geohashToBBox( hashNorthEast ),
		lngStep       = Math.round( ( boxNorthEast[ 0 ] - boxSouthWest[ 0 ] ) / perLng ),
		latStep       = Math.round( ( boxNorthEast[ 1 ] - boxSouthWest[ 1 ] ) / perLat );
 
	return {
		latStep,
		lngStep,
		hashSouthWest
	};
}
 
module.exports = getBBoxStartingPoint;