Skip to content

Commit

Permalink
Add Python bindings (#30)
Browse files Browse the repository at this point in the history
* Add Python bindings

* fix project layout
  • Loading branch information
kylebarron authored Mar 26, 2024
1 parent 3177ca0 commit d1db7df
Show file tree
Hide file tree
Showing 12 changed files with 563 additions and 0 deletions.
160 changes: 160 additions & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
5 changes: 5 additions & 0 deletions python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## [0.1.0] - YYYY-MM-DD

- Initial public release.
24 changes: 24 additions & 0 deletions python/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "geo-index"
version = "0.1.0"
authors = ["Kyle Barron <[email protected]>"]
edition = "2021"
description = "Fast, memory-efficient 2D spatial indexes for Python."
readme = "README.md"
repository = "https://github.com/kylebarron/geo-index"
license = "MIT OR Apache-2.0"
keywords = ["python", "geospatial"]
categories = ["science::geo"]
rust-version = "1.75"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "_rust"
crate-type = ["cdylib"]

[dependencies]
bytes = "1"
geo-index = { path = "../", features = ["rayon"] }
numpy = "0.20"
pyo3 = { version = "0.20", features = ["abi3-py38"] }
thiserror = "1"
3 changes: 3 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# geo-index

Fast, memory-efficient 2D spatial indexes for Python.
18 changes: 18 additions & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[build-system]
requires = ["maturin>=1.4.0,<2.0"]
build-backend = "maturin"

[project]
name = "geo-index"
requires-python = ">=3.8"
dependencies = []
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]

[tool.maturin]
features = ["pyo3/extension-module"]
module-name = "geo_index._rust"
python-source = "python"
4 changes: 4 additions & 0 deletions python/python/geo_index/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from ._rust import *
from ._rust import ___version

__version__: str = ___version()
54 changes: 54 additions & 0 deletions python/python/geo_index/_rust.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from typing import Literal, Optional, Self, Union

import numpy as np
from numpy.typing import NDArray

from .enums import RTreeMethod

IntFloat = Union[int, float]
RTreeMethodT = Literal["hilbert", "str"]

class KDTree:
@classmethod
def from_interleaved(
cls,
coords: NDArray[np.float64],
*,
node_size: Optional[int] = None,
) -> Self: ...
@classmethod
def from_separated(
cls,
x: NDArray[np.float64],
y: NDArray[np.float64],
*,
node_size: Optional[int] = None,
) -> Self: ...
def range(
self, min_x: IntFloat, min_y: IntFloat, max_x: IntFloat, max_y: IntFloat
) -> NDArray[np.uintc]: ...
def within(self, qx: IntFloat, qy: IntFloat, r: IntFloat) -> NDArray[np.uintc]: ...

class RTree:
@classmethod
def from_interleaved(
cls,
boxes: NDArray[np.float64],
*,
method: RTreeMethod | RTreeMethodT = RTreeMethod.Hilbert,
node_size: Optional[int] = None,
) -> Self: ...
@classmethod
def from_separated(
cls,
min_x: NDArray[np.float64],
min_y: NDArray[np.float64],
max_x: NDArray[np.float64],
max_y: NDArray[np.float64],
*,
method: RTreeMethod | RTreeMethodT = RTreeMethod.Hilbert,
node_size: Optional[int] = None,
) -> Self: ...
def search(
self, min_x: IntFloat, min_y: IntFloat, max_x: IntFloat, max_y: IntFloat
) -> NDArray[np.uintc]: ...
26 changes: 26 additions & 0 deletions python/python/geo_index/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from enum import Enum, auto


class StrEnum(str, Enum):
def __new__(cls, value, *args, **kwargs):
if not isinstance(value, (str, auto)):
raise TypeError(
f"Values of StrEnums must be strings: {value!r} is a {type(value)}"
)
return super().__new__(cls, value, *args, **kwargs)

def __str__(self):
return str(self.value)

def _generate_next_value_(name, *_):
return name.lower()


class RTreeMethod(StrEnum):
Hilbert = auto()
"""Use hilbert curves for sorting the RTree
"""

STR = auto()
"""Use the Sort-Tile-Recursive algorithm for sorting the RTree
"""
Empty file.
Loading

0 comments on commit d1db7df

Please sign in to comment.