如何将turf.lineChunk的输出转换为turf.points格式?
解决方案
turf.lineChunk 返回的是LineString类型的FeatureCollection,而 turf.points 要求传入的是**[经度, 纬度]格式的二维坐标数组**,直接传递 chunk.features 会因类型不匹配失效。你需要先提取分段的坐标数据,再转换为目标格式。
情况1:提取所有分段的全部顶点(起点+终点)
// 提取所有chunk的坐标并合并为一维数组 const allCoordinates = chunk.features.flatMap(feature => feature.geometry.coordinates); // 转换为turf.points格式 const points = turf.points(allCoordinates);
情况2:仅提取每个分段的起点(或终点)
如果只需要每个分段的起始点,取坐标数组的第一个元素;若要终点则取最后一个元素:
// 提取每个chunk的起点坐标 const startPoints = chunk.features.map(feature => feature.geometry.coordinates[0]); const points = turf.points(startPoints); // 若要提取终点,替换为: // const endPoints = chunk.features.map(feature => feature.geometry.coordinates.at(-1));
验证使用
转换后的 points 是Point类型的FeatureCollection,可直接用于 turf.pointsWithinPolygon:
var inside = turf.pointsWithinPolygon(points, turfpolygon);
内容的提问来源于stack exchange,提问作者tj26
相关产品推荐
相关产品推荐

