turf-skills
Turf.js spatial analysis skill for Claude Code.
Usage
CODEBLOCK0
Actions
Measurement 测量
| Action | Description | Required | Optional |
|---|
| INLINECODE0 | Distance between two points 两点距离 | input, input2 | units |
| INLINECODE1 |
Area of polygon 多边形面积(m²) | input | |
|
length | Length of line 线长度 | input | units |
|
bearing | Bearing between points 方位角 | input, input2 | |
|
destination | Point at distance+bearing 目标点 | input | distance, direction, units |
|
midpoint | Midpoint of two points 中点 | input, input2 | |
|
center | Center of features 中心点 | input | |
|
centroid | Centroid 质心 | input | |
|
centerOfMass | Center of mass 重心 | input | |
|
along | Point along line 沿线取点 | input | distance, units |
|
bbox | Bounding box 边界框 | input | |
|
bboxPolygon | Bbox as polygon 边界框多边形 | input or bbox | |
|
envelope | Envelope 包络矩形 | input | |
Query 空间查询
| Action | Description | Required | Optional |
|---|
| INLINECODE13 | Point in polygon 点在多边形内 | input(point), input2(polygon) | |
| INLINECODE14 |
Feature contains another 包含 | input, input2 | |
|
booleanCrosses | Features cross 交叉 | input, input2 | |
|
booleanDisjoint | Features disjoint 不相交 | input, input2 | |
|
booleanEqual | Features equal 相等 | input, input2 | |
|
booleanIntersects | Features intersect 相交 | input, input2 | |
|
booleanOverlap | Features overlap 重叠 | input, input2 | |
|
booleanWithin | Feature within another 在内部 | input, input2 | |
|
booleanParallel | Lines parallel 平行 | input, input2 | |
|
booleanPointOnLine | Point on line 点在线上 | input(point), input2(line) | |
|
nearestPoint | Nearest point in collection 最近点 | input(point), input2(FC) | |
Transformation 变换
| Action | Description | Required | Optional |
|---|
| INLINECODE24 | Buffer zone 缓冲区 | input | radius, units |
| INLINECODE25 |
Union polygons 合并 | input, input2 | |
|
intersect | Intersect polygons 求交 | input, input2 | |
|
difference | Difference polygons 求差 | input, input2 | |
|
simplify | Simplify geometry 简化 | input | tolerance |
|
convex | Convex hull 凸包 | input | |
|
concave | Concave hull 凹包 | input | maxEdge, units |
|
dissolve | Dissolve polygons 融合 | input(FC) | propertyName |
|
voronoi | Voronoi diagram 泰森多边形 | input(points) | bbox |
|
tin | TIN 不规则三角网 | input(points) | propertyName |
|
bezierSpline | Bezier spline 贝塞尔曲线 | input(line) | resolution, sharpness |
|
transformRotate | Rotate 旋转 | input | angle |
|
transformScale | Scale 缩放 | input | factor |
|
transformTranslate | Translate 平移 | input | distance, direction, units |
Helpers 创建
| Action | Description | Required | Optional |
|---|
| INLINECODE38 | Create point 创建点 | coordinates | properties |
| INLINECODE39 |
Create line 创建线 | coordinates | properties |
|
polygon | Create polygon 创建面 | coordinates | properties |
|
multiPoint | Create MultiPoint | coordinates | properties |
|
multiLineString | Create MultiLineString | coordinates | properties |
|
multiPolygon | Create MultiPolygon | coordinates | properties |
|
randomPoint | Random points 随机点 | | count, bbox |
|
randomLineString | Random lines 随机线 | | count, bbox |
|
randomPolygon | Random polygons 随机面 | | count, bbox |
Interpolation 插值/网格
| Action | Description | Required | Optional |
|---|
| INLINECODE47 | IDW interpolation 反距离加权插值 | input(points) | cellSide, gridType, propertyName, units |
| INLINECODE48 |
Isobands 等值面 | input(points) | breaks, propertyName |
|
isolines | Isolines 等值线 | input(points) | breaks, propertyName |
|
hexGrid | Hex grid 六边形网格 | bbox, cellSide | units |
|
pointGrid | Point grid 点网格 | bbox, cellSide | units |
|
squareGrid | Square grid 方形网格 | bbox, cellSide | units |
|
triangleGrid | Triangle grid 三角网格 | bbox, cellSide | units |
Clustering 聚类
| Action | Description | Required | Optional |
|---|
| INLINECODE54 | K-means clustering K均值聚类 | input(points FC) | numberOfClusters |
| INLINECODE55 |
DBSCAN clustering 密度聚类 | input(points FC) | maxDistance, units, minPoints |
Examples
Calculate distance 计算距离
CODEBLOCK1
Point in polygon 点在多边形内
CODEBLOCK2
Buffer 缓冲区
CODEBLOCK3
Read from file 从文件读取
CODEBLOCK4
Output to file 输出到文件
CODEBLOCK5
Create geometry 创建几何
CODEBLOCK6
Grid generation 网格生成
CODEBLOCK7
Clustering 聚类分析
CODEBLOCK8
Natural Language Patterns
Common Queries 常见查询
| Query Pattern | Action | Example |
|---|
| "distance between [coordinates]" | distance | "Calculate distance between [120,30] and [121,31]" |
| "area of [polygon]" |
area | "Find area of this polygon" |
| "length of [line]" | length | "Calculate length of this line in km" |
| "is [point] inside [polygon]" | booleanPointInPolygon | "Is this point inside the polygon?" |
| "buffer [radius] km/mi around [point]" | buffer | "Create a 5km buffer around this point" |
| "convex hull of [points]" | convex | "Compute convex hull of these points" |
| "cluster points into [n] groups" | clustersKmeans | "Cluster these points into 5 groups" |
| "nearest point to [location]" | nearestPoint | "Find nearest point to [120,30]" |
| "simplify geometry" | simplify | "Simplify this geometry" |
| "union/intersect/difference" | union/intersect/difference | "Union these two polygons" |
| "generate hex/square/triangle grid" | hexGrid/squareGrid/triangleGrid | "Generate hex grid for this area" |
| "rotate/scale/translate" | transformRotate/transformScale/transformTranslate | "Rotate this polygon by 45 degrees" |
Chinese Query Patterns 中文查询模式
| 查询模式 | 操作 | 示例 |
|---|
| "计算...的距离" | distance | "计算北京和上海的距离" |
| "计算...的面积" |
area | "计算这个多边形的面积" |
| "点是否在多边形内" | booleanPointInPolygon | "天安门是否在五环内?" |
| "创建...缓冲区" | buffer | "创建5公里缓冲区" |
| "计算凸包" | convex | "计算这些点的凸包" |
| "聚类分析" | clustersKmeans/clustersDbscan | "对这些点进行聚类" |
| "查找最近点" | nearestPoint | "查找距离[120,30]最近的点" |
| "生成网格" | hexGrid/squareGrid | "生成六边形网格" |
| "旋转/缩放/平移" | transformRotate/transformScale/transformTranslate | "旋转多边形45度" |
Real-World Scenarios 实际场景
CODEBLOCK9
When to Use This Skill 何时使用此技能
ALWAYS Use For 务必使用:
- - Complex operations: buffer, grid generation, clustering, interpolation
- GeoJSON output required: when the result needs to be valid GeoJSON
- Multiple operations: chaining spatial analyses
- File I/O: reading/writing GeoJSON files
Consider Direct Turf.js For 考虑直接使用:
- - Simple one-off calculations: distance, area, bearing (if already in a Node.js script)
- Performance-critical loops: when calling the same operation thousands of times
Performance Notes 性能说明:
- - Skill CLI has startup overhead (~1-2 seconds)
- For complex operations, skill is faster and more efficient
- Skill ensures consistent Turf.js implementation
Best Practices 最佳实践
1. Input Validation 输入验证
Always validate coordinates before processing:
- - Longitude: -180 to 180
- Latitude: -90 to 90
- Ensure polygons are closed (first == last coordinate)
2. Units 单位
- - Default unit: kilometers (千米)
- Options: kilometers, miles, meters, degrees, radians
- Always specify units explicitly for clarity
3. File Handling 文件处理
- - Use
--file for reading GeoJSON files - Use
--output to save results - Supports both
.geojson and .json extensions
4. Error Handling 错误处理
- - Check that required parameters are provided
- Validate GeoJSON format before passing to actions
- Handle empty results gracefully
Real-World Use Cases 实际应用场景
1. Delivery Zone Planning 配送范围规划
CODEBLOCK10
2. Geographic Fence Check 地理围栏检测
CODEBLOCK11
3. Store Location Clustering 门店位置聚类
CODEBLOCK12
4. Heat Map Grid 热力图网格
CODEBLOCK13
5. Route Simplification 路线简化
CODEBLOCK14
Input/Output Formats 输入输出格式
GeoJSON Feature Structure
CODEBLOCK15
Supported Geometry Types
- -
Point - 点 - INLINECODE61 - 线
- INLINECODE62 - 面
- INLINECODE63 - 多点
- INLINECODE64 - 多线
- INLINECODE65 - 多面
- INLINECODE66 - 要素集合
Troubleshooting 故障排除
Common Issues 常见问题
Issue: "Error: Unknown action"
- - Solution: Use
--list to see available actions
Issue: "Error: Cannot read property"
- - Solution: Check GeoJSON format, ensure proper nesting
Issue: Empty result for intersection/union
- - Solution: Geometries may not overlap; check coordinates
Issue: Distance calculation seems wrong
- - Solution: Verify units parameter (default is kilometers)
Additional Resources 更多资源
- - Turf.js Documentation: https://turfjs.org/
- GeoJSON Specification: https://geojson.org/
- Coordinate Reference Systems: https://epsg.io/
turf-skills
用于 Claude Code 的 Turf.js 空间分析技能。
使用方法
bash
turf-skills --action <名称> [选项]
操作
Measurement 测量
| 操作 | 描述 | 必填参数 | 可选参数 |
|---|
| distance | 两点距离 | input, input2 | units |
| area |
多边形面积(m²) | input | |
| length | 线长度 | input | units |
| bearing | 方位角 | input, input2 | |
| destination | 目标点 | input | distance, direction, units |
| midpoint | 中点 | input, input2 | |
| center | 中心点 | input | |
| centroid | 质心 | input | |
| centerOfMass | 重心 | input | |
| along | 沿线取点 | input | distance, units |
| bbox | 边界框 | input | |
| bboxPolygon | 边界框多边形 | input or bbox | |
| envelope | 包络矩形 | input | |
Query 空间查询
| 操作 | 描述 | 必填参数 | 可选参数 |
|---|
| booleanPointInPolygon | 点在多边形内 | input(point), input2(polygon) | |
| booleanContains |
包含 | input, input2 | |
| booleanCrosses | 交叉 | input, input2 | |
| booleanDisjoint | 不相交 | input, input2 | |
| booleanEqual | 相等 | input, input2 | |
| booleanIntersects | 相交 | input, input2 | |
| booleanOverlap | 重叠 | input, input2 | |
| booleanWithin | 在内部 | input, input2 | |
| booleanParallel | 平行 | input, input2 | |
| booleanPointOnLine | 点在线上 | input(point), input2(line) | |
| nearestPoint | 最近点 | input(point), input2(FC) | |
Transformation 变换
| 操作 | 描述 | 必填参数 | 可选参数 |
|---|
| buffer | 缓冲区 | input | radius, units |
| union |
合并 | input, input2 | |
| intersect | 求交 | input, input2 | |
| difference | 求差 | input, input2 | |
| simplify | 简化 | input | tolerance |
| convex | 凸包 | input | |
| concave | 凹包 | input | maxEdge, units |
| dissolve | 融合 | input(FC) | propertyName |
| voronoi | 泰森多边形 | input(points) | bbox |
| tin | 不规则三角网 | input(points) | propertyName |
| bezierSpline | 贝塞尔曲线 | input(line) | resolution, sharpness |
| transformRotate | 旋转 | input | angle |
| transformScale | 缩放 | input | factor |
| transformTranslate | 平移 | input | distance, direction, units |
Helpers 创建
| 操作 | 描述 | 必填参数 | 可选参数 |
|---|
| point | 创建点 | coordinates | properties |
| lineString |
创建线 | coordinates | properties |
| polygon | 创建面 | coordinates | properties |
| multiPoint | 创建多点 | coordinates | properties |
| multiLineString | 创建多线 | coordinates | properties |
| multiPolygon | 创建多面 | coordinates | properties |
| randomPoint | 随机点 | | count, bbox |
| randomLineString | 随机线 | | count, bbox |
| randomPolygon | 随机面 | | count, bbox |
Interpolation 插值/网格
| 操作 | 描述 | 必填参数 | 可选参数 |
|---|
| interpolate | 反距离加权插值 | input(points) | cellSide, gridType, propertyName, units |
| isobands |
等值面 | input(points) | breaks, propertyName |
| isolines | 等值线 | input(points) | breaks, propertyName |
| hexGrid | 六边形网格 | bbox, cellSide | units |
| pointGrid | 点网格 | bbox, cellSide | units |
| squareGrid | 方形网格 | bbox, cellSide | units |
| triangleGrid | 三角网格 | bbox, cellSide | units |
Clustering 聚类
| 操作 | 描述 | 必填参数 | 可选参数 |
|---|
| clustersKmeans | K均值聚类 | input(points FC) | numberOfClusters |
| clustersDbscan |
密度聚类 | input(points FC) | maxDistance, units, minPoints |
示例
计算距离
bash
turf-skills --action distance \
--input {type:Point,coordinates:[120,30]} \
--input2 {type:Point,coordinates:[121,31]} \
--units kilometers
点在多边形内
bash
turf-skills --action booleanPointInPolygon \
--input {type:Point,coordinates:[120.5,30.5]} \
--input2 {type:Polygon,coordinates:[[[120,30],[121,30],[121,31],[120,31],[120,30]]]}
缓冲区
bash
turf-skills --action buffer \
--input {type:Point,coordinates:[120,30]} \
--radius 5 --units kilometers
从文件读取
bash
turf-skills --action area --file polygon.geojson
turf-skills --action booleanContains --file polygon.geojson --file2 point.geojson
输出到文件
bash
turf-skills --action buffer --file point.geojson --radius 10 --output result.geojson
创建几何
bash
turf-skills --action point --coordinates [120,30] --properties {name:test}
turf-skills --action randomPoint --count 100 --bbox [120,30,121,31]
网格生成
bash
turf-skills --action hexGrid --bbox [120,30,121,31] --cellSide 5 --units kilometers
聚类分析
bash
turf-skills --action clustersKmeans --file points.geojson --numberOfClusters 5
turf-skills --action clustersDbscan --file points.geojson --maxDistance 1 --minPoints 3
自然语言模式
常见查询
| 查询模式 | 操作 | 示例 |
|---|
| distance between [coordinates] | distance | Calculate distance between [120,30] and [121,31] |
| area of [polygon] |
area | Find area of this polygon |
| length of [line] | length | Calculate length of this line in km |
| is [point] inside [polygon] | booleanPointInPolygon | Is this point inside the polygon? |
| buffer [radius] km/mi around [point]| buffer | Create a 5km buffer around this point |
| convex hull of [points]