An open API service indexing awesome lists of open source software.

https://github.com/tugayesilyurt/geospatial-indexing-using-redis-geohash-spring-boot

Implementing Geospatial Indexing in Spring Boot Using Redis Geohash
https://github.com/tugayesilyurt/geospatial-indexing-using-redis-geohash-spring-boot

geohash geospatial-data geospatial-indexes redis spring-boot

Last synced: 3 months ago
JSON representation

Implementing Geospatial Indexing in Spring Boot Using Redis Geohash

Awesome Lists containing this project

README

          

# Project

Implementing Geospatial Indexing in Spring Boot Using Redis Geohash

## Read this article on Medium

[Medium Article](https://medium.com/@htyesilyurt/implementing-geospatial-indexing-in-spring-boot-using-redis-geohash-bd7b2b77e4d4)

## Geospatial Indexing Example

## Tech Stack

- Java 21
- Spring Boot 3
- Redis
- Redis Geohash
- Geospatial Indexing
- Docker

## Installation

- follow the steps:

```shell
docker-compose up -d
cd find-nearest-vehicle
./mvnw spring-boot:run
```

## Redis Geohash - Adding Geospatial Data In Spring Boot

Redis offers the GEOADD command to add geospatial data. You can use it to add a single or multiple locations at once. Let's start with a single location.

## Using GeoOperations For Adding In Spring Boot

```java
private final GeoOperations geoOperations;
public static final String vehicleLocation = "vehicle_location";

public void add(VehicleLocationRequest request) {
Point point = new Point(request.getLongitude(), request.getLatitude());
geoOperations.add(vehicleLocation, point, request.getVehicleName());
}
```

## Redis Geohash - Fetching Geospatial Data In Spring Boot

Fetching geospatial data is performed with GEODIST, GEORADIUS, or GEORADIUSBYMEMBER. Let's focus on GEORADIUS, which allows us to query locations within a specific radius of a point.

## Using GeoOperations For Fetching In Spring Boot

```java
public List findNearestVehicles(Double longitude, Double latitude, int km) {

RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands
.GeoRadiusCommandArgs.newGeoRadiusArgs().includeCoordinates()
.includeDistance().sortAscending().limit(10);

Circle circle = new Circle(new Point(longitude, latitude)
,new Distance(km, Metrics.KILOMETERS));
GeoResults> response =
geoOperations.radius(vehicleLocation, circle, args);

List vehicleLocationResponses = new ArrayList<>();
response.getContent().stream().forEach(data -> {

vehicleLocationResponses.add(VehicleLocationResponse.builder()
.vehicleName(data.getContent().getName())
.averageDistance(data.getDistance().toString())
.point(data.getContent().getPoint())
.hash(geoOperations.hash(vehicleLocation,data.getContent().getName()).stream().findFirst().get())
.build());

});

return vehicleLocationResponses;
}
```