Git Product home page Git Product logo

polygons's Introduction

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     = {1},
  year      = {2024},
  publisher = {Zenodo},
  version   = {v0.3.3},
  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.

polygons's People

Contributors

bast avatar dvtomas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

polygons's Issues

Make python/rayon dependencies optional

I really just need the code to compute inside/outside, and the distance from polygon. Rayon/Python dependencies are a burden to me, but I understand for others it's a crucial piece of the code. I suggest making those two dependencies (independently) optional.

install problem

ERROR: Command errored out with exit status 4294967295:
command: 'c:\Users\anaconda3\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\\AppData\Local\Temp\pip-req-build-cvo_l3i0\setup.py'"'"'; file='"'"'C:\Users\\AppData\Local\Temp\pip-req-build-cvo_l3i0\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record 'C:\Users\AppData\Local\Temp\pip-record-cqk1526x\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\Users\anaconda3\Include\polygons'
cwd: C:\Users\AppData\Local\Temp\pip-req-build-cvo_l3i0
Complete output (14 lines):
c:\Users\anaconda3\lib\site-packages\setuptools\dist.py:454: UserWarning: Normalizing '0.0.0-alpha-1' to '0.0.0a1'
warnings.warn(tmpl.format(**locals()))
running install
running build
c:\Users\anaconda3\Scripts\cmake.exe -DCMAKE_BUILD_TYPE=release -DENABLE_OPENMP=True ..
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18363.
-- Found OpenMP_C: -openmp
-- Found OpenMP_CXX: -openmp
-- Found OpenMP: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users//AppData/Local/Temp/pip-req-build-cvo_l3i0/build_setup_py
make
Error while running Make
----------------------------------------
ERROR: Command errored out with exit status 4294967295: 'c:\Users\anaconda3\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\\AppData\Local\Temp\pip-req-build-cvo_l3i0\setup.py'"'"'; file='"'"'C:\Users\\AppData\Local\Temp\pip-req-build-cvo_l3i0\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record 'C:\Users\AppData\Local\Temp\pip-record-cqk1526x\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\Users\anaconda3\Include\polygons' Check the logs for full command output

Also give index of nearest point and index of polygon if inside polygon

Excuse me, I'm sorry to bother you. There are some questions i would like to ask you during the operation of the existing program.
First, when viewing the distance from a point to a polygon, a point can only output the distance to one polygon. Is this output polygon the nearest polygon to this point?
Second, If the polygon mentioned above is the nearest polygon, can we know which polygon it is? How can we output the information for this polygon?(For example, the name or information of a school district)
Third, the program that estimates whether a point is in a polygon does not say which polygon it is in, so how can we get the name or information of the polygon?
Thank you for taking time out of your busy schedule to answer my questions. Wish you all the best in your work!

Python 3.11

Hi,

I can't install the library with Python 3.11. Do you plan to make it available?

Thank you,

Pablo.

Rename functions

  • contains_points -> points_are_inside
  • get_distances_edge -> distances_to_nearest_edges
  • get_distances_vertex -> distances_to_nearest_vertices
  • get_closest_vertices -> nearest_vertices

Consider also merging distances_to_nearest_vertices and nearest_vertices to nearest_vertices which returns both the indices and the distances since they are computed at the same time.

boundary and polygon code

Excuse me, I'm sorry to bother you.Last time I asked about boundary and polygon information, I wonder whether the code has been updated?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.