Giter VIP home page Giter VIP logo

h3-duckdb's Introduction

Extension Test DuckDB Version H3 Version License

This is a DuckDB extension that adds support for the H3 discrete global grid system, so you can index points and geometries to hexagons in SQL.

Get started

Load from the community extensions repository:

INSTALL h3 FROM community;
LOAD h3;

Test running an H3 function:

SELECT h3_cell_to_latlng('822d57fffffffff');

Or, using the integer API, which generally has better performance:

SELECT h3_cell_to_latlng(586265647244115967);

Implemented functions

This extension implements the entire H3 API. The full list of functions is below.

All functions support H3 indexes specified as UBIGINT (uint64) or BIGINT (int64), but the unsigned one is preferred and is returned when the extension can't detect which one to use. The unsigned and signed APIs are identical. Many functions also support VARCHAR H3 index input and output.

Full list of functions

Function Notes Description
h3_latlng_to_cell u Convert latitude/longitude coordinate to cell ID
h3_cell_to_lat v Convert cell ID to latitude
h3_cell_to_lng v Convert cell ID to longitude
h3_cell_to_latlng v Convert cell ID to latitude/longitude
h3_cell_to_boundary_wkt v Convert cell ID to cell boundary
h3_get_resolution v Get resolution number of cell ID
h3_get_base_cell_number v Get base cell number of cell ID
h3_string_to_h3 u Convert VARCHAR cell ID to UBIGINT
h3_h3_to_string i Convert BIGINT or UBIGINT cell ID to VARCHAR
h3_is_valid_cell v True if this is a valid cell ID
h3_is_res_class_iii v True if the cell's resolution is class III
h3_is_pentagon v True if the cell is a pentagon
h3_get_icosahedron_faces v List of icosahedron face IDs the cell is on
h3_cell_to_parent v Get coarser cell for a cell
h3_cell_to_children v Get finer cells for a cell
h3_cell_to_center_child v Get the center finer cell for a cell
h3_cell_to_child_pos v Get a sub-indexing number for a cell inside a parent
h3_child_pos_to_cell v Convert parent and sub-indexing number to a cell ID
h3_compact_cells i Convert a set of single-resolution cells to the minimal mixed-resolution set
h3_uncompact_cells i Convert a mixed-resolution set to a single-resolution set of cells
h3_grid_disk i Find cells within a grid distance
h3_grid_disk_distances i Find cells within a grid distance, sorted by distance
h3_grid_disk_unsafe i Find cells within a grid distance, with no pentagon distortion
h3_grid_disk_distances_unsafe i Find cells within a grid distance, sorted by distance, with no pentagon distortion
h3_grid_ring_unsafe i Find cells exactly a grid distance away, with no pentagon distortion
h3_grid_path_cells i Find a grid path to connect two cells
h3_grid_distance i Find the grid distance between two cells
h3_cell_to_local_ij i Convert a cell ID to a local I,J coordinate space
h3_local_ij_to_cell i Convert a local I,J coordinate to a cell ID
h3_cell_to_vertex i Get the vertex ID for a cell ID and vertex number
h3_cell_to_vertexes i Get all vertex IDs for a cell ID
h3_vertex_to_lat i Convert a vertex ID to latitude
h3_vertex_to_lng i Convert a vertex ID to longitude
h3_vertex_to_latlng i Convert a vertex ID to latitude/longitude coordinate
h3_is_valid_vertex v True if passed a valid vertex ID
h3_is_valid_directed_edge v True if passed a valid directed edge ID
h3_origin_to_directed_edges i Get all directed edge IDs for a cell ID
h3_directed_edge_to_cells i Convert a directed edge ID to origin/destination cell IDs
h3_get_directed_edge_origin i Convert a directed edge ID to origin cell ID
h3_get_directed_edge_destination i Convert a directed edge ID to destination cell ID
h3_cells_to_directed_edge i Convert an origin/destination pair to directed edge ID
h3_are_neighbor_cells i True if the two cell IDs are directly adjacent
h3_directed_edge_to_boundary_wkt v Convert directed edge ID to linestring WKT
h3_get_hexagon_area_avg Get average area of a hexagon cell at resolution
h3_cell_area v Get the area of a cell ID
h3_edge_length v Get the length of a directed edge ID
h3_get_num_cells Get the number of cells at a resolution
h3_get_res0_cells u Get all resolution 0 cells
h3_get_pentagons u Get all pentagons at a resolution
h3_great_circle_distance Compute the great circle distance between two points (haversine)
h3_cells_to_multi_polygon_wkt v Convert a set of cells to multipolygon WKT
h3_polygon_wkt_to_cells u Convert polygon WKT to a set of cells

