|
| 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 | +} |
0 commit comments