google s2 java开发文档
生活随笔
收集整理的這篇文章主要介紹了
google s2 java开发文档
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近需要做運營商信號地圖,需要檢索某一區域用戶上報的信號質量問題,所以需要用到空間檢索能力,經過調研,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>? 2.獲取cell的四個頂點
class Gps {private double wgLat;private double wgLon;public Gps(double wgLat, double wgLon) {setWgLat(wgLat);setWgLon(wgLon);}public double getWgLat() {return wgLat;}public void setWgLat(double wgLat) {this.wgLat = wgLat;}public double getWgLon() {return wgLon;}public void setWgLon(double wgLon) {this.wgLon = wgLon;}@Overridepublic String toString() {return wgLat + "," + wgLon;} }//由于google s2默認使用gps坐標系,在國內無法使用,需要轉換為國內的gcj坐標或者bd09坐標,轉換方法可自行上網查詢 List<Gps> points = new ArrayList();S2CellId curId = new S2CellId(35234364754L); S2Cell curCell = new S2Cell(curId); S2LatLng v0 = new S2LatLng(curCell.getVertex(0)); //將gps坐標轉換為bd09坐標 Gps gps0 = PositionUtils.gps84_To_Bd09(v0.latDegrees(), v0.lngDegrees()); points.add(gps0);S2LatLng v1 = new S2LatLng(curCell.getVertex(1)); Gps gps1 = PositionUtils.gps84_To_Bd09(v1.latDegrees(), v1.lngDegrees()); points.add(gps1);S2LatLng v2 = new S2LatLng(curCell.getVertex(2)); Gps gps2 = PositionUtils.gps84_To_Bd09(v2.latDegrees(), v2.lngDegrees()); points.add(gps2);S2LatLng v3 = new S2LatLng(curCell.getVertex(3)); Gps gps3 = PositionUtils.gps84_To_Bd09(v3.latDegrees(), v3.lngDegrees()); points.add(gps3);return points;3.獲取level為7的cell的所有level為9的cell
S2CellId cellId = new S2CellId(3654375874L); childrenCell(cellId,9);public static List<S2CellId> childrenCell(S2CellId cellId, int desLevel) {List<S2CellId> list = childrenCellId(new ArrayList<S2CellId>(), cellId, cellId.level(), desLevel);return list;}private static List<S2CellId> childrenCellId(List<S2CellId> list, S2CellId cellId, int currentLevel, int desLevel) {if (currentLevel < desLevel) {long interval = cellId.childEnd().id() - cellId.childBegin().id();interval = interval / 4;for (int i = 0; i < 4; i++) {long id = cellId.childBegin().id() + interval * i;S2CellId newCellId = new S2CellId(id);//System.out.println(newCellId);childrenCellId(list, newCellId, currentLevel + 1, desLevel);}} else {list.add(cellId);return list;}return list;}4.獲取矩形區域內所有的cell
S2LatLng startS2 = S2LatLng.fromDegrees(lat, lng); S2LatLng endS2 = S2LatLng.fromDegrees(lat1, lng1); S2LatLngRect rect = new S2LatLngRect(startS2, endS2);//獲取region區域內的cell S2RegionCoverer s2RegionCoverer = new S2RegionCoverer(); s2RegionCoverer.setMaxLevel(14); s2RegionCoverer.setMinLevel(13); S2CellUnion covering = s2RegionCoverer.getCovering(rect); return covering.cellIds();?
總結
以上是生活随笔為你收集整理的google s2 java开发文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷P1510-精卫填海(01背包)
- 下一篇: 无参rce