Skip to content

Commit ca72433

Browse files
authored
upgrade to v4.1.0 (#121)
* upgrade to v4.1.0 * Add PR number * add cellToChildrenSize * update release date
1 parent e78463c commit ca72433

File tree

9 files changed

+209
-9
lines changed

9 files changed

+209
-9
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ file [H3Core.java](./src/main/java/com/uber/h3core/H3Core.java), and support
77
for the Linux x64 and Darwin x64 platforms.
88

99
## Unreleased Changes
10+
## [4.1.0] - 2023-01-19
11+
### Added
12+
- `cellToChildPos` and `childPosToCell` functions. (#121)
13+
- Made the `cellToChildrenSize` function public. (#121)
14+
15+
### Changed
16+
- Upgraded the core library to v4.1.0. (#121)
17+
1018
## [4.0.2] - 2022-09-21
1119
### Changed
1220
- Upgraded the core library to v4.0.1. (#113)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![Coverage Status](https://coveralls.io/repos/github/uber/h3-java/badge.svg?branch=master)](https://coveralls.io/github/uber/h3-java?branch=master)
77
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
88
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.uber/h3/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.uber/h3)
9-
[![H3 Version](https://img.shields.io/badge/h3-v4.0.1-blue.svg)](https://github.com/uber/h3/releases/tag/v4.0.1)
9+
[![H3 Version](https://img.shields.io/badge/h3-v4.1.0-blue.svg)](https://github.com/uber/h3/releases/tag/v4.1.0)
1010

1111
This library provides Java bindings for the [H3 Core Library](https://github.com/uber/h3). For API reference, please see the [H3 Documentation](https://h3geo.org/).
1212

@@ -18,14 +18,14 @@ Add it to your pom.xml:
1818
<dependency>
1919
<groupId>com.uber</groupId>
2020
<artifactId>h3</artifactId>
21-
<version>4.0.2</version>
21+
<version>4.1.0</version>
2222
</dependency>
2323
```
2424

2525
Or, using Gradle:
2626

2727
```gradle
28-
compile("com.uber:h3:4.0.2")
28+
compile("com.uber:h3:4.1.0")
2929
```
3030

3131
Encode a location into a hexagon address:

docs/releasing.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ export GPG_TTY=$(tty)
2626
### docker: Error response from daemon: error while creating mount source path
2727

2828
This has been seen when the source path is not shared from the host in the Docker settings. Even if the source path appears to have been shared, if the source path is a symbolic link, you may need to reshare it from Docker Preferences.
29+
30+
### fatal error: jni.h: No such file or directory
31+
32+
This can occur when `JAVA_HOME` is not set.

h3version.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
h3.git.reference=v4.0.1
1+
h3.git.reference=v4.1.0

src/main/c/h3-java/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ project(h3-java LANGUAGES C)
3838
include_directories(${H3_BUILD_ROOT}/src/h3lib/include)
3939

4040
if(USE_NATIVE_JNI)
41+
message("USE_NATIVE_JNI")
42+
# Disable unneeded parts of FindJNI
43+
# https://stackoverflow.com/questions/51047978/cmake-could-not-find-jni
44+
set(JAVA_AWT_LIBRARY NotNeeded)
45+
set(JAVA_JVM_LIBRARY NotNeeded)
4146
find_package(JNI REQUIRED)
4247

4348
include_directories(${JNI_INCLUDE_DIRS})
4449
else()
50+
message("using /java")
4551
include_directories(/java/include)
4652
# TODO Provide correct jni_md.h
4753
include_directories(/java/include/linux)

src/main/c/h3-java/src/jniapi.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019, 2022 Uber Technologies, Inc.
2+
* Copyright 2017-2019, 2022-2023 Uber Technologies, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1353,3 +1353,35 @@ JNIEXPORT jboolean JNICALL Java_com_uber_h3core_NativeMethods_isValidVertex(
13531353
JNIEnv *env, jobject thiz, jlong h3) {
13541354
return isValidVertex(h3);
13551355
}
1356+
1357+
/*
1358+
* Class: com_uber_h3core_NativeMethods
1359+
* Method: cellToChildPos
1360+
* Signature: (JI)J
1361+
*/
1362+
JNIEXPORT jlong JNICALL Java_com_uber_h3core_NativeMethods_cellToChildPos(
1363+
JNIEnv *env, jobject thiz, jlong child, jint parentRes) {
1364+
jlong pos;
1365+
H3Error err = cellToChildPos(child, parentRes, &pos);
1366+
if (err) {
1367+
ThrowH3Exception(env, err);
1368+
return 0;
1369+
}
1370+
return pos;
1371+
}
1372+
1373+
/*
1374+
* Class: com_uber_h3core_NativeMethods
1375+
* Method: childPosToCell
1376+
* Signature: (JJI)J
1377+
*/
1378+
JNIEXPORT jlong JNICALL Java_com_uber_h3core_NativeMethods_childPosToCell(
1379+
JNIEnv *env, jobject thiz, jlong childPos, jlong parent, jint childRes) {
1380+
H3Index out;
1381+
H3Error err = childPosToCell(childPos, parent, childRes, &out);
1382+
if (err) {
1383+
ThrowH3Exception(env, err);
1384+
return 0;
1385+
}
1386+
return out;
1387+
}

src/main/java/com/uber/h3core/H3Core.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019, 2022 Uber Technologies, Inc.
2+
* Copyright 2017-2019, 2022-2023 Uber Technologies, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -719,6 +719,16 @@ public String cellToCenterChild(String h3, int childRes) {
719719
return h3ToString(cellToCenterChild(stringToH3(h3), childRes));
720720
}
721721

722+
/** Returns the number of children the cell index has at the given resolution. */
723+
public long cellToChildrenSize(long cell, int childRes) {
724+
return h3Api.cellToChildrenSize(cell, childRes);
725+
}
726+
727+
/** Returns the number of children the cell index has at the given resolution. */
728+
public long cellToChildrenSize(String cellAddress, int childRes) {
729+
return cellToChildrenSize(stringToH3(cellAddress), childRes);
730+
}
731+
722732
/**
723733
* Returns the center child at the given resolution.
724734
*
@@ -1118,6 +1128,38 @@ public boolean isValidVertex(String h3Address) {
11181128
return h3Api.isValidVertex(stringToH3(h3Address));
11191129
}
11201130

1131+
/**
1132+
* Returns the position of the child cell within an ordered list of all children of the cell's
1133+
* parent at the specified resolution parentRes.
1134+
*/
1135+
public long cellToChildPos(String childAddress, int parentRes) {
1136+
return cellToChildPos(stringToH3(childAddress), parentRes);
1137+
}
1138+
1139+
/**
1140+
* Returns the position of the child cell within an ordered list of all children of the cell's
1141+
* parent at the specified resolution parentRes.
1142+
*/
1143+
public long cellToChildPos(long child, int parentRes) {
1144+
return h3Api.cellToChildPos(child, parentRes);
1145+
}
1146+
1147+
/**
1148+
* Returns the child cell at a given position within an ordered list of all children of parent at
1149+
* the specified resolution childRes.
1150+
*/
1151+
public long childPosToCell(long childPos, long parent, int childRes) {
1152+
return h3Api.childPosToCell(childPos, parent, childRes);
1153+
}
1154+
1155+
/**
1156+
* Returns the child cell at a given position within an ordered list of all children of parent at
1157+
* the specified resolution childRes.
1158+
*/
1159+
public String childPosToCell(long childPos, String parentAddress, int childRes) {
1160+
return h3ToString(childPosToCell(childPos, stringToH3(parentAddress), childRes));
1161+
}
1162+
11211163
/** Transforms a collection of H3 indexes in string form to a list of H3 indexes in long form. */
11221164
private List<Long> stringToH3List(Collection<String> collection) {
11231165
return collection.stream().map(this::stringToH3).collect(Collectors.toList());

src/main/java/com/uber/h3core/NativeMethods.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,8 @@ native void polygonToCells(
139139
native void vertexToLatLng(long h3, double[] latLng);
140140

141141
native boolean isValidVertex(long h3);
142+
143+
native long cellToChildPos(long child, int parentRes);
144+
145+
native long childPosToCell(long childPos, long parent, int childRes);
142146
}

src/test/java/com/uber/h3core/TestHierarchy.java

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019, 2022 Uber Technologies, Inc.
2+
* Copyright 2019, 2022-2023 Uber Technologies, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,13 @@
1616
package com.uber.h3core;
1717

1818
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNotEquals;
1920
import static org.junit.Assert.assertTrue;
21+
import static org.junit.Assert.fail;
2022

2123
import com.google.common.collect.ImmutableList;
2224
import com.uber.h3core.exceptions.H3Exception;
25+
import com.uber.h3core.util.LatLng;
2326
import java.util.ArrayList;
2427
import java.util.Collection;
2528
import java.util.HashSet;
@@ -74,8 +77,8 @@ public void testH3ToChildren() {
7477

7578
// res 0 pentagon has 5 hexagon children and 1 pentagon child at res 1.
7679
// Total output will be:
77-
// 5 * 7 children of res 1 hexagons
78-
// 6 children of res 1 pentagon
80+
// 5 * 7 children of res 1 hexagons
81+
// 6 children of res 1 pentagon
7982
assertEquals(5 * 7 + 6, pentagonChildren.size());
8083

8184
// Don't crash
@@ -188,4 +191,105 @@ public void testH3ToCenterChildNegative() {
188191
public void testH3ToCenterChildOutOfRange() {
189192
h3.cellToCenterChild("8928308280fffff", 16);
190193
}
194+
195+
@Test
196+
public void testCellToChildPos() {
197+
assertEquals(0, h3.cellToChildPos(0x88283080ddfffffL, 8));
198+
assertEquals(6, h3.cellToChildPos(0x88283080ddfffffL, 7));
199+
assertEquals(41, h3.cellToChildPos("88283080ddfffff", 6));
200+
}
201+
202+
@Test(expected = H3Exception.class)
203+
public void testCellToChildPosError() {
204+
h3.cellToChildPos(0x88283080ddfffffL, 9);
205+
}
206+
207+
@Test(expected = H3Exception.class)
208+
public void testCellToChildPosError2() {
209+
h3.cellToChildPos("88283080ddfffff", 9);
210+
}
211+
212+
@Test
213+
public void childPosToCell() {
214+
assertEquals(0x88283080ddfffffL, h3.childPosToCell(0, 0x88283080ddfffffL, 8));
215+
assertEquals(
216+
0x88283080ddfffffL, h3.childPosToCell(6, h3.cellToParent(0x88283080ddfffffL, 7), 8));
217+
assertEquals(
218+
"88283080ddfffff", h3.childPosToCell(41, h3.cellToParentAddress("88283080ddfffff", 6), 8));
219+
}
220+
221+
@Test(expected = H3Exception.class)
222+
public void testChildPosToCellError() {
223+
h3.childPosToCell(-1, 0x88283080ddfffffL, 9);
224+
}
225+
226+
@Test(expected = H3Exception.class)
227+
public void testChildPosToCellError2() {
228+
h3.childPosToCell(10000, "88283080ddfffff", 9);
229+
}
230+
231+
@Test
232+
public void cellToChildPosRoundTrip() {
233+
// These are somewhat arbitrary, but cover a few different parts of the globe
234+
List<LatLng> testLatLngs =
235+
ImmutableList.of(
236+
new LatLng(37.81331899988944, -122.409290778685),
237+
new LatLng(64.2868041, 8.7824902),
238+
new LatLng(5.8815246, 54.3336044),
239+
new LatLng(-41.4486737, 143.918175));
240+
241+
for (LatLng ll : testLatLngs) {
242+
for (int res = 0; res < 16; res++) {
243+
long child = h3.latLngToCell(ll.lat, ll.lng, res);
244+
long parent = h3.cellToParent(child, 0);
245+
long pos = h3.cellToChildPos(child, 0);
246+
long cell = h3.childPosToCell(pos, parent, res);
247+
assertNotEquals("sanity check that pos is not a reference to child", child, pos);
248+
assertEquals("round trip produced the same cell", child, cell);
249+
}
250+
}
251+
}
252+
253+
@Test
254+
public void childPosAndChildrenSize() {
255+
// one hexagon, one pentagon
256+
for (String index : ImmutableList.of("80bffffffffffff", "80a7fffffffffff")) {
257+
for (int res = 0; res < 16; res++) {
258+
long count = h3.cellToChildrenSize(index, res);
259+
assertTrue(
260+
"count has the right order of magnitude",
261+
Math.pow(6, res) <= count && count <= Math.pow(7, res));
262+
263+
String child = h3.childPosToCell(count - 1, index, res);
264+
long pos = h3.cellToChildPos(child, 0);
265+
assertEquals("got expected round trip", count - 1, pos);
266+
267+
try {
268+
h3.childPosToCell(count, index, res);
269+
fail("Should have thrown, one more is out of range");
270+
} catch (H3Exception e) {
271+
// expected
272+
assertEquals(2 /* E_DOMAIN */, e.getCode());
273+
}
274+
}
275+
}
276+
}
277+
278+
@Test(expected = H3Exception.class)
279+
public void cellToChildrenSizeError() {
280+
// Invalid resolution
281+
h3.cellToChildrenSize("88283080ddfffff", 5);
282+
}
283+
284+
@Test(expected = H3Exception.class)
285+
public void cellToChildrenSizeError2() {
286+
// Invalid resolution
287+
h3.cellToChildrenSize(0x88283080ddfffffL, -1);
288+
}
289+
290+
@Test(expected = H3Exception.class)
291+
public void cellToChildrenSizeError3() {
292+
// Invalid index
293+
h3.cellToChildrenSize(0xffffffffffffffffL, 9);
294+
}
191295
}

0 commit comments

Comments
 (0)