Skip to main content

A python driver to the polyclip functions

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypolyclip-1.1.0.tar.gz (27.5 kB view details)

Uploaded Source

Built Distributions

pypolyclip-1.1.0-cp312-cp312-win_amd64.whl (22.5 kB view details)

Uploaded 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 view details)

Uploaded 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 view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

pypolyclip-1.1.0-cp312-cp312-macosx_10_9_x86_64.whl (20.7 kB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

pypolyclip-1.1.0-cp311-cp311-win_amd64.whl (22.5 kB view details)

Uploaded 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 view details)

Uploaded 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 view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

pypolyclip-1.1.0-cp311-cp311-macosx_10_9_x86_64.whl (20.7 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

pypolyclip-1.1.0-cp310-cp310-win_amd64.whl (22.5 kB view details)

Uploaded 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 view details)

Uploaded 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 view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

pypolyclip-1.1.0-cp310-cp310-macosx_10_9_x86_64.whl (20.7 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pypolyclip-1.1.0-cp39-cp39-win_amd64.whl (22.5 kB view details)

Uploaded 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 view details)

Uploaded 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 view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

pypolyclip-1.1.0-cp39-cp39-macosx_10_9_x86_64.whl (20.7 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

File details

Details for the file pypolyclip-1.1.0.tar.gz.

File metadata

  • Download URL: pypolyclip-1.1.0.tar.gz
  • Upload date:
  • Size: 27.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pypolyclip-1.1.0.tar.gz
Algorithm Hash digest
SHA256 312e31034a5b8c4ef4226d391ace649c2475b35eb81795cc315e3bc66366ddb5
MD5 2fc4ae78dfd60de23813810e0e451af0
BLAKE2b-256 283383b01ff926e926de0a9693b3bf5867e8500293ec26da2465e0a8569a991c

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 dd3ce07e4220dbb521898bcc06bd73a4d65702ec5af52455840122f05d407c12
MD5 b0f6bbd6a6504d99d28cbf8486c59aaa
BLAKE2b-256 bf86e33c8f2307aa41c255dff75b17afce9025a45a214afae2ba53ec8cfa85fa

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9c538b65b212e15efe816a34f66e11599476606c53401ff9c0ce9b8009a758a1
MD5 e29169db7aabf2841e3a4567474cd0dd
BLAKE2b-256 ee56deef8779f20841a0bc2aa1faab84a13f407886e03eb2b9ab865a1192c6b2

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d1bf0c6473cce778d7152233d2d2c374a79d8335953a8de68db365cd0d192a89
MD5 644748df91e456b9985301c59d81a3bc
BLAKE2b-256 c3592f89ec0f65466e21aa43dedabbddba9680b2fba797b1105a4e14491652d4

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2640de8e33f9cb43b9fc7ff67d2cc4da7bef74f54a95b732fa38caf2eaa7b234
MD5 416c0affe03b6928ab8335adb323cc0f
BLAKE2b-256 072588ed0dad2101f8f66f71b56162689f4f7e4c3a50571ab0aab37d245af8c3

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2c72223e5ecc8894ea8d5e01a4bfe5b355dd0be85b0132b87b3baaba590fbc30
MD5 96c003f3cb20b24ea8ae50d932991d66
BLAKE2b-256 7ec8a19274e0ba2985df273502870f1e8f3b226cae3876d74c559625c963e0b1

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4475d61a715ff9a39d97af69ccc882be661a4f21f5c13a106e3b5abda713d5ec
MD5 6487dad66453d37472ed35fef98c6806
BLAKE2b-256 3637aa97cf6754b5d895eb10513afc9a3d666d7da5e20ba88de3583c3e99024e

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b123639e48a72828a2dee6aa73c7686206a0ebce0800f60987638a0be2ccbab2
MD5 2972ed0a9f35515803b79c44c2342b84
BLAKE2b-256 96b32897937e1ab152e6226308fbb0d810356845fa1f8631f4437e0af5a9751b

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 68ade279b466183c51834cb27cfcf44b52facfea215443e57bb280457f137d2e
MD5 6b7b7f120420d6ea2fec34841558249e
BLAKE2b-256 ceb66320bc73f3a0eb06a92bc2451e25f9a647cbe1e3a5ecbdcb64906480e897

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3aba1e8af46da4ca77b13a75ab5d07da4a8cdcfc66938fb424d9efa1d0bfecd2
MD5 127afa14373778b17a29a20836753cef
BLAKE2b-256 386dfa9a43bbe1202c0b7c326a9eafe7af1163a73e4b5e2ee116c0b6f9cc23b3

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ac2b5bf2875524f372e39b1fcf8fc5391803d18d44ec208220018cf2e0aa3903
MD5 cf07026c652533fb326c4951b7509a42
BLAKE2b-256 42fe49b108fcf850b1cdd67318c40084254517c0e929c341637c27f49038aec4

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4994dd61d5df3f6a13c7f0944225ae65f6ede2a4d46a3be9a62f3be8ca134292
MD5 3acddc0d3879d4046efc6441cf8be3bc
BLAKE2b-256 168d87b895f7e95bd2be77ee4e5d5f15b62c0f5d0dde460a82c4317b41d5390e

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ef67a2cc62dffd4b1f20d35e0efbbd0c7460847768e43e7dbbe54bb6288b185b
MD5 06930adc3f39c3c98c089781ae549246
BLAKE2b-256 2a46b9e3ec31f81a5c84e34609397fff80319a81413de7f32709924e7f3e7851

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 bd6ee56701da8f8f7cffb2eb8d3d2a593d1d6c1b663accc1bc0e675451f50779
MD5 8a6b03aafbb1816df2a5dc48ed3b9765
BLAKE2b-256 072f29e9ba0444165732d469cda3c32edf3c22dec60da7b0f2c04ed27aee067c

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b081f13043cf2db051edf56cef1b0dcbc7a97d917dd097e498fd1e9eefd19d42
MD5 e7bf7d787ab343af6d12bf241c4f91b7
BLAKE2b-256 6c043e4ef467e1c2780bd646912e62adaca8ce546ba574ed1557361bd8e8d840

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cb57afb0fc3a2044cc2b934675ca884fbc4cda1705a6be09e3612201b73d41d2
MD5 0b8239cc044c5d335f6ad7f0e385654a
BLAKE2b-256 5c8c754fe7fa257d200ff10e80187b43c6776c728515453d508ff6a36f8cf893

See more details on using hashes here.

File details

Details for the file pypolyclip-1.1.0-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.1.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 16e88f23fa27d89041b80c5b87150a0438b6617ecaf61e3e7a965b4554c2e28b
MD5 f409e6e0b4da01bc93b57a2d479e7b64
BLAKE2b-256 b06ed30be2e169ff9f81bd189fbab6ff44b583c1bf68a58af80fe6aa999dfe43

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page