Skip to content

Commit

Permalink
Merge a7745e1 into e78463c
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacbrodsky committed Jan 18, 2023
2 parents e78463c + a7745e1 commit 25c51b9
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ file [H3Core.java](./src/main/java/com/uber/h3core/H3Core.java), and support
for the Linux x64 and Darwin x64 platforms.

## Unreleased Changes
## [4.1.0] - 2023-01-18
### Added
- `cellToChildPos` and `childPosToCell` functions. (#121)

### Changed
- Upgraded the core library to v4.1.0. (#121)

## [4.0.2] - 2022-09-21
### Changed
- Upgraded the core library to v4.0.1. (#113)
Expand Down
4 changes: 4 additions & 0 deletions docs/releasing.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ export GPG_TTY=$(tty)
### docker: Error response from daemon: error while creating mount source path

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.

### fatal error: jni.h: No such file or directory

This can occur when `JAVA_HOME` is not set.
2 changes: 1 addition & 1 deletion h3version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h3.git.reference=v4.0.1
h3.git.reference=v4.1.0
6 changes: 6 additions & 0 deletions src/main/c/h3-java/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ project(h3-java LANGUAGES C)
include_directories(${H3_BUILD_ROOT}/src/h3lib/include)

if(USE_NATIVE_JNI)
message("USE_NATIVE_JNI")
# Disable unneeded parts of FindJNI
# https://stackoverflow.com/questions/51047978/cmake-could-not-find-jni
set(JAVA_AWT_LIBRARY NotNeeded)
set(JAVA_JVM_LIBRARY NotNeeded)
find_package(JNI REQUIRED)

include_directories(${JNI_INCLUDE_DIRS})
else()
message("using /java")
include_directories(/java/include)
# TODO Provide correct jni_md.h
include_directories(/java/include/linux)
Expand Down
34 changes: 33 additions & 1 deletion src/main/c/h3-java/src/jniapi.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2019, 2022 Uber Technologies, Inc.
* Copyright 2017-2019, 2022-2023 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1353,3 +1353,35 @@ JNIEXPORT jboolean JNICALL Java_com_uber_h3core_NativeMethods_isValidVertex(
JNIEnv *env, jobject thiz, jlong h3) {
return isValidVertex(h3);
}

/*
* Class: com_uber_h3core_NativeMethods
* Method: cellToChildPos
* Signature: (JI)J
*/
JNIEXPORT jlong JNICALL Java_com_uber_h3core_NativeMethods_cellToChildPos(
JNIEnv *env, jobject thiz, jlong child, jint parentRes) {
jlong pos;
H3Error err = cellToChildPos(child, parentRes, &pos);
if (err) {
ThrowH3Exception(env, err);
return 0;
}
return pos;
}

/*
* Class: com_uber_h3core_NativeMethods
* Method: childPosToCell
* Signature: (JJI)J
*/
JNIEXPORT jlong JNICALL Java_com_uber_h3core_NativeMethods_childPosToCell(
JNIEnv *env, jobject thiz, jlong childPos, jlong parent, jint childRes) {
H3Index out;
H3Error err = childPosToCell(childPos, parent, childRes, &out);
if (err) {
ThrowH3Exception(env, err);
return 0;
}
return out;
}
34 changes: 33 additions & 1 deletion src/main/java/com/uber/h3core/H3Core.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2019, 2022 Uber Technologies, Inc.
* Copyright 2017-2019, 2022-2023 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1118,6 +1118,38 @@ public boolean isValidVertex(String h3Address) {
return h3Api.isValidVertex(stringToH3(h3Address));
}

/**
* Returns the position of the child cell within an ordered list of all children of the cell's
* parent at the specified resolution parentRes.
*/
public long cellToChildPos(String childAddress, int parentRes) {
return cellToChildPos(stringToH3(childAddress), parentRes);
}

/**
* Returns the position of the child cell within an ordered list of all children of the cell's
* parent at the specified resolution parentRes.
*/
public long cellToChildPos(long child, int parentRes) {
return h3Api.cellToChildPos(child, parentRes);
}

/**
* Returns the child cell at a given position within an ordered list of all children of parent at
* the specified resolution childRes.
*/
public long childPosToCell(long childPos, long parent, int childRes) {
return h3Api.childPosToCell(childPos, parent, childRes);
}

/**
* Returns the child cell at a given position within an ordered list of all children of parent at
* the specified resolution childRes.
*/
public String childPosToCell(long childPos, String parentAddress, int childRes) {
return h3ToString(childPosToCell(childPos, stringToH3(parentAddress), childRes));
}

/** Transforms a collection of H3 indexes in string form to a list of H3 indexes in long form. */
private List<Long> stringToH3List(Collection<String> collection) {
return collection.stream().map(this::stringToH3).collect(Collectors.toList());
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/uber/h3core/NativeMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,8 @@ native void polygonToCells(
native void vertexToLatLng(long h3, double[] latLng);

native boolean isValidVertex(long h3);

native long cellToChildPos(long child, int parentRes);

native long childPosToCell(long childPos, long parent, int childRes);
}
38 changes: 37 additions & 1 deletion src/test/java/com/uber/h3core/TestHierarchy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019, 2022 Uber Technologies, Inc.
* Copyright 2019, 2022-2023 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -188,4 +188,40 @@ public void testH3ToCenterChildNegative() {
public void testH3ToCenterChildOutOfRange() {
h3.cellToCenterChild("8928308280fffff", 16);
}

@Test
public void testCellToChildPos() {
assertEquals(0, h3.cellToChildPos(0x88283080ddfffffL, 8));
assertEquals(6, h3.cellToChildPos(0x88283080ddfffffL, 7));
assertEquals(41, h3.cellToChildPos("88283080ddfffff", 6));
}

@Test(expected = H3Exception.class)
public void testCellToChildPosError() {
h3.cellToChildPos(0x88283080ddfffffL, 9);
}

@Test(expected = H3Exception.class)
public void testCellToChildPosError2() {
h3.cellToChildPos("88283080ddfffff", 9);
}

@Test
public void childPosToCell() {
assertEquals(0x88283080ddfffffL, h3.childPosToCell(0, 0x88283080ddfffffL, 8));
assertEquals(
0x88283080ddfffffL, h3.childPosToCell(6, h3.cellToParent(0x88283080ddfffffL, 7), 8));
assertEquals(
"88283080ddfffff", h3.childPosToCell(41, h3.cellToParentAddress("88283080ddfffff", 6), 8));
}

@Test(expected = H3Exception.class)
public void testChildPosToCellError() {
h3.childPosToCell(-1, 0x88283080ddfffffL, 9);
}

@Test(expected = H3Exception.class)
public void testChildPosToCellError2() {
h3.childPosToCell(10000, "88283080ddfffff", 9);
}
}

0 comments on commit 25c51b9

Please sign in to comment.