跳转到主要内容

Python的polyclip函数驱动程序

项目描述

PyPolyClip

CI Tests coverage Zenodo

一个用于在像素网格上裁剪多边形的Python包。

polyclip函数最初是为CUBISM项目开发的 Smith et al. 2007 (PASP 119, 1133)

安装

可以通过命令行使用pip安装此包

pip install pypolyclip

描述

polyclip代码采用Sutherland-Hodgman算法将简单多边形裁剪成由正方形像素组成的镶嵌网格。因此,这与通常在两个任意多边形之间进行裁剪的类似包不同。

可以通过运行测试模块 test_pypolyclip.py 来生成以下示例图形

在每张图中,与给定多边形重叠的每个像素的笛卡尔坐标都会标注出该像素被覆盖的区域面积(像素面积定义为1)。因此,每个多边形各个像素面积的总和应该等于该多边形的总面积。

Pypolyclip 使用一个坐标网格,其中整数像素坐标位于每个像素的左下角,从0开始。要在整数像素坐标位于像素中心的坐标网格上裁剪多边形,需要将输入多边形的x和y顶点各加0.5像素。

第一张图显示了具有不同顶点数的多边形的裁剪,这内部需要使用“for循环”。然而,如果所有多边形的顶点数相同(如第二图所示),则内部使用 NumPy 来提高性能,提升几个百分点。

示例用法

这个第一个例子演示了具有相同顶点数的多边形

import numpy as np
from pypolyclip import clip_multi

# define the size of the pixel grid
naxis = (100, 100)

# create 3 polygons to clip

# the x-vertices of the polygon
px = np.array([[3.4, 3.4, 4.4, 4.4],
               [3.5, 3.5, 4.3, 4.3],
               [3.1, 3.1, 3.9, 3.9]])

# the y-vertices of the polygon
py = np.array([[1.4, 1.9, 1.9, 1.4],
               [3.7, 4.4, 4.4, 3.7],
               [2.1, 2.9, 2.9, 2.1]])

# call the clipper
xc, yc, area, slices = clip_multi(px, py, naxis)

# xc, yc are the grid indices with overlapping pixels.
# area is the overlapping area on a given pixel.
# slices is a list of slice objects to link between the input polygons
# and the clipped pixel grid.

# the slices object can be used to get the area of each polygon
for i, s in enumerate(slices):
    print(f'total area for polygon {i}={np.sum(area[s])}')

这个第二个例子演示了裁剪具有不同顶点数的多边形。请注意,与第一个例子中的NumPy数组不同,这里的 pxpy 是列表的列表。

import numpy as np
from pypolyclip import clip_multi

# define the size of the pixel grid
naxis = (100, 100)

# create 3 polygons to clip

# the x-vertices of the polygon
px = [[3.4, 3.4, 4.4, 4.8, 4.4],
      [3.5, 3.5, 4.3, 4.3],
      [3.1, 3.8, 3.1]]

# the y-vertices of the polygon
py = [[1.4, 1.9, 1.9, 1.65, 1.4],
      [3.7, 4.4, 4.4, 3.7],
      [2.1, 2.1, 3.4]]

# call the clipper
xc, yc, area, slices = clip_multi(px, py, naxis)

# xc, yc are the grid indices with overlapping pixels.
# area is the overlapping area on a given pixel.
# slices is a list of slice objects to link between the input polygons
# and the clipped pixel grid.

# the slices object can be used to get the area of each polygon
for i, s in enumerate(slices):
    print(f'total area for polygon {i}={np.sum(area[s])}')

项目详情


下载文件

下载适用于您的平台文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分发

pypolyclip-1.1.0.tar.gz (27.5 kB 查看哈希值)

上传时间

构建分发

pypolyclip-1.1.0-cp312-cp312-win_amd64.whl (22.5 kB 查看哈希值)

上传时间 CPython 3.12 Windows x86-64

pypolyclip-1.1.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (39.4 kB 查看哈希值)

上传时间 CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

pypolyclip-1.1.0-cp312-cp312-macosx_11_0_arm64.whl (20.7 kB 查看哈希值)

上传时间 CPython 3.12 macOS 11.0+ ARM64

pypolyclip-1.1.0-cp312-cp312-macosx_10_9_x86_64.whl (20.7 kB 查看哈希值)

上传时间 CPython 3.12 macOS 10.9+ x86-64

pypolyclip-1.1.0-cp311-cp311-win_amd64.whl (22.5 kB 查看哈希值)

上传时间 CPython 3.11 Windows x86-64

pypolyclip-1.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.6 kB 查看哈希值)

上传时间 CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

pypolyclip-1.1.0-cp311-cp311-macosx_11_0_arm64.whl (20.8 kB 查看哈希值)

上传时间 CPython 3.11 macOS 11.0+ ARM64

pypolyclip-1.1.0-cp311-cp311-macosx_10_9_x86_64.whl (20.7 kB 查看哈希值)

上传时间 CPython 3.11 macOS 10.9+ x86-64

pypolyclip-1.1.0-cp310-cp310-win_amd64.whl (22.5 kB 查看哈希值)

上传时间 CPython 3.10 Windows x86-64

pypolyclip-1.1.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.6 kB 查看哈希值)

上传时间 CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

pypolyclip-1.1.0-cp310-cp310-macosx_11_0_arm64.whl (20.8 kB 查看哈希值)

上传时间 CPython 3.10 macOS 11.0+ ARM64

pypolyclip-1.1.0-cp310-cp310-macosx_10_9_x86_64.whl (20.7 kB 查看哈希值)

上传时间 CPython 3.10 macOS 10.9+ x86-64

pypolyclip-1.1.0-cp39-cp39-win_amd64.whl (22.5 kB 查看哈希值)

上传时间 CPython 3.9 Windows x86-64

pypolyclip-1.1.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.3 kB 查看哈希值)

上传于 CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

pypolyclip-1.1.0-cp39-cp39-macosx_11_0_arm64.whl (20.8 kB 查看哈希值)

上传于 CPython 3.9 macOS 11.0+ ARM64

pypolyclip-1.1.0-cp39-cp39-macosx_10_9_x86_64.whl (20.7 kB 查看哈希值)

上传于 CPython 3.9 macOS 10.9+ x86-64