名称: shapely-compute 描述: 使用Shapely进行计算几何 - 创建几何形状、布尔操作、测量、空间关系判断 触发词: [“几何”, “多边形”, “交集”, “面积”, “包含”, “点间距离”, “缓冲区”, “凸包”, “质心”, “WKT”]
使用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
从坐标创建几何对象。
# 点
uv run python scripts/shapely_compute.py create point --coords "1,2"
# 线(2个或更多点)
uv run python scripts/shapely_compute.py create line --coords "0,0 1,1 2,0"
# 多边形(3个或更多点,自动闭合)
uv run python scripts/shapely_compute.py create polygon --coords "0,0 1,0 1,1 0,1"
# 带孔的多边形
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"
# 多点
uv run python scripts/shapely_compute.py create multipoint --coords "0,0 1,1 2,2"
# 多线串(管道分隔的线)
uv run python scripts/shapely_compute.py create multilinestring --coords "0,0 1,1|2,2 3,3"
# 多多边形(管道分隔的多边形)
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(操作)
布尔几何操作。
# 两个多边形的交集
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))"
# 并集
uv run python scripts/shapely_compute.py op union --g1 "POLYGON(...)" --g2 "POLYGON(...)"
# 差集(g1 - g2)
uv run python scripts/shapely_compute.py op difference --g1 "POLYGON(...)" --g2 "POLYGON(...)"
# 对称差集(XOR)
uv run python scripts/shapely_compute.py op symmetric_difference --g1 "..." --g2 "..."
# 缓冲区(扩展/侵蚀)
uv run python scripts/shapely_compute.py op buffer --g1 "POINT(0 0)" --g2 "1.5"
# 凸包
uv run python scripts/shapely_compute.py op convex_hull --g1 "MULTIPOINT((0 0),(1 1),(0 2),(2 0))"
# 外包矩形(边界框)
uv run python scripts/shapely_compute.py op envelope --g1 "POLYGON(...)"
# 简化(减少点)
uv run python scripts/shapely_compute.py op simplify --g1 "LINESTRING(...)" --g2 "0.5"
pred(关系判断)
空间关系测试(返回布尔值)。
# 多边形是否包含点?
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)"
# 几何形状是否相交?
uv run python scripts/shapely_compute.py pred intersects --g1 "..." --g2 "..."
# g1是否在g2内部?
uv run python scripts/shapely_compute.py pred within --g1 "POINT(1 1)" --g2 "POLYGON(...)"
# 几何形状是否接触(共享边界)?
uv run python scripts/shapely_compute.py pred touches --g1 "..." --g2 "..."
# 几何形状是否交叉?
uv run python scripts/shapely_compute.py pred crosses --g1 "LINESTRING(...)" --g2 "LINESTRING(...)"
# 几何形状是否不相交(无交集)?
uv run python scripts/shapely_compute.py pred disjoint --g1 "..." --g2 "..."
# 几何形状是否重叠?
uv run python scripts/shapely_compute.py pred overlaps --g1 "..." --g2 "..."
# 几何形状是否相等?
uv run python scripts/shapely_compute.py pred equals --g1 "..." --g2 "..."
# g1是否覆盖g2?
uv run python scripts/shapely_compute.py pred covers --g1 "..." --g2 "..."
# g1是否被g2覆盖?
uv run python scripts/shapely_compute.py pred covered_by --g1 "..." --g2 "..."
measure(测量)
几何测量。
# 面积(多边形)
uv run python scripts/shapely_compute.py measure area --geom "POLYGON((0 0,1 0,1 1,0 1,0 0))"
# 长度(线、多边形周长)
uv run python scripts/shapely_compute.py measure length --geom "LINESTRING(0 0,3 4)"
# 质心
uv run python scripts/shapely_compute.py measure centroid --geom "POLYGON((0 0,2 0,2 2,0 2,0 0))"
# 边界(minx, miny, maxx, maxy)
uv run python scripts/shapely_compute.py measure bounds --geom "POLYGON(...)"
# 外环(仅多边形)
uv run python scripts/shapely_compute.py measure exterior_ring --geom "POLYGON(...)"
# 所有测量一次完成
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)"
# 返回: {"distance": 5.0, "g1_type": "Point", "g2_type": "Point"}
transform(变换)
仿射变换。
# 平移(移动)
uv run python scripts/shapely_compute.py transform translate \
--geom "POLYGON((0 0,1 0,1 1,0 1,0 0))" --params "5,10"
# 参数: dx,dy 或 dx,dy,dz
# 旋转(度,默认围绕质心)
uv run python scripts/shapely_compute.py transform rotate \
--geom "POLYGON((0 0,1 0,1 1,0 1,0 0))" --params "45"
# 参数: 角度 或 角度,原点_x,原点_y
# 缩放(默认从质心)
uv run python scripts/shapely_compute.py transform scale \
--geom "POLYGON((0 0,1 0,1 1,0 1,0 0))" --params "2,2"
# 参数: sx,sy 或 sx,sy,原点_x,原点_y
# 倾斜
uv run python scripts/shapely_compute.py transform skew \
--geom "POLYGON(...)" --params "15,0"
# 参数: xs,ys(度)
validate / makevalid(验证/修复)
检查和修复几何形状有效性。
# 检查是否有效
uv run python scripts/shapely_compute.py validate --geom "POLYGON((0 0,1 0,1 1,0 1,0 0))"
# 返回: {"is_valid": true, "type": "Polygon", ...}
# 修复无效几何形状(如自相交)
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))"
# 返回: {"coords": [[0,0],[1,0],[1,1],[0,1],[0,0]], "type": "Polygon"}
fromwkt(从WKT解析)
解析WKT并获取几何信息。
uv run python scripts/shapely_compute.py fromwkt "POLYGON((0 0,1 0,1 1,0 1,0 0))"
# 返回: {"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: 有效性标志- 测量特定字段(面积、长度、距离等)
常见用例
| 用例 | 命令 |
|---|---|
| 碰撞检测 | 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可视化