Skip to content

Commit 18054da

Browse files
Isaac Brodskynrabinowitz
Isaac Brodsky
andauthored
Add h3_cell_to_latlng (#3)
* Add h3_cell_to_latlng * Update src/main/java/com/foursquare/presto/h3/CellToLatLngFunction.java Co-authored-by: Nick Rabinowitz <[email protected]> Co-authored-by: Nick Rabinowitz <[email protected]>
1 parent ee6bd18 commit 18054da

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.foursquare.presto.h3;
2+
3+
import static com.facebook.presto.common.type.DoubleType.DOUBLE;
4+
5+
import com.facebook.presto.common.block.Block;
6+
import com.facebook.presto.common.block.BlockBuilder;
7+
import com.facebook.presto.common.type.StandardTypes;
8+
import com.facebook.presto.spi.function.Description;
9+
import com.facebook.presto.spi.function.ScalarFunction;
10+
import com.facebook.presto.spi.function.SqlNullable;
11+
import com.facebook.presto.spi.function.SqlType;
12+
import com.uber.h3core.util.LatLng;
13+
14+
/**
15+
* Wraps {@link com.uber.h3core.H3Core#cellToLatLng(long)}. Produces a row of latitude, longitude
16+
* degrees.
17+
*/
18+
public final class CellToLatLngFunction {
19+
@ScalarFunction(value = "h3_cell_to_latlng")
20+
@Description("Convert H3 index to degrees lat/lng")
21+
@SqlNullable
22+
@SqlType("ARRAY(DOUBLE)")
23+
public static Block cellToLatLng(@SqlType(StandardTypes.BIGINT) long h3) {
24+
try {
25+
LatLng latLng = H3Plugin.h3.cellToLatLng(h3);
26+
// TODO: It would be nice to return this as a ROW(lat DOUBLE, lng DOUBLE)
27+
// but that is blocked on https://github.com/prestodb/presto/issues/18494
28+
// (determining how to build the Block to return)
29+
BlockBuilder blockBuilder = DOUBLE.createFixedSizeBlockBuilder(2);
30+
DOUBLE.writeDouble(blockBuilder, latLng.lat);
31+
DOUBLE.writeDouble(blockBuilder, latLng.lng);
32+
return blockBuilder.build();
33+
} catch (Exception e) {
34+
return null;
35+
}
36+
}
37+
}

src/main/java/com/foursquare/presto/h3/H3Plugin.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static int longToInt(long l) {
4747
@Override
4848
public Set<Class<?>> getFunctions() {
4949
return ImmutableSet.<Class<?>>builder()
50-
.add(LatLngToCellFunction.class, CellToParentFunction.class)
50+
.add(LatLngToCellFunction.class, CellToLatLngFunction.class, CellToParentFunction.class)
5151
.build();
5252
}
5353
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.foursquare.presto.h3;
2+
3+
import static com.foursquare.presto.h3.H3PluginTest.assertQueryResults;
4+
import static com.foursquare.presto.h3.H3PluginTest.createQueryRunner;
5+
6+
import com.facebook.presto.testing.QueryRunner;
7+
import com.google.common.collect.ImmutableList;
8+
import java.util.Collections;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.TestInstance;
11+
import org.junit.jupiter.api.TestInstance.Lifecycle;
12+
13+
@TestInstance(Lifecycle.PER_CLASS)
14+
public class CellToLatLngFunctionTest {
15+
@Test
16+
public void testCellToLatLng() {
17+
try (QueryRunner queryRunner = createQueryRunner()) {
18+
assertQueryResults(
19+
queryRunner,
20+
"SELECT h3_cell_to_latlng(578536630256664575)",
21+
ImmutableList.of(
22+
ImmutableList.of(ImmutableList.of(2.300882111626747, -5.245390296777327))));
23+
24+
assertQueryResults(
25+
queryRunner,
26+
"SELECT h3_cell_to_latlng(null)",
27+
ImmutableList.of(Collections.singletonList(null)));
28+
assertQueryResults(
29+
queryRunner,
30+
"SELECT h3_cell_to_latlng(-1)",
31+
ImmutableList.of(Collections.singletonList(null)));
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)