最近需要做运营商信号地图,需要检索某一区域用户上报的信号质量问题,所以需要用到空间检索能力,经过调研,geohash和google s2都可以实现相关的能力,但是geohash只有12级别,无法做细粒度的索引,google s2有30level,针对1公里甚至1cm都可以做索引,最终选择google s2 的level12 13 14三个级别作为索引。
maven依赖
<!-- google s2 -->
<dependency>
<groupId>io.sgr</groupId>
<artifactId>s2-geometry-library-java</artifactId>
<version>1.0.0</version>
</dependency>
- 根据坐标建立cell,level选择13级别,覆盖范围1平方公里。将cell存入数据库中,作为索引字段
import com.google.common.geometry.*;
double lat=30.2;
double lng=116.3;
int currentLevel =13;
S2LatLng s2LatLng = S2LatLng.fromDegrees(lat, lng);
S2CellId cellId = S2CellId.fromLatLng(s2LatLng).parent(currentLevel);
2.获取cell的四个顶点
class Gps {
private double wgLat;
private