项目作者: bast

项目描述 :
Fast points-in-polygon test and distances to polygons.
高级语言: Rust
项目地址: git://github.com/bast/polygons.git
创建时间: 2017-04-18T10:51:07Z
项目社区:https://github.com/bast/polygons

开源协议:GNU General Public License v3.0

下载


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 12th Gen Intel i7-12700T at 4.6 GHz):

  • distances to nearest edges: 320 ms
  • distances to nearest vertices: 290 ms
  • check whether points are inside or outside: 45 ms

Installation using pip

  1. $ 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

If you use this tool in a program or publication, please acknowledge its
author(s):

  1. @misc{polygons,
  2. author = {Bast, Radovan},
  3. title = {Polygons: Fast points-in-polygon test and distances to polygons},
  4. month = {03},
  5. year = {2025},
  6. publisher = {Zenodo},
  7. version = {v0.3.5},
  8. doi = {10.5281/zenodo.3825616},
  9. url = {https://doi.org/10.5281/zenodo.3825616}
  10. }

Python example

  1. import polygons
  2. # polygon_points is a list of lists
  3. # the library has been developed to perform
  4. # with very many polygons - this is just to have a simple example
  5. # in this example the polygons have the same number of points but there
  6. # is no restriction like this, this is only an example
  7. polygon_points = [
  8. [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)],
  9. [(0.0, 2.0), (1.0, 2.0), (1.0, 3.0), (0.0, 3.0)],
  10. ]
  11. # the more points you compute in one go, the better
  12. # here using two points to make a simple example but if you have many points
  13. # then compute a thousand or a million in one go
  14. # so that the library can parallelize over the points
  15. points = [(0.5, 0.5), (0.5, -0.5)]
  16. # parameters for the tree construction:
  17. # - each tree node has 4 children nodes
  18. # - each leaf collects 4 edges
  19. # you can try different parameters and check the timing
  20. # they (should) have no effect on the results apart from timing
  21. num_edges_children = 4
  22. num_nodes_children = 4
  23. tree = polygons.build_search_tree(
  24. polygon_points, num_edges_children, num_nodes_children
  25. )
  26. inside = polygons.points_are_inside(tree, points)
  27. print(inside) # [True, False]
  28. # indices are the indices of the nearest polygon vertices (counted
  29. # consecutively)
  30. indices, distances = polygons.distances_nearest_vertices(tree, points)
  31. print(indices) # [0, 0]
  32. print(distances) # [0.7071067811865476, 0.7071067811865476]
  33. distances = polygons.distances_nearest_edges(tree, points)
  34. print(distances) # [0.5, 0.5]
  35. indices, distances = polygons.distances_nearest_vertices(
  36. tree, [(0.6, 0.6), (0.5, -0.5)]
  37. )
  38. print(indices) # [2, 0]
  39. print(distances) # [0.5656854249492381, 0.7071067811865476]

References which were used during coding

Development notes

Running the benchmark:

  1. $ cargo test --release -- --ignored --nocapture

Python interface inspired by https://github.com/dev-cafe/rustafarian.

Building and testing the Python interface:

  1. $ maturin develop

Image

Social media preview generated using https://github.com/qrohlf/trianglify.