• Stars
    star
    146
  • Rank 252,769 (Top 5 %)
  • Language
    Lua
  • License
    Other
  • Created over 8 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A helper library for Redis geospatial indices

geo.lua - helper library for Redis geospatial indices 🌍

This is a Lua library containing miscellaneous geospatial helper routines for use with Redis. It requires the Redis GEO API, available from v3.2.

In broad strokes, the library provides:

The library is strictly 📏 metric, sorry. Metric system adoption worldwide

Using geo.lua

The library is an ordinary Redis Lua script - use EVAL or EVALSHA to call it. The following example demonstrates usage from the prompt:

$ redis-cli SCRIPT LOAD "$(cat geo.lua)"
"fd07..."
$ redis-cli EVALSHA fd07... 0 help
 1) "geo.lua (0.1.5): A helper library for Redis geospatial indices"
 ...

Library API

GEO API completeness

GEOBEARING KEYS[1] geoset ARGV[2] member1 3] member2

Return the initial and final bearing between two members.

Time complexity: O(1).

For information about the calculation refer to http://mathforum.org/library/drmath/view/55417.html.

Return: Array reply, specifically the initial and final bearings.

GEOPATHLEN KEYS[1] geoset ARGV[2] member [...]

The length of a path between members.

Time complexity: O(N) where N is the number of members.

Note: This is basically a variadic form for GEODIST.

Return: String reply, specifically the length in meters.

GEODEL KEYS[1] geoset ARGV[2] member [...]

Delete members from a geoset.

Time complexity: O(M*log(N)) with N being the number of elements in the geoset and M the number of elements to be removed.

This command is an alias for ZREM to disprove the GEO incompleteness theorem. Technically it should be called GEOREM however.

Return: Integer reply, specifically the number of members actually deleted.

2D geometries and polygon search

Redis' geosets allow storing (and querying) of Point (2D, x/y, longitude & latitude) geometries. Other geometries can be stored serialized (using msgpack) in a Redis Hash data structure - the geomash. The geoset and the geomash are used for storing geometries in the following manner:

Geometry Storage GEOMETRYADD GEOMETRYGET GEOMETRYFILTER GEOJSONADD
Point geoset N/A N/A No Yes
Polygon geomash Yes Yes Yes Yes
MultiPolygon geomash TODO TODO TODO RODO
MultiPoint TODO TODO TODO No TODO
LineString TODO TODO TODO TODO TODO
MultiLineString TODO TODO TODO TODO TODO
GeometryCollection TODO TODO TODO TODO TODO

GEOMETRYADD KEYS[1] geomash ARGV[2] geometry-type 3] id 4..]

Upsert a single geometry to a geomash.

Time complexity:

ARGV[4] and above describe the geometry. Depending on the geometry's type:

  • POLYGON - each pair of ARGVs (e.g., 4 and 5, 6 and 7, ...) represent the coordinates of a vertex (longitude and latitude). A minimum of 4 vertices are required to define a polygon, with the first and last vertices being equal (a LineRing).

Note: there is no GEOMETYREM, use HDEL instead.

Return: Integer reply, specifically 0 if updated and 1 if added.

GEOMETRYGET KEYS[1] geomash ARGV[a] [WITHPERIMETER|WITHBOX|WITHCIRCLE] [...] ARGV[2] id [...]

Returns geometries' coordinates.

Time complexity:

The reply can be enriched with meta data about the geometry. The following sub-commands are supported, depending on the type of geometry.

  • WITHPERIMETER: For Polygon, The total length of edges
  • WITHBOX: The bounding box of the geometry
  • WITHCIRCLE: The bounding circle of the geometry

