{"size": 1631, "ext": "js", "max_stars_repo_path": "src/measurement/index.src.js", "max_stars_repo_name": "top2017/farmbuild-web-mapping", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/measurement/index.src.js", "max_issues_repo_name": "top2017/farmbuild-web-mapping", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/measurement/index.src.js", "max_forks_repo_name": "top2017/farmbuild-web-mapping", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.6376811594, "max_line_length": 99, "alphanum_fraction": 0.6450030656}
'use strict';

/**
 * webmapping measurement
 * @type {object}
 * @namespace webmapping.measurement
 */

angular.module('farmbuild.webmapping')
	.factory('webMappingMeasurement',
	function (validations,
	          webMappingConverter,
	          $log) {
		var _isDefined = validations.isDefined,
            _googleProjection = 'EPSG:3857',
            _openlayersDefaultProjection = 'EPSG:4326',
			_converter = webMappingConverter;

		function _areas(features) {
			$log.info('calculating area of features ...', features);
			try {
				return turf.area(features) * 0.0001;
			} catch (e) {
				$log.error(e);
			}
		};

		/**
		 * Calculates are of a feature
		 * @method area
		 * @returns {number} area in hectare
		 * @param {!ol.Feature} feature
		 * @memberof webmapping.measurement
		 */
		function _area(feature) {
			$log.info('calculating area of polygon ...', feature);
			feature = _converter.featureToGeoJson(feature, _openlayersDefaultProjection, _googleProjection);
			try {
				return turf.area(feature) * 0.0001;
			} catch (e) {
				$log.error(e);
			}
		};

		/**
		 * Calculates length of a line
		 * @method length
		 * @returns {number} length in metre
		 * @param {!ol.Feature} feature
		 * @memberof webmapping.measurement
		 */
		function _length(feature) {
			$log.info('calculating length of line ...', feature);
			feature = _converter.featureToGeoJson(feature, _openlayersDefaultProjection, _googleProjection);
			try {
				return turf.lineDistance(feature, 'kilometers') * 1000;
			} catch (e) {
				$log.error(e);
			}
		};

		return {
			area: _area,
			areas: _areas,
			length: _length
		}

	});
