Skip to main content

pypolyclip

CI Tests coverage Zenodo

A Python package to clip polygons against a pixel grid.

The polyclip functions were originally developed for the CUBISM project Smith et al. 2007 (PASP 119, 1133).

Installation

The package can be installed using pip from the command line:

pip install pypolyclip

Description

The polyclip code employs the Sutherland-Hodgman algorithm to clip simple polygons against a tessellated grid of square pixels. Therefore, this differs from similar packages, which often clip between two arbitrary polygons.

The test module test_pypolyclip.py can be run to produce the following example figures:

In each figure, the Cartesian coordinates for each pixel that overlaps with a given polygon are labeled with the area of that pixel that is covered (the area of a pixel is defined as 1). Therefore, the sum of the areas of the individual pixels for each polygon should be the total area of the polygon.

Pypolyclip uses a coordinate grid where integer pixel coordinates are located at the lower-left corner of each pixel, starting from zero. To clip polygons on a coordinate grid where integer pixel coordinates are located at the center of pixels, one will need to add 0.5 pixel to both the x and y vertices of the input polygons.

The first figure shows clipping of polygons with differing numbers of vertices, which internally requires the use of "for loops". However, if the number of vertices is the same for all polygons (such as the second figure), then NumPy is used internally to improve performance by several percent.

Example usage

This first example demonstrates polygons with the same number of vertices:

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])}')

This second example demonstrates clipping polygons that have a different number of vertices. Note that px and py are lists of lists instead of NumPy arrays as in the first example.

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])}')

Metadata

Release files for pypolyclip 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pypolyclip 1.2.0
File Size Uploaded Provenance
pypolyclip-1.2.0.tar.gz 166.5 kB Without provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for pypolyclip 1.2.0
File
pypolyclip-1.2.0-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
pypolyclip-1.2.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.28+ x86-64, Linux glibc 2.5+ x86-64 Details
pypolyclip-1.2.0-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details
pypolyclip-1.2.0-cp314-cp314-macosx_10_15_x86_64.whl CPython 3.14 CPython 3.14 macOS 10.15+ x86-64 Details
pypolyclip-1.2.0-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
pypolyclip-1.2.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.5+ x86-64, Linux glibc 2.28+ x86-64 Details
pypolyclip-1.2.0-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
pypolyclip-1.2.0-cp313-cp313-macosx_10_13_x86_64.whl CPython 3.13 CPython 3.13 macOS 10.13+ x86-64 Details
pypolyclip-1.2.0-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
pypolyclip-1.2.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.5+ x86-64, Linux glibc 2.28+ x86-64 Details
pypolyclip-1.2.0-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
pypolyclip-1.2.0-cp312-cp312-macosx_10_13_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.13+ x86-64 Details
pypolyclip-1.2.0-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
pypolyclip-1.2.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.5+ x86-64, Linux glibc 2.28+ x86-64 Details
pypolyclip-1.2.0-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
pypolyclip-1.2.0-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details

Total release size: 571.2 kB

Release files / pypolyclip-1.2.0.tar.gz

Download URL pypolyclip-1.2.0.tar.gz
Size 166.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1acc60e5337c90d7c4bcf9f77037a8a6e4427fddaa7f598bf3c0cac2e0e68b91
BLAKE2b-256 checksum
How to use checksums
4a201436811539932f67374ab6f65022b0f00f4f3bbc7c717e6c5b0b10d9ee52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp314-cp314-win_amd64.whl

Download URL pypolyclip-1.2.0-cp314-cp314-win_amd64.whl
Size 22.5 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
dbe4654116e7367ee46ff10053052491efd67c3c3ca096d0d4aaf194d74b8437
BLAKE2b-256 checksum
How to use checksums
b6755a572ac02a11fb9ce1a42b4187482f3370f16581d9d294ff5904baea40a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL pypolyclip-1.2.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 38.8 kB
Tags CPython 3.14 Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
c91c85ba7579ea51f9570fe4dab97682416118e132795760ca207404a078e244
BLAKE2b-256 checksum
How to use checksums
c281532ec21b94290ae6de36ef2bf57e9da4d4c739cb41aa7398ac28ce6d50b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp314-cp314-macosx_11_0_arm64.whl

Download URL pypolyclip-1.2.0-cp314-cp314-macosx_11_0_arm64.whl
Size 20.2 kB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
fffa6d2c38bba3bd732450ac952cae32b25a77a5e6bc73c8db22be11d3a80d81
BLAKE2b-256 checksum
How to use checksums
7cac2c6c6857c1d832f336e3d0152da19e5ec4b359ff927ba7c5b26440f8f471
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp314-cp314-macosx_10_15_x86_64.whl

Download URL pypolyclip-1.2.0-cp314-cp314-macosx_10_15_x86_64.whl
Size 20.1 kB
Tags CPython 3.14 macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
b00aec9a4157797954f21c40fae787561e0bf9909ecfbb18c62dc9946152dfc5
BLAKE2b-256 checksum
How to use checksums
0ec636821b1d957e3749489fa1d0f299c89479e7cbe80e5322d4d65b708ebb44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp313-cp313-win_amd64.whl

