Skip to main content

Fast points-in-polygon test and distances to polygons.

Project description

test status license badge link to Crates link to PyPI link to Zenodo/DOI

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

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.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

polygons-0.3.5-cp313-cp313-win_amd64.whl (183.6 kB view details)

Uploaded CPython 3.13Windows x86-64

polygons-0.3.5-cp313-cp313-manylinux_2_34_x86_64.whl (285.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

polygons-0.3.5-cp313-cp313-macosx_11_0_arm64.whl (247.1 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

polygons-0.3.5-cp312-cp312-win_amd64.whl (183.9 kB view details)

Uploaded CPython 3.12Windows x86-64

polygons-0.3.5-cp312-cp312-manylinux_2_34_x86_64.whl (285.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

polygons-0.3.5-cp312-cp312-macosx_11_0_arm64.whl (246.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

polygons-0.3.5-cp311-cp311-win_amd64.whl (183.8 kB view details)

Uploaded CPython 3.11Windows x86-64

polygons-0.3.5-cp311-cp311-manylinux_2_34_x86_64.whl (286.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

polygons-0.3.5-cp311-cp311-macosx_11_0_arm64.whl (249.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

polygons-0.3.5-cp310-cp310-win_amd64.whl (183.8 kB view details)

Uploaded CPython 3.10Windows x86-64

polygons-0.3.5-cp310-cp310-manylinux_2_34_x86_64.whl (287.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

polygons-0.3.5-cp310-cp310-macosx_11_0_arm64.whl (249.6 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file polygons-0.3.5-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for polygons-0.3.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7eddda43cbbe3a72c8676badd43b4dc7d720fc1b9f1a32ba3b0f5cd1338d49c4
MD5 862d668850b20179aac1954c07ac1214
BLAKE2b-256 fd9233c5c9f7b87b1dba856029daff13129dbbf08a9737f44fa12824068357c4

See more details on using hashes here.

File details

Details for the file polygons-0.3.5-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for polygons-0.3.5-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 e0d4ff9993d5570e0863205d665dc5522a4079f41a26611416f343ab321ded9f
MD5 de31ef70e4f711f72ca1cceb927660e2
BLAKE2b-256 0e7145baf0f4c7af2c90204da4ca066c48055df237b2db08afbc8d9192193a70

See more details on using hashes here.

File details

Details for the file polygons-0.3.5-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polygons-0.3.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d82ba96665c44bfd47ad3ccb96d994e5f01310c2023ad241239b9502d2cab07a
MD5 6a4b419f3c87e4bd8b0ebf3fed0c31b4
BLAKE2b-256 8101a280dd4098dffc1aab310f44deb320a1ecd8ab14bd5ed1dd9c04de338415

See more details on using hashes here.

File details

Details for the file polygons-0.3.5-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for polygons-0.3.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2b60f2486801a7ff1e1391109801a12ada5f1ecc7410aeea3e0773bf550428b2
MD5 575f2747f92b55d47b8c10f501fcce85
BLAKE2b-256 2ece6ba366aafa7a5f927ebb6c0eb96213c3e4bf62b2e7ce3c42ba8d88eaf5d1

See more details on using hashes here.

File details

Details for the file polygons-0.3.5-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for polygons-0.3.5-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 5fd49ad3172163794c6e3202e923b51692e5389261b3b7ba11c5f8fc119e647a
MD5 5d7d7e7c7602f8398e3d1f7daa254da7
BLAKE2b-256 b701ab3d5f7326069a85258655cdbdf8d06b28f2ff399cf23b115d1ce9b946db

See more details on using hashes here.

File details

Details for the file polygons-0.3.5-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polygons-0.3.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a82067e58377417723d948e428c0aaba1ac43ca64d2a383d710bb026b6d41742
MD5 3f148a6c7c1346a95fbf98a04955e001
BLAKE2b-256 630930e2ad4cd6f3b1f5693d88697e87e6dd60d66cbd1ed716826d8647d97e3a

See more details on using hashes here.

File details

Details for the file polygons-0.3.5-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for polygons-0.3.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f69b165f4b454eccc8177226189c7d95fd1d4999b8be7003e504e7bfe3a54453
MD5 aa345115f648f4c146ddb4d92bab1af3
BLAKE2b-256 59815ca25fc359e050f86201fdda0fadda12a5e43bd2af01d964ddaf24bcaf09

See more details on using hashes here.

File details

Details for the file polygons-0.3.5-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for polygons-0.3.5-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 1b8110470a7d1339b5c3948aa736e1cee42c158475c91fed0ef8854e95b415bb
MD5 438b75baa6044c765e3ad20565ac72d4
BLAKE2b-256 b27266326daacfb7e122dca000103b064897b1043cc6f18b72bf2de1f3dec247

See more details on using hashes here.

File details

Details for the file polygons-0.3.5-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polygons-0.3.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a762d3fb27be4adfcfc4053ef6a47c43b33bbb2f8a6f0c2882d85eb2242d3da0
MD5 788c9a1882af03381ea59baff2b6c815
BLAKE2b-256 26a941e997cfdaa33032aaa0922a93bc6accae69289006a5b4e651e910cef080

See more details on using hashes here.

File details

Details for the file polygons-0.3.5-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for polygons-0.3.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b8f456d8fa139583ba66f265beb02fb7c20e44ef5ea59a050bdc724122309e0f
MD5 cecae7f564fed757cf87577cb08ec0cb
BLAKE2b-256 8c023abfe0e8fd9e9a8d11200d759eaca7cfdbc441c85e3c0432ebd8f5711d95

See more details on using hashes here.

File details

Details for the file polygons-0.3.5-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for polygons-0.3.5-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 65895021a60dc1fa61f3a1d11bd8511c576ed4862a3937ad0f5568b9bad10c82
MD5 fc799755419d660ef6fb7ff36b13094e
BLAKE2b-256 b05f66490794fa419635702769634fad498b5b41c466b7fea7d940e0868fe919

See more details on using hashes here.

File details

Details for the file polygons-0.3.5-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polygons-0.3.5-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 069cf8de2214b0195025da824b464d80b7aa5afee1457f71930bdc54e5351076
MD5 46814e6fbf1695fd1535bed8e3e12da2
BLAKE2b-256 51bd640a49a1790480707c85de57f3ff2f47aaad1307da2ed648b6dc3a4c37e5

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