Skip to main content

Simple library for perform quick nearby search for geo spatial data.

Requirements

  • python-geohash

It was written and tested on Python 2.7.

Installation

Get it from pypi:

pip install geoindex

or github:

pip install -e git://github.com/gusdan/geoindex.git#egg=geoindex

Simple use

If we have 100000 geo coordinates and we have to find some nearby location to given point and with given radius we can do something like this:

from geoindex import GeoGridIndex, GeoPoint

geo_index = GeoGridIndex()
for _ in range(10000):
    lat = random.random()*180 - 90
    lng = random.random()*360 - 180
    index.add_point(GeoPoint(lat, lng))

center_point = GeoPoint(37.7772448, -122.3955118)
for distance, point in index.get_nearest_points(center_point, 10, 'km'):
    print("We found {0} in {1} km".format(point, distance))

Search with associated data

When we fill index we can pass ref to GeoPoint as reference to some object and use it after:

from geoindex import GeoGridIndex, GeoPoint

index = GeoGridIndex()
for airport in get_all_airports():
    index.add_point(GeoPoint(lat, lng, ref=airport))

center_point = GeoPoint(37.7772448, -122.3955118)
for distance, point in index.get_nearest_points(center_point, 10, 'km'):
    print("We airport {0} in {1} km".format(point.ref, distance))

Performance

Creating index with 10000 random points and nearby search for each point took about 400ms. See tests/test_performance.py for more details.

How does it work

For perform quick search GeoGridIndex uses Geohash for each point and store it in internal dictionary. When we initialize GeoGridIndex we pass precision to constructor, based on it we divide all spaces with grid and store each point inside that grid. When we search nearest points we define cell with center point and 8 neighbors and check all points from these cells for distance to center.

Each cell has size dependent on precision, bellow you can find grid’s cell size and precision.

Precision

Cell size

1

5000

2

1260

3

156

4

40

5

4.8

6

1.22

7

0.152

8

0.038

If you have created GeoGridIndex with precision = 4 it means what all points will be assign to grid with cell size = 40 km. And we can run search with radius less than 40/2km. If we’d like to make bigger radius we should create index with less precision 3.

But in other side if we create index with smallest precision (1) and will run search (get_nearest_points) with small radius (1km for example) it will works fine, but it will check all points inside 9 grid cells with size 5000km and it can be not so fast.

Metadata

Release files for geoindex 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for geoindex 0.0.1
File Size Uploaded
geoindex-0.0.1.tar.gz 4.7 kB Details

Release files / geoindex-0.0.1.tar.gz

Download URL geoindex-0.0.1.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ba4d6a5c0f8716adfa31b33f1773e1b8f81342d9aacb31ced5100807f34ebd91
BLAKE2b-256 checksum
How to use checksums
99b5020117b2986a6229e52fdfaa80f0e164ec733b71c59c680ea672c8d5a331
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page