Notes

  • v: Supports VARCHAR, UBIGINT, and BIGINT input and output.
  • i: Supports UBIGINT and BIGINT input and output. (TODO for these to support VARCHAR too.)
  • u: Supports UBIGINT output only.

Alternative download / install

If you'd like to install the H3 extension from the version published here, rather than the community extension version, you will need to run DuckDB with the unsigned option:

duckdb -unsigned

Load the extension:

INSTALL h3 FROM 'https://pub-cc26a6fd5d8240078bd0c2e0623393a5.r2.dev';
LOAD h3;

If you want to directly download the latest version of the extension: Linux AMD64 Linux AMD64 GCC4 Linux Arm64 OSX AMD64 OSX Arm64 wasm eh wasm mvp wasm threads Windows AMD64

Development

To build, type:

git submodule update --init
GEN=ninja make release

You will need Git, CMake, and a C compiler. The build instructions suggest using ninja because it enables parallelism by default. Using make instead is fine, but you will want to enable the following parallelism option, because building DuckDB can take a very long time (>=1 hour is not unusual). Run the below replacing 4 with the number of CPU cores on your machine.

CMAKE_BUILD_PARALLEL_LEVEL=4 make duckdb_release release

To run, run the bundled duckdb shell:

./build/release/duckdb -unsigned

Load the extension:

load 'build/release/extension/h3/h3.duckdb_extension';

To run tests:

make test

To update the submodules to latest upstream, run:

make update_deps

License

h3-duckdb Copyright 2022 Isaac Brodsky. Licensed under the Apache 2.0 License.

H3 Copyright 2018 Uber Technologies Inc. (Apache 2.0 License)

DGGRID Copyright (c) 2015 Southern Oregon University

DuckDB Copyright 2018-2022 Stichting DuckDB Foundation (MIT License)

DuckDB extension-template Copyright 2018-2022 DuckDB Labs BV (MIT License)

h3-duckdb's People

Contributors

carlopi avatar isaacbrodsky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

h3-duckdb's Issues

Getting an error using `h3_h3_to_string` on a H3 cell stored in a column

In my instance of DuckDB this query works fine:

select h3_h3_to_string(622236751406759935::ubigint);

However when I try to apply h3_h3_to_string to a column in my data with H3 cells stored, generated using h3_latlng_to_cell I get the following error:

????INVALID VALUE - Invalid unicode (byte sequence mismatch) detected in value construction?????

I am using DuckDB 0.7.1 and not the version that is packaged when building the H3 extension

Add setting to return VARCHAR by default instead of UBIGINT

For functions like h3_latlng_to_cell, a user could configure the extension to return VARCHAR instead of UBIGINT, if they want to take the debuggability/portability vs performance trade-off. This would apply to functions that currently return only UBIGINT because they cannot bind to the appropriate signature.

See here for an example of adding settings for an extension: https://github.com/duckdb/duckdb/blob/c89fe1055cfa9a98e3408b97657703334a1c9f1b/extension/httpfs/httpfs_extension.cpp#L20

Cannot install extension using `install 'h3ext' from '...';` syntax

D install 'h3ext' from 'http://pub-cc26a6fd5d8240078bd0c2e0623393a5.r2.dev';
HTTP Error: Failed to download extension "h3ext" at URL "http://pub-cc26a6fd5d8240078bd0c2e0623393a5.r2.dev/v0.10.1/linux_amd64/h3ext.duckdb_extension.gz"

Candidate extensions: "s3", "http", "inet"

Perhaps this is because Cloudflare always redirects to HTTPS, and DuckDB does not support HTTPS? Trying to download from the same URL with scheme https gets misdetected as a local file path.

I think what is happening here is that DuckDB has an assumption that extensions are distributed via S3 and that S3 does not enforce HTTPS.

Depends on duckdb/duckdb#9835

Possibly incorrect H3_7 results

I want to convert the following location to an h3_7.

$ h3 latLngToCell \
    --resolution 7 \
    --latitude   70.63539478040273 \
    --longitude -160.04058837894056

This is the value I'm expecting:

870d1c130ffffff

The h3_7 produced by your extension doesn't match the above. I'm not sure if this is due to an integer casting issue or something else.

SELECT ST_X(ST_CENTROID(ST_GEOMFROMTEXT('POINT(-160.04058837894056 70.63539478040273)'))) x,
       ST_Y(ST_CENTROID(ST_GEOMFROMTEXT('POINT(-160.04058837894056 70.63539478040273)'))) y,
       h3_latlng_to_cell(
                    ST_X(ST_CENTROID(ST_GEOMFROMTEXT('POINT(-160.04058837894056 70.63539478040273)'))),
                    ST_Y(ST_CENTROID(ST_GEOMFROMTEXT('POINT(-160.04058837894056 70.63539478040273)'))),
                    9)::bigint as h3_7_uint64,
       printf('%X',
            h3_latlng_to_cell(
                    ST_X(ST_CENTROID(ST_GEOMFROMTEXT('POINT(-160.04058837894056 70.63539478040273)'))),
                    ST_Y(ST_CENTROID(ST_GEOMFROMTEXT('POINT(-160.04058837894056 70.63539478040273)'))),
                    9)::bigint) as h3_7;
