Geospatial Indexes in Redis

Discovering Geospatial Indexes in Redis

Welcome! Now that we've explored bitmaps in Redis and learned how to handle individual bits within a string, let's take a step further into the fascinating world of Geospatial Indexes. This lesson is a crucial part of our series on advanced Redis data structures designed to extend your data-handling capabilities.

What You'll Learn

In this lesson, you will discover the power of geospatial indexing in Redis. Specifically, you will learn:

  1. How to add geographical coordinates (latitude and longitude) to a sorted set using the GEOADD command.
  2. How to calculate the distance between two locations using the GEODIST command.

Complete Example

Here is a complete example of adding locations and calculating the distance between them:

C++
#include <boost/redis.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/consign.hpp>
#include <iostream>
#include <optional>
#include <string>

#include <boost/redis/src.hpp>

namespace net = boost::asio;
using boost::redis::connection;
using boost::redis::config;
using boost::redis::logger;
using boost::redis::request;
using boost::redis::response;

int main() {
    try {
        net::io_context ioc;

        auto conn = std::make_shared<connection>(ioc.get_executor());

        config cfg;
        conn->async_run(cfg, logger{logger::level::disabled},
                        net::consign(net::detached, conn));

        request req;
        req.push("GEOADD", "locations", "13.361389", "38.115556", "Palermo");
        req.push("GEOADD", "locations", "15.087269", "37.502669", "Catania");
        req.push("GEODIST", "locations", "Palermo", "Catania", "km");

        response<std::int64_t, std::int64_t, std::string> resp;

        conn->async_exec(req, resp,
            [conn, &resp](boost::system::error_code ec, std::size_t) {
                if (!ec) {
                    const auto& geoadd1_res = std::get<0>(resp);
                    if (geoadd1_res) {
                        std::cout << "GEOADD Palermo Response: " << geoadd1_res.value() << "\n";
                    }

                    const auto& geoadd2_res = std::get<1>(resp);
                    if (geoadd2_res) {
                        std::cout << "GEOADD Catania Response: " << geoadd2_res.value() << "\n";
                    }

                    const auto& dist = std::get<2>(resp);
                    if (dist) {
                        const auto& s = dist.value();
                        std::cout << "Distance between Palermo and Catania: " << s << " km\n";
                    } else {
                        std::cout << "Distance: (nil)\n";
                    }
                } else {
                    std::cerr << "Error executing Redis commands: " << ec.message() << "\n";
                }
                conn->cancel();
            });

        ioc.run();
    } catch (const std::exception& e) {
        std::cerr << "Exception: " << e.what() << "\n";
        return 1;
    }
    return 0;
}

Now let's break down how this code works step by step.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal