Skip to main content

Nearest point query for any kd-tree implementation

Project description

KdQuery is a package that defines one possible implementation of kd-trees using python lists to avoid recursion and most importantly it defines a general method to find the nearest node for any kd-tree implementation.

Getting Started

Prerequisites

  • Python version 3.6 installed locally

  • Pip installed locally

Installing

The package can easily be installed via pip:

pip install kdquery

Usage

The Tree class with the default settings

from kdquery import Tree

# Create a kd-tree (k = 2 and capacity = 10000 by default)
tree = Tree()

# Insert points with some attached data (or not)
tree.insert((9, 1), {'description': 'point in the plane', 'label': 6})
tree.insert((1, -8))
tree.insert((-3, 3), data=None)
tree.insert((0.2, 3.89), ["blue", "yellow", "python"])

# Recover the data attached to (0, 3)
node_id = tree.insert((0, 3), 'Important data')
node = tree.get_node(node_id)
print(node.data)  # 'Important data'

# Find the node in the tree that is nearest to a given point
query = (7.2, 1.2)
node_id, dist = tree.find_nearest_point(query)
print(dist)  # 1.8110770276274832

The Tree class with the optional arguments

from kdquery import Tree

x_limits = [-100, 100]
y_limits = [-10000, 250]
z_limits = [-1500, 10]
region = [x_limits, y_limits, z_limits]

capacity = 3000000

# 3d-tree with capacity of 3000000 nodes
tree = Tree(3, capacity, region)

The nearest_point method

Let’s say that you work with some positions over the superface of the Earth in your application and that to store this data you implement a kd-tree where each node is represented as an element of an array with these specifications:

import numpy as np

node_dtype = np.dtype([
   ('longitude', 'float64'),
   ('latitude', 'float64'),
   ('limit_left', 'float64'),
   ('limit_right', 'float64'),
   ('limit_bottom', 'float64'),
   ('limit_top', 'float64'),
   ('dimension', 'float64'),
   ('left', 'int32'),
   ('right', 'int32')
])

If given a point over the surface of the Earth you need to find the nearest position of your database, you can use the nearest_point method from this package. You only need to define a method that receives the index of a node in this representation and returns the coordinates of the node, the region where it is and the indices to the left and right child. For the implementation mentioned above, it could be something like:

def get_properties(node_id):
    node = tree[node_id]

    horizontal_limits = [node['limit_left'], node['limit_right']]
    vertical_limits = [node['limit_bottom'], node['limit_top']]

    # The region of the space definied by the node
    region = [horizontal_limits, vertical_limits]

    # The position of the point in the space
    coordinates = (node['longitude']), node['latitude']))

    # The dimension of the space divided by this node
    # 0 for longitude and 1 for latitude in this case
    dimension = node['dimension']

    # If you want this node to be considered
    # Set to true if this feature is not predicted by your implementation
    active = True

    # Indices to left and right children
    left, right = node['left'], node['right']

    return coordinates, region, dimension, active, left, right

To call the method:

import kdquery

def spherical_dist(point1, point2):
    <statement-1>
    .
    .
    .
    <statement-N>
    return dist

query = (2.21, 48.65)
root_id = 0  # index of the root
node_id, dist = kdquery.nearest_point(query, root_id, get_properties,
                                      spherical_dist)

Project details


Download files

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

Source Distribution

KdQuery-0.2.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

KdQuery-0.2.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file KdQuery-0.2.2.tar.gz.

File metadata

  • Download URL: KdQuery-0.2.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for KdQuery-0.2.2.tar.gz
Algorithm Hash digest
SHA256 f3ee45269e9c81f947636371ef416742812763094a03fc6b04eb547a66553667
MD5 d36680968537af6ef9e650b28ec4e8f8
BLAKE2b-256 9093f9d499cc28908d0dafc17e33b722398d54bd673325edfe5622e72ab34551

See more details on using hashes here.

File details

Details for the file KdQuery-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for KdQuery-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3de1e784c294db4823a30de4fbd52d80d86e1be16f4d122f6a3df0c945f23573
MD5 42eb539ba5a0a61839c93ae0f241f71d
BLAKE2b-256 e4dc305312056db45a654f21623255ca81ae3e503e8850bdb336e7eaec920094

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