┌─────────────────────┬───────────────────┬────────────────────┬─────────────────┐
│          x          │         y         │    h3_7_uint64     │      h3_7       │
│       double        │      double       │       int64        │     varchar     │
├─────────────────────┼───────────────────┼────────────────────┼─────────────────┤
│ -160.04058837894056 │ 70.63539478040273 │ 619503384080678911 │ 898EB0BA947FFFF │
└─────────────────────┴───────────────────┴────────────────────┴─────────────────┘

I tried flipping X and Y but this didn't help.

SELECT ST_X(ST_CENTROID(ST_GEOMFROMTEXT('POINT(-160.04058837894056 70.63539478040273)'))) x,
       ST_Y(ST_CENTROID(ST_GEOMFROMTEXT('POINT(-160.04058837894056 70.63539478040273)'))) y,
       h3_latlng_to_cell(
                    ST_Y(ST_CENTROID(ST_GEOMFROMTEXT('POINT(-160.04058837894056 70.63539478040273)'))),
                    ST_X(ST_CENTROID(ST_GEOMFROMTEXT('POINT(-160.04058837894056 70.63539478040273)'))),
                    9)::bigint as h3_7_uint64,
       printf('%X',
            h3_latlng_to_cell(
                    ST_Y(ST_CENTROID(ST_GEOMFROMTEXT('POINT(-160.04058837894056 70.63539478040273)'))),
                    ST_X(ST_CENTROID(ST_GEOMFROMTEXT('POINT(-160.04058837894056 70.63539478040273)'))),
                    9)::bigint) as h3_7;
┌─────────────────────┬───────────────────┬────────────────────┬─────────────────┐
│          x          │         y         │    h3_7_uint64     │      h3_7       │
│       double        │      double       │       int64        │     varchar     │
├─────────────────────┼───────────────────┼────────────────────┼─────────────────┤
│ -160.04058837894056 │ 70.63539478040273 │ 617049409871151103 │ 890332B443BFFFF │
└─────────────────────┴───────────────────┴────────────────────┴─────────────────┘

prebuilt binaries?

Thanks for providing this amazing extension, it works wonderfully and provides a very valuable set of features!

Apologies if this is a silly question, but is it possible to distribute this extension as a pre-built binary, like other extensions in duckdb? If so, is that on your road map at some point?

Casting uint64 to hex?

What is the best way to convert a value like 610496184837996543 originating from h3_latlng_to_cell into 878eb0baa000000 as a varchar(15)?

My best attempt below results in INVALID VALUE.

SELECT h3_h3_to_string(
        h3_latlng_to_cell(
                ST_X(ST_CENTROID(ST_GEOMFROMTEXT(tile))),
                ST_Y(ST_CENTROID(ST_GEOMFROMTEXT(tile))),
                7)) h3_7_str,

       h3_latlng_to_cell(
                ST_X(ST_CENTROID(ST_GEOMFROMTEXT(tile))),
                ST_Y(ST_CENTROID(ST_GEOMFROMTEXT(tile))),
                7) h3_7,

       ST_ASTEXT(ST_CENTROID(ST_GEOMFROMTEXT(tile))) as centroid,

       ST_CENTROID(ST_GEOMFROMTEXT(tile)) centroid_wkb
