关于 shapely-compute
shapely-compute 是一项介绍如何使用 Shapely 执行计算几何命令行操作的技能,适用于空间判定、测量、变换和有效性检查,并以 JSON 输出结果。
主要功能
- 01根据坐标创建几何对象
- 02执行几何布尔运算
- 03判断空间关系
- 04测量面积、长度和距离
- 05变换并验证几何对象
- 06解析 WKT 并提取坐标
适用场景
- 01检查图形是否相交或包含点
- 02计算面积、长度或距离
- 03创建缓冲区或合并图形
- 04修复无效几何对象
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ parcadei/ continuous-claude-v3 --skill shapely-compute安装后在 Claude Code 中输入/shapely-compute确认已加载。
详细介绍
使用 Shapely 进行计算几何
适用场景
- 创建几何形状(点、线、多边形)
- 布尔运算(交集、并集、差集)
- 空间谓词(包含、相交、位于内部)
- 测量(面积、长度、距离、质心)
- 几何变换(平移、旋转、缩放)
- 验证并修复无效几何
快速参考
| 我想要…… | 命令 | 示例 |
|---|---|---|
| 创建几何图形 | create | create polygon --coords "0,0 1,0 1,1 0,1" |
| 求交集 | op intersection | op intersection --g1 "POLYGON(...)" --g2 "POLYGON(...)" |
| 检查是否包含 | pred contains | pred contains --g1 "POLYGON(...)" --g2 "POINT(0.5 0.5)" |
| 计算面积 | measure area | measure area --geom "POLYGON(...)" |
| 计算距离 | distance | distance --g1 "POINT(0 0)" --g2 "POINT(3 4)" |
| 变换几何图形 | transform translate | transform translate --geom "..." --params "1,2" |
| 验证几何图形 | validate | validate --geom "POLYGON(...)" |
命令
create
根据坐标创建几何对象。
# Point
uv run python scripts/shapely_compute.py create point --coords "1,2"
# Line (2+ points)
uv run python scripts/shapely_compute.py create line --coords "0,0 1,1 2,0"
# Polygon (3+ points, auto-closes)
uv run python scripts/shapely_compute.py create polygon --coords "0,0 1,0 1,1 0,1"
# Polygon with hole
uv run python scripts/shapely_compute.py create polygon --coords "0,0 10,0 10,10 0,10" --holes "2,2 8,2 8,8 2,8"
# MultiPoint
uv run python scripts/shapely_compute.py create multipoint --coords "0,0 1,1 2,2"
# MultiLineString (pipe-separated lines)
uv run python scripts/shapely_compute.py create multilinestring --coords "0,0 1,1|2,2 3,3"
# MultiPolygon (pipe-separated polygons)
uv run python scripts/shapely_compute.py create multipolygon --coords "0,0 1,0 1,1 0,1|2,2 3,2 3,3 2,3"
op(运算)
执行几何布尔运算。
# Intersection of two polygons
uv run python scripts/shapely_compute.py op intersection \
--g1 "POLYGON((0 0,2 0,2 2,0 2,0 0))" \
--g2 "POLYGON((1 1,3 1,3 3,1 3,1 1))"
# Union
uv run python scripts/shapely_compute.py op union --g1 "POLYGON(...)" --g2 "POLYGON(...)"
# Difference (g1 - g2)
uv run python scripts/shapely_compute.py op difference --g1 "POLYGON(...)" --g2 "POLYGON(...)"
# Symmetric difference (XOR)
uv run python scripts/shapely_compute.py op symmetric_difference --g1 "..." --g2 "..."
# Buffer (expand/erode)
uv run python scripts/shapely_compute.py op buffer --g1 "POINT(0 0)" --g2 "1.5"
# Convex hull
uv run python scripts/shapely_compute.py op convex_hull --g1 "MULTIPOINT((0 0),(1 1),(0 2),(2 0))"
# Envelope (bounding box)
uv run python scripts/shapely_compute.py op envelope --g1 "POLYGON(...)"
# Simplify (reduce points)
uv run python scripts/shapely_compute.py op simplify --g1 "LINESTRING(...)" --g2 "0.5"
pred(谓词)
测试空间关系(返回布尔值)。
# Does polygon contain point?
uv run python scripts/shapely_compute.py pred contains \
--g1 "POLYGON((0 0,2 0,2 2,0 2,0 0))" \
--g2 "POINT(1 1)"
# Do geometries intersect?
uv run python scripts/shapely_compute.py pred intersects --g1 "..." --g2 "..."
# Is g1 within g2?
uv run python scripts/shapely_compute.py pred within --g1 "POINT(1 1)" --g2 "POLYGON(...)"
# Do geometries touch (share boundary)?
uv run python scripts/shapely_compute.py pred touches --g1 "..." --g2 "..."
# Do geometries cross?
uv run python scripts/shapely_compute.py pred crosses --g1 "LINESTRING(...)" --g2 "LINESTRING(...)"
# Are geometries disjoint (no intersection)?
uv run python scripts/shapely_compute.py pred disjoint --g1 "..." --g2 "..."
# Do geometries overlap?
uv run python scripts/shapely_compute.py pred overlaps --g1 "..." --g2 "..."
# Are geometries equal?
uv run python scripts/shapely_compute.py pred equals --g1 "..." --g2 "..."
# Does g1 cover g2?
uv run python scripts/shapely_compute.py pred covers --g1 "..." --g2 "..."
# Is g1 covered by g2?
uv run python scripts/shapely_compute.py pred covered_by --g1 "..." --g2 "..."
measure
测量几何属性。
# Area (polygons)
uv run python scripts/shapely_compute.py measure area --geom "POLYGON((0 0,1 0,1 1,0 1,0 0))"
# Length (lines, polygon perimeter)
uv run python scripts/shapely_compute.py measure length --geom "LINESTRING(0 0,3 4)"
# Centroid
uv run python scripts/shapely_compute.py measure centroid --geom "POLYGON((0 0,2 0,2 2,0 2,0 0))"
# Bounds (minx, miny, maxx, maxy)
uv run python scripts/shapely_compute.py measure bounds --geom "POLYGON(...)"
# Exterior ring (polygon only)
uv run python scripts/shapely_compute.py measure exterior_ring --geom "POLYGON(...)"
# All measurements at once
uv run python scripts/shapely_compute.py measure all --geom "POLYGON((0 0,2 0,2 2,0 2,0 0))"
distance
计算几何图形之间的距离。
uv run python scripts/shapely_compute.py distance --g1 "POINT(0 0)" --g2 "POINT(3 4)"
# Returns: {"distance": 5.0, "g1_type": "Point", "g2_type": "Point"}
transform
执行仿射变换。
# Translate (move)
uv run python scripts/shapely_compute.py transform translate \
--geom "POLYGON((0 0,1 0,1 1,0 1,0 0))" --params "5,10"
# params: dx,dy or dx,dy,dz
# Rotate (degrees, around centroid by default)
uv run python scripts/shapely_compute.py transform rotate \
--geom "POLYGON((0 0,1 0,1 1,0 1,0 0))" --params "45"
# params: angle or angle,origin_x,origin_y
# Scale (from centroid by default)
uv run python scripts/shapely_compute.py transform scale \
--geom "POLYGON((0 0,1 0,1 1,0 1,0 0))" --params "2,2"
# params: sx,sy or sx,sy,origin_x,origin_y
# Skew
uv run python scripts/shapely_compute.py transform skew \
--geom "POLYGON(...)" --params "15,0"
# params: xs,ys (degrees)
validate / makevalid
检查并修复几何图形的有效性。
# Check if valid
uv run python scripts/shapely_compute.py validate --geom "POLYGON((0 0,1 0,1 1,0 1,0 0))"
# Returns: {"is_valid": true, "type": "Polygon", ...}
# Fix invalid geometry (self-intersecting, etc.)
uv run python scripts/shapely_compute.py makevalid --geom "POLYGON((0 0,2 2,2 0,0 2,0 0))"
coords
从几何图形中提取坐标。
uv run python scripts/shapely_compute.py coords --geom "POLYGON((0 0,1 0,1 1,0 1,0 0))"
# Returns: {"coords": [[0,0],[1,0],[1,1],[0,1],[0,0]], "type": "Polygon"}
fromwkt
解析 WKT 并获取几何图形信息。
uv run python scripts/shapely_compute.py fromwkt "POLYGON((0 0,1 0,1 1,0 1,0 0))"
# Returns: {"type": "Polygon", "bounds": [...], "area": 1.0, ...}
几何类型
point- 单个坐标 (x, y) 或 (x, y, z)line/linestring- 由相连点组成的序列polygon- 带有可选孔洞的闭合形状multipoint、multilinestring、multipolygon- 几何集合
输入格式
- 坐标字符串:
"0,0 1,0 1,1 0,1"(以空格分隔的 x,y 坐标对) - WKT:
"POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))"
输出格式
所有命令均返回 JSON,包含:
wkt:结果几何图形的 WKT 表示type:几何类型(Point、LineString、Polygon 等)bounds:(minx, miny, maxx, maxy)is_valid、is_empty:有效性标记- 与测量相关的字段(area、length、distance 等)
常见用例
| 用例 | 命令 |
|---|---|
| 碰撞检测 | pred intersects |
| 点在多边形内 | pred contains |
| 面积计算 | measure area |
| 缓冲区 | op buffer |
| 形状合并 | op union |
| 形状相减 | op difference |
| 边界框 | op envelope 或 measure bounds |
| 简化路径 | op simplify |
相关技能
/math-mode- 完整的数学编排(SymPy、Z3)/math-plot- 使用 matplotlib 进行可视化
评论
支持 Markdown