Return: Array reply, specifically:`

  • The geometry's type
  • The geometry's coordinates
  • When called with WITHPERIMETER, the total length of edges
  • When called with WITHBOX, the minimum and maximum coordinates of the bounding box, as well as it's bounding circle's radius
  • When called with WITHCIRCLE, the center coordinates and radius of bounding circle

GEOMETRYFILTER KEYS[1] geoset 2] geomash a] [target] ARGV[a] [STORE|WITHCOORD] [...] ARGV[2] id

Search for geoset members inside a geometry.

Time complexity:

This command performs a GEORADIUS search that contains the geometry's bounding box. The results are then filtered using a Point-In-Polygon (PIP) algorithm ([source](https://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html#The C Code)).

The following sub-commands are supported:

  • STORE: Stores the search results in the target geoset
  • WITHCOORD: Returns the members' coordinates as well

Return: Array reply, specifically the members and their coordinates (if called with WITHCOORD). When the STORE directive is used, the reply is an Integer that indicates the number of members that were upserted to the target geoset.

GeoJSON

A minimal implementation of the spec's v1.0 for decoding and encoding geoset and geomash members from/to FeatureCollections and Features.

GEOJSONADD KEYS[1] geoset 2] geomash ARGV[1] GeoJSON

Time complexity: O(log(N)) for each feature in the GeoJSON object, where N is the number of members in the geoset.

Upsert points to the geoset, other geometries to the geomash. A valid input GeoJSON object must be FeatureCollection. Each Feature's type must be Point or a Polygon, and the feature's properties must include a member named id.

Return: Integer reply, specifically the number of features upserted.

GEOJSONENCODE KEYS[1] geoset ARGV[2] 3] [arg] [...]

Time complexity: depends on the GEO command and its arguments.

Encodes the reply of GEO commands as a GeoJSON object. Valid GEO commands are:

  • GEOHASH
  • GEOPOS
  • GEORADIUS and GEORADIUSBYMEMBER
  • [GEOMETRYFILTER]

Return: String, specifically the reply of the GEO command encoded as a GeoJSON object.

Location updates

Implements a real-time location tracking mechanism. Inspired by Matt Stancliff @mattsta.

GEOTRACK KEYS[1] geoset ARGV[2] longitude 3] latitude 4] member [...]

Time complexity: O(log(N)+M+P) for each item added, where N is the number of elements in the geoset, M is the number of clients subscribed to the receiving channel and P is the total number of subscribed patterns (by any client).

GEOADDs a member and PUBLISHs on channel __geo:<geoset>:<member> a message with the format of <longitude>:<latitude>.

Clients can track updates made to a specific member by subscribing to that member's channel (i.e. SUBSCRIBE__geo:<geoset>:<member>) or to all members updates (i.e. PSUBSCRIBE__geo:<geoset>:*).

Return: Integer reply, specifically the number of members upserted.

xyzsets

Redis' geospatial indices only encode the longitude and latitude of members with no regard to their altitude. An xyzset uses two sorted sets, one as geoset and the other for storing altitudes.

GEOZADD KEYS[1] geoset 2] azset ARGV[2] logitude 3] latitude 4] altitude 5] member [...]

Time complexity: O(log(N)) for each item added, where N is the number of elements in the geoset.

Upsert members. Altitude is given as meters above (or below) sea level.

Return: Integer reply, specifically the number of members upserted.

GEOZREM KEYS[1] geoset 2] azset ARGV[2] member [...]

Time complexity: O(M*log(N)) with N being the number of elements in the geoset and M the number of elements to be removed.

Remove members.

Return: Integer reply, specifically the number of members actually deleted.

GEOZPOS KEYS[1] geoset 2] azset ARGV[2] member [...]

Time complexity: O(log(N)) for each member requested, where N is the number of elements in the geoset.

The position of members in 3D.

Return: Array reply, specifically the members and their positions.

TODO: GEOZDIST

Returns the distance between members.

TODO: GEOZCYLINDER

Perform cylinder-bound search.

TODO: GEOZCYLINDERBYMEMBER

Perform cylinder-bound search by member (a 20 characters command!).

TODO: GEOZSPHERE

Useful for directing air traffic and impact research (bombs, comets).

TODO: GEOZCONE

Good for comparing 👽 sightings vis a vis 🐮 abduction data.

Motility

Storing each member's vector in an additional hash data structure, where the field is the member's name and the value is the serialized vector (bearing & velocity).

TODO: GEOMADD KEYS[1] geoset 2] vector hash ARGV[2] longitude 3] latitude 4] bearing 5] velocity 6] member [...]

Upsert members. Bearing given in degrees, velocity in meters/second.

TODO: GEOMREM KEYS[1] geoset 2] vector hash ARGV[2] member [...]

Remove members.

TODO: GEOMPOSWHEN KEYS[1] geoset 2] vector hash ARGV[2] seconds 3] member [...]

Project members position in future or past.

TODO: GEOMMEETWHENWHERE KEYS[1] geoset 2] vector hash ARGV[2] member1 3] member2

Help solving basic math exercises.

License

3-Clause BSD.

Contributing

You're encouraged to contribute to the open source geo.lua project. There are two ways you can do so.

Issues

If you encounter an issue while using the geo.lua library, please report it at the project's issues tracker. Feature suggestions are also welcome.

Pull request

Code contributions to the geo.lua project can be made using pull requests. To submit a pull request:

  1. Fork this project.
  2. Make and commit your changes.
  3. Submit your changes as a pull request.

More Repositories

1

redis-cluster-proxy

A proxy for Redis clusters.
C
994
star
2

spark-redis

A connector for Spark that allows reading and writing to/from Redis cluster
Scala
939
star
3

memtier_benchmark

NoSQL Redis and Memcache traffic generation and benchmarking tool.
C++
904
star
4

redisraft

A Redis Module that make it possible to create a consistent Raft cluster from multiple Redis instances.
C
819
star
5

redis-enterprise-k8s-docs

Python
154
star
6

redis-quartz

A Quartz Scheduler JobStore that uses Redis for persistent storage.
Java
144
star
7

redis-lua-debugger

A Redis Lua script for debugging Redis Lua scripts
Lua
88
star
8

raft

C implementation of the Raft Consensus protocol, BSD licensed
C
68
star
9

spark-redis-ml

A spark package for loading Spark ML models to Redis-ML
Scala
63
star
10

redislabs-docs

Source for docs.redis.com (Redis Enterprise, Redis Cloud, RedisInsight, and more).
Python
63
star
11

redis-recommend

Simple Redis Go recommendation engine
Go
61
star
12

redis-migrate

Small utility for interactively migrating a bunch of redis servers to another bunch of redis servers.
Python
59
star
13

sentinel_tunnel

A local proxy server for discovering and to Redis via Sentinel
Go
45
star
14

redis-microservices-for-dummies

Sample application described in Redis Microservices for Dummies
Python
35
star
15

terraform-provider-rediscloud

Terraform Redis Cloud Provider: Deploy, update, and manage Redis Cloud databases as code through HashiCorp Terraform
Go
30
star
16

redis-fs

Distributed in memory filesystem
Python
26
star
17

triemap

C implementation of a compressed trie lookup map
C
21
star
18

bmemcached-cli

memcached command line interface with SASL (binary protocol) support
Python
20
star
19

redis-webcli

A tiny Flask app to provide access to Redis through a web form.
Python
17
star
20

redis-for-dummies

JavaScript
16
star
21

ReSearch

Redis search and indexing in Java
Java
15
star
22

lua-redimension

Redis multi-dimensional query library in Lua
Lua
14
star
23

rediscloud-node-sample

Redis Cloud on Express Sample
HTML
14
star
24

redislabs-helm

Helm charts for Redis Enterprise
Smarty
13
star
25

vault-plugin-database-redis-enterprise

HashiCorp Vault Plugins for Redis Enterprise
Go
13
star
26

rmtest

redis module unit tests with python (deprecated) please see RLTest
Python
12
star
27

redis-watch

home of the open source Redis Watch - a newsletter about Everything and Anything Redis
12
star
28

eredis

Embedded Redis experiment
C
10
star
29

redis-stream

Exposes Redis stream through the command line
C
10
star
30

gkemarketplace

Python
9
star
31

mbdirector

Memtier benchmark front-end
Python
8
star
32

redis-pipeline

Non blocking pipelined python client for redis and redis protocol parser.
Python
6
star
33

luascript

Easy Lua Handling
Java
6
star
34

jedis-ml

Add Redis-ML commands to Jedis
Java
6
star
35

memcache_populator

Python script for populating a memcahed sever from GarantiaData exported CSV files.
Python
6
star
36

pubsub-sub-bench

Redis Pub/Sub Subscriber Workload generator
Go
5
star
37

docker-library-redis

Shell
5
star
38

engineering-blog

Redis Labs Engineering Blog
HTML
5
star
39

gesher

K8s Admission control proxy
Go
5
star
40

rediscloud-openshift-quickstart

A Ruby and Redis-Cloud hosted redis bootstrap for RedHat's OpenShift cloud.
Ruby
5
star
41

redis-java-complex-benchmark

This benchmark stress redis with unionstore command
Java
4
star
42

rmr

POC Redis Module MapReduce operations
C
4
star
43

rediscloud-java-sample

Redis Cloud on Play Sample
HTML
4
star
44

redis-edge-azure-iot

JavaScript
4
star
45

redis-enterprise-azure

Automated Redis Enterprise Pack (a.k.a RP or a.k.a RLEC) test env deployment to Azure
Shell
4
star
46

rediscloud-ruby-sinatra-sample

Redis Cloud on Sinatra Sample
HTML
4
star
47

rgm

C
3
star
48

redis-ee-docker

Docker-based Redis Enterprise cluster infrastructure
Shell
3
star
49

rediscloud-go-api

Go SDK for Redis Enterprise Cloud Pro
Go
3
star
50

java-high-availability-test

this java program check recovery and also data consistency to redis.
Java
3
star
51

redis_extensions

C
3
star
52

redismodules.com

The repository of redismodules.com
Python
2
star
53

crdbmemcalc

CRDB/Redis memory calculator for synthetic datasets
Python
2
star
54

memcachedcloud-node-sample

Memcached Cloud on Express Sample
HTML
2
star
55

memcachedcloud-openshift-quickstart

A Ruby app using Memcahced Cloud Service on Red Hat's OpenShift
Ruby
2
star
56

rmnotify

A library for key and event space Notifications for Redis Modules
C
2
star
57

mkdocs-versions-menu

An MkDocs plugin that generates a versions menu for the mkdocs-material theme.
Python
2
star
58

rmadm

Python
2
star
59

spark-redis-blockstore

Apache spack off heap cache block manager over redis - see https://github.com/RedisLabs/spark-redis
Scala
2
star
60

memcachedcloud-php-sample

Memcached Cloud on PHP Sample
PHP
1
star
61

memcachedcloud-ruby-sinatra-sample

Memcached cloud on Sinatra Sample
HTML
1
star
62

memcachedcloud-django-sample

Memcached Cloud on Django Sample
Python
1
star
63

testing-workshop

Rust
1
star
64

rsctl

Command line control utility for RediSearch on RedisLabs Enterprise Cluster
Python
1
star
65

rediscloud-php-sample

Redis Cloud on PHP Sample
PHP
1
star
66

DockerHub

Docker Hub for Redis Enterprise and OSS Modules
Shell
1
star
67

rediscloud-django-sample

Redis Cloud on Django Sample
Python
1
star
68

pulumi-rediscloud

Redis Cloud Pulumi Resource Provider
Go
1
star