本文整理汇总了TypeScript中@turf/meta.segmentEach函数的典型用法代码示例。如果您正苦于以下问题:TypeScript segmentEach函数的具体用法?TypeScript segmentEach怎么用?TypeScript segmentEach使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了segmentEach函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: booleanOverlap
/**
* Compares two geometries of the same dimension and returns true if their intersection set results in a geometry
* different from both but of the same dimension. It applies to Polygon/Polygon, LineString/LineString,
* Multipoint/Multipoint, MultiLineString/MultiLineString and MultiPolygon/MultiPolygon.
*
* @name booleanOverlap
* @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} feature1 input
* @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} feature2 input
* @returns {boolean} true/false
* @example
* var poly1 = turf.polygon([[[0,0],[0,5],[5,5],[5,0],[0,0]]]);
* var poly2 = turf.polygon([[[1,1],[1,6],[6,6],[6,1],[1,1]]]);
* var poly3 = turf.polygon([[[10,10],[10,15],[15,15],[15,10],[10,10]]]);
*
* turf.booleanOverlap(poly1, poly2)
* //=true
* turf.booleanOverlap(poly2, poly3)
* //=false
*/
export default function booleanOverlap(
feature1: Feature<any> | Geometry,
feature2: Feature<any> | Geometry,
): boolean {
const geom1 = getGeom(feature1);
const geom2 = getGeom(feature2);
const type1 = geom1.type;
const type2 = geom2.type;
if (type1 !== type2) throw new Error('features must be of the same type');
if (type1 === 'Point') throw new Error('Point geometry not supported');
// features must be not equal
const equality = new GeojsonEquality({precision: 6});
if (equality.compare(feature1, feature2)) return false;
let overlap = 0;
switch (type1) {
case 'MultiPoint':
const coords1 = coordAll(feature1);
const coords2 = coordAll(feature2);
coords1.forEach((coord1) => {
coords2.forEach((coord2) => {
if (coord1[0] === coord2[0] && coord1[1] === coord2[1]) overlap++;
});
});
break;
case 'LineString':
case 'MultiLineString':
segmentEach(feature1, (segment1) => {
segmentEach(feature2, (segment2) => {
if (lineOverlap(segment1, segment2).features.length) overlap++;
});
});
break;
case 'Polygon':
case 'MultiPolygon':
segmentEach(feature1, (segment1) => {
segmentEach(feature2, (segment2) => {
if (lineIntersect(segment1, segment2).features.length) overlap++;
});
});
break;
}
return overlap > 0;
}
示例2: pointToLineDistance
/**
* Returns the minimum distance between a {@link Point} and a {@link LineString}, being the distance from a line the
* minimum distance between the point and any segment of the `LineString`.
*
* @name pointToLineDistance
* @param {Feature<Point>|Array<number>} pt Feature or Geometry
* @param {Feature<LineString>} line GeoJSON Feature or Geometry
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units="kilometers"] can be anything supported by turf/convertLength
* (ex: degrees, radians, miles, or kilometers)
* @param {string} [options.method="geodesic"] wether to calculate the distance based on geodesic (spheroid) or
* planar (flat) method. Valid options are 'geodesic' or 'planar'.
* @returns {number} distance between point and line
* @example
* var pt = turf.point([0, 0]);
* var line = turf.lineString([[1, 1],[-1, 1]]);
*
* var distance = turf.pointToLineDistance(pt, line, {units: 'miles'});
* //=69.11854715938406
*/
function pointToLineDistance(pt: Coord, line: Feature<LineString> | LineString, options: {
units?: Units,
method?: "geodesic" | "planar",
} = {}): number {
// Optional parameters
if (!options.method) { options.method = "geodesic"; }
if (!options.units) { options.units = "kilometers"; }
// validation
if (!pt) { throw new Error("pt is required"); }
if (Array.isArray(pt)) { pt = point(pt);
} else if (pt.type === "Point") { pt = feature(pt);
} else { featureOf(pt, "Point", "point"); }
if (!line) { throw new Error("line is required"); }
if (Array.isArray(line)) { line = lineString(line);
} else if (line.type === "LineString") { line = feature(line);
} else { featureOf(line, "LineString", "line"); }
let distance = Infinity;
const p = pt.geometry.coordinates;
segmentEach(line, (segment) => {
const a = segment!.geometry.coordinates[0];
const b = segment!.geometry.coordinates[1];
const d = distanceToSegment(p, a, b, options);
if (d < distance) { distance = d; }
});
return convertLength(distance, "degrees", options.units);
}
示例3: booleanOverlap
/**
* Compares two geometries of the same dimension and returns true if their intersection set results in a geometry
* different from both but of the same dimension. It applies to Polygon/Polygon, LineString/LineString,
* Multipoint/Multipoint, MultiLineString/MultiLineString and MultiPolygon/MultiPolygon.
*
* @name booleanOverlap
* @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} feature1 input
* @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} feature2 input
* @returns {boolean} true/false
* @example
* var poly1 = turf.polygon([[[0,0],[0,5],[5,5],[5,0],[0,0]]]);
* var poly2 = turf.polygon([[[1,1],[1,6],[6,6],[6,1],[1,1]]]);
* var poly3 = turf.polygon([[[10,10],[10,15],[15,15],[15,10],[10,10]]]);
*
* turf.booleanOverlap(poly1, poly2)
* //=true
* turf.booleanOverlap(poly2, poly3)
* //=false
*/
function booleanOverlap(feature1: Feature<any> | Geometry, feature2: Feature<any> | Geometry): boolean {
// validation
if (!feature1) throw new Error('feature1 is required');
if (!feature2) throw new Error('feature2 is required');
var type1 = getType(feature1);
var type2 = getType(feature2);
if (type1 !== type2) throw new Error('features must be of the same type');
if (type1 === 'Point') throw new Error('Point geometry not supported');
// features must be not equal
var equality = new GeojsonEquality({precision: 6});
if (equality.compare(feature1, feature2)) return false;
var overlap = 0;
switch (type1) {
case 'MultiPoint':
var coords1 = coordAll(feature1);
var coords2 = coordAll(feature2);
coords1.forEach(function (coord1) {
coords2.forEach(function (coord2) {
if (coord1[0] === coord2[0] && coord1[1] === coord2[1]) overlap++;
});
});
break;
case 'LineString':
case 'MultiLineString':
segmentEach(feature1, function (segment1) {
segmentEach(feature2, function (segment2) {
if (lineOverlap(segment1, segment2).features.length) overlap++;
});
});
break;
case 'Polygon':
case 'MultiPolygon':
segmentEach(feature1, function (segment1) {
segmentEach(feature2, function (segment2) {
if (lineIntersect(segment1, segment2).features.length) overlap++;
});
});
break;
}
return overlap > 0;
}
示例4: segmentEach
segmentEach(feature1, (segment1) => {
segmentEach(feature2, (segment2) => {
if (lineIntersect(segment1, segment2).features.length) overlap++;
});
});
示例5: 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,
//.........这里部分代码省略.........
示例6: segmentEach
segmentEach(feature1, function (segment1) {
segmentEach(feature2, function (segment2) {
if (lineIntersect(segment1, segment2).features.length) overlap++;
});
});
示例7: rbush
/**
* Takes any LineString or Polygon and returns the overlapping lines between both features.
*
* @name lineOverlap
* @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line1 any LineString or Polygon
* @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line2 any LineString or Polygon
* @param {Object} [options={}] Optional parameters
* @param {number} [options.tolerance=0] Tolerance distance to match overlapping line segments (in kilometers)
* @returns {FeatureCollection<LineString>} lines(s) that are overlapping between both features
* @example
* var line1 = turf.lineString([[115, -35], [125, -30], [135, -30], [145, -35]]);
* var line2 = turf.lineString([[115, -25], [125, -30], [135, -30], [145, -25]]);
*
* var overlapping = turf.lineOverlap(line1, line2);
*
* //addToMap
* var addToMap = [line1, line2, overlapping]
*/
function lineOverlap<G1 extends LineString|MultiLineString|Polygon|MultiPolygon, G2 extends LineString|MultiLineString|Polygon|MultiPolygon>(
line1: Feature<G1> | G1,
line2: Feature<G2> | G2,
options: {tolerance?: number}={}
): FeatureCollection<LineString> {
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
var tolerance = options.tolerance || 0;
// Containers
var features = [];
// Create Spatial Index
var tree = rbush();
// To-Do -- HACK way to support typescript
const line: any = lineSegment(line1);
tree.load(line);
var overlapSegment;
// Line Intersection
// Iterate over line segments
segmentEach(line2, function (segment) {
var doesOverlaps = false;
// Iterate over each segments which falls within the same bounds
featureEach(tree.search(segment), function (match) {
if (doesOverlaps === false) {
var coordsSegment = getCoords(segment).sort();
var coordsMatch: any = getCoords(match).sort();
// Segment overlaps feature
if (equal(coordsSegment, coordsMatch)) {
doesOverlaps = true;
// Overlaps already exists - only append last coordinate of segment
if (overlapSegment) overlapSegment = concatSegment(overlapSegment, segment);
else overlapSegment = segment;
// Match segments which don't share nodes (Issue #901)
} else if (
(tolerance === 0) ?
booleanPointOnLine(coordsSegment[0], match) && booleanPointOnLine(coordsSegment[1], match) :
nearestPointOnLine(match, coordsSegment[0]).properties.dist <= tolerance &&
nearestPointOnLine(match, coordsSegment[1]).properties.dist <= tolerance) {
doesOverlaps = true;
if (overlapSegment) overlapSegment = concatSegment(overlapSegment, segment);
else overlapSegment = segment;
} else if (
(tolerance === 0) ?
booleanPointOnLine(coordsMatch[0], segment) && booleanPointOnLine(coordsMatch[1], segment) :
nearestPointOnLine(segment, coordsMatch[0]).properties.dist <= tolerance &&
nearestPointOnLine(segment, coordsMatch[1]).properties.dist <= tolerance) {
// Do not define (doesOverlap = true) since more matches can occur within the same segment
// doesOverlaps = true;
if (overlapSegment) overlapSegment = concatSegment(overlapSegment, match);
else overlapSegment = match;
}
}
});
// Segment doesn't overlap - add overlaps to results & reset
if (doesOverlaps === false && overlapSegment) {
features.push(overlapSegment);
overlapSegment = undefined;
}
});
// Add last segment if exists
if (overlapSegment) features.push(overlapSegment);
return featureCollection(features);
}