Download URL pypolyclip-1.2.0-cp313-cp313-win_amd64.whl
Size 22.2 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
0a44ce5a73f937f4b4ce0dc1a07c3e41294b8a977f55e872f3f554ded0924ace
BLAKE2b-256 checksum
How to use checksums
5feb5cb07af56ce0025ab80cef35c4d20982efb59b1c2d6a8549e7bc4f7dc287
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL pypolyclip-1.2.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 38.8 kB
Tags CPython 3.13 Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
d45b192443b69759ef4eb47509cc358777a4f0466b5dfee4eb986ec17a88fce3
BLAKE2b-256 checksum
How to use checksums
5c66d5dfb27c5ce443697cc155b1e93372eb81ce5b1d2c960f3433405e6b09b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp313-cp313-macosx_11_0_arm64.whl

Download URL pypolyclip-1.2.0-cp313-cp313-macosx_11_0_arm64.whl
Size 20.2 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
80c7587f960d65c3d1216f83c9159a20068445f563fbf2cf47eb6cc6ff4683a9
BLAKE2b-256 checksum
How to use checksums
935e6749bc4370ad03476593ff88bd7702df35a4d391443e2fc11ffa4f05f9c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp313-cp313-macosx_10_13_x86_64.whl

Download URL pypolyclip-1.2.0-cp313-cp313-macosx_10_13_x86_64.whl
Size 20.1 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
93b1837671ed838697dbeb108170e1a462d2334e250048a52df3dbec69b5d549
BLAKE2b-256 checksum
How to use checksums
687cddf069a6a072c07d17273f16c8c6cb06c754393239c2d5c5af15086563fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp312-cp312-win_amd64.whl

Download URL pypolyclip-1.2.0-cp312-cp312-win_amd64.whl
Size 22.2 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
3cf2ce37d4eaaa1769afa5d65ff15c1f2e47e4c5de02a9003f5735f89353a519
BLAKE2b-256 checksum
How to use checksums
85f541755ee1bbf9a158a1c6a1557608bf0072f03013b0222fae8abb6d1573bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL pypolyclip-1.2.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 38.8 kB
Tags CPython 3.12 Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
9c0919511d23f57c0387dd0474dc093cce8638c6aa27295b54b1597a5dbf3218
BLAKE2b-256 checksum
How to use checksums
705d4fc836b76395a4036db73b61fc4f4a9db0dd7b44e2753828549bcc2615c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp312-cp312-macosx_11_0_arm64.whl

Download URL pypolyclip-1.2.0-cp312-cp312-macosx_11_0_arm64.whl
Size 20.2 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7028be88ccf263283846fdb02db67138cbcc6389eaa393b96160ff0096c4c068
BLAKE2b-256 checksum
How to use checksums
df35d423d92ccfc38a9ba74ee826c6840eaa8921ae4ef6bf1618a4c47323cba1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp312-cp312-macosx_10_13_x86_64.whl

Download URL pypolyclip-1.2.0-cp312-cp312-macosx_10_13_x86_64.whl
Size 20.1 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
f8243e8394dd0f6e2134e4f0b075ee41e4de633bb92b40cd2c87d15b713838a8
BLAKE2b-256 checksum
How to use checksums
36a86ccb974359d355132c98c5102abcd1426e80c43d132a73498ebb028edb29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp311-cp311-win_amd64.whl

Download URL pypolyclip-1.2.0-cp311-cp311-win_amd64.whl
Size 22.1 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
de95dbb3509bd2d23547eda895913393bb8bc6596fa7567587c1c028db084b4c
BLAKE2b-256 checksum
How to use checksums
ac33d1d53897155215e10a74ad59b59177bd743f6ea380f47e57e2a87c31e58f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL pypolyclip-1.2.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 37.9 kB
Tags CPython 3.11 Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
b05c5b3614f261e0639ae20d33b3dd9010a76a51d49cbafd64e8020ae9928b4d
BLAKE2b-256 checksum
How to use checksums
3ff83f115905807ea6730433f3f601e234da5a2e730c76eaf5145ec31378f5c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp311-cp311-macosx_11_0_arm64.whl

Download URL pypolyclip-1.2.0-cp311-cp311-macosx_11_0_arm64.whl
Size 20.3 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
2101f2716cc4566cad4df7b73ec02a822c3bf4c96ccb30075695abc93ac69823
BLAKE2b-256 checksum
How to use checksums
88d6d4b7244c352f0a7dfd9c1ef059a10fae9dd244a30accf9fd9c1e173e85d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / pypolyclip-1.2.0-cp311-cp311-macosx_10_9_x86_64.whl

Download URL pypolyclip-1.2.0-cp311-cp311-macosx_10_9_x86_64.whl
Size 20.2 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
54661b2a308ca47c0c9c206d7877652cbed4808e8e891d2201098cd7a458991b
BLAKE2b-256 checksum
How to use checksums
d79afd3e831d81f7207ea25e68da8ebf204da4d0462a205cd6f41244c3ce5447
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Security for pypolyclip 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page