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.2.0.tar.gz (166.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pypolyclip-1.2.0-cp314-cp314-win_amd64.whl (22.5 kB view details)

Uploaded CPython 3.14Windows x86-64

pypolyclip-1.2.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (38.8 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

pypolyclip-1.2.0-cp314-cp314-macosx_11_0_arm64.whl (20.2 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

pypolyclip-1.2.0-cp314-cp314-macosx_10_15_x86_64.whl (20.1 kB view details)

Uploaded CPython 3.14macOS 10.15+ x86-64

pypolyclip-1.2.0-cp313-cp313-win_amd64.whl (22.2 kB view details)

Uploaded CPython 3.13Windows x86-64

pypolyclip-1.2.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (38.8 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

pypolyclip-1.2.0-cp313-cp313-macosx_11_0_arm64.whl (20.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pypolyclip-1.2.0-cp313-cp313-macosx_10_13_x86_64.whl (20.1 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

pypolyclip-1.2.0-cp312-cp312-win_amd64.whl (22.2 kB view details)

Uploaded CPython 3.12Windows x86-64

pypolyclip-1.2.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (38.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

pypolyclip-1.2.0-cp312-cp312-macosx_11_0_arm64.whl (20.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pypolyclip-1.2.0-cp312-cp312-macosx_10_13_x86_64.whl (20.1 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

pypolyclip-1.2.0-cp311-cp311-win_amd64.whl (22.1 kB view details)

Uploaded CPython 3.11Windows x86-64

pypolyclip-1.2.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (37.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

pypolyclip-1.2.0-cp311-cp311-macosx_11_0_arm64.whl (20.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

pypolyclip-1.2.0-cp311-cp311-macosx_10_9_x86_64.whl (20.2 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: pypolyclip-1.2.0.tar.gz
  • Upload date:
  • Size: 166.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pypolyclip-1.2.0.tar.gz
Algorithm Hash digest
SHA256 1acc60e5337c90d7c4bcf9f77037a8a6e4427fddaa7f598bf3c0cac2e0e68b91
MD5 1ff35495321d6a5b8be9e4b2721b31dd
BLAKE2b-256 4a201436811539932f67374ab6f65022b0f00f4f3bbc7c717e6c5b0b10d9ee52

See more details on using hashes here.

File details

Details for the file pypolyclip-1.2.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: pypolyclip-1.2.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pypolyclip-1.2.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 dbe4654116e7367ee46ff10053052491efd67c3c3ca096d0d4aaf194d74b8437
MD5 b6eeb96cbd26b8b792b6b5e47103d119
BLAKE2b-256 b6755a572ac02a11fb9ce1a42b4187482f3370f16581d9d294ff5904baea40a7

See more details on using hashes here.

File details

Details for the file pypolyclip-1.2.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.2.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 c91c85ba7579ea51f9570fe4dab97682416118e132795760ca207404a078e244
MD5 9ccc780c097e10bcc40438bfb147f3c6
BLAKE2b-256 c281532ec21b94290ae6de36ef2bf57e9da4d4c739cb41aa7398ac28ce6d50b6

See more details on using hashes here.

File details

Details for the file pypolyclip-1.2.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.2.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fffa6d2c38bba3bd732450ac952cae32b25a77a5e6bc73c8db22be11d3a80d81
MD5 c7408fd1a11730aee73f117710cdbd13
BLAKE2b-256 7cac2c6c6857c1d832f336e3d0152da19e5ec4b359ff927ba7c5b26440f8f471

See more details on using hashes here.

File details

Details for the file pypolyclip-1.2.0-cp314-cp314-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.2.0-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 b00aec9a4157797954f21c40fae787561e0bf9909ecfbb18c62dc9946152dfc5
MD5 0ef051d132fee4c3c04f9cf6e0b90b8d
BLAKE2b-256 0ec636821b1d957e3749489fa1d0f299c89479e7cbe80e5322d4d65b708ebb44

See more details on using hashes here.

File details

Details for the file pypolyclip-1.2.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: pypolyclip-1.2.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pypolyclip-1.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 0a44ce5a73f937f4b4ce0dc1a07c3e41294b8a977f55e872f3f554ded0924ace
MD5 2e8aeb6a14b30874209dca3397b90322
BLAKE2b-256 5feb5cb07af56ce0025ab80cef35c4d20982efb59b1c2d6a8549e7bc4f7dc287

See more details on using hashes here.

File details

Details for the file pypolyclip-1.2.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.2.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 d45b192443b69759ef4eb47509cc358777a4f0466b5dfee4eb986ec17a88fce3
MD5 e2f5b6d5dba2f1bd7b0ef7a1f174487d
BLAKE2b-256 5c66d5dfb27c5ce443697cc155b1e93372eb81ce5b1d2c960f3433405e6b09b2

See more details on using hashes here.

File details

Details for the file pypolyclip-1.2.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 80c7587f960d65c3d1216f83c9159a20068445f563fbf2cf47eb6cc6ff4683a9
MD5 bfa2506a679ee3cadd09f31534cdd800
BLAKE2b-256 935e6749bc4370ad03476593ff88bd7702df35a4d391443e2fc11ffa4f05f9c7

See more details on using hashes here.

File details

Details for the file pypolyclip-1.2.0-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.2.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 93b1837671ed838697dbeb108170e1a462d2334e250048a52df3dbec69b5d549
MD5 3e113831aa3b91bab01d748620c3796f
BLAKE2b-256 687cddf069a6a072c07d17273f16c8c6cb06c754393239c2d5c5af15086563fd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypolyclip-1.2.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pypolyclip-1.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3cf2ce37d4eaaa1769afa5d65ff15c1f2e47e4c5de02a9003f5735f89353a519
MD5 54a334c39aaabfa634c5672c431ccf31
BLAKE2b-256 85f541755ee1bbf9a158a1c6a1557608bf0072f03013b0222fae8abb6d1573bc

See more details on using hashes here.

File details

Details for the file pypolyclip-1.2.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.2.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 9c0919511d23f57c0387dd0474dc093cce8638c6aa27295b54b1597a5dbf3218
MD5 4e8928b7456849f35db0a8c97c8626af
BLAKE2b-256 705d4fc836b76395a4036db73b61fc4f4a9db0dd7b44e2753828549bcc2615c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pypolyclip-1.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7028be88ccf263283846fdb02db67138cbcc6389eaa393b96160ff0096c4c068
MD5 ec1fb6af170da6ba15e438c7d1eab504
BLAKE2b-256 df35d423d92ccfc38a9ba74ee826c6840eaa8921ae4ef6bf1618a4c47323cba1

See more details on using hashes here.

File details

Details for the file pypolyclip-1.2.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.2.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 f8243e8394dd0f6e2134e4f0b075ee41e4de633bb92b40cd2c87d15b713838a8
MD5 bc1b7834022d6e91b5b51e79b6a97a2d
BLAKE2b-256 36a86ccb974359d355132c98c5102abcd1426e80c43d132a73498ebb028edb29

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypolyclip-1.2.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pypolyclip-1.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 de95dbb3509bd2d23547eda895913393bb8bc6596fa7567587c1c028db084b4c
MD5 d130c7b9cf6b063adf372934a92809d3
BLAKE2b-256 ac33d1d53897155215e10a74ad59b59177bd743f6ea380f47e57e2a87c31e58f

See more details on using hashes here.

File details

Details for the file pypolyclip-1.2.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for pypolyclip-1.2.0-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 b05c5b3614f261e0639ae20d33b3dd9010a76a51d49cbafd64e8020ae9928b4d
MD5 3860c2c00daaea3977f5eed812f4be00
BLAKE2b-256 3ff83f115905807ea6730433f3f601e234da5a2e730c76eaf5145ec31378f5c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pypolyclip-1.2.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2101f2716cc4566cad4df7b73ec02a822c3bf4c96ccb30075695abc93ac69823
MD5 646835dd5917d57d1b47a379ebc2e6ef
BLAKE2b-256 88d6d4b7244c352f0a7dfd9c1ef059a10fae9dd244a30accf9fd9c1e173e85d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pypolyclip-1.2.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 54661b2a308ca47c0c9c206d7877652cbed4808e8e891d2201098cd7a458991b
MD5 a5d5f02323aedf8ece9c22fc755679c7
BLAKE2b-256 d79afd3e831d81f7207ea25e68da8ebf204da4d0462a205cd6f41244c3ce5447

See more details on using hashes here.

Supported by

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