shapely-compute

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Computational Geometry with Shapely

基于Shapely的计算几何

When to Use

适用场景

  • Creating geometric shapes (points, lines, polygons)
  • Boolean operations (intersection, union, difference)
  • Spatial predicates (contains, intersects, within)
  • Measurements (area, length, distance, centroid)
  • Geometry transformations (translate, rotate, scale)
  • Validating and fixing invalid geometries
  • 创建几何图形(点、线、多边形)
  • 布尔运算(交集、并集、差集)
  • 空间关系判断(包含、相交、在内部)
  • 测量(面积、长度、距离、质心)
  • 几何变换(平移、旋转、缩放)
  • 验证与修复无效几何图形

Quick Reference

速查指南

I want to...CommandExample
Create geometry
create
create polygon --coords "0,0 1,0 1,1 0,1"
Intersection
op intersection
op intersection --g1 "POLYGON(...)" --g2 "POLYGON(...)"
Check contains
pred contains
pred contains --g1 "POLYGON(...)" --g2 "POINT(0.5 0.5)"
Calculate area
measure area
measure area --geom "POLYGON(...)"
Distance
distance
distance --g1 "POINT(0 0)" --g2 "POINT(3 4)"
Transform
transform translate
transform translate --geom "..." --params "1,2"
Validate
validate
validate --geom "POLYGON(...)"
我想要...命令示例
创建几何图形
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(...)"

Commands

命令说明

create

create

Create geometric objects from coordinates.
bash
undefined
从坐标创建几何对象。
bash
undefined

Point

Point

uv run python scripts/shapely_compute.py create point --coords "1,2"
uv run python scripts/shapely_compute.py create point --coords "1,2"

Line (2+ points)

Line (2+ points)

uv run python scripts/shapely_compute.py create line --coords "0,0 1,1 2,0"
uv run python scripts/shapely_compute.py create line --coords "0,0 1,1 2,0"

Polygon (3+ points, auto-closes)

Polygon (3+ points, auto-closes)

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 1,0 1,1 0,1"

Polygon with hole

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"
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

MultiPoint

uv run python scripts/shapely_compute.py create multipoint --coords "0,0 1,1 2,2"
uv run python scripts/shapely_compute.py create multipoint --coords "0,0 1,1 2,2"

MultiLineString (pipe-separated lines)

MultiLineString (pipe-separated lines)

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 multilinestring --coords "0,0 1,1|2,2 3,3"

MultiPolygon (pipe-separated polygons)

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"
undefined
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"
undefined

op (operations)

op (operations)

Boolean geometry operations.
bash
undefined
几何布尔运算。
bash
undefined

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))"
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(...)"
uv run python scripts/shapely_compute.py op union --g1 "POLYGON(...)" --g2 "POLYGON(...)"

Difference (g1 - g2)

差集(g1 - g2)

uv run python scripts/shapely_compute.py op difference --g1 "POLYGON(...)" --g2 "POLYGON(...)"
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 "..."
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"
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))"
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(...)"
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"
undefined
uv run python scripts/shapely_compute.py op simplify --g1 "LINESTRING(...)" --g2 "0.5"
undefined

pred (predicates)

pred (predicates)

Spatial relationship tests (returns boolean).
bash
undefined
空间关系测试(返回布尔值)。
bash
undefined

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)"
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 "..."
uv run python scripts/shapely_compute.py pred intersects --g1 "..." --g2 "..."

Is g1 within 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 within --g1 "POINT(1 1)" --g2 "POLYGON(...)"

Do geometries touch (share boundary)?

几何图形是否相切(共享边界)?

uv run python scripts/shapely_compute.py pred touches --g1 "..." --g2 "..."
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(...)"
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 "..."
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 "..."
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 "..."
uv run python scripts/shapely_compute.py pred equals --g1 "..." --g2 "..."

Does g1 cover g2?

g1是否覆盖g2?

uv run python scripts/shapely_compute.py pred covers --g1 "..." --g2 "..."
uv run python scripts/shapely_compute.py pred covers --g1 "..." --g2 "..."

Is g1 covered by g2?

g1是否被g2覆盖?

uv run python scripts/shapely_compute.py pred covered_by --g1 "..." --g2 "..."
undefined
uv run python scripts/shapely_compute.py pred covered_by --g1 "..." --g2 "..."
undefined

measure

measure

Geometric measurements.
bash
undefined
几何测量。
bash
undefined

Area (polygons)

面积(多边形)

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 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)"
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))"
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)

边界范围(minx, miny, maxx, maxy)

