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.8 - 3.12
  • 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     = {10},
  year      = {2024},
  publisher = {Zenodo},
  version   = {v0.3.4},
  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

polygons-0.3.4-cp312-none-win_amd64.whl (172.2 kB view details)

Uploaded CPython 3.12 Windows x86-64

polygons-0.3.4-cp312-cp312-manylinux_2_34_x86_64.whl (319.5 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.34+ x86-64

polygons-0.3.4-cp312-cp312-macosx_11_0_arm64.whl (273.9 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

polygons-0.3.4-cp311-none-win_amd64.whl (172.7 kB view details)

Uploaded CPython 3.11 Windows x86-64

polygons-0.3.4-cp311-cp311-manylinux_2_34_x86_64.whl (321.4 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.34+ x86-64

polygons-0.3.4-cp311-cp311-macosx_11_0_arm64.whl (274.7 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

polygons-0.3.4-cp310-none-win_amd64.whl (172.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

polygons-0.3.4-cp310-cp310-manylinux_2_34_x86_64.whl (321.4 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.34+ x86-64

polygons-0.3.4-cp310-cp310-macosx_11_0_arm64.whl (275.1 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

polygons-0.3.4-cp39-none-win_amd64.whl (172.9 kB view details)

Uploaded CPython 3.9 Windows x86-64

polygons-0.3.4-cp39-cp39-manylinux_2_34_x86_64.whl (321.5 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.34+ x86-64

polygons-0.3.4-cp39-cp39-macosx_11_0_arm64.whl (275.4 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

polygons-0.3.4-cp38-none-win_amd64.whl (173.0 kB view details)

Uploaded CPython 3.8 Windows x86-64

polygons-0.3.4-cp38-cp38-manylinux_2_34_x86_64.whl (321.6 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.34+ x86-64

polygons-0.3.4-cp38-cp38-macosx_11_0_arm64.whl (275.5 kB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

File details

Details for the file polygons-0.3.4-cp312-none-win_amd64.whl.

File metadata

File hashes

Hashes for polygons-0.3.4-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 c4e92c0194a7fff83ab64f87061895e2ab20df8392d27f07b0710b75a19a0499
MD5 22ebba6c0bb699ddb2b4cbc303e1f5aa
BLAKE2b-256 93d55a68a9ee60c4d97cb091236a87ce4c4a0d32ae654d57ccd38fb89419b80d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polygons-0.3.4-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 630adba3403507923340fcad7d7d9acfcef0a45e2c590db727be2344c16d1c79
MD5 3877b2c397f469954b3e1ce8b828cc08
BLAKE2b-256 ce84d19515ab45a65b763f15b4a21248818a817941b9259e44fa99465fa6c321

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polygons-0.3.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ebaef114a5bbcde928809108422c4f84c02626a77bb17c2f7211bea851eb1048
MD5 f95c71dbf023172bbea5292db14d768b
BLAKE2b-256 c23e842def9c548fca47abddaf7058302e5f46797a9dcff5d48631bdead97952

See more details on using hashes here.

File details

Details for the file polygons-0.3.4-cp311-none-win_amd64.whl.

File metadata

File hashes

Hashes for polygons-0.3.4-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 4ca9a8f3076df688abda6438bb31bfacfd983a0dc03daef6c7a94c71fde7b963
MD5 23b9cc8238dfb73c420c93a2993e1dd2
BLAKE2b-256 4b8b694cc8c86c2b7e74207fa6cb68ba912ff3df3eef6c230711760cedd52994

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polygons-0.3.4-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 ca83f3aa3e546ae0eca0fea98e5c1889905c5386745d8b592aab67b127d3e395
MD5 c742fe6cd2ecbb0a248dfda071bb6153
BLAKE2b-256 06b91a6650042de48c0de7a445c5a2b51eb8534ced4278f76667e97fa451207d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polygons-0.3.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 220a99d7c7f2290e80424a7412cd3654630b84fd4776e78e923b724fe9f7c27f
MD5 9d5c2b58a77272e58648e3f7c0a49add
BLAKE2b-256 51890036282dfe4e13b79a2303eb7ff03331d5604ef208b035283e41a6741ba2

See more details on using hashes here.

File details

Details for the file polygons-0.3.4-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for polygons-0.3.4-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 de188eaeb10bb13672544ae2d656e480a6fd784e6ab62679eadc0b97c2c06192
MD5 9c9873881dd75d798bed6676a694117c
BLAKE2b-256 7f366737b5328f7e9d77ce06090f625da9e3a980bd2bd1f55ee62bb917dbf760

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polygons-0.3.4-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a516dd17fff7549158035cf9d4cc0beb1c1909dd8d70981605b2a42c920cda26
MD5 bc267498aaefd4281e066eb19d589496
BLAKE2b-256 d63855708cd48c13da98b4fd6473c60741f950123296db95c4896984a3215f04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polygons-0.3.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4839d145afb61d277a0de763be3739723b7746cb649b98816f14208c72d84da8
MD5 9bf416dee7cfe9e8b2fd04000db730a3
BLAKE2b-256 a9cf15cfb196713951866becf2a55a21845e96d2c9928c433f69d1742c4a6cdc

See more details on using hashes here.

File details

Details for the file polygons-0.3.4-cp39-none-win_amd64.whl.

File metadata

File hashes

Hashes for polygons-0.3.4-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 6243f50423f3fa248e0e1665ae595b0da9d748c17bdefced48bd91fc0a188b5b
MD5 c38f2c72e64e438a928cc4e00870efd5
BLAKE2b-256 71598a580e0a26a5dafacd21f576e13b37023044eaecef2643355e1d133f1d60

See more details on using hashes here.

File details

Details for the file polygons-0.3.4-cp39-cp39-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for polygons-0.3.4-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 22419b154736d38be3186e85f1ef5532bb4dd29bf61e9eb669d096985afa9ef4
MD5 de6000d4bab232b71f5bf54d695d68ad
BLAKE2b-256 d9bcb50db33f0be99d1ec7525f55bf5faa61704403b5a54b61b59bd8d358e920

See more details on using hashes here.

File details

Details for the file polygons-0.3.4-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polygons-0.3.4-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0dd390cf1f04e0252e5e5f539564daff8f06b376d81f32ea6293744b8074a98d
MD5 4d134606c7775d34e2e00091e144d1ff
BLAKE2b-256 ce13c5cb5ed77a7bd1b98c0111804f83f88c6231ff3b9892a385590281f69b54

See more details on using hashes here.

File details

Details for the file polygons-0.3.4-cp38-none-win_amd64.whl.

File metadata

File hashes

Hashes for polygons-0.3.4-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 d075089d0ded0e9c19c8274db1e955586282276b22efd9d7b0ff7c7eeac8793b
MD5 2c1068be59641ac937acf297822f7907
BLAKE2b-256 83e646a792ad536ffb02ce07b07cc4c0db1779cbf2cdec70e51a98fd2635dd47

See more details on using hashes here.

File details

Details for the file polygons-0.3.4-cp38-cp38-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for polygons-0.3.4-cp38-cp38-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a553c1003970185c4f65d228cb5b2d4d0aed16d00d9071f2f28e1ca75e113dcf
MD5 c8292059467949cfbc1eb2b4a60605d9
BLAKE2b-256 dcb3317fc215ef7f585fb95b824d009399af767028c661186f929d6455eebf9f

See more details on using hashes here.

File details

Details for the file polygons-0.3.4-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polygons-0.3.4-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ca06d93152692b2b6a5e6c75867c0047cf9971f22b993b8b8a86c2911d6c2b4c
MD5 581edefbe77235567112296329b31108
BLAKE2b-256 46172d7c4e8c002c22ae1c0bc540552f0757c5a1220d0d56b957d9b170be51ee

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