FROM mobile_perf
LIMIT 1;
┌──────────────────────┬────────────────────┬────────────────────────────────┬────────────────────────────────────────────┐
│       h3_7_str       │        h3_7        │            centroid            │                centroid_wkb                │
│       varchar        │       uint64       │            varchar             │                 geography                  │
├──────────────────────┼────────────────────┼────────────────────────────────┼────────────────────────────────────────────┤
│ ????INVALID VALUE …  │ 610496184837996543 │ POINT(-160.04058837894056 70…  │ 0101000000B70400804C0164C03F78DE4EAAA85140 │
└──────────────────────┴────────────────────┴────────────────────────────────┴────────────────────────────────────────────┘

Attempting to compile for DuckDB-Wasm, error when executing functions

Hi! First of all, fantastic work on this library. I've been using it a bit with DuckDB locally and it's super helpful for many analysis tasks.

Now I'm going a step further and attempting to build this library for the DuckDB Wasm distribution. After a bit of fiddling around with various build settings, I was successfully able to build a h3ext.duckdb_extension.wasm file which I can load into a Wasm instance of DuckDB with load 'h3ext.duckdb_extension.wasm'. Great!

Now I'm running into an issue when I try to call functions from this library. When I call any of the functions, I get a rather cryptic error message. For example:

TypeError: Cannot read properties of undefined (reading 'apply') at wasmImports.<computed>.stub._.<computed> (duckdb-browser-eh.worker.c067f4b4d5eff325690c.js:2:17523)

image

The interesting thing is that duckdb seems to have registered the functions correctly, because if I call them with the wrong types, it gives me a helpful error message:

image

So it seems that something is going on when the functions actually execute, or when results are returned.

I have no idea how to even begin to debug this... any ideas? Thanks!

Recent main trunk core dump on linux

duckdb -unsigned
v0.9.2 3c695d7ba9
Enter ".help" for usage hints.
D load h3ext;
[1] 133840 floating point exception (core dumped) duckdb -unsigned

x86_64 GNU/Linux

release build

(same commit works on Mac OSX)

Add noexcept versions of functions

Right now any invalid input in the table may result in an exception and the query failing. A noexcept version (returns H3Error or NULL on error?) would make that easier to use. This should probably be the default unless the user opts in to throwing, as it will terminate the entire query.

Performing a polyfill?

I've loaded a GeoJSON file into a DuckDB table that has two columns (own, geom) by leveraging the spatial extension.

I assume in order to perform a polyfill of polygons, I need to use the h3_polygon_wkt_to_cells.

In order to get a sense of the output, I tried executing the following SQL:

select h3_polygon_wkt_to_cells(geom,11) from nm_sma limit 1;

I'm seeing the following error message which I don't know how to interpret:

Error: Invalid Error: Invalid WKT: expected a hole loop or ')' at pos 990

Could someone point me in the right direction?

weird behavior with h3_polygon_wkt_to_cells()

buffer1_g.json
buffer2_g.json
buffer3_g.json

These are 3 buffers around river Garonne (1000, 2000 and 3000 meters).
I want to get H3 cells for these buffers.

FROM st_read('buffer2_g.json')
SELECT geom.h3_polygon_wkt_to_cells(8);	

returns an empty list, which is unexpected.

FROM st_read('buffer1_g.json')
SELECT geom.h3_polygon_wkt_to_cells(8);	

returns a list with 2 443 cells: OK.

FROM st_read('buffer3_g.json')
SELECT geom.h3_polygon_wkt_to_cells(8);	

returns a list with 5 479 cells: OK.

Precompiled binary releases?

I suspect having users compile DuckDB and your extension might be off-putting. The process usually took over an hour for me.

It would be nice if I could instruct my readers to launch DuckDB with an unsigned flag and install a binary from GitHub.

Using with DuckDB python api

Firstly, thanks for the great extension. Just wondering if it is possible to use this extension along side the python version of DuckDB.

[Bug] Wrong output when converting between hex and int format

I found the following bug when translating between the h3 hex and int format. It would be great if you can fix the bug or supply me with information about my wrongdoing.

image

Details:

  • The representation of the resulting string has the wrong format
  • The referenced h3Index ("85283473fffffff") should point to (37.34579337536848, -121.9763759725512)

Cache duckdb build in CI

The DuckDB build part of CI takes a very long time; it might be best to cache that build and only retrigger it when tracking a newer upstream version. #3 changes the Makefile to no longer automatically pull in new revisions from upstream since I would like that to be a conscious action.

Building extension in windows 64 bit

I am getting this error when I am trying to build the bindings in my Windows operating system. When I tried to build the extension, I got this error.
I am not an expert on building extensions, but I couldn’t find any solution.
I am running on Windows 64-bit operating system.
Thanks.
build_tpch_issue

Core dump when loading extension in Ubuntu Linux

Hi I cant load Extension.
I've tried with https://pub-cc26a6fd5d8240078bd0c2e0623393a5.r2.dev/v0.10.1/linux_amd64/h3ext.duckdb_extension.gz and with https://pub-cc26a6fd5d8240078bd0c2e0623393a5.r2.dev/v0.10.1/linux_amd64/h3ext.duckdb_extension.gz

jtorres@europa2:~/$ duckdb -unsigned
-- Loading resources from /home/jtorres/.duckdbrc
v0.10.1 4a89d97db8
Enter ".help" for usage hints.
D install 'h3ext.duckdb_extension';
D load 'h3ext';
Violación de segmento (`core' generado)

It works only if I build from sources and run with bundled duckdb

Thank you for your work

build question

I tried to follow the readme to install the h3 extension. However I failed at running CMAKE_BUILD_PARALLEL_LEVEL=4 make duckdb_release release at the terminal, which gives me the error 'CMAKE_BUILD_PARALLEL_LEVEL' is not recognized as an internal or external command, operable program or batch file.

I have cmake 3.13.1 installed, and my system is windows 11

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.