当前位置:网站首页>地理位置数据存储方案——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;
}
}五 实战思路
边栏推荐
- Lombok common notes
- Sword finger offer 06 Print linked list from end to end
- [paper notes] semi supervised object detection (ssod)
- 基于神经标签搜索,中科院&微软亚研零样本多语言抽取式摘要入选ACL 2022
- Brief introduction to class loading process
- 到底要不要去外包公司?这篇带你全面了解外包那些坑!
- JSON module dictionary and string conversion
- 剑指 Offer 07. 重建二叉树
- Pytorch | how to save and load pytorch models?
- [paper notes] street view change detection with deconvolutional networks
猜你喜欢

基于神经标签搜索,中科院&微软亚研零样本多语言抽取式摘要入选ACL 2022

异步处理容易出错的点

Generic - learning notes
How to debug grpc by postman

剑指 Offer 05. 替换空格
Cloning and importing DOM nodes

MySQL performance optimization - index optimization

到底要不要去外包公司?这篇带你全面了解外包那些坑!

Sword finger offer 09 Implementing queues with two stacks

合宙Air32F103CBT6开发板上手报告
随机推荐
剑指 Offer 07. 重建二叉树
异步处理容易出错的点
Sampling method and descriptive statistical function in R language
MySQL modify field statement
JVM memory region details
Simulating Sir disease transmission model with netlogo
Record the time to read the file (the system cannot find the specified path)
CV pre training model set
Is it safe to open a stock account through the account opening link given by the account manager? I want to open an account
Solve the go project compilation error go mod: no such file or directory
Sword finger offer 09 Implementing queues with two stacks
股票开户用什么app最安全?知道的给说一下吧
Brief object memory layout
剑指 Offer 03. 数组中重复的数字
Arthas source code learning-1
Why is it said that restarting can solve 90% of the problems
国信金太阳靠谱吗?是否合法?开股票账户安全吗?
JSON module dictionary and string conversion
李飞飞团队将ViT用在机器人身上,规划推理最高提速512倍,还cue了何恺明的MAE
sql优化的几种方式