因为工作需要用到六边形地图网格,所以接触到了uber h3
网上资料并不是太多,在这里记录一下
首先介绍一下
H3 是 uber 设计的六边形空间索引,可以通过经纬度获取所在的 h3 六边形边界,每个经纬度对应的六边形都是确定的,每个六边形唯一对应了一个 h3index。在业务开发中,我们可以通过 h3index 来对地理空间中的对象做聚合,本质还是将经纬度查询转换成了 h3index 的查询。
h3的层级,h3一共0-15有16个层级,从低到高,划分的粒度逐渐越来越细致,网格数也越来越多,注意它是由六边形和五边形共同组成的;
![](https://gitcode.net/lpg432/typora_images/-/raw/master/uberh3/image-20230417212941829.png)
具体为什么,可以看看设计根源和官方资料,先跑起来,出现效果才有兴趣!
可以看看这个:https://blog.csdn.net/whynottrythis/article/details/129765463?spm=1001.2014.3001.5506
再说说uber在进行网格编码时,如何做的,对于如此庞大数量的网格索引,官方也是采用一种faceIJK坐标系的编码方式,可以看看:http://it.taocms.org/04/25109.htm (我以为会是树形结构存储呢)
下面看看案例
我使用的是JAVA版
<!--h3地图网格,如果和区域相关,建议使用此版本-->
<dependency>
<groupId>com.uber</groupId>
<artifactId>h3</artifactId>
<version>4.1.1</version>
</dependency>
案例,根据提到的写相应接口就可以了
package com.lipengg.controller;
import com.uber.h3core.H3Core;
import com.uber.h3core.util.LatLng;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @author lipengg
* @date 2023/4/17
*/
public class Test {
public static void main(String[] args) throws IOException {
H3Core h3Core = H3Core.newInstance();
// 将一个经纬度坐标和层级转换为h3区域
// 获得的meshIndex时long类型网格索引
long meshIndex = h3Core.latLngToCell(30.0, 30.0, 2);
System.out.println("meshIndex:"+meshIndex);
// 将网格索引变为六边形区域
List<LatLng> latLngs = h3Core.cellToBoundary(meshIndex);
System.out.println("六边形区域:"+latLngs);
// 在4.1.1中有获取区域的索引,相信用到这个的人挺多
List<LatLng> areas = new ArrayList<>();
areas.add(new LatLng(66.66,55.55));
areas.add(new LatLng(33.33,55.55));
areas.add(new LatLng(33.33,77.77));
areas.add(new LatLng(66.66,77.77));
// 区域内的网格索引
List<Long> longs = h3Core.polygonToCells(areas, null, 2);
// 转换为六边形
for (Long aLong : longs) {
// 将网格索引变为六边形区域
List<LatLng> latLngList = h3Core.cellToBoundary(aLong);
// System.out.println(latLngList);
}
// 如何获取某一层级下的全量网格呢?api中只提供了0层级的全量网格,往下看
// 0层级下的全量网格
Collection<Long> res0Cells = h3Core.getRes0Cells();
// 0层级下的网格个数
long numCells = h3Core.getNumCells(0);
System.out.println("层级0下的网格索引数:"+numCells);
// 重点来了,以层级2为例
int res2IndexNum = 0;
for (Long res0Cell : res0Cells) {
// 此时用res0Cell网格索引和子层级2就获取到了子层级下的索引
// 这里就要注意到h3在存储网格索引时使用的编码结构了,官方叫
// FaceIJK坐标系,可以参考我帖的链接了解一下
List<Long> longs1 = h3Core.cellToChildren(res0Cell, 2);
res2IndexNum = res2IndexNum + longs1.size();
}
System.out.println("层级2下的网格索引数:"+res2IndexNum);
}
}
官方api文档
其余的可以参考api文档,我当初也是看了文档才知道
https://h3geo.org/docs/api/indexing