A python driver to the polyclip functions
Project description
pypolyclip
A python driver for polyclip written in C by J.D. Smith.
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 tessalated grid of square pixels.
Therefore, this differs from similar packages, which often clip between
two arbitrary polygons. The testing function test/test_pypolyclip.py
can be invoked to produce the following example figures:
The first figure shows clipping of polygons with differing numbers of
vertices. Programmatically, this requires explicit for-loops in Python,
but if the number of vertices is the same for all polygons (such as
the second figure), the numpy
can be used to improve performance by
several percent. 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 (recalling 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 area of the polygon.
Example usage
This first example demonstrates polygons with the same number of vertices:
# import relevant modules
import pypolyclip
import numpy as np
# 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]], dtype=float)
# 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]], dtype=float)
# call the clipper
xc, yc, area, slices = pypolyclip.multi(px, py, naxis)
# xc, yc are the coordinates in the grid
# area is the relative pixel area in that grid cell
# slices is a list of slice objects to link between the polygons and clipped pixel grid
# use these things like
for i, s in enumerate(slices):
print(f'total area for polygon {i}={np.sum(area[s])}')
This second example demonstrates polygons with the different number of
vertices (notice the difference is in the datatype of the px
and py
variables):
# import relevant modules
import pypolyclip
import numpy as np
# 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 = pypolyclip.multi(px, py, naxis)
# xc, yc are the coordinates in the grid
# area is the relative pixel area in that grid cell
# slices is a list of slice objects to link between the polygons and clipped pixel grid
# use these things like
for i, s in enumerate(slices):
print(f'total area for polygon {i}={np.sum(area[s])}')
See also test/test_pypolyclip.py
for examples.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pypolyclip-0.1a0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 547c2af22591cc8a837cccde51eb9e3053a210a258cea80e129816c048f64806 |
|
MD5 | a4a7a5d74653815dd608f673d00d1d76 |
|
BLAKE2b-256 | 792904ec482f225148d03db75c3dde2576deb6c92cf73fbb296bb6204ae79df3 |
Hashes for pypolyclip-0.1a0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b15ef5e3d5efa6beb9ddee9dbde2b14c717ba8d66ea1cc8a40c32757388d91f |
|
MD5 | 82e5c381b3dbb3059effd754ad57eea9 |
|
BLAKE2b-256 | ceffd78f63e82ae1e661ab94b7fd20a8f182a737555cdc477b9ea555167db0ac |
Hashes for pypolyclip-0.1a0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa0f763eabd015198fd6f7a59185743807a9d4b6995a0633cc7cb19062288533 |
|
MD5 | 8f4d3ba8668c8a7831b4bf732ba41029 |
|
BLAKE2b-256 | bf787cdc1ecbf7efe98778fec2ea309e64dd35db2296b0465413222ba4188242 |
Hashes for pypolyclip-0.1a0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f1ccba01b22566f9892730f109486ef948663cb7feaf66af214796099df10f8 |
|
MD5 | 1b08c49f4ecda37a57c936e540bd60f4 |
|
BLAKE2b-256 | f9e13b3af3d25010f1d8754d7c4fd04211725f2a8f34d56728172df29dfa1f1f |
Hashes for pypolyclip-0.1a0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09c46db111c2d0066c7624e2b5cf17dc80ec7bd49c85a47b7d30c9d6bac668e6 |
|
MD5 | c2572bb833a97d8f5235f6d9dbfb78e7 |
|
BLAKE2b-256 | fda9a49176fac33b0223bae01606c942cafa6dd4cd6f2c1f170fcd2ac2222897 |
Hashes for pypolyclip-0.1a0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6ebce4813b8c598177e4a550734ae9709ba570ea1768a7ae5db9eec60cf5ab5 |
|
MD5 | a09a0e86c82d4bc5265ec8feceab22cf |
|
BLAKE2b-256 | 225b6e0693032db24c721affd4e04f4f7b118e3198bea2f9dc35c32d9502adac |
Hashes for pypolyclip-0.1a0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e488cfc586065bf67e3de7c35bb7a4b86191ee3840ee73a51354938626fe44df |
|
MD5 | 9cea868ec8bd15cce244acec9068a941 |
|
BLAKE2b-256 | cae6e5f93218d3043cd6f933afffc7b5b280846bb9e6b627d6a01fee2b47461d |
Hashes for pypolyclip-0.1a0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | edadcc670e9a0ad0230ae8fc90998901c6b66ae8791d1b7fcc732bcdfd2d8224 |
|
MD5 | 56944fc187c8730a31dbe02343719043 |
|
BLAKE2b-256 | 0699c8dfc942c90d58bac85a43aaec96b8c663a5490af85109bcd9adf69cad5c |
Hashes for pypolyclip-0.1a0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf6fdf7c6da809f00fc697833861efc5302b407851ed095da7bd3a839839526f |
|
MD5 | e7f65a11d1a635f441619c852c2f3028 |
|
BLAKE2b-256 | 95562f6af9c2ead990df42c2e57269f2fb4515e91f81b2ff6a3713190fb3c52e |
Hashes for pypolyclip-0.1a0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2675c4312f8fa898eaa6862a7609d434817f66aa04463a2f393d5e9b203a60a2 |
|
MD5 | b0b6e321261657444e2c0a705bb273f5 |
|
BLAKE2b-256 | 2fcbfe51553b8637835204f47ab9ae97661d9ff4fcd01d50599e0ff9fe08ea7f |
Hashes for pypolyclip-0.1a0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebc16d8af0f544ce7412bc8daa2ed01036880d3e98b373dd586611d0582d6ba6 |
|
MD5 | ec172138dcf1397685635cd206417bbb |
|
BLAKE2b-256 | b3e4ce780642a6ff92c4c4969ff552b3394406b0e885ec0005e31514aca758be |
Hashes for pypolyclip-0.1a0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd7918fe6a82e932ccc9883604eb096abb4a2d954f0099bfd4e839dbf16e62c2 |
|
MD5 | 7076889e95f0d4be235a727cb6250b41 |
|
BLAKE2b-256 | 044dda701ae2ce1728d00482c114bda42ff579a7fe0de112f5837fecc255e544 |