# How to use @turf/center-mean - 2 common examples

## To help you get started, we’ve selected a few @turf/center-mean examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately. Turfjs / turf / packages / turf-standard-deviational-ellipse / index.js View on Github ``````function standardDeviationalEllipse(points, options) {
// Optional params
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
var steps = options.steps || 64;
var weight = options.weight;
var properties = options.properties || {};

// Validation:
if (!isNumber(steps)) throw new Error('steps must be a number');
if (!isObject(properties)) throw new Error('properties must be a number');

// Calculate mean center:
var meanCenter = centerMean(points, {weight: weight});

// Calculate angle of rotation:
// [X, Y] = mean center of all [x, y].
// theta = arctan( (A + B) / C )
// A = sum((x - X)^2) - sum((y - Y)^2)
// B = sqrt(A^2 + 4(sum((x - X)(y - Y))^2))
// C = 2(sum((x - X)(y - Y)))

var xDeviationSquaredSum = 0;
var yDeviationSquaredSum = 0;
var xyDeviationSum = 0;

featureEach(points, function(point){
xDeviation = getCoords(point) - getCoords(theMeanCenter);
yDeviation = getCoords(point) - getCoords(theMeanCenter);
xDeviationSquaredSum += Math.pow(xDeviation, 2);`````` Turfjs / turf / packages / turf-center-median / index.ts View on Github ``````function centerMedian(
features: FeatureCollection,
options: { weight?: string, tolerance?: number, counter?: number} = {}
): Feature,
[key: string]: any
}&gt; {
// Optional params
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
var counter = options.counter || 10;
if (!isNumber(counter)) throw new Error('counter must be a number');
var weightTerm = options.weight;

// Calculate mean center:
var meanCenter = centerMean(features, {weight: options.weight});

// Calculate center of every feature:
var centroids: any = featureCollection([]);
featureEach(features, function (feature) {
centroids.features.push(centroid(feature, {properties: {weight: feature.properties[weightTerm]}}));
});

centroids.properties = {
tolerance: options.tolerance,
medianCandidates: []
};
return findMedian(meanCenter.geometry.coordinates, [0, 0], centroids, counter);
}``````

## @turf/center-mean

turf center-mean module GitHub MIT Latest version published 2 years ago

74 / 100