Polygons: Fast points-in-polygon test and distances to polygons
Computes distances to polygon edges and vertices and can check whether points are inside/outside.
This library is optimized to perform well with hundreds or thousands of polygons and thousands or millions of points.
Example timings (190 polygons, 1 M reference points, run on i7-10710U):
- distances to nearest edges: 0.7 s
- distances to nearest vertices: 0.6 s
- check whether points are inside or outside: 0.1 s
Installation using pip
$ pip install polygons
Supported versions
- Python: 3.10 - 3.13
- Operating systems: Linux, macOS, and Windows
Capabilities
- Check whether points are inside or outside polygons
- Nearest distances to edges
- Nearest distances to vertices
Recommended citation
If you use this tool in a program or publication, please acknowledge its author(s):
@misc{polygons,
author = {Bast, Radovan},
title = {Polygons: Fast points-in-polygon test and distances to polygons},
month = {03},
year = {2025},
publisher = {Zenodo},
version = {v0.3.5},
doi = {10.5281/zenodo.3825616},
url = {https://doi.org/10.5281/zenodo.3825616}
}
Python example
import polygons
# polygon_points is a list of lists
# the library has been developed to perform
# with very many polygons - this is just to have a simple example
# in this example the polygons have the same number of points but there
# is no restriction like this, this is only an example
polygon_points = [
[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)],
[(0.0, 2.0), (1.0, 2.0), (1.0, 3.0), (0.0, 3.0)],
]
# the more points you compute in one go, the better
# here using two points to make a simple example but if you have many points
# then compute a thousand or a million in one go
# so that the library can parallelize over the points
points = [(0.5, 0.5), (0.5, -0.5)]
# parameters for the tree construction:
# - each tree node has 4 children nodes
# - each leaf collects 4 edges
# you can try different parameters and check the timing
# they (should) have no effect on the results apart from timing
num_edges_children = 4
num_nodes_children = 4
tree = polygons.build_search_tree(
polygon_points, num_edges_children, num_nodes_children
)
inside = polygons.points_are_inside(tree, points)
print(inside) # [True, False]
# indices are the indices of the nearest polygon vertices (counted
# consecutively)
indices, distances = polygons.distances_nearest_vertices(tree, points)
print(indices) # [0, 0]
print(distances) # [0.7071067811865476, 0.7071067811865476]
distances = polygons.distances_nearest_edges(tree, points)
print(distances) # [0.5, 0.5]
indices, distances = polygons.distances_nearest_vertices(
tree, [(0.6, 0.6), (0.5, -0.5)]
)
print(indices) # [2, 0]
print(distances) # [0.5656854249492381, 0.7071067811865476]
References which were used during coding
- http://geomalgorithms.com/a03-_inclusion.html
- https://en.wikipedia.org/wiki/Point_in_polygon
- https://en.wikipedia.org/wiki/Binary_space_partitioning
Development notes
Running the benchmark:
$ cargo test --release -- --ignored --nocapture
Python interface inspired by https://github.com/dev-cafe/rustafarian.
Building and testing the Python interface:
$ maturin develop
Image
Social media preview generated using https://github.com/qrohlf/trianglify.
Metadata
Release files for polygons 0.3.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
Total release size: 2.9 MB
Release files / polygons-0.3.5-cp313-cp313-win_amd64.whl
| Download URL | polygons-0.3.5-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 183.6 kB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
7eddda43cbbe3a72c8676badd43b4dc7d720fc1b9f1a32ba3b0f5cd1338d49c4
|
|
BLAKE2b-256 checksum How to use checksums |
fd9233c5c9f7b87b1dba856029daff13129dbbf08a9737f44fa12824068357c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.3
|
Release files / polygons-0.3.5-cp313-cp313-manylinux_2_34_x86_64.whl
| Download URL | polygons-0.3.5-cp313-cp313-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 285.3 kB |
| Tags | CPython 3.13 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
e0d4ff9993d5570e0863205d665dc5522a4079f41a26611416f343ab321ded9f
|
|
BLAKE2b-256 checksum How to use checksums |
0e7145baf0f4c7af2c90204da4ca066c48055df237b2db08afbc8d9192193a70
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.3
|
Release files / polygons-0.3.5-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | polygons-0.3.5-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 247.1 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
d82ba96665c44bfd47ad3ccb96d994e5f01310c2023ad241239b9502d2cab07a
|
|
BLAKE2b-256 checksum How to use checksums |
8101a280dd4098dffc1aab310f44deb320a1ecd8ab14bd5ed1dd9c04de338415
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.3
|
Release files / polygons-0.3.5-cp312-cp312-win_amd64.whl
| Download URL | polygons-0.3.5-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 183.9 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
2b60f2486801a7ff1e1391109801a12ada5f1ecc7410aeea3e0773bf550428b2
|
|
BLAKE2b-256 checksum How to use checksums |
2ece6ba366aafa7a5f927ebb6c0eb96213c3e4bf62b2e7ce3c42ba8d88eaf5d1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.3
|
Release files / polygons-0.3.5-cp312-cp312-manylinux_2_34_x86_64.whl
| Download URL | polygons-0.3.5-cp312-cp312-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 285.4 kB |
| Tags | CPython 3.12 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
5fd49ad3172163794c6e3202e923b51692e5389261b3b7ba11c5f8fc119e647a
|
|
BLAKE2b-256 checksum How to use checksums |
b701ab3d5f7326069a85258655cdbdf8d06b28f2ff399cf23b115d1ce9b946db
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.3
|
Release files / polygons-0.3.5-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | polygons-0.3.5-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 246.9 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
a82067e58377417723d948e428c0aaba1ac43ca64d2a383d710bb026b6d41742
|
|
BLAKE2b-256 checksum How to use checksums |
630930e2ad4cd6f3b1f5693d88697e87e6dd60d66cbd1ed716826d8647d97e3a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.3
|
Release files / polygons-0.3.5-cp311-cp311-win_amd64.whl
| Download URL | polygons-0.3.5-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 183.8 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
f69b165f4b454eccc8177226189c7d95fd1d4999b8be7003e504e7bfe3a54453
|
|
BLAKE2b-256 checksum How to use checksums |
59815ca25fc359e050f86201fdda0fadda12a5e43bd2af01d964ddaf24bcaf09
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.3
|
Release files / polygons-0.3.5-cp311-cp311-manylinux_2_34_x86_64.whl
| Download URL | polygons-0.3.5-cp311-cp311-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 286.9 kB |
| Tags | CPython 3.11 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
1b8110470a7d1339b5c3948aa736e1cee42c158475c91fed0ef8854e95b415bb
|
|
BLAKE2b-256 checksum How to use checksums |
b27266326daacfb7e122dca000103b064897b1043cc6f18b72bf2de1f3dec247
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.3
|
Release files / polygons-0.3.5-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | polygons-0.3.5-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 249.6 kB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
a762d3fb27be4adfcfc4053ef6a47c43b33bbb2f8a6f0c2882d85eb2242d3da0
|
|
BLAKE2b-256 checksum How to use checksums |
26a941e997cfdaa33032aaa0922a93bc6accae69289006a5b4e651e910cef080
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.3
|
Release files / polygons-0.3.5-cp310-cp310-win_amd64.whl
| Download URL | polygons-0.3.5-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 183.8 kB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
b8f456d8fa139583ba66f265beb02fb7c20e44ef5ea59a050bdc724122309e0f
|
|
BLAKE2b-256 checksum How to use checksums |
8c023abfe0e8fd9e9a8d11200d759eaca7cfdbc441c85e3c0432ebd8f5711d95
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.3
|
Release files / polygons-0.3.5-cp310-cp310-manylinux_2_34_x86_64.whl
| Download URL | polygons-0.3.5-cp310-cp310-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 287.0 kB |
| Tags | CPython 3.10 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
65895021a60dc1fa61f3a1d11bd8511c576ed4862a3937ad0f5568b9bad10c82
|
|
BLAKE2b-256 checksum How to use checksums |
b05f66490794fa419635702769634fad498b5b41c466b7fea7d940e0868fe919
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.3
|
Release files / polygons-0.3.5-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | polygons-0.3.5-cp310-cp310-macosx_11_0_arm64.whl |
|---|---|
| Size | 249.6 kB |
| Tags | CPython 3.10 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
069cf8de2214b0195025da824b464d80b7aa5afee1457f71930bdc54e5351076
|
|
BLAKE2b-256 checksum How to use checksums |
51bd640a49a1790480707c85de57f3ff2f47aaad1307da2ed648b6dc3a4c37e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.3
|