当前位置:网站首页>地理位置数据存储方案——Redis GEO
地理位置数据存储方案——Redis GEO
2022-06-25 15:36:00 【InfoQ】
一 题外话
二 GEO存储方案与空间索引
2.1 存储方案
2.2 空间索引
三 Redis GEO
3.1 命令
- geoadd:添加地理位置的坐标。
- geopos:获取地理位置的坐标。
- geodist:计算两个位置之间的距离。
- georadius:根据用户给定的经纬度坐标来获取指定范围内的地理位置集合。
- georadiusbymember:根据储存在位置集合里面的某个地点获取指定范围内的地理位置集合。
- geohash:返回一个或多个位置对象的 geohash 值。
3.2 原理:redis源码解析
3.2.1 数据结构简述
3.2.2 geo.h
#ifndef __GEO_H__
#define __GEO_H__
#include "server.h"
/* Structures used inside geo.c in order to represent points and array of
* points on the earth. */
typedef struct geoPoint {
double longitude;
double latitude;
double dist;
double score;
char *member;
} geoPoint;
typedef struct geoArray {
struct geoPoint *array;
size_t buckets;
size_t used;
} geoArray;
#endif3.2.3 geo.c




3.3 操作实践
3.3.1 redis环境
3.3.2 命令行客户端连接
redis-cli -h mylocalhost -p 8179 --raw
3.3.3 geo-zset操作验证




四 springframework与redis geo
package tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class RedisGeoTool {
@Autowired
private RedisTemplate<String,String> redisTemplate;
/**
* 添加节点及位置信息
* @param geoKey 位置集合
* @param pointName 位置点标识
* @param longitude 经度
* @param latitude 纬度
*/
public void geoAdd(String geoKey, String pointName, double longitude, double latitude){
Point point = new Point(longitude, latitude);
redisTemplate.opsForGeo().add(geoKey, point, pointName);
}
/**
*
* @param longitude
* @param latitude
* @param radius
* @param geoKey
* @param metricUnit 距离单位,例如 Metrics.KILOMETERS
* @param metricUnit
* @return
*/
public List<GeoResult<RedisGeoCommands.GeoLocation<String>>> findRadius(String geoKey
, double longitude, double latitude, double radius, Metrics metricUnit, int limit){
// 设置检索范围
Point point = new Point(longitude, latitude);
Circle circle = new Circle(point, new Distance(radius, metricUnit));
// 定义返回结果参数,如果不指定默认只返回content即保存的member信息
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs
.newGeoRadiusArgs().includeDistance().includeCoordinates()
.sortAscending()
.limit(limit);
GeoResults<RedisGeoCommands.GeoLocation<String>> results = redisTemplate.opsForGeo().radius(geoKey, circle, args);
List<GeoResult<RedisGeoCommands.GeoLocation<String>>> list = results.getContent();
return list;
}
/**
* 计算指定key下两个成员点之间的距离
* @param geoKey
* @param member1
* @param member2
* @param unit 单位
* @return
*/
public Distance calDistance(String geoKey, String member1, String member2
, RedisGeoCommands.DistanceUnit unit){
Distance distance = redisTemplate.opsForGeo()
.distance(geoKey, member1, member2, unit);
return distance;
}
/**
* 根据成员点名称查询位置信息
* @param geoKey geo key
* @param members 名称数组
* @return
*/
public List<Point> geoPosition(String geoKey, String[] members){
List<Point> points = redisTemplate.opsForGeo().position(geoKey, members);
return points;
}
}五 实战思路
边栏推荐
猜你喜欢

04. binary tree

Kali SSH Remote Login

双目3D感知(一):双目初步认识

Optimization of lazyagg query rewriting in parsing data warehouse

Pytorch distributed test pit summary

Popular cross domain

Brief introduction to class loading process

Distributed transaction solution

Agent and classloader

Postman usage notes, interface framework notes
随机推荐
Summary of regularization methods
Es data synchronization mode
Using reentrantlock and synchronized to implement blocking queue
Finally, we can figure out whether the binding event in the tag is bracketed or not
[paper notes] street view change detection with deconvolutional networks
Postman usage notes, interface framework notes
Why do I need message idempotence?
Differences and solutions of redis cache avalanche, cache penetration and cache breakdown
Completabilefuture of asynchronous tools for concurrent programming
CV pre training model set
[paper notes] semi supervised object detection (ssod)
剑指 Offer 09. 用两个栈实现队列
Download and installation tutorial of consumer
0706-- replace fields in the use case, such as mobile phone number or ID
Distributed token
Sword finger offer 09 Implementing queues with two stacks
Internal class learning notes
What is the safest app for stock account opening? Tell me what you know
0703 interface automation - MySQL database connection, encapsulation, adding database verification in use cases
MySQL transaction characteristics and implementation principle