uv run python scripts/shapely_compute.py measure bounds --geom "POLYGON(...)"
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(...)"
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))"
undefined
uv run python scripts/shapely_compute.py measure all --geom "POLYGON((0 0,2 0,2 2,0 2,0 0))"
undefined

distance

distance

Distance between geometries.
bash
uv run python scripts/shapely_compute.py distance --g1 "POINT(0 0)" --g2 "POINT(3 4)"
几何图形之间的距离。
bash
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"}

返回: {"distance": 5.0, "g1_type": "Point", "g2_type": "Point"}

undefined
undefined

transform

transform

Affine transformations.
bash
undefined
仿射变换。
bash
undefined

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"
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

params: dx,dy 或 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"
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

params: 角度 或 角度,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"
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

params: sx,sy 或 sx,sy,origin_x,origin_y

Skew

斜切

uv run python scripts/shapely_compute.py transform skew
--geom "POLYGON(...)" --params "15,0"
uv run python scripts/shapely_compute.py transform skew
--geom "POLYGON(...)" --params "15,0"

params: xs,ys (degrees)

params: xs,ys(角度)

undefined
undefined

validate / makevalid

validate / makevalid

Check and fix geometry validity.
bash
undefined
检查并修复几何图形的有效性。
bash
undefined

Check if valid

检查是否有效

uv run python scripts/shapely_compute.py validate --geom "POLYGON((0 0,1 0,1 1,0 1,0 0))"
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", ...}

返回: {"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))"
undefined
uv run python scripts/shapely_compute.py makevalid --geom "POLYGON((0 0,2 2,2 0,0 2,0 0))"
undefined

coords

coords

Extract coordinates from geometry.
bash
uv run python scripts/shapely_compute.py coords --geom "POLYGON((0 0,1 0,1 1,0 1,0 0))"
从几何图形中提取坐标。
bash
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"}

返回: {"coords": [[0,0],[1,0],[1,1],[0,1],[0,0]], "type": "Polygon"}

undefined
undefined

fromwkt

fromwkt

Parse WKT and get geometry information.
bash
uv run python scripts/shapely_compute.py fromwkt "POLYGON((0 0,1 0,1 1,0 1,0 0))"
解析WKT并获取几何图形信息。
bash
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, ...}

返回: {"type": "Polygon", "bounds": [...], "area": 1.0, ...}

undefined
undefined

Geometry Types

几何类型

  • point
    - Single coordinate (x, y) or (x, y, z)
  • line
    /
    linestring
    - Sequence of connected points
  • polygon
    - Closed shape with optional holes
  • multipoint
    ,
    multilinestring
    ,
    multipolygon
    - Collections
  • point
    - 单个坐标(x, y)或(x, y, z)
  • line
    /
    linestring
    - 连续点序列
  • polygon
    - 闭合图形,可包含孔洞
  • multipoint
    ,
    multilinestring
    ,
    multipolygon
    - 集合类型

Input Formats

输入格式

  • Coordinates string:
    "0,0 1,0 1,1 0,1"
    (space-separated x,y pairs)
  • WKT:
    "POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))"
  • 坐标字符串:
    "0,0 1,0 1,1 0,1"
    (空格分隔的x,y对)
  • WKT:
    "POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))"

Output Format

输出格式

All commands return JSON with:
  • wkt
    : WKT representation of result geometry
  • type
    : Geometry type (Point, LineString, Polygon, etc.)
  • bounds
    : (minx, miny, maxx, maxy)
  • is_valid
    ,
    is_empty
    : Validity flags
  • Measurement-specific fields (area, length, distance, etc.)
所有命令返回JSON格式数据,包含:
  • wkt
    : 结果几何图形的WKT表示
  • type
    : 几何类型(Point, LineString, Polygon等)
  • bounds
    : (minx, miny, maxx, maxy)
  • is_valid
    ,
    is_empty
    : 有效性标记
  • 测量相关字段(area, length, distance等)

Common Use Cases

常见使用场景

Use CaseCommand
Collision detection
pred intersects
Point-in-polygon
pred contains
Area calculation
measure area
Buffer zones
op buffer
Shape combination
op union
Shape subtraction
op difference
Bounding box
op envelope
or
measure bounds
Simplify path
op simplify
使用场景命令
碰撞检测
pred intersects
点-in-多边形判断
pred contains
面积计算
measure area
缓冲区域生成
op buffer
图形合并
op union
图形裁剪
op difference
边界框获取
op envelope
measure bounds
路径简化
op simplify

Related Skills

相关技能

  • /math-mode
    - Full math orchestration (SymPy, Z3)
  • /math-plot
    - Visualization with matplotlib
  • /math-mode
    - 完整数学编排(SymPy, Z3)
  • /math-plot
    - 基于matplotlib的可视化