Uber H3算法封装成JAR包以及常用API调用

由于H3的库都是用C语言写的,如果想用Java代码测试,github已经有现成的H3-Java库,与H3的C库绑定好了

地址:https://github.com/uber/h3-java

比如测试代码需要调用现成H3-Java里封装好的方法,最直接的方法将h3-java打一个JAR包,具体执行 

cd h3-java
mvn package

打包的时候,会在h3-java/target目录下重新clone h3目录,比较慢,如果需要,可以提前先将h3拉下来

包打好了之后,添加到测试工程当中

NewImage

此时需要调用一些H3的方法测试

比如来一个geo经纬度和h3索引之间的转换

package 大LBS系统回归测试用例库.新一代栅格系统;

import java.io.IOException;

import com.caocao.testgroup.scenariotest.BaseTest;
import com.uber.h3core.H3Core;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
* Copyright (C), scenario-test
* FileName: H3APITest
* Author: lihui
* Date: 2018/9/17
*/

public class H3APITest extends BaseTest {

//H3所有API
private H3Core h3;

@BeforeClass
public void before() throws IOException {
h3 = H3Core.newInstance();
}

@Test(description = "经纬度转换为H3索引")
public void testGeoToH3() {
System.out.println(h3.geoToH3(30.209012, 120.208449, 8));
}
}

这样就能正常返回对应索引

 NewImage

这里提供的库方法很多

可以查看com/uber/h3core/NativeMethods里定义了一堆native方法

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.uber.h3core;

import com.uber.h3core.util.GeoCoord;
import java.util.ArrayList;
import java.util.List;

final class NativeMethods {
NativeMethods() {
}

native int maxH3ToChildrenSize(long var1, int var3);

native void h3ToChildren(long var1, int var3, long[] var4);

native boolean h3IsValid(long var1);

native int h3GetBaseCell(long var1);

native boolean h3IsPentagon(long var1);

native long geoToH3(double var1, double var3, int var5);

native void h3ToGeo(long var1, double[] var3);

native int h3ToGeoBoundary(long var1, double[] var3);

native int maxKringSize(int var1);

native void kRing(long var1, int var3, long[] var4);

native void kRingDistances(long var1, int var3, long[] var4, int[] var5);

native int hexRange(long var1, int var3, long[] var4);

native int hexRing(long var1, int var3, long[] var4);

native int h3Distance(long var1, long var3);

native int maxPolyfillSize(double[] var1, int[] var2, double[] var3, int var4);

native void polyfill(double[] var1, int[] var2, double[] var3, int var4, long[] var5);

native void h3SetToLinkedGeo(long[] var1, ArrayList<List<List<GeoCoord>>> var2);

native int compact(long[] var1, long[] var2);

native int maxUncompactSize(long[] var1, int var2);

native int uncompact(long[] var1, int var2, long[] var3);

native double hexAreaKm2(int var1);

native double hexAreaM2(int var1);

native double edgeLengthKm(int var1);

native double edgeLengthM(int var1);

native long numHexagons(int var1);

native boolean h3IndexesAreNeighbors(long var1, long var3);

native long getH3UnidirectionalEdge(long var1, long var3);

native boolean h3UnidirectionalEdgeIsValid(long var1);

native long getOriginH3IndexFromUnidirectionalEdge(long var1);

native long getDestinationH3IndexFromUnidirectionalEdge(long var1);

native void getH3IndexesFromUnidirectionalEdge(long var1, long[] var3);

native void getH3UnidirectionalEdgesFromHexagon(long var1, long[] var3);

native int getH3UnidirectionalEdgeBoundary(long var1, double[] var3);
}

有兴趣的可以看下每个方法的含义,测试当中可以通过直接调用这些方法进行涉及到H3算法的测试

发表回复