本文整理汇总了TypeScript中@turf/helpers.isNumber函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isNumber函数的具体用法?TypeScript isNumber怎么用?TypeScript isNumber使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isNumber函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: containsNumber
export function containsNumber(coordinates: any[]): boolean {
if (coordinates.length > 1 && isNumber(coordinates[0]) && isNumber(coordinates[1])) {
return true;
}
if (Array.isArray(coordinates[0]) && coordinates[0].length) {
return containsNumber(coordinates[0]);
}
throw new Error("coordinates must only contain numbers");
}
示例2: along
/**
* Takes a {@link LineString} and returns a {@link Point} at a specified distance along the line.
*
* @name along
* @param {Feature<LineString>} line input line
* @param {number} distance distance along the line
* @param {Object} [options] Optional parameters
* @param {string} [options.units="kilometers"] can be degrees, radians, miles, or kilometers
* @returns {Feature<Point>} Point `distance` `units` along the line
* @example
* var line = turf.lineString([[-83, 30], [-84, 36], [-78, 41]]);
* var options = {units: 'miles'};
*
* var along = turf.along(line, 200, options);
*
* //addToMap
* var addToMap = [along, line]
*/
function along(line: Feature<LineString>| LineString, distance: number, options: {
units?: Units
} = {}): Feature<Point> {
// Optional parameters
if (!isObject(options)) throw new Error('options is invalid');
// Validation
let coords;
if (line.type === 'Feature') coords = line.geometry.coordinates;
else if (line.type === 'LineString') coords = line.coordinates;
else throw new Error('input must be a LineString Feature or Geometry');
if (!isNumber(distance)) throw new Error('distance must be a number');
let travelled = 0;
for (let i = 0; i < coords.length; i++) {
if (distance >= travelled && i === coords.length - 1) break;
else if (travelled >= distance) {
const overshot = distance - travelled;
if (!overshot) return point(coords[i]);
else {
const direction = bearing(coords[i], coords[i - 1]) - 180;
const interpolated = destination(coords[i], overshot, direction, options);
return interpolated;
}
} else {
travelled += measureDistance(coords[i], coords[i + 1], options);
}
}
return point(coords[coords.length - 1]);
}
示例3: centerMedian
/**
* Takes a {@link FeatureCollection} of points and calculates the median center,
* algorithimically. The median center is understood as the point that is
* requires the least total travel from all other points.
*
* Turfjs has four different functions for calculating the center of a set of
* data. Each is useful depending on circumstance.
*
* `@turf/center` finds the simple center of a dataset, by finding the
* midpoint between the extents of the data. That is, it divides in half the
* farthest east and farthest west point as well as the farthest north and
* farthest south.
*
* `@turf/center-of-mass` imagines that the dataset is a sheet of paper.
* The center of mass is where the sheet would balance on a fingertip.
*
* `@turf/center-mean` takes the averages of all the coordinates and
* produces a value that respects that. Unlike `@turf/center`, it is
* sensitive to clusters and outliers. It lands in the statistical middle of a
* dataset, not the geographical. It can also be weighted, meaning certain
* points are more important than others.
*
* `@turf/center-median` takes the mean center and tries to find, iteratively,
* a new point that requires the least amount of travel from all the points in
* the dataset. It is not as sensitive to outliers as `@turf/center-mean`, but it is
* attracted to clustered data. It, too, can be weighted.
*
* **Bibliography**
*
* Harold W. Kuhn and Robert E. Kuenne, “An Efficient Algorithm for the
* Numerical Solution of the Generalized Weber Problem in Spatial
* Economics,” _Journal of Regional Science_ 4, no. 2 (1962): 21–33,
* doi:{@link https://doi.org/10.1111/j.1467-9787.1962.tb00902.x}.
*
* James E. Burt, Gerald M. Barber, and David L. Rigby, _Elementary
* Statistics for Geographers_, 3rd ed., New York: The Guilford
* Press, 2009, 150–151.
*
* @name centerMedian
* @param {FeatureCollection<any>} features Any GeoJSON Feature Collection
* @param {Object} [options={}] Optional parameters
* @param {string} [options.weight] the property name used to weight the center
* @param {number} [options.tolerance=0.001] the difference in distance between candidate medians at which point the algorighim stops iterating.
* @param {number} [options.counter=10] how many attempts to find the median, should the tolerance be insufficient.
* @returns {Feature<Point>} The median center of the collection
* @example
* var points = turf.points([[0, 0], [1, 0], [0, 1], [5, 8]]);
* var medianCenter = turf.centerMedian(points);
*
* //addToMap
* var addToMap = [points, medianCenter]
*/
function centerMedian(
features: FeatureCollection<any>,
options: { weight?: string, tolerance?: number, counter?: number} = {}
): Feature<Point, {
medianCandidates: Array<Position>,
[key: string]: any
}> {
// 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);
}
示例4: randomLineString
export function randomLineString(count?: number, options: {
bbox?: BBox,
num_vertices?: number,
max_length?: number,
max_rotation?: number,
} = {}): FeatureCollection<LineString, any> {
// Optional parameters
options = options || {};
if (!isObject(options)) { throw new Error("options is invalid"); }
const bbox = options.bbox;
let num_vertices = options.num_vertices;
let max_length = options.max_length;
let max_rotation = options.max_rotation;
if (count === undefined || count === null) { count = 1; }
// Default parameters
if (!isNumber(num_vertices) || num_vertices === undefined || num_vertices < 2) { num_vertices = 10; }
if (!isNumber(max_length) || max_length === undefined) { max_length = 0.0001; }
if (!isNumber(max_rotation) || max_rotation === undefined) { max_rotation = Math.PI / 8; }
const features = [];
for (let i = 0; i < count; i++) {
const startingPoint = randomPosition(bbox);
const vertices = [startingPoint];
for (let j = 0; j < num_vertices - 1; j++) {
const priorAngle = (j === 0) ?
Math.random() * 2 * Math.PI :
Math.tan(
(vertices[j][1] - vertices[j - 1][1]) /
(vertices[j][0] - vertices[j - 1][0]),
);
const angle = priorAngle + (Math.random() - 0.5) * max_rotation * 2;
const distance = Math.random() * max_length;
vertices.push([
vertices[j][0] + distance * Math.cos(angle),
vertices[j][1] + distance * Math.sin(angle),
]);
}
features.push(lineString(vertices));
}
return featureCollection(features);
}
示例5: randomPolygon
export function randomPolygon(count?: number, options: {
bbox?: BBox,
num_vertices?: number,
max_radial_length?: number,
} = {}): FeatureCollection<Polygon, any> {
// Default param
if (count === undefined || count === null) { count = 1; }
if (!isNumber(options.num_vertices) || options.num_vertices === undefined) {
options.num_vertices = 10;
}
if (!isNumber(options.max_radial_length) || options.max_radial_length === undefined) {
options.max_radial_length = 10;
}
const features = [];
for (let i = 0; i < count; i++) {
let vertices: any[] = [];
const circleOffsets = Array.apply(null, new Array(options.num_vertices + 1)).map(Math.random);
// Sum Offsets
circleOffsets.forEach((cur: any, index: number, arr: any[]) => {
arr[index] = (index > 0) ? cur + arr[index - 1] : cur;
});
// scaleOffsets
circleOffsets.forEach((cur: any) => {
cur = cur * 2 * Math.PI / circleOffsets[circleOffsets.length - 1];
const radialScaler = Math.random();
vertices.push([
radialScaler * (options.max_radial_length || 10) * Math.sin(cur),
radialScaler * (options.max_radial_length || 10) * Math.cos(cur),
]);
});
vertices[vertices.length - 1] = vertices[0]; // close the ring
// center the polygon around something
vertices = vertices.map(vertexToCoordinate(randomPosition(options.bbox)));
features.push(polygon([vertices]));
}
return featureCollection(features);
}
示例6: geomEach
geomEach(geojson, function (geom, featureIndex, properties) {
let weight = properties[options.weight];
weight = (weight === undefined || weight === null) ? 1 : weight;
if (!isNumber(weight)) throw new Error('weight value must be a number for feature index ' + featureIndex);
weight = Number(weight);
if (weight > 0) {
coordEach(geom, function (coord) {
sumXs += coord[0] * weight;
sumYs += coord[1] * weight;
sumNs += weight;
});
}
});
示例7: featureEach
featureEach(centroids, function (theCentroid: any) {
var weightValue = theCentroid.properties.weight;
var weight = (weightValue === undefined || weightValue === null) ? 1 : weightValue;
weight = Number(weight);
if (!isNumber(weight)) throw new Error('weight value must be a number');
if (weight > 0) {
centroidCount += 1;
var distanceFromCandidate = weight * distance(theCentroid, candidateMedian);
if (distanceFromCandidate === 0) distanceFromCandidate = 1;
var k = weight / distanceFromCandidate;
candidateXsum += theCentroid.geometry.coordinates[0] * k;
candidateYsum += theCentroid.geometry.coordinates[1] * k;
kSum += k;
}
});
示例8: convert
/**
* Converts a GeoJSON coordinates to the defined `projection`
*
* @private
* @param {GeoJSON} geojson GeoJSON Feature or Geometry
* @param {string} projection defines the projection system to convert the coordinates to
* @param {Object} [options] Optional parameters
* @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
* @returns {GeoJSON} Converted GeoJSON
*/
function convert(geojson: any, projection: string, options: {mutate?: boolean} = {}): any {
// Optional parameters
options = options || {};
var mutate = options.mutate;
// Validation
if (!geojson) throw new Error('geojson is required');
// Handle Position
if (Array.isArray(geojson) && isNumber(geojson[0])) geojson = (projection === 'mercator') ? convertToMercator(geojson) : convertToWgs84(geojson);
// Handle GeoJSON
else {
// Handle possible data mutation
if (mutate !== true) geojson = clone(geojson);
coordEach(geojson, function (coord) {
var newCoord = (projection === 'mercator') ? convertToMercator(coord) : convertToWgs84(coord);
coord[0] = newCoord[0];
coord[1] = newCoord[1];
});
}
return geojson;
}