src/cdist.js
/**
 * Generates distance from origin to each point
 *
 * @param	{Object}	origin	GeoJSON point representing the origin
 * @param	{Object}	pgrid	GeoJSON FeatureCollection of points
 * @param	{Object}	options 
 * @return {Object}	pgrid with distance metrics assigned
 */
Generates distance from origin to each point
@param       {Object}        origin  GeoJSON point representing the origin
@param       {Object}        pgrid   GeoJSON FeatureCollection of points
@param       {Object}        options
@return {Object}     pgrid with distance metrics assigned
Generates distance from origin to each point @param { Object } origin GeoJSON point representing the origin @param { Object } pgrid GeoJSON FeatureCollection of points @param { Object } options @return { Object } pgrid with distance metrics assigned
async function cdist(origin, pgrid, options) {
	const chunkOptions = {
		chunkSize: 1000,
		delay: 0
	};
	const chunks = _.chunk(pgrid.features, chunkOptions.chunkSize);
	async function _single(feature) {
		const coordinates = [
			[ origin.coordinates[1], origin.coordinates[0] ],
			[ feature.geometry.coordinates[1], feature.geometry.coordinates[0] ]
		];
		const option = {
			coordinates,
			json: true 
		};
		try {
			let result;
			switch(options.provider) {
				case 'osrm':
					result = await routeOSRM(option, options);
					break;
				case 'valhalla':
					result = await routeValhalla(option, options);
					break;
			}
			feature.properties.distance = (result && result.routes.length > 0) ? result.routes[0].distance * 0.001 : Number.MAX_VALUE;
		} catch (error) {
		}
	}
	for (let i = 0; i < chunks.length; i++) {
		await Promise.all(chunks[i].map(_single));
		log(`Computing distances: ${(i / chunks.length * 100).toFixed(2)}%`);
	}
	log.success('Computing distances');
	return pgrid;
}
async function cdist ( origin , pgrid , options ) { const chunkOptions = { chunkSize : 1000 , delay : 0 } ; const chunks = _ . chunk ( pgrid . features , chunkOptions . chunkSize ) ; async function _single ( feature ) { const coordinates = [ [ origin . coordinates [ 1 ] , origin . coordinates [ 0 ] ] , [ feature . geometry . coordinates [ 1 ] , feature . geometry . coordinates [ 0 ] ] ] ; const option = { coordinates , json : true } ; try { let result ; switch ( options . provider ) { case 'osrm' : result = await routeOSRM ( option , options ) ; break ; case 'valhalla' : result = await routeValhalla ( option , options ) ; break ; } feature . properties . distance = ( result && result . routes . length > 0 ) ? result . routes [ 0 ] . distance * 0.001 : Number . MAX_VALUE ; } catch ( error ) { } } for ( let i = 0 ; i < chunks . length ; i ++ ) { await Promise . all ( chunks [ i ] . map ( _single ) ) ; log ( ` ${ ( i / chunks . length * 100 ) . toFixed ( 2 ) } ` ) ; } log . success ( 'Computing distances' ) ; return pgrid ; }
Generates distance from origin to each point
 @param       {Object}        origin  GeoJSON point representing the origin
 @param       {Object}        pgrid   GeoJSON FeatureCollection of points
 @param       {Object}        options
 @return {Object}     pgrid with distance metrics assigned
Generates distance from origin to each point @param { Object } origin GeoJSON point representing the origin @param { Object } pgrid GeoJSON FeatureCollection of points @param { Object } options @return { Object } pgrid with distance metrics assigned
/**
	 * Default option values
	 */ /**
	 * Separate into chunks
	 */ /**
	 * Create the mapping function
	 */ // Automatically parses the JSON string in the response // console.log(error); /**
	 * Process each chunk
	 */