本文整理匯總了TypeScript中@turf/centroid.default函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript default函數的具體用法?TypeScript default怎麽用?TypeScript default使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了default函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: switch
/**
* Takes any {@link Feature} or a {@link FeatureCollection} and returns its [center of mass](https://en.wikipedia.org/wiki/Center_of_mass) using this formula: [Centroid of Polygon](https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon).
*
* @name centerOfMass
* @param {GeoJSON} geojson GeoJSON to be centered
* @param {Object} [options={}] Optional Parameters
* @param {Object} [options.properties={}] Translate Properties to Feature
* @returns {Feature<Point>} the center of mass
* @example
* var polygon = turf.polygon([[[-81, 41], [-88, 36], [-84, 31], [-80, 33], [-77, 39], [-81, 41]]]);
*
* var center = turf.centerOfMass(polygon);
*
* //addToMap
* var addToMap = [polygon, center]
*/
function centerOfMass<P = Properties>(geojson: any, options: {
properties?: P,
} = {}): Feature<Point, P> {
switch (getType(geojson)) {
case 'Point':
return point(getCoord(geojson), options.properties);
case 'Polygon':
var coords = [];
coordEach(geojson, function (coord) {
coords.push(coord);
});
// First, we neutralize the feature (set it around coordinates [0,0]) to prevent rounding errors
// We take any point to translate all the points around 0
var centre = centroid(geojson, {properties: options.properties});
var translation = centre.geometry.coordinates;
var sx = 0;
var sy = 0;
var sArea = 0;
var i, pi, pj, xi, xj, yi, yj, a;
var neutralizedPoints = coords.map(function (point) {
return [
point[0] - translation[0],
point[1] - translation[1]
];
});
for (i = 0; i < coords.length - 1; i++) {
// pi is the current point
pi = neutralizedPoints[i];
xi = pi[0];
yi = pi[1];
// pj is the next point (pi+1)
pj = neutralizedPoints[i + 1];
xj = pj[0];
yj = pj[1];
// a is the common factor to compute the signed area and the final coordinates
a = xi * yj - xj * yi;
// sArea is the sum used to compute the signed area
sArea += a;
// sx and sy are the sums used to compute the final coordinates
sx += (xi + xj) * a;
sy += (yi + yj) * a;
}
// Shape has no area: fallback on turf.centroid
if (sArea === 0) {
return centre;
} else {
// Compute the signed area, and factorize 1/6A
var area = sArea * 0.5;
var areaFactor = 1 / (6 * area);
// Compute the final coordinates, adding back the values that have been neutralized
return point([
translation[0] + areaFactor * sx,
translation[1] + areaFactor * sy
], options.properties);
}
default:
// Not a polygon: Compute the convex hull and work with that
var hull = convex(geojson);
if (hull) return centerOfMass(hull, {properties: options.properties});
// Hull is empty: fallback on the centroid
else return centroid(geojson, {properties: options.properties});
}
}
示例2: directionalMean
/**
* @typedef {Object} DirectionalMeanLine
* @property {number} cartesianAngle the mean angle of all lines. (measure from due earth counterclockwise).
* @property {number} bearingAngle the mean angle of all lines. (bearing).
* @property {number} circularVariance the extent to which features all point in the same direction.
* the value ranges 0-1, the bigger the value, the more variation in directions between lines.
* @property {number} averageX the centroid of all lines.
* @property {number} averageY the centroid of all line.
* @property {number} averageLength the average length of line.
* @property {number} countOfLines the count of features.
*/
/**
* This module calculate the average angle of a set of lines, measuring the trend of it.
* It can be used in both project coordinate system and geography coordinate system.
* It can handle segments of line or the whole line.
* @name directionalMean
* @param {FeatureCollection<LineString>} lines
* @param {object} [options={}]
* @param {boolean} [options.planar=true] whether the spatial reference system is projected or geographical.
* @param {boolean} [options.segment=false] whether treat a LineString as a whole or a set of segments.
* @returns {DirectionalMeanLine} Directional Mean Line
* @example
*
* var lines = turf.lineStrings([
* [[110, 45], [120, 50]],
* [[100, 50], [115, 55]],
* ])
* var directionalMeanLine = turf.directionalMean(lines);
* // => directionalMeanLine
*/
export default function directionalMean(lines: FeatureCollection<LineString>, options: {
planar?: boolean;
segment?: boolean;
} = {}): DirectionalMeanLine {
const isPlanar: boolean = !!options.planar; // you can't use options.planar || true here.
const isSegment: boolean = options.segment || false;
let sigmaSin: number = 0;
let sigmaCos: number = 0;
let countOfLines: number = 0;
let sumOfLen: number = 0;
const centroidList: Array<Feature<Point>> = [];
if (isSegment) {
segmentEach(lines, (currentSegment: any) => { // todo fix turf-meta's declaration file
const [sin1, cos1]: [number, number] = getCosAndSin(currentSegment.geometry.coordinates, isPlanar);
const lenOfLine = getLengthOfLineString(currentSegment, isPlanar);
if (isNaN(sin1) || isNaN(cos1)) {
return;
} else {
sigmaSin += sin1;
sigmaCos += cos1;
countOfLines += 1;
sumOfLen += lenOfLine;
centroidList.push(centroid(currentSegment));
}
});
// planar and segment
} else {
// planar and non-segment
featureEach(lines, (currentFeature: Feature<LineString>, featureIndex: number) => {
if (currentFeature.geometry.type !== "LineString") {
throw new Error("shold to support MultiLineString?");
}
const [sin1, cos1]: [number, number] = getCosAndSin(currentFeature.geometry.coordinates, isPlanar);
const lenOfLine = getLengthOfLineString(currentFeature, isPlanar);
if (isNaN(sin1) || isNaN(cos1)) {
return;
} else {
sigmaSin += sin1;
sigmaCos += cos1;
countOfLines += 1;
sumOfLen += lenOfLine;
centroidList.push(centroid(currentFeature));
}
});
}
const cartesianAngle: number = getAngleBySinAndCos(sigmaSin, sigmaCos);
const bearingAngle: number = bearingToCartesian(cartesianAngle);
const circularVariance = getCircularVariance(sigmaSin, sigmaCos, countOfLines);
const averageLength = sumOfLen / countOfLines;
const centroidOfLines = centroid(featureCollection(centroidList));
const [averageX, averageY]: number[] = getCoord(centroidOfLines);
let meanLinestring;
if (isPlanar) {
meanLinestring = getMeanLineString([averageX, averageY], cartesianAngle, averageLength, isPlanar);
} else {
meanLinestring = getMeanLineString([averageX, averageY], bearingAngle, averageLength, isPlanar);
}
return lineString(meanLinestring, {
averageLength,
averageX,
averageY,
bearingAngle,
cartesianAngle,
circularVariance,
countOfLines,
//.........這裏部分代碼省略.........
示例3: segmentEach
segmentEach(lines, (currentSegment: any) => { // todo fix turf-meta's declaration file
const [sin1, cos1]: [number, number] = getCosAndSin(currentSegment.geometry.coordinates, isPlanar);
const lenOfLine = getLengthOfLineString(currentSegment, isPlanar);
if (isNaN(sin1) || isNaN(cos1)) {
return;
} else {
sigmaSin += sin1;
sigmaCos += cos1;
countOfLines += 1;
sumOfLen += lenOfLine;
centroidList.push(centroid(currentSegment));
}
});
示例4: Error
featureEach(lines, (currentFeature: Feature<LineString>, featureIndex: number) => {
if (currentFeature.geometry.type !== "LineString") {
throw new Error("shold to support MultiLineString?");
}
const [sin1, cos1]: [number, number] = getCosAndSin(currentFeature.geometry.coordinates, isPlanar);
const lenOfLine = getLengthOfLineString(currentFeature, isPlanar);
if (isNaN(sin1) || isNaN(cos1)) {
return;
} else {
sigmaSin += sin1;
sigmaCos += cos1;
countOfLines += 1;
sumOfLen += lenOfLine;
centroidList.push(centroid(currentFeature));
}
});
示例5: featureEach
featureEach(dataset, (feature) => {
features.push(centroid(feature));
});