From 298f17faae4d3466fd6fc3b6d64dad107181bb25 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Thu, 19 May 2022 19:48:25 -0400 Subject: [PATCH 01/60] WIP: Starting work on v4, The Sequel (#238) * cimport for h3fake2 doesn't seem to work * back to where we were * meh, close to what i was hoping for * h3fake2 in github actions * comment out current bindings that consume the h3c v3 library * don't need the coveragerc file at the moment; clear out c files in /tests * sdist needs h3fake2 * try CIBW_BEFORE_BUILD to get h3fake2 * need h3fake2 for cibuildwheel tests * update h3 C library * comment out most of h3lib.pxd * calling from v4 c lib works! (tests dont' work yet tho) * tests shall pass * starting to move over first few functions * little steps * util submodule fully moved over; some hackery with h3fake2 errors * getting even trickier with h3fake2 errors * few more simple functions * geo_to_h3 and h3_to_geo * grid_distance function * grid_disk * parents and kids * compact/uncompact * getNumCells * comment out sdist test temporarily * commenting out `make_sdist` seems to have turned off the other CI jobs * no, it was that we're pushing to the dev branch * keep the sdist job in there, but commented out --- .coveragerc | 3 - .github/workflows/coverage-lint.yml | 5 +- .github/workflows/tests.yml | 5 +- .github/workflows/wheels.yml | 53 ++-- makefile | 2 + src/h3/_cy/CMakeLists.txt | 1 - src/h3/_cy/__init__.py | 41 +++- src/h3/_cy/cells.pxd | 20 +- src/h3/_cy/cells.pyx | 356 +++++++++++++++------------ src/h3/_cy/edges.pxd | 20 +- src/h3/_cy/edges.pyx | 140 +++++------ src/h3/_cy/geo.pxd | 6 +- src/h3/_cy/geo.pyx | 365 ++++++++++++++-------------- src/h3/_cy/h3lib.pxd | 232 +++++++++--------- src/h3/_cy/to_multipoly.pyx | 104 ++++---- src/h3/_cy/unstable_vect.pyx | 89 ------- src/h3/_cy/util.pxd | 6 +- src/h3/_cy/util.pyx | 12 +- src/h3/_version.py | 37 +-- src/h3/unstable/__init__.py | 9 - src/h3/unstable/v4.py | 125 ---------- src/h3/unstable/vect.py | 105 -------- src/h3lib | 2 +- tests/cython_example.pyx | 28 +-- tests/h3fake2_errors.py | 15 ++ tests/test_cells_and_edges.py | 2 +- tests/test_cython.py | 40 +-- tests/test_length_area.py | 2 +- tests/test_polyfill.py | 6 +- tests/test_unstable_vect.py | 69 ------ 30 files changed, 804 insertions(+), 1096 deletions(-) delete mode 100644 .coveragerc delete mode 100644 src/h3/_cy/unstable_vect.pyx delete mode 100644 src/h3/unstable/__init__.py delete mode 100644 src/h3/unstable/v4.py delete mode 100644 src/h3/unstable/vect.py create mode 100644 tests/h3fake2_errors.py delete mode 100644 tests/test_unstable_vect.py diff --git a/.coveragerc b/.coveragerc deleted file mode 100644 index fbf9e0e..0000000 --- a/.coveragerc +++ /dev/null @@ -1,3 +0,0 @@ -[run] -omit = - */h3/unstable/* diff --git a/.github/workflows/coverage-lint.yml b/.github/workflows/coverage-lint.yml index e1d0bbd..5110236 100644 --- a/.github/workflows/coverage-lint.yml +++ b/.github/workflows/coverage-lint.yml @@ -2,9 +2,9 @@ name: coverage-lint on: push: - branches: [master] + branches: [master, dev_v4] pull_request: - branches: [master] + branches: [master, dev_v4] jobs: tests: @@ -23,6 +23,7 @@ jobs: - name: Install from source run: | pip install --upgrade pip setuptools wheel + pip install git+https://github.com/ajfriend/h3fake2.git pip install .[all] - name: Lint diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f2c5682..0b9883d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,9 +2,9 @@ name: tests on: push: - branches: [master] + branches: [master, dev_v4] pull_request: - branches: [master] + branches: [master, dev_v4] jobs: tests: @@ -44,6 +44,7 @@ jobs: - name: Install from source run: | pip install --upgrade pip setuptools wheel + pip install git+https://github.com/ajfriend/h3fake2.git pip install .[all] - name: Tests diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index 5f75525..71f47d5 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -2,9 +2,9 @@ name: wheels on: push: - branches: [master] + branches: [master, dev_v4] pull_request: - branches: [master] + branches: [master, dev_v4] types: # Opened, synchronize, and reopened are the default types # We add ready_for_review to additionally trigger when converting from draft to non-draft @@ -18,33 +18,34 @@ on: - published jobs: - make_sdist: - name: SDist - if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.draft }} - runs-on: ubuntu-latest + # make_sdist: + # name: SDist + # if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.draft }} + # runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - with: - submodules: recursive + # steps: + # - uses: actions/checkout@v3 + # with: + # submodules: recursive - - name: Make sdist - run: | - pipx run build --sdist + # - name: Make sdist + # run: | + # pipx run build --sdist - - name: Install from sdist - run: | - pip install --upgrade pip setuptools wheel - cp dist/h3-*.tar.gz h3.tar.gz - pip install h3.tar.gz[all] + # - name: Install from sdist + # run: | + # pip install --upgrade pip setuptools wheel + # pip install git+https://github.com/ajfriend/h3fake2.git + # cp dist/h3-*.tar.gz h3.tar.gz + # pip install h3.tar.gz[all] - - name: Test sdist - run: pytest --cov=h3 --full-trace + # - name: Test sdist + # run: pytest --cov=h3 --full-trace - - name: Upload artifacts to GitHub - uses: actions/upload-artifact@v3 - with: - path: ./dist + # - name: Upload artifacts to GitHub + # uses: actions/upload-artifact@v3 + # with: + # path: ./dist make_cibw_v1_wheels: name: "cibuildwheel v1: ${{ matrix.name }}" @@ -74,6 +75,8 @@ jobs: CIBW_TEST_COMMAND: pytest {project}/tests CIBW_ARCHS_LINUX: auto aarch64 CIBW_BUILD: ${{ matrix.build }} + CIBW_BEFORE_BUILD: pip install git+https://github.com/ajfriend/h3fake2.git + CIBW_BEFORE_TEST: pip install git+https://github.com/ajfriend/h3fake2.git - name: Check with Twine run: | @@ -152,6 +155,8 @@ jobs: CIBW_ARCHS_MACOS: x86_64 arm64 CIBW_BUILD: ${{ matrix.build }} CIBW_SKIP: ${{ matrix.skip }} + CIBW_BEFORE_BUILD: pip install git+https://github.com/ajfriend/h3fake2.git + CIBW_BEFORE_TEST: pip install git+https://github.com/ajfriend/h3fake2.git - name: Check with Twine run: | diff --git a/makefile b/makefile index 6e44c02..7c2845e 100644 --- a/makefile +++ b/makefile @@ -12,6 +12,7 @@ init: purge git submodule update --init python -m venv env env/bin/pip install --upgrade pip wheel setuptools + env/bin/pip install git+https://github.com/ajfriend/h3fake2.git env/bin/pip install .[all] env/bin/pip install -r requirements.in @@ -23,6 +24,7 @@ clear: -@find . -type d -name '*.egg-info' | xargs rm -r -@find . -type f -name '*.pyc' | xargs rm -r -@find . -type d -name '*.ipynb_checkpoints' | xargs rm -r + -@find ./tests -type f -name '*.c' | xargs rm -r rebuild: clear env/bin/pip install .[all] diff --git a/src/h3/_cy/CMakeLists.txt b/src/h3/_cy/CMakeLists.txt index 0d7e78c..386557c 100644 --- a/src/h3/_cy/CMakeLists.txt +++ b/src/h3/_cy/CMakeLists.txt @@ -22,7 +22,6 @@ add_cython_file(geo) add_cython_file(cells) add_cython_file(edges) add_cython_file(to_multipoly) -add_cython_file(unstable_vect) # Include pyx and pxd files in distribution for use by Cython API install( diff --git a/src/h3/_cy/__init__.py b/src/h3/_cy/__init__.py index bb4dfc0..6b17b2d 100644 --- a/src/h3/_cy/__init__.py +++ b/src/h3/_cy/__init__.py @@ -18,27 +18,44 @@ from .cells import ( is_pentagon, get_base_cell, resolution, - parent, + is_res_class_iii, distance, disk, - ring, + parent, children, + center_child, compact, uncompact, num_hexagons, +) + +from h3fake2._cy import ( + # is_cell, + # is_pentagon, + # get_base_cell, + # resolution, + # parent, + # distance, + # disk, + ring, + # children, + # compact, + # uncompact, + # num_hexagons, mean_hex_area, cell_area, line, - is_res_class_iii, + # is_res_class_iii, get_pentagon_indexes, get_res0_indexes, - center_child, + # center_child, get_faces, experimental_h3_to_local_ij, experimental_local_ij_to_h3, ) -from .edges import ( +# from .edges import ( +from h3fake2._cy import ( are_neighbors, edge, is_edge, @@ -53,6 +70,17 @@ from .edges import ( from .geo import ( geo_to_h3, h3_to_geo, + # polyfill_polygon, + # polyfill_geojson, + # polyfill, + # cell_boundary, + # edge_boundary, + # point_dist, +) + +from h3fake2._cy import ( + # geo_to_h3, + # h3_to_geo, polyfill_polygon, polyfill_geojson, polyfill, @@ -61,7 +89,8 @@ from .geo import ( point_dist, ) -from .to_multipoly import ( +# from .to_multipoly import ( +from h3fake2._cy import ( h3_set_to_multi_polygon ) diff --git a/src/h3/_cy/cells.pxd b/src/h3/_cy/cells.pxd index 1071938..4e2d441 100644 --- a/src/h3/_cy/cells.pxd +++ b/src/h3/_cy/cells.pxd @@ -6,20 +6,20 @@ cpdef int get_base_cell(H3int h) except -1 cpdef int resolution(H3int h) except -1 cpdef int distance(H3int h1, H3int h2) except -1 cpdef H3int[:] disk(H3int h, int k) -cpdef H3int[:] _ring_fallback(H3int h, int k) -cpdef H3int[:] ring(H3int h, int k) +# cpdef H3int[:] _ring_fallback(H3int h, int k) +# cpdef H3int[:] ring(H3int h, int k) cpdef H3int parent(H3int h, res=*) except 0 cpdef H3int[:] children(H3int h, res=*) cpdef H3int center_child(H3int h, res=*) except 0 cpdef H3int[:] compact(const H3int[:] hu) cpdef H3int[:] uncompact(const H3int[:] hc, int res) cpdef int64_t num_hexagons(int resolution) except -1 -cpdef double mean_hex_area(int resolution, unit=*) except -1 -cpdef double cell_area(H3int h, unit=*) except -1 -cpdef H3int[:] line(H3int start, H3int end) +# cpdef double mean_hex_area(int resolution, unit=*) except -1 +# cpdef double cell_area(H3int h, unit=*) except -1 +# cpdef H3int[:] line(H3int start, H3int end) cpdef bool is_res_class_iii(H3int h) -cpdef H3int[:] get_pentagon_indexes(int res) -cpdef H3int[:] get_res0_indexes() -cpdef get_faces(H3int h) -cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except * -cpdef H3int experimental_local_ij_to_h3(H3int origin, int i, int j) except 0 +# cpdef H3int[:] get_pentagon_indexes(int res) +# cpdef H3int[:] get_res0_indexes() +# cpdef get_faces(H3int h) +# cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except * +# cpdef H3int experimental_local_ij_to_h3(H3int origin, int i, int j) except 0 diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 4206b81..265e4c3 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -24,15 +24,17 @@ cpdef bool is_cell(H3int h): ------- boolean """ - return h3lib.h3IsValid(h) == 1 + return h3lib.isValidCell(h) == 1 + cpdef bool is_pentagon(H3int h): - return h3lib.h3IsPentagon(h) == 1 + return h3lib.isPentagon(h) == 1 + cpdef int get_base_cell(H3int h) except -1: check_cell(h) - return h3lib.h3GetBaseCell(h) + return h3lib.getBaseCellNumber(h) cpdef int resolution(H3int h) except -1: @@ -41,96 +43,109 @@ cpdef int resolution(H3int h) except -1: """ check_cell(h) - return h3lib.h3GetResolution(h) + return h3lib.getResolution(h) cpdef int distance(H3int h1, H3int h2) except -1: - """ compute the hex-distance between two hexagons + """ Compute the grid distance between two cells """ + cdef: + int64_t distance + h3lib.H3Error err + check_cell(h1) check_cell(h2) - d = h3lib.h3Distance(h1,h2) - - if d < 0: + err = h3lib.gridDistance(h1, h2, &distance) + if err: + # todo: do error handling later s = 'Cells are too far apart to compute distance: {} and {}' s = s.format(hex(h1), hex(h2)) raise H3ValueError(s) - return d + return distance cpdef H3int[:] disk(H3int h, int k): """ Return cells at grid distance `<= k` from `h`. """ + cdef: + int64_t n + h3lib.H3Error err + check_cell(h) check_distance(k) - n = h3lib.maxKringSize(k) + # ignoring error for now + err = h3lib.maxGridDiskSize(k, &n) ptr = create_ptr(n) # todo: return a "smart" pointer that knows its length? - h3lib.kRing(h, k, ptr) + err = h3lib.gridDisk(h, k, ptr) # ignoring error again! mv = create_mv(ptr, n) return mv -cpdef H3int[:] _ring_fallback(H3int h, int k): - """ - `ring` tries to call `h3lib.hexRing` first; if that fails, we call - this function, which relies on `h3lib.kRingDistances`. +# cpdef H3int[:] _ring_fallback(H3int h, int k): +# """ +# `ring` tries to call `h3lib.hexRing` first; if that fails, we call +# this function, which relies on `h3lib.kRingDistances`. - Failures for `h3lib.hexRing` happen when the algortihm runs into a pentagon. - """ - check_cell(h) - check_distance(k) +# Failures for `h3lib.hexRing` happen when the algortihm runs into a pentagon. +# """ +# check_cell(h) +# check_distance(k) - n = h3lib.maxKringSize(k) - # array of h3 cells - ptr = create_ptr(n) +# n = h3lib.maxKringSize(k) +# # array of h3 cells +# ptr = create_ptr(n) - # array of cell distances from `h` - dist_ptr = stdlib.calloc(n, sizeof(int)) - if dist_ptr is NULL: - raise MemoryError() +# # array of cell distances from `h` +# dist_ptr = stdlib.calloc(n, sizeof(int)) +# if dist_ptr is NULL: +# raise MemoryError() - h3lib.kRingDistances(h, k, ptr, dist_ptr) +# h3lib.kRingDistances(h, k, ptr, dist_ptr) - distances = dist_ptr - distances.callback_free_data = stdlib.free +# distances = dist_ptr +# distances.callback_free_data = stdlib.free - for i,v in enumerate(distances): - if v != k: - ptr[i] = 0 +# for i,v in enumerate(distances): +# if v != k: +# ptr[i] = 0 - mv = create_mv(ptr, n) +# mv = create_mv(ptr, n) - return mv +# return mv -cpdef H3int[:] ring(H3int h, int k): - """ Return cells at grid distance `== k` from `h`. - Collection is "hollow" for k >= 1. - """ - check_cell(h) - check_distance(k) +# cpdef H3int[:] ring(H3int h, int k): +# """ Return cells at grid distance `== k` from `h`. +# Collection is "hollow" for k >= 1. +# """ +# check_cell(h) +# check_distance(k) - n = 6*k if k > 0 else 1 - ptr = create_ptr(n) +# n = 6*k if k > 0 else 1 +# ptr = create_ptr(n) - flag = h3lib.hexRing(h, k, ptr) +# flag = h3lib.hexRing(h, k, ptr) - # if we drop into the failure state, we might be tempted to not create - # this mv, but creating the mv is exactly what guarantees that we'll free - # the memory. context manager would be better here, if we can figure out - # how to do that - mv = create_mv(ptr, n) +# # if we drop into the failure state, we might be tempted to not create +# # this mv, but creating the mv is exactly what guarantees that we'll free +# # the memory. context manager would be better here, if we can figure out +# # how to do that +# mv = create_mv(ptr, n) - if flag != 0: - mv = _ring_fallback(h, k) +# if flag != 0: +# mv = _ring_fallback(h, k) - return mv +# return mv cpdef H3int parent(H3int h, res=None) except 0: + cdef: + H3int parent + h3lib.H3Error err + check_cell(h) if res is None: @@ -141,10 +156,17 @@ cpdef H3int parent(H3int h, res=None) except 0: raise H3ResolutionError(msg) check_res(res) + err = h3lib.cellToParent(h, res, &parent) + + return parent - return h3lib.h3ToParent(h, res) cpdef H3int[:] children(H3int h, res=None): + cdef: + H3int child + h3lib.H3Error err + int64_t N + check_cell(h) if res is None: @@ -155,16 +177,20 @@ cpdef H3int[:] children(H3int h, res=None): raise H3ResolutionError(msg) check_res(res) + err = h3lib.cellToChildrenSize(h, res, &N) - n = h3lib.maxH3ToChildrenSize(h, res) - - ptr = create_ptr(n) - h3lib.h3ToChildren(h, res, ptr) - mv = create_mv(ptr, n) + ptr = create_ptr(N) + err = h3lib.cellToChildren(h, res, ptr) + mv = create_mv(ptr, N) return mv + cpdef H3int center_child(H3int h, res=None) except 0: + cdef: + H3int child + h3lib.H3Error err + check_cell(h) if res is None: @@ -175,8 +201,9 @@ cpdef H3int center_child(H3int h, res=None) except 0: raise H3ResolutionError(msg) check_res(res) + err = h3lib.cellToCenterChild(h, res, &child) - return h3lib.h3ToCenterChild(h, res) + return child @@ -186,6 +213,9 @@ cpdef H3int[:] compact(const H3int[:] hu): # `&hu[0]` **requires** a dereference. For Cython, checking for array # length of zero and returning early seems like the easiest solution. # note: open to better ideas! + cdef: + h3lib.H3Error err + if len(hu) == 0: return empty_memory_view() @@ -193,14 +223,16 @@ cpdef H3int[:] compact(const H3int[:] hu): check_cell(h) ptr = create_ptr(len(hu)) - flag = h3lib.compact(&hu[0], ptr, len(hu)) + err = h3lib.compactCells(&hu[0], ptr, len(hu)) mv = create_mv(ptr, len(hu)) - if flag != 0: + if err: + # todo: additional error processing raise H3ValueError('Could not compact set of hexagons!') return mv + # todo: https://stackoverflow.com/questions/50684977/cython-exception-type-for-a-function-returning-a-typed-memoryview # apparently, memoryviews are python objects, so we don't need to do the except clause cpdef H3int[:] uncompact(const H3int[:] hc, int res): @@ -209,23 +241,30 @@ cpdef H3int[:] uncompact(const H3int[:] hc, int res): # `&hc[0]` **requires** a dereference. For Cython, checking for array # length of zero and returning early seems like the easiest solution. # note: open to better ideas! + cdef: + h3lib.H3Error err + int64_t N + if len(hc) == 0: return empty_memory_view() for h in hc: check_cell(h) - N = h3lib.maxUncompactSize(&hc[0], len(hc), res) + # ignoring error for now + err = h3lib.uncompactCellsSize(&hc[0], len(hc), res, &N) ptr = create_ptr(N) - flag = h3lib.uncompact( - &hc[0], len(hc), - ptr, N, + err = h3lib.uncompactCells( + &hc[0], + len(hc), + ptr, + N, res ) mv = create_mv(ptr, N) - if flag != 0: + if err: raise H3ValueError('Could not uncompact set of hexagons!') return mv @@ -233,143 +272,148 @@ cpdef H3int[:] uncompact(const H3int[:] hc, int res): cpdef int64_t num_hexagons(int resolution) except -1: check_res(resolution) + cdef: + h3lib.H3Error err + int64_t num_cells - return h3lib.numHexagons(resolution) + err = h3lib.getNumCells(resolution, &num_cells) + + return num_cells -cpdef double mean_hex_area(int resolution, unit='km^2') except -1: - check_res(resolution) +# cpdef double mean_hex_area(int resolution, unit='km^2') except -1: +# check_res(resolution) - area = h3lib.hexAreaKm2(resolution) +# area = h3lib.hexAreaKm2(resolution) - # todo: multiple units - convert = { - 'km^2': 1.0, - 'm^2': 1000*1000.0 - } +# # todo: multiple units +# convert = { +# 'km^2': 1.0, +# 'm^2': 1000*1000.0 +# } - try: - area *= convert[unit] - except: - raise H3ValueError('Unknown unit: {}'.format(unit)) +# try: +# area *= convert[unit] +# except: +# raise H3ValueError('Unknown unit: {}'.format(unit)) - return area +# return area -cpdef double cell_area(H3int h, unit='km^2') except -1: - check_cell(h) +# cpdef double cell_area(H3int h, unit='km^2') except -1: +# check_cell(h) - if unit == 'rads^2': - area = h3lib.cellAreaRads2(h) - elif unit == 'km^2': - area = h3lib.cellAreaKm2(h) - elif unit == 'm^2': - area = h3lib.cellAreaM2(h) - else: - raise H3ValueError('Unknown unit: {}'.format(unit)) +# if unit == 'rads^2': +# area = h3lib.cellAreaRads2(h) +# elif unit == 'km^2': +# area = h3lib.cellAreaKm2(h) +# elif unit == 'm^2': +# area = h3lib.cellAreaM2(h) +# else: +# raise H3ValueError('Unknown unit: {}'.format(unit)) - return area +# return area -cpdef H3int[:] line(H3int start, H3int end): - check_cell(start) - check_cell(end) +# cpdef H3int[:] line(H3int start, H3int end): +# check_cell(start) +# check_cell(end) - n = h3lib.h3LineSize(start, end) +# n = h3lib.h3LineSize(start, end) - if n < 0: - s = "Couldn't find line between cells {} and {}" - s = s.format(hex(start), hex(end)) - raise H3ValueError(s) +# if n < 0: +# s = "Couldn't find line between cells {} and {}" +# s = s.format(hex(start), hex(end)) +# raise H3ValueError(s) - ptr = create_ptr(n) - flag = h3lib.h3Line(start, end, ptr) - mv = create_mv(ptr, n) +# ptr = create_ptr(n) +# flag = h3lib.h3Line(start, end, ptr) +# mv = create_mv(ptr, n) - if flag != 0: - s = "Couldn't find line between cells {} and {}" - s = s.format(hex(start), hex(end)) - raise H3ValueError(s) +# if flag != 0: +# s = "Couldn't find line between cells {} and {}" +# s = s.format(hex(start), hex(end)) +# raise H3ValueError(s) - return mv +# return mv cpdef bool is_res_class_iii(H3int h): - return h3lib.h3IsResClassIII(h) == 1 + return h3lib.isResClassIII(h) == 1 -cpdef H3int[:] get_pentagon_indexes(int res): - check_res(res) +# cpdef H3int[:] get_pentagon_indexes(int res): +# check_res(res) - n = h3lib.pentagonIndexCount() +# n = h3lib.pentagonIndexCount() - ptr = create_ptr(n) - h3lib.getPentagonIndexes(res, ptr) - mv = create_mv(ptr, n) +# ptr = create_ptr(n) +# h3lib.getPentagonIndexes(res, ptr) +# mv = create_mv(ptr, n) - return mv +# return mv -cpdef H3int[:] get_res0_indexes(): - n = h3lib.res0IndexCount() +# cpdef H3int[:] get_res0_indexes(): +# n = h3lib.res0IndexCount() - ptr = create_ptr(n) - h3lib.getRes0Indexes(ptr) - mv = create_mv(ptr, n) +# ptr = create_ptr(n) +# h3lib.getRes0Indexes(ptr) +# mv = create_mv(ptr, n) - return mv +# return mv -cpdef get_faces(H3int h): - check_cell(h) +# cpdef get_faces(H3int h): +# check_cell(h) - n = h3lib.maxFaceCount(h) +# n = h3lib.maxFaceCount(h) - cdef int* ptr = stdlib.calloc(n, sizeof(int)) - if (n > 0) and (not ptr): - raise MemoryError() +# cdef int* ptr = stdlib.calloc(n, sizeof(int)) +# if (n > 0) and (not ptr): +# raise MemoryError() - h3lib.h3GetFaces(h, ptr) +# h3lib.h3GetFaces(h, ptr) - faces = ptr - faces = {f for f in faces if f >= 0} - stdlib.free(ptr) +# faces = ptr +# faces = {f for f in faces if f >= 0} +# stdlib.free(ptr) - return faces +# return faces -cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except *: - cdef: - int flag - h3lib.CoordIJ c +# cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except *: +# cdef: +# int flag +# h3lib.CoordIJ c - check_cell(origin) - check_cell(h) +# check_cell(origin) +# check_cell(h) - flag = h3lib.experimentalH3ToLocalIj(origin, h, &c) +# flag = h3lib.experimentalH3ToLocalIj(origin, h, &c) - if flag != 0: - s = "Couldn't find local (i,j) between cells {} and {}." - s = s.format(hex(origin), hex(h)) - raise H3ValueError(s) +# if flag != 0: +# s = "Couldn't find local (i,j) between cells {} and {}." +# s = s.format(hex(origin), hex(h)) +# raise H3ValueError(s) - return c.i, c.j +# return c.i, c.j -cpdef H3int experimental_local_ij_to_h3(H3int origin, int i, int j) except 0: - cdef: - int flag - h3lib.CoordIJ c - H3int out +# cpdef H3int experimental_local_ij_to_h3(H3int origin, int i, int j) except 0: +# cdef: +# int flag +# h3lib.CoordIJ c +# H3int out - check_cell(origin) +# check_cell(origin) - c.i, c.j = i, j +# c.i, c.j = i, j - flag = h3lib.experimentalLocalIjToH3(origin, &c, &out) +# flag = h3lib.experimentalLocalIjToH3(origin, &c, &out) - if flag != 0: - s = "Couldn't find cell at local ({},{}) from cell {}." - s = s.format(i, j, hex(origin)) - raise H3ValueError(s) +# if flag != 0: +# s = "Couldn't find cell at local ({},{}) from cell {}." +# s = s.format(i, j, hex(origin)) +# raise H3ValueError(s) - return out +# return out diff --git a/src/h3/_cy/edges.pxd b/src/h3/_cy/edges.pxd index 465b82d..cdcf4d0 100644 --- a/src/h3/_cy/edges.pxd +++ b/src/h3/_cy/edges.pxd @@ -1,11 +1,11 @@ -from .h3lib cimport bool, H3int +# from .h3lib cimport bool, H3int -cpdef bool are_neighbors(H3int h1, H3int h2) -cpdef H3int edge(H3int origin, H3int destination) except 1 -cpdef bool is_edge(H3int e) -cpdef H3int edge_origin(H3int e) except 1 -cpdef H3int edge_destination(H3int e) except 1 -cpdef (H3int, H3int) edge_cells(H3int e) except * -cpdef H3int[:] edges_from_cell(H3int origin) -cpdef double mean_edge_length(int resolution, unit=*) except -1 -cpdef double edge_length(H3int e, unit=*) except -1 +# cpdef bool are_neighbors(H3int h1, H3int h2) +# cpdef H3int edge(H3int origin, H3int destination) except 1 +# cpdef bool is_edge(H3int e) +# cpdef H3int edge_origin(H3int e) except 1 +# cpdef H3int edge_destination(H3int e) except 1 +# cpdef (H3int, H3int) edge_cells(H3int e) except * +# cpdef H3int[:] edges_from_cell(H3int origin) +# cpdef double mean_edge_length(int resolution, unit=*) except -1 +# cpdef double edge_length(H3int e, unit=*) except -1 diff --git a/src/h3/_cy/edges.pyx b/src/h3/_cy/edges.pyx index 6560006..27397d9 100644 --- a/src/h3/_cy/edges.pyx +++ b/src/h3/_cy/edges.pyx @@ -1,100 +1,100 @@ -cimport h3lib -from .h3lib cimport bool, H3int +# cimport h3lib +# from .h3lib cimport bool, H3int -from .util cimport ( - check_cell, - check_edge, - check_res, - create_ptr, - create_mv, -) +# from .util cimport ( +# check_cell, +# check_edge, +# check_res, +# create_ptr, +# create_mv, +# ) -from .util import H3ValueError +# from .util import H3ValueError -cpdef bool are_neighbors(H3int h1, H3int h2): - check_cell(h1) - check_cell(h2) +# cpdef bool are_neighbors(H3int h1, H3int h2): +# check_cell(h1) +# check_cell(h2) - return h3lib.h3IndexesAreNeighbors(h1, h2) == 1 +# return h3lib.h3IndexesAreNeighbors(h1, h2) == 1 -cpdef H3int edge(H3int origin, H3int destination) except 1: - check_cell(origin) - check_cell(destination) +# cpdef H3int edge(H3int origin, H3int destination) except 1: +# check_cell(origin) +# check_cell(destination) - if h3lib.h3IndexesAreNeighbors(origin, destination) != 1: - s = 'Cells are not neighbors: {} and {}' - s = s.format(hex(origin), hex(destination)) - raise H3ValueError(s) +# if h3lib.h3IndexesAreNeighbors(origin, destination) != 1: +# s = 'Cells are not neighbors: {} and {}' +# s = s.format(hex(origin), hex(destination)) +# raise H3ValueError(s) - return h3lib.getH3UnidirectionalEdge(origin, destination) +# return h3lib.getH3UnidirectionalEdge(origin, destination) -cpdef bool is_edge(H3int e): - return h3lib.h3UnidirectionalEdgeIsValid(e) == 1 +# cpdef bool is_edge(H3int e): +# return h3lib.h3UnidirectionalEdgeIsValid(e) == 1 -cpdef H3int edge_origin(H3int e) except 1: - # without the check, with an invalid input, the function will just return 0 - check_edge(e) +# cpdef H3int edge_origin(H3int e) except 1: +# # without the check, with an invalid input, the function will just return 0 +# check_edge(e) - return h3lib.getOriginH3IndexFromUnidirectionalEdge(e) +# return h3lib.getOriginH3IndexFromUnidirectionalEdge(e) -cpdef H3int edge_destination(H3int e) except 1: - check_edge(e) +# cpdef H3int edge_destination(H3int e) except 1: +# check_edge(e) - return h3lib.getDestinationH3IndexFromUnidirectionalEdge(e) +# return h3lib.getDestinationH3IndexFromUnidirectionalEdge(e) -cpdef (H3int, H3int) edge_cells(H3int e) except *: - check_edge(e) +# cpdef (H3int, H3int) edge_cells(H3int e) except *: +# check_edge(e) - return edge_origin(e), edge_destination(e) +# return edge_origin(e), edge_destination(e) -cpdef H3int[:] edges_from_cell(H3int origin): - """ Returns the 6 (or 5 for pentagons) directed edges - for the given origin cell - """ - check_cell(origin) +# cpdef H3int[:] edges_from_cell(H3int origin): +# """ Returns the 6 (or 5 for pentagons) directed edges +# for the given origin cell +# """ +# check_cell(origin) - ptr = create_ptr(6) - h3lib.getH3UnidirectionalEdgesFromHexagon(origin, ptr) - mv = create_mv(ptr, 6) +# ptr = create_ptr(6) +# h3lib.getH3UnidirectionalEdgesFromHexagon(origin, ptr) +# mv = create_mv(ptr, 6) - return mv +# return mv -cpdef double mean_edge_length(int resolution, unit='km') except -1: - check_res(resolution) +# cpdef double mean_edge_length(int resolution, unit='km') except -1: +# check_res(resolution) - length = h3lib.edgeLengthKm(resolution) +# length = h3lib.edgeLengthKm(resolution) - # todo: multiple units - convert = { - 'km': 1.0, - 'm': 1000.0 - } +# # todo: multiple units +# convert = { +# 'km': 1.0, +# 'm': 1000.0 +# } - try: - length *= convert[unit] - except: - raise H3ValueError('Unknown unit: {}'.format(unit)) +# try: +# length *= convert[unit] +# except: +# raise H3ValueError('Unknown unit: {}'.format(unit)) - return length +# return length -cpdef double edge_length(H3int e, unit='km') except -1: - check_edge(e) +# cpdef double edge_length(H3int e, unit='km') except -1: +# check_edge(e) - # todo: maybe kick this logic up to the python level - # it might be a little cleaner, because we can do the "switch statement" - # with a dict, but would require exposing more C functions +# # todo: maybe kick this logic up to the python level +# # it might be a little cleaner, because we can do the "switch statement" +# # with a dict, but would require exposing more C functions - if unit == 'rads': - length = h3lib.exactEdgeLengthRads(e) - elif unit == 'km': - length = h3lib.exactEdgeLengthKm(e) - elif unit == 'm': - length = h3lib.exactEdgeLengthM(e) - else: - raise H3ValueError('Unknown unit: {}'.format(unit)) +# if unit == 'rads': +# length = h3lib.exactEdgeLengthRads(e) +# elif unit == 'km': +# length = h3lib.exactEdgeLengthKm(e) +# elif unit == 'm': +# length = h3lib.exactEdgeLengthM(e) +# else: +# raise H3ValueError('Unknown unit: {}'.format(unit)) - return length +# return length diff --git a/src/h3/_cy/geo.pxd b/src/h3/_cy/geo.pxd index 1a2c1c3..37e32a0 100644 --- a/src/h3/_cy/geo.pxd +++ b/src/h3/_cy/geo.pxd @@ -2,6 +2,6 @@ from .h3lib cimport H3int cpdef H3int geo_to_h3(double lat, double lng, int res) except 1 cpdef (double, double) h3_to_geo(H3int h) except * -cpdef double point_dist( - double lat1, double lng1, - double lat2, double lng2, unit=*) except -1 +# cpdef double point_dist( +# double lat1, double lng1, +# double lat2, double lng2, unit=*) except -1 diff --git a/src/h3/_cy/geo.pyx b/src/h3/_cy/geo.pyx index dc2c1d7..fe6c93b 100644 --- a/src/h3/_cy/geo.pyx +++ b/src/h3/_cy/geo.pyx @@ -16,263 +16,268 @@ from .util import H3ValueError cpdef H3int geo_to_h3(double lat, double lng, int res) except 1: cdef: - h3lib.GeoCoord c + h3lib.LatLng c + H3int out + h3lib.H3Error err check_res(res) - c = deg2coord(lat, lng) - return h3lib.geoToH3(&c, res) + # todo: just ignoring the error for now, check in the future + err = h3lib.latLngToCell(&c, res, &out) + + return out cpdef (double, double) h3_to_geo(H3int h) except *: """Map an H3 cell into its centroid geo-coordinate (lat/lng)""" cdef: - h3lib.GeoCoord c + h3lib.LatLng c + h3lib.H3Error err check_cell(h) - h3lib.h3ToGeo(h, &c) + err = h3lib.cellToLatLng(h, &c) return coord2deg(c) -cdef h3lib.Geofence make_geofence(geos, bool lnglat_order=False) except *: - """ +# cdef h3lib.Geofence make_geofence(geos, bool lnglat_order=False) except *: +# """ - The returned `Geofence` must be freed with a call to `free_geofence`. - - Parameters - ---------- - geos : list or tuple - GeoFence: A sequence of >= 3 (lat, lng) pairs where the last - element may or may not be same as the first (to form a closed loop). - The order of the pairs may be either clockwise or counterclockwise. - lnglat_order : bool - If True, assume coordinate pairs like (lng, lat) - If False, assume coordinate pairs like (lat, lng) - """ - cdef: - h3lib.Geofence gf +# The returned `Geofence` must be freed with a call to `free_geofence`. + +# Parameters +# ---------- +# geos : list or tuple +# GeoFence: A sequence of >= 3 (lat, lng) pairs where the last +# element may or may not be same as the first (to form a closed loop). +# The order of the pairs may be either clockwise or counterclockwise. +# lnglat_order : bool +# If True, assume coordinate pairs like (lng, lat) +# If False, assume coordinate pairs like (lat, lng) +# """ +# cdef: +# h3lib.Geofence gf - gf.numVerts = len(geos) +# gf.numVerts = len(geos) - gf.verts = stdlib.calloc(gf.numVerts, sizeof(h3lib.GeoCoord)) +# gf.verts = stdlib.calloc(gf.numVerts, sizeof(h3lib.GeoCoord)) - if lnglat_order: - latlng = (g[::-1] for g in geos) - else: - latlng = geos +# if lnglat_order: +# latlng = (g[::-1] for g in geos) +# else: +# latlng = geos - for i, (lat, lng) in enumerate(latlng): - gf.verts[i] = deg2coord(lat, lng) +# for i, (lat, lng) in enumerate(latlng): +# gf.verts[i] = deg2coord(lat, lng) - return gf +# return gf -cdef free_geofence(h3lib.Geofence* gf): - stdlib.free(gf.verts) - gf.verts = NULL +# cdef free_geofence(h3lib.Geofence* gf): +# stdlib.free(gf.verts) +# gf.verts = NULL -cdef class GeoPolygon: - cdef: - h3lib.GeoPolygon gp +# cdef class GeoPolygon: +# cdef: +# h3lib.GeoPolygon gp - def __cinit__(self, outer, holes=None, bool lnglat_order=False): - """ +# def __cinit__(self, outer, holes=None, bool lnglat_order=False): +# """ - Parameters - ---------- - outer : list or tuple - GeoFence - A GeoFence is a sequence of >= 3 (lat, lng) pairs where the last - element may or may not be same as the first (to form a closed loop). - The order of the pairs may be either clockwise or counterclockwise. - holes : list or tuple - A sequence of GeoFences - lnglat_order : bool - If True, assume coordinate pairs like (lng, lat) - If False, assume coordinate pairs like (lat, lng) +# Parameters +# ---------- +# outer : list or tuple +# GeoFence +# A GeoFence is a sequence of >= 3 (lat, lng) pairs where the last +# element may or may not be same as the first (to form a closed loop). +# The order of the pairs may be either clockwise or counterclockwise. +# holes : list or tuple +# A sequence of GeoFences +# lnglat_order : bool +# If True, assume coordinate pairs like (lng, lat) +# If False, assume coordinate pairs like (lat, lng) - """ - if holes is None: - holes = [] +# """ +# if holes is None: +# holes = [] - self.gp.geofence = make_geofence(outer, lnglat_order) - self.gp.numHoles = len(holes) - self.gp.holes = NULL +# self.gp.geofence = make_geofence(outer, lnglat_order) +# self.gp.numHoles = len(holes) +# self.gp.holes = NULL - if len(holes) > 0: - self.gp.holes = stdlib.calloc(len(holes), sizeof(h3lib.Geofence)) - for i, hole in enumerate(holes): - self.gp.holes[i] = make_geofence(hole, lnglat_order) +# if len(holes) > 0: +# self.gp.holes = stdlib.calloc(len(holes), sizeof(h3lib.Geofence)) +# for i, hole in enumerate(holes): +# self.gp.holes[i] = make_geofence(hole, lnglat_order) - def __dealloc__(self): - free_geofence(&self.gp.geofence) +# def __dealloc__(self): +# free_geofence(&self.gp.geofence) - for i in range(self.gp.numHoles): - free_geofence(&self.gp.holes[i]) +# for i in range(self.gp.numHoles): +# free_geofence(&self.gp.holes[i]) - stdlib.free(self.gp.holes) +# stdlib.free(self.gp.holes) -def polyfill_polygon(outer, int res, holes=None, bool lnglat_order=False): - """ Set of hexagons whose center is contained in a polygon. +# def polyfill_polygon(outer, int res, holes=None, bool lnglat_order=False): +# """ Set of hexagons whose center is contained in a polygon. - The polygon is defined as in the GeoJson standard, with an exterior - LinearRing `outer` and a list of LinearRings `holes`, which define any - holes in the polygon. +# The polygon is defined as in the GeoJson standard, with an exterior +# LinearRing `outer` and a list of LinearRings `holes`, which define any +# holes in the polygon. - Each LinearRing may be in clockwise or counter-clockwise order - (right-hand rule or not), and may or may not be a closed loop (where the last - element is equal to the first). - The GeoJSON spec requires the right-hand rule, and a closed loop, but - this function will work with any input format. +# Each LinearRing may be in clockwise or counter-clockwise order +# (right-hand rule or not), and may or may not be a closed loop (where the last +# element is equal to the first). +# The GeoJSON spec requires the right-hand rule, and a closed loop, but +# this function will work with any input format. - Parameters - ---------- - outer : list or tuple - A LinearRing, a sequence of (lat/lng) or (lng/lat) pairs - res : int - The resolution of the output hexagons - holes : list or tuple - A collection of LinearRings, describing any holes in the polygon - lnglat_order : bool - If True, assume coordinate pairs like (lng, lat) - If False, assume coordinate pairs like (lat, lng) - """ +# Parameters +# ---------- +# outer : list or tuple +# A LinearRing, a sequence of (lat/lng) or (lng/lat) pairs +# res : int +# The resolution of the output hexagons +# holes : list or tuple +# A collection of LinearRings, describing any holes in the polygon +# lnglat_order : bool +# If True, assume coordinate pairs like (lng, lat) +# If False, assume coordinate pairs like (lat, lng) +# """ - check_res(res) - gp = GeoPolygon(outer, holes=holes, lnglat_order=lnglat_order) +# check_res(res) +# gp = GeoPolygon(outer, holes=holes, lnglat_order=lnglat_order) - n = h3lib.maxPolyfillSize(&gp.gp, res) - ptr = create_ptr(n) +# n = h3lib.maxPolyfillSize(&gp.gp, res) +# ptr = create_ptr(n) - h3lib.polyfill(&gp.gp, res, ptr) - mv = create_mv(ptr, n) +# h3lib.polyfill(&gp.gp, res, ptr) +# mv = create_mv(ptr, n) - return mv +# return mv -def polyfill_geojson(geojson, int res): - """ Set of hexagons whose center is contained in a GeoJson Polygon object. +# def polyfill_geojson(geojson, int res): +# """ Set of hexagons whose center is contained in a GeoJson Polygon object. - The polygon is defined exactly as in the GeoJson standard, so - `geojson` should be a dictionary like: - { - 'type': 'Polygon', - 'coordinates': [...] - } +# The polygon is defined exactly as in the GeoJson standard, so +# `geojson` should be a dictionary like: +# { +# 'type': 'Polygon', +# 'coordinates': [...] +# } - 'coordinates' should be a list of LinearRings, where the first ring describes - the exterior boundary of the Polygon, and any subsequent LinearRings - describe holes in the polygon. +# 'coordinates' should be a list of LinearRings, where the first ring describes +# the exterior boundary of the Polygon, and any subsequent LinearRings +# describe holes in the polygon. - Note that we don't provide an option for the order of the coordinates, - as the GeoJson standard requires them to be in lng/lat order. +# Note that we don't provide an option for the order of the coordinates, +# as the GeoJson standard requires them to be in lng/lat order. - Parameters - ---------- - geojson : dict - res : int - The resolution of the output hexagons - """ +# Parameters +# ---------- +# geojson : dict +# res : int +# The resolution of the output hexagons +# """ - # todo: this one could handle multipolygons... +# # todo: this one could handle multipolygons... - if geojson['type'] != 'Polygon': - raise ValueError('Only Polygon GeoJSON supported') +# if geojson['type'] != 'Polygon': +# raise ValueError('Only Polygon GeoJSON supported') - coords = geojson['coordinates'] +# coords = geojson['coordinates'] - out = polyfill_polygon(coords[0], res, holes=coords[1:], lnglat_order=True) +# out = polyfill_polygon(coords[0], res, holes=coords[1:], lnglat_order=True) - return out +# return out -def polyfill(dict geojson, int res, bool geo_json_conformant=False): - """ Light wrapper around `polyfill_geojson` to provide backward compatibility. - """ +# def polyfill(dict geojson, int res, bool geo_json_conformant=False): +# """ Light wrapper around `polyfill_geojson` to provide backward compatibility. +# """ - try: - gj_type = geojson['type'] - except KeyError: - raise KeyError("`geojson` dict must have key 'type'.") from None +# try: +# gj_type = geojson['type'] +# except KeyError: +# raise KeyError("`geojson` dict must have key 'type'.") from None - if gj_type != 'Polygon': - raise ValueError('Only Polygon GeoJSON supported') +# if gj_type != 'Polygon': +# raise ValueError('Only Polygon GeoJSON supported') - if geo_json_conformant: - out = polyfill_geojson(geojson, res) - else: - coords = geojson['coordinates'] - out = polyfill_polygon(coords[0], res, holes=coords[1:], lnglat_order=False) +# if geo_json_conformant: +# out = polyfill_geojson(geojson, res) +# else: +# coords = geojson['coordinates'] +# out = polyfill_polygon(coords[0], res, holes=coords[1:], lnglat_order=False) - return out +# return out -def cell_boundary(H3int h, bool geo_json=False): - """Compose an array of geo-coordinates that outlines a hexagonal cell""" - cdef: - h3lib.GeoBoundary gb +# def cell_boundary(H3int h, bool geo_json=False): +# """Compose an array of geo-coordinates that outlines a hexagonal cell""" +# cdef: +# h3lib.GeoBoundary gb - check_cell(h) +# check_cell(h) - h3lib.h3ToGeoBoundary(h, &gb) +# h3lib.h3ToGeoBoundary(h, &gb) - verts = tuple( - coord2deg(gb.verts[i]) - for i in range(gb.num_verts) - ) +# verts = tuple( +# coord2deg(gb.verts[i]) +# for i in range(gb.num_verts) +# ) - if geo_json: - #lat/lng -> lng/lat and last point same as first - verts += (verts[0],) - verts = tuple(v[::-1] for v in verts) +# if geo_json: +# #lat/lng -> lng/lat and last point same as first +# verts += (verts[0],) +# verts = tuple(v[::-1] for v in verts) - return verts +# return verts -def edge_boundary(H3int edge, bool geo_json=False): - """ Returns the GeoBoundary containing the coordinates of the edge - """ - cdef: - h3lib.GeoBoundary gb +# def edge_boundary(H3int edge, bool geo_json=False): +# """ Returns the GeoBoundary containing the coordinates of the edge +# """ +# cdef: +# h3lib.GeoBoundary gb - check_edge(edge) +# check_edge(edge) - h3lib.getH3UnidirectionalEdgeBoundary(edge, &gb) +# h3lib.getH3UnidirectionalEdgeBoundary(edge, &gb) - # todo: move this verts transform into the GeoBoundary object - verts = tuple( - coord2deg(gb.verts[i]) - for i in range(gb.num_verts) - ) +# # todo: move this verts transform into the GeoBoundary object +# verts = tuple( +# coord2deg(gb.verts[i]) +# for i in range(gb.num_verts) +# ) - if geo_json: - #lat/lng -> lng/lat and last point same as first - verts += (verts[0],) - verts = tuple(v[::-1] for v in verts) +# if geo_json: +# #lat/lng -> lng/lat and last point same as first +# verts += (verts[0],) +# verts = tuple(v[::-1] for v in verts) - return verts +# return verts -cpdef double point_dist( - double lat1, double lng1, - double lat2, double lng2, unit='km') except -1: +# cpdef double point_dist( +# double lat1, double lng1, +# double lat2, double lng2, unit='km') except -1: - a = deg2coord(lat1, lng1) - b = deg2coord(lat2, lng2) +# a = deg2coord(lat1, lng1) +# b = deg2coord(lat2, lng2) - if unit == 'rads': - d = h3lib.pointDistRads(&a, &b) - elif unit == 'km': - d = h3lib.pointDistKm(&a, &b) - elif unit == 'm': - d = h3lib.pointDistM(&a, &b) - else: - raise H3ValueError('Unknown unit: {}'.format(unit)) +# if unit == 'rads': +# d = h3lib.pointDistRads(&a, &b) +# elif unit == 'km': +# d = h3lib.pointDistKm(&a, &b) +# elif unit == 'm': +# d = h3lib.pointDistM(&a, &b) +# else: +# raise H3ValueError('Unknown unit: {}'.format(unit)) - return d +# return d diff --git a/src/h3/_cy/h3lib.pxd b/src/h3/_cy/h3lib.pxd index 5fa05cc..633af81 100644 --- a/src/h3/_cy/h3lib.pxd +++ b/src/h3/_cy/h3lib.pxd @@ -3,6 +3,7 @@ from cpython cimport bool from libc.stdint cimport int64_t ctypedef stdint.uint64_t H3int +ctypedef stdint.uint32_t H3Error ctypedef basestring H3str cdef extern from "h3api.h": @@ -12,157 +13,160 @@ cdef extern from "h3api.h": ctypedef stdint.uint64_t H3Index - ctypedef struct GeoCoord: + ctypedef struct LatLng: double lat # in radians - double lng "lon" # in radians + double lng # in radians - ctypedef struct GeoBoundary: - int num_verts "numVerts" - GeoCoord verts[10] # MAX_CELL_BNDRY_VERTS - - ctypedef struct Geofence: - int numVerts - GeoCoord *verts - - ctypedef struct GeoPolygon: - Geofence geofence - int numHoles - Geofence *holes - - ctypedef struct GeoMultiPolygon: - int numPolygons - GeoPolygon *polygons - - ctypedef struct LinkedGeoCoord: - GeoCoord data "vertex" - LinkedGeoCoord *next - - # renaming these for clarity - ctypedef struct LinkedGeoLoop: - LinkedGeoCoord *data "first" - LinkedGeoCoord *_data_last "last" # not needed in Cython bindings - LinkedGeoLoop *next - - ctypedef struct LinkedGeoPolygon: - LinkedGeoLoop *data "first" - LinkedGeoLoop *_data_last "last" # not needed in Cython bindings - LinkedGeoPolygon *next - - ctypedef struct CoordIJ: - int i - int j - - H3Index geoToH3(const GeoCoord *g, int res) nogil - - void h3ToGeo(H3Index h3, GeoCoord *g) nogil - - void h3ToGeoBoundary(H3Index h3, GeoBoundary *gp) - - int maxKringSize(int k) - - int hexRange(H3Index origin, int k, H3Index *out) - - int hexRangeDistances(H3Index origin, int k, H3Index *out, int *distances) - - int h3Distance(H3Index origin, H3Index h3) - - int hexRanges(H3Index *h3Set, int length, int k, H3Index *out) - - void kRing(H3Index origin, int k, H3Index *out) - - void kRingDistances(H3Index origin, int k, H3Index *out, int *distances) - - int hexRing(H3Index origin, int k, H3Index *out) - - int maxPolyfillSize(const GeoPolygon *geoPolygon, int res) - - void polyfill(const GeoPolygon *geoPolygon, int res, H3Index *out) - - void h3SetToLinkedGeo(const H3Index *h3Set, const int numHexes, LinkedGeoPolygon *out) - - void destroyLinkedPolygon(LinkedGeoPolygon *polygon) + int isValidCell(H3Index h) nogil + int isPentagon(H3Index h) nogil + int isResClassIII(H3Index h) nogil + int isValidDirectedEdge(H3Index edge) nogil double degsToRads(double degrees) nogil - double radsToDegs(double radians) nogil - stdint.int64_t numHexagons(int res) + int getResolution(H3Index h) nogil + int getBaseCellNumber(H3Index h) nogil - int h3GetResolution(H3Index h) nogil + H3Error latLngToCell(const LatLng *g, int res, H3Index *out) nogil + H3Error cellToLatLng(H3Index h, LatLng *) nogil + H3Error gridDistance(H3Index h1, H3Index h2, int64_t *distance) nogil - int h3GetBaseCell(H3Index h) + H3Error maxGridDiskSize(int k, int64_t *out) nogil # num/out/N? + H3Error gridDisk(H3Index h, int k, H3Index *out) nogil - H3Index stringToH3(const char *str) + H3Error cellToParent( H3Index h, int parentRes, H3Index *parent) nogil + H3Error cellToCenterChild(H3Index h, int childRes, H3Index *child) nogil - void h3ToString(H3Index h, char *str, size_t sz) + H3Error cellToChildrenSize(H3Index h, int childRes, int64_t *num) nogil # num/out/N? + H3Error cellToChildren( H3Index h, int childRes, H3Index *children) nogil - int h3IsValid(H3Index h) + H3Error compactCells( + const H3Index *cells_u, + H3Index *cells_c, + const int num_u + ) nogil + H3Error uncompactCellsSize( + const H3Index *cells_c, + const int64_t num_c, + const int res, + int64_t *num_u + ) nogil + H3Error uncompactCells( + const H3Index *cells_c, + const int num_c, + H3Index *cells_u, + const int num_u, + const int res + ) nogil - H3Index h3ToParent(H3Index h, int parentRes) nogil + H3Error getNumCells(int res, int64_t *out) - int maxH3ToChildrenSize(H3Index h, int childRes) + # ctypedef struct GeoBoundary: + # int num_verts "numVerts" + # GeoCoord verts[10] # MAX_CELL_BNDRY_VERTS - void h3ToChildren(H3Index h, int childRes, H3Index *children) + # ctypedef struct Geofence: + # int numVerts + # GeoCoord *verts - int compact(const H3Index *h3Set, H3Index *compactedSet, const int numHexes) + # ctypedef struct GeoPolygon: + # Geofence geofence + # int numHoles + # Geofence *holes - int maxUncompactSize(const H3Index *compactedSet, const int numHexes, const int res) + # ctypedef struct GeoMultiPolygon: + # int numPolygons + # GeoPolygon *polygons - int uncompact(const H3Index *compactedSet, const int numHexes, H3Index *h3Set, const int maxHexes, const int res) + # ctypedef struct LinkedGeoCoord: + # GeoCoord data "vertex" + # LinkedGeoCoord *next - int h3IsResClassIII(H3Index h) + # # renaming these for clarity + # ctypedef struct LinkedGeoLoop: + # LinkedGeoCoord *data "first" + # LinkedGeoCoord *_data_last "last" # not needed in Cython bindings + # LinkedGeoLoop *next - int h3IsPentagon(H3Index h) + # ctypedef struct LinkedGeoPolygon: + # LinkedGeoLoop *data "first" + # LinkedGeoLoop *_data_last "last" # not needed in Cython bindings + # LinkedGeoPolygon *next - int pentagonIndexCount() + # ctypedef struct CoordIJ: + # int i + # int j - void getPentagonIndexes(int res, H3Index *out) + # void h3ToGeoBoundary(H3Index h3, GeoBoundary *gp) - int res0IndexCount() + # int hexRange(H3Index origin, int k, H3Index *out) - void getRes0Indexes(H3Index *out) + # int hexRangeDistances(H3Index origin, int k, H3Index *out, int *distances) - H3Index h3ToCenterChild(H3Index h, int res) + # int hexRanges(H3Index *h3Set, int length, int k, H3Index *out) - int h3IndexesAreNeighbors(H3Index origin, H3Index destination) + # void kRingDistances(H3Index origin, int k, H3Index *out, int *distances) - H3Index getH3UnidirectionalEdge(H3Index origin, H3Index destination) + # int hexRing(H3Index origin, int k, H3Index *out) - int h3UnidirectionalEdgeIsValid(H3Index edge) + # int maxPolyfillSize(const GeoPolygon *geoPolygon, int res) - H3Index getOriginH3IndexFromUnidirectionalEdge(H3Index edge) + # void polyfill(const GeoPolygon *geoPolygon, int res, H3Index *out) - H3Index getDestinationH3IndexFromUnidirectionalEdge(H3Index edge) + # void h3SetToLinkedGeo(const H3Index *h3Set, const int numHexes, LinkedGeoPolygon *out) - void getH3IndexesFromUnidirectionalEdge(H3Index edge, H3Index *originDestination) + # void destroyLinkedPolygon(LinkedGeoPolygon *polygon) - void getH3UnidirectionalEdgesFromHexagon(H3Index origin, H3Index *edges) + # H3Index stringToH3(const char *str) - void getH3UnidirectionalEdgeBoundary(H3Index edge, GeoBoundary *gb) + # void h3ToString(H3Index h, char *str, size_t sz) - int h3LineSize(H3Index start, H3Index end) - int h3Line(H3Index start, H3Index end, H3Index *out) + # int pentagonIndexCount() - int maxFaceCount(H3Index h3) - void h3GetFaces(H3Index h3, int *out) + # void getPentagonIndexes(int res, H3Index *out) - int experimentalH3ToLocalIj(H3Index origin, H3Index h3, CoordIJ *out) - int experimentalLocalIjToH3(H3Index origin, const CoordIJ *ij, H3Index *out) + # int res0IndexCount() - double hexAreaKm2(int res) nogil - double hexAreaM2(int res) nogil + # void getRes0Indexes(H3Index *out) - double cellAreaRads2(H3Index h) nogil - double cellAreaKm2(H3Index h) nogil - double cellAreaM2(H3Index h) nogil + # int h3IndexesAreNeighbors(H3Index origin, H3Index destination) - double edgeLengthKm(int res) nogil - double edgeLengthM(int res) nogil + # H3Index getH3UnidirectionalEdge(H3Index origin, H3Index destination) - double exactEdgeLengthRads(H3Index edge) nogil - double exactEdgeLengthKm(H3Index edge) nogil - double exactEdgeLengthM(H3Index edge) nogil + # H3Index getOriginH3IndexFromUnidirectionalEdge(H3Index edge) - double pointDistRads(const GeoCoord *a, const GeoCoord *b) nogil - double pointDistKm(const GeoCoord *a, const GeoCoord *b) nogil - double pointDistM(const GeoCoord *a, const GeoCoord *b) nogil + # H3Index getDestinationH3IndexFromUnidirectionalEdge(H3Index edge) + + # void getH3IndexesFromUnidirectionalEdge(H3Index edge, H3Index *originDestination) + + # void getH3UnidirectionalEdgesFromHexagon(H3Index origin, H3Index *edges) + + # void getH3UnidirectionalEdgeBoundary(H3Index edge, GeoBoundary *gb) + + # int h3LineSize(H3Index start, H3Index end) + # int h3Line(H3Index start, H3Index end, H3Index *out) + + # int maxFaceCount(H3Index h3) + # void h3GetFaces(H3Index h3, int *out) + + # int experimentalH3ToLocalIj(H3Index origin, H3Index h3, CoordIJ *out) + # int experimentalLocalIjToH3(H3Index origin, const CoordIJ *ij, H3Index *out) + + # double hexAreaKm2(int res) nogil + # double hexAreaM2(int res) nogil + + # double cellAreaRads2(H3Index h) nogil + # double cellAreaKm2(H3Index h) nogil + # double cellAreaM2(H3Index h) nogil + + # double edgeLengthKm(int res) nogil + # double edgeLengthM(int res) nogil + + # double exactEdgeLengthRads(H3Index edge) nogil + # double exactEdgeLengthKm(H3Index edge) nogil + # double exactEdgeLengthM(H3Index edge) nogil + + # double pointDistRads(const GeoCoord *a, const GeoCoord *b) nogil + # double pointDistKm(const GeoCoord *a, const GeoCoord *b) nogil + # double pointDistM(const GeoCoord *a, const GeoCoord *b) nogil diff --git a/src/h3/_cy/to_multipoly.pyx b/src/h3/_cy/to_multipoly.pyx index 8e5ca55..296a932 100644 --- a/src/h3/_cy/to_multipoly.pyx +++ b/src/h3/_cy/to_multipoly.pyx @@ -1,75 +1,75 @@ -cimport h3lib -from h3lib cimport H3int -from .util cimport check_cell, coord2deg +# cimport h3lib +# from h3lib cimport H3int +# from .util cimport check_cell, coord2deg -# todo: it's driving me crazy that these three functions are all essentially the same linked list walker... -# grumble: no way to do iterators in with cdef functions! -cdef walk_polys(const h3lib.LinkedGeoPolygon* L): - out = [] - while L: - out += [walk_loops(L.data)] - L = L.next +# # todo: it's driving me crazy that these three functions are all essentially the same linked list walker... +# # grumble: no way to do iterators in with cdef functions! +# cdef walk_polys(const h3lib.LinkedGeoPolygon* L): +# out = [] +# while L: +# out += [walk_loops(L.data)] +# L = L.next - return out +# return out -cdef walk_loops(const h3lib.LinkedGeoLoop* L): - out = [] - while L: - out += [walk_coords(L.data)] - L = L.next +# cdef walk_loops(const h3lib.LinkedGeoLoop* L): +# out = [] +# while L: +# out += [walk_coords(L.data)] +# L = L.next - return out +# return out -cdef walk_coords(const h3lib.LinkedGeoCoord* L): - out = [] - while L: - out += [coord2deg(L.data)] - L = L.next +# cdef walk_coords(const h3lib.LinkedGeoCoord* L): +# out = [] +# while L: +# out += [coord2deg(L.data)] +# L = L.next - return out +# return out -# todo: tuples instead of lists? -def _to_multi_polygon(const H3int[:] hexes): - cdef: - h3lib.LinkedGeoPolygon polygon +# # todo: tuples instead of lists? +# def _to_multi_polygon(const H3int[:] hexes): +# cdef: +# h3lib.LinkedGeoPolygon polygon - for h in hexes: - check_cell(h) +# for h in hexes: +# check_cell(h) - h3lib.h3SetToLinkedGeo(&hexes[0], len(hexes), &polygon) +# h3lib.h3SetToLinkedGeo(&hexes[0], len(hexes), &polygon) - out = walk_polys(&polygon) +# out = walk_polys(&polygon) - # we're still responsible for cleaning up the passed in `polygon`, - # but not a problem here, since it is stack allocated - h3lib.destroyLinkedPolygon(&polygon) +# # we're still responsible for cleaning up the passed in `polygon`, +# # but not a problem here, since it is stack allocated +# h3lib.destroyLinkedPolygon(&polygon) - return out +# return out -def _geojson_loop(loop): - """ Swap lat/lng order and close loop. - """ - loop = [e[::-1] for e in loop] - loop += [loop[0]] +# def _geojson_loop(loop): +# """ Swap lat/lng order and close loop. +# """ +# loop = [e[::-1] for e in loop] +# loop += [loop[0]] - return loop +# return loop -def h3_set_to_multi_polygon(const H3int[:] hexes, geo_json=False): - # todo: gotta be a more elegant way to handle these... - if len(hexes) == 0: - return [] +# def h3_set_to_multi_polygon(const H3int[:] hexes, geo_json=False): +# # todo: gotta be a more elegant way to handle these... +# if len(hexes) == 0: +# return [] - multipoly = _to_multi_polygon(hexes) +# multipoly = _to_multi_polygon(hexes) - if geo_json: - multipoly = [ - [_geojson_loop(loop) for loop in poly] - for poly in multipoly - ] +# if geo_json: +# multipoly = [ +# [_geojson_loop(loop) for loop in poly] +# for poly in multipoly +# ] - return multipoly +# return multipoly diff --git a/src/h3/_cy/unstable_vect.pyx b/src/h3/_cy/unstable_vect.pyx deleted file mode 100644 index 316ac12..0000000 --- a/src/h3/_cy/unstable_vect.pyx +++ /dev/null @@ -1,89 +0,0 @@ -cimport h3lib -from h3lib cimport H3int -from .util cimport deg2coord - -from cython cimport boundscheck, wraparound -from libc.math cimport sqrt, sin, cos, asin - -cdef double haversineDistance(double th1, double ph1, double th2, double ph2) nogil: - cdef: - double dx, dy, dz - double R = 6371.0088 - - ph1 -= ph2 - - dz = sin(th1) - sin(th2) - dx = cos(ph1) * cos(th1) - cos(th2) - dy = sin(ph1) * cos(th1) - - return asin(sqrt(dx*dx + dy*dy + dz*dz) / 2)*2*R - - -@boundscheck(False) -@wraparound(False) -cpdef void haversine_vect( - const H3int[:] a, - const H3int[:] b, - double[:] out -) nogil: - - cdef h3lib.GeoCoord p1, p2 - - with nogil: - # todo: add these back in when cython 3.0 comes out - #assert len(a) == len(b) - #assert len(a) <= len(out) - - for i in range(len(a)): - h3lib.h3ToGeo(a[i], &p1) - h3lib.h3ToGeo(b[i], &p2) - out[i] = haversineDistance( - p1.lat, p1.lng, - p2.lat, p2.lng - ) - - -@boundscheck(False) -@wraparound(False) -cpdef void geo_to_h3_vect( - const double[:] lat, - const double[:] lng, - int res, - H3int[:] out -) nogil: - - cdef h3lib.GeoCoord c - - with nogil: - for i in range(len(lat)): - c = deg2coord(lat[i], lng[i]) - out[i] = h3lib.geoToH3(&c, res) - - -@boundscheck(False) -@wraparound(False) -cpdef void h3_to_parent_vect( - const H3int[:] h, - int[:] res, - H3int[:] out -) nogil: - - cdef Py_ssize_t i - - with nogil: - for i in range(len(h)): - out[i] = h3lib.h3ToParent(h[i], res[i]) - - -@boundscheck(False) -@wraparound(False) -cpdef void h3_get_resolution_vect( - const H3int[:] h, - int[:] out, -) nogil: - - cdef Py_ssize_t i - - with nogil: - for i in range(len(h)): - out[i] = h3lib.h3GetResolution(h[i]) diff --git a/src/h3/_cy/util.pxd b/src/h3/_cy/util.pxd index 06cc26a..6a34b75 100644 --- a/src/h3/_cy/util.pxd +++ b/src/h3/_cy/util.pxd @@ -1,7 +1,7 @@ -from .h3lib cimport H3int, H3str, GeoCoord +from .h3lib cimport H3int, H3str, LatLng -cdef GeoCoord deg2coord(double lat, double lng) nogil -cdef (double, double) coord2deg(GeoCoord c) nogil +cdef LatLng deg2coord(double lat, double lng) nogil +cdef (double, double) coord2deg(LatLng c) nogil cpdef H3int hex2int(H3str h) except? 0 cpdef H3str int2hex(H3int x) diff --git a/src/h3/_cy/util.pyx b/src/h3/_cy/util.pyx index dbf17da..bbb63e4 100644 --- a/src/h3/_cy/util.pyx +++ b/src/h3/_cy/util.pyx @@ -1,13 +1,13 @@ from libc cimport stdlib from cython.view cimport array -from .h3lib cimport H3int, H3str, h3IsValid, h3UnidirectionalEdgeIsValid +from .h3lib cimport H3int, H3str, isValidCell, isValidDirectedEdge cimport h3lib -cdef h3lib.GeoCoord deg2coord(double lat, double lng) nogil: +cdef h3lib.LatLng deg2coord(double lat, double lng) nogil: cdef: - h3lib.GeoCoord c + h3lib.LatLng c c.lat = h3lib.degsToRads(lat) c.lng = h3lib.degsToRads(lng) @@ -15,7 +15,7 @@ cdef h3lib.GeoCoord deg2coord(double lat, double lng) nogil: return c -cdef (double, double) coord2deg(h3lib.GeoCoord c) nogil: +cdef (double, double) coord2deg(h3lib.LatLng c) nogil: return ( h3lib.radsToDegs(c.lat), h3lib.radsToDegs(c.lng) @@ -77,11 +77,11 @@ cdef check_cell(H3int h): is incorrect, but in a format that is easily compared to `str` inputs. """ - if h3IsValid(h) == 0: + if isValidCell(h) == 0: raise H3CellError('Integer is not a valid H3 cell: {}'.format(hex(h))) cdef check_edge(H3int e): - if h3UnidirectionalEdgeIsValid(e) == 0: + if isValidDirectedEdge(e) == 0: raise H3EdgeError('Integer is not a valid H3 edge: {}'.format(hex(e))) cdef check_res(int res): diff --git a/src/h3/_version.py b/src/h3/_version.py index eb5cae0..41540d5 100644 --- a/src/h3/_version.py +++ b/src/h3/_version.py @@ -1,24 +1,25 @@ -__version__ = '3.7.4' +__version__ = '4.0.0' __description__ = 'Hierarchical hexagonal geospatial indexing system' __url__ = 'https://github.com/uber/h3-py' -__license__ = "Apache 2.0 License" +__license__ = 'Apache 2.0 License' __author__ = 'Uber Technologies' __author_email__ = 'AJ Friend ' __classifiers__ = [ - "Development Status :: 5 - Production/Stable", - "Intended Audience :: Developers", - "Intended Audience :: Science/Research", - "License :: OSI Approved :: Apache Software License", - "Programming Language :: C", - "Programming Language :: Cython", - "Programming Language :: Python :: 2.7", - "Programming Language :: Python :: 3.5", - "Programming Language :: Python :: 3.6", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Operating System :: MacOS :: MacOS X", - "Operating System :: POSIX :: Linux", - "Operating System :: Microsoft :: Windows", - "Topic :: Scientific/Engineering :: GIS", + 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'Intended Audience :: Science/Research', + 'License :: OSI Approved :: Apache Software License', + 'Programming Language :: C', + 'Programming Language :: Cython', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', + 'Operating System :: MacOS :: MacOS X', + 'Operating System :: POSIX :: Linux', + 'Operating System :: Microsoft :: Windows', + 'Topic :: Scientific/Engineering :: GIS', ] diff --git a/src/h3/unstable/__init__.py b/src/h3/unstable/__init__.py deleted file mode 100644 index 5c1839c..0000000 --- a/src/h3/unstable/__init__.py +++ /dev/null @@ -1,9 +0,0 @@ -# flake8: noqa - -import warnings -warnings.warn( - 'Modules under `h3.unstable` are experimental, and may change at any time.' -) - -from . import v4 -from . import vect diff --git a/src/h3/unstable/v4.py b/src/h3/unstable/v4.py deleted file mode 100644 index 833cd6e..0000000 --- a/src/h3/unstable/v4.py +++ /dev/null @@ -1,125 +0,0 @@ -# flake8: noqa - -from ..api.basic_str import ( - # todo: implement is_valid_index - - compact as - compact_cells, - - edge_length as - get_hexagon_edge_length_avg, - - geo_to_h3 as - point_to_cell, - - get_destination_h3_index_from_unidirectional_edge as - get_directed_edge_destination, - - get_h3_indexes_from_unidirectional_edge as - directed_edge_to_cells, - - get_h3_unidirectional_edge as - cells_to_directed_edge, - - get_h3_unidirectional_edge_boundary as - directed_edge_to_boundary, - - get_h3_unidirectional_edges_from_hexagon as - origin_to_directed_edges, - - get_origin_h3_index_from_unidirectional_edge as - get_directed_edge_origin, - - get_pentagon_indexes as - get_pentagons, - - get_res0_indexes as - get_res0_cells, - - h3_distance as - grid_distance, - - h3_get_base_cell as - get_base_cell_number, - - h3_get_faces as - get_icosahedron_faces, - - h3_get_resolution as - get_resolution, - - h3_indexes_are_neighbors as - are_neighbor_cells, - - h3_is_pentagon as - is_pentagon, - - h3_is_res_class_III as - is_res_class_III, - - h3_is_valid as - is_valid_cell, - - h3_line as - grid_path_cells, - - h3_set_to_multi_polygon as - cells_to_multipolygon, - - h3_to_center_child as - cell_to_center_child, - - h3_to_children as - cell_to_children, - - h3_to_geo as - cell_to_point, - - h3_to_geo_boundary as - cell_to_boundary, - - h3_to_parent as - cell_to_parent, - - h3_to_string as - int_to_str, - - h3_unidirectional_edge_is_valid as - is_valid_directed_edge, - - hex_area as - get_hexagon_area_avg, - - # hex_range as _, - # hex_range_distances as _, # not sure we want this one; easy for user to implement - # hex_ranges as _, - - hex_ring as - grid_ring, - - k_ring as - grid_disk, - - k_ring_distances as - grid_disk_distances, - - num_hexagons as - get_num_cells, - - polyfill as - polygon_to_cells, - - polyfill_geojson as - geojson_to_cells, # still need to figure out the final polyfill interface - - #polyfill_polygon as _, - - string_to_h3 as - str_to_int, - - uncompact as - uncompact_cells, - - versions as - versions, -) diff --git a/src/h3/unstable/vect.py b/src/h3/unstable/vect.py deleted file mode 100644 index 122c07d..0000000 --- a/src/h3/unstable/vect.py +++ /dev/null @@ -1,105 +0,0 @@ -from .._cy import unstable_vect as _vect - -import numpy as np - - -def h3_to_parent(h, res=None): - """ - Get parent of arrays of cells. - - Parameters - ---------- - h : array of H3Cells - res: int or None, optional - The resolution for the parent - If `None`, then `res = resolution(h) - 1` - - Returns - ------- - array of H3Cells - """ - h = np.array(h, dtype=np.uint64) - out = np.zeros(len(h), dtype=np.uint64) - - if res is None: - res = h3_get_resolution(h) - 1 - else: - res = np.full(h.shape, res, dtype=np.intc) - - _vect.h3_to_parent_vect(h, res, out) - - return out - - -def h3_get_resolution(h): - """ - Return the resolution of an array of H3 cells. - - Parameters - ---------- - h : H3Cell - - Returns - ------- - array of int - """ - h = np.array(h, dtype=np.uint64) - out = np.zeros(len(h), dtype=np.intc) - - _vect.h3_get_resolution_vect(h, out) - - return out - - -def geo_to_h3(lats, lngs, res): - """ - Convert arrays describing lat/lng pairs to cells. - - Parameters - ---------- - lats, lngs : arrays of floats - - res: int - Resolution for output cells. - - Returns - ------- - array of H3Cells - """ - lats = np.array(lats, dtype=np.float64) - lngs = np.array(lngs, dtype=np.float64) - - assert len(lats) == len(lngs) - - out = np.zeros(len(lats), dtype='uint64') - - _vect.geo_to_h3_vect(lats, lngs, res, out) - - return out - - -def cell_haversine(a, b): - """ - Compute haversine distance between the centers of cells given in - arrays a and b. - - - Parameters - ---------- - a, b : arrays of H3Cell - - Returns - ------- - float - Haversine distance in kilometers - """ - a = np.array(a, dtype=np.uint64) - b = np.array(b, dtype=np.uint64) - - assert len(a) == len(b) - - out = np.zeros(len(a), dtype='double') - - _vect.haversine_vect(a, b, out) - - return out diff --git a/src/h3lib b/src/h3lib index ee292a9..294c20e 160000 --- a/src/h3lib +++ b/src/h3lib @@ -1 +1 @@ -Subproject commit ee292a96906767162f2f530b46fc2428b2555748 +Subproject commit 294c20ed7d447122d5ab37260896d4ef7579ebfb diff --git a/tests/cython_example.pyx b/tests/cython_example.pyx index e8d2859..c19fe21 100644 --- a/tests/cython_example.pyx +++ b/tests/cython_example.pyx @@ -1,18 +1,18 @@ -from cython cimport boundscheck, wraparound +# from cython cimport boundscheck, wraparound -from h3._cy.h3lib cimport H3int -from h3._cy.geo cimport geo_to_h3 +# from h3._cy.h3lib cimport H3int +# from h3._cy.geo cimport geo_to_h3 -@boundscheck(False) -@wraparound(False) -cpdef void geo_to_h3_vect( - const double[:] lat, - const double[:] lng, - int res, - H3int[:] out -): +# @boundscheck(False) +# @wraparound(False) +# cpdef void geo_to_h3_vect( +# const double[:] lat, +# const double[:] lng, +# int res, +# H3int[:] out +# ): - cdef Py_ssize_t i +# cdef Py_ssize_t i - for i in range(len(lat)): - out[i] = geo_to_h3(lat[i], lng[i], res) +# for i in range(len(lat)): +# out[i] = geo_to_h3(lat[i], lng[i], res) diff --git a/tests/h3fake2_errors.py b/tests/h3fake2_errors.py new file mode 100644 index 0000000..da56887 --- /dev/null +++ b/tests/h3fake2_errors.py @@ -0,0 +1,15 @@ +# flake8: noqa +""" +Some of the tests expect to catch certain errors. +As we transition, we'll have errors of each type. +pytest functions can accept tuples of errors to check for either one. +""" + +import h3 +import h3fake2 + +H3ValueError = (h3.H3ValueError, h3fake2.H3ValueError) +H3CellError = (h3.H3CellError, h3fake2.H3CellError) +H3ResolutionError = (h3.H3ResolutionError, h3fake2.H3ResolutionError) +H3EdgeError = (h3.H3EdgeError, h3fake2.H3EdgeError) +H3DistanceError = (h3.H3DistanceError, h3fake2.H3DistanceError) diff --git a/tests/test_cells_and_edges.py b/tests/test_cells_and_edges.py index 173033a..014f519 100644 --- a/tests/test_cells_and_edges.py +++ b/tests/test_cells_and_edges.py @@ -1,7 +1,7 @@ import h3 import pytest -from h3 import ( +from .h3fake2_errors import ( H3ValueError, H3CellError, H3ResolutionError, diff --git a/tests/test_cython.py b/tests/test_cython.py index 30566b1..0acf307 100644 --- a/tests/test_cython.py +++ b/tests/test_cython.py @@ -1,28 +1,28 @@ -import numpy as np +# import numpy as np -# Avoid checking for import-error here because cython_example may not have -# been compiled yet. -try: - from .cython_example import geo_to_h3_vect # pylint: disable=import-error -except ImportError: - geo_to_h3_vect = None +# # Avoid checking for import-error here because cython_example may not have +# # been compiled yet. +# try: +# from .cython_example import geo_to_h3_vect # pylint: disable=import-error +# except ImportError: +# geo_to_h3_vect = None -np.random.seed(0) +# np.random.seed(0) -def test_cython_api(): - if geo_to_h3_vect is None: - print("Not running Cython test because cython example was not compiled") - return +# def test_cython_api(): +# if geo_to_h3_vect is None: +# print("Not running Cython test because cython example was not compiled") +# return - N = 100000 +# N = 100000 - lats, lngs = np.random.uniform(0, 90, N), np.random.uniform(0, 90, N) - res = 9 +# lats, lngs = np.random.uniform(0, 90, N), np.random.uniform(0, 90, N) +# res = 9 - lats = np.array(lats, dtype=np.float64) - lngs = np.array(lngs, dtype=np.float64) - out = np.zeros(len(lats), dtype="uint64") - geo_to_h3_vect(lats, lngs, res, out) +# lats = np.array(lats, dtype=np.float64) +# lngs = np.array(lngs, dtype=np.float64) +# out = np.zeros(len(lats), dtype="uint64") +# geo_to_h3_vect(lats, lngs, res, out) - assert out[0] == 617284541015654399 +# assert out[0] == 617284541015654399 diff --git a/tests/test_length_area.py b/tests/test_length_area.py index 8236199..2a1b5d7 100644 --- a/tests/test_length_area.py +++ b/tests/test_length_area.py @@ -1,7 +1,7 @@ import h3 import pytest -from h3 import H3ValueError +from .h3fake2_errors import H3ValueError def approx2(a, b): diff --git a/tests/test_polyfill.py b/tests/test_polyfill.py index 4739009..a02560d 100644 --- a/tests/test_polyfill.py +++ b/tests/test_polyfill.py @@ -2,6 +2,8 @@ import h3 import itertools import pytest +from .h3fake2_errors import H3ResolutionError + def reverse(loop): return list(reversed(loop)) @@ -209,10 +211,10 @@ def test_resolution(): 'coordinates': [[]], } - with pytest.raises(h3.H3ResolutionError): + with pytest.raises(H3ResolutionError): h3.polyfill(d, -1) - with pytest.raises(h3.H3ResolutionError): + with pytest.raises(H3ResolutionError): h3.polyfill(d, 16) diff --git a/tests/test_unstable_vect.py b/tests/test_unstable_vect.py deleted file mode 100644 index a556aae..0000000 --- a/tests/test_unstable_vect.py +++ /dev/null @@ -1,69 +0,0 @@ -import h3.api.numpy_int as h3 -import numpy as np # only run this test suite if numpy is installed -import pytest - -with pytest.warns( - UserWarning, - match = 'Modules under `h3.unstable` are experimental', -): - import h3.unstable.vect as h3_vect - - -def test_h3_to_parent(): - # At res 9 - h = np.array([617700169958555647, 617700169958555647], np.uint64) - - # Default to res - 1 - arr1 = h3_vect.h3_to_parent(h) - arr2 = h3_vect.h3_to_parent(h, 8) - assert np.array_equal(arr1, arr2) - - # Same as other h3 bindings - arr1 = h3_vect.h3_to_parent(h) - arr2 = np.array(list(map(h3.h3_to_parent, h)), dtype=np.uint64) - assert np.array_equal(arr1, arr2) - - # Test with a number passed to res - arr1 = h3_vect.h3_to_parent(h, 7) - arr2 = np.array([h3.h3_to_parent(c, 7) for c in h], dtype=np.uint64) - assert np.array_equal(arr1, arr2) - - # Test with an array passed to res - arr1 = h3_vect.h3_to_parent(h, np.array([7, 5], np.intc)) - arr2 = h3_vect.h3_to_parent(h, [7, 5]) - - arr3 = [] - for c, res in zip(h, [7, 5]): - arr3.append(h3.h3_to_parent(c, res)) - - assert np.array_equal(arr1, arr2) - assert np.array_equal(arr1, np.array(arr3, dtype=np.uint64)) - - # Test with array-like (but not np.array) cell input - arr1 = h3_vect.h3_to_parent(list(h)) - arr2 = np.array(list(map(h3.h3_to_parent, h)), dtype=np.uint64) - assert np.array_equal(arr1, arr2) - - -def test_h3_to_parent_multiple_res(): - h = np.array([617700169958555647, 613196570331971583], np.uint64) - - # Cells at res 9, 8 - assert list(h3_vect.h3_get_resolution(h)) == [9, 8] - - # Same as other h3 bindings - arr1 = h3_vect.h3_to_parent(h) - arr2 = np.array(list(map(h3.h3_to_parent, h)), dtype=np.uint64) - assert np.array_equal(arr1, arr2) - - # Parent cells are 8, 7 - parents = h3_vect.h3_to_parent(h) - assert list(h3_vect.h3_get_resolution(parents)) == [8, 7] - - -def test_h3_get_resolution(): - h = np.array([617700169958555647], np.uint64) - - arr1 = h3_vect.h3_get_resolution(h) - arr2 = np.array(list(map(h3.h3_get_resolution, h)), dtype=np.intc) - assert np.array_equal(arr1, arr2) From 4ec0a92f109821ad338f701542ab490f585993af Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Tue, 24 May 2022 20:18:44 -0700 Subject: [PATCH 02/60] ajfriend/more v4 funcs (#241) * get_pentagon_indexes and get_res0_indexes * line * cell area functions --- src/h3/_cy/__init__.py | 15 +++-- src/h3/_cy/cells.pxd | 10 +-- src/h3/_cy/cells.pyx | 123 +++++++++++++++++++--------------- src/h3/_cy/h3lib.pxd | 34 +++++----- tests/test_cells_and_edges.py | 10 +-- 5 files changed, 105 insertions(+), 87 deletions(-) diff --git a/src/h3/_cy/__init__.py b/src/h3/_cy/__init__.py index 6b17b2d..cfe34c7 100644 --- a/src/h3/_cy/__init__.py +++ b/src/h3/_cy/__init__.py @@ -27,6 +27,11 @@ from .cells import ( compact, uncompact, num_hexagons, + get_pentagon_indexes, + get_res0_indexes, + line, + mean_hex_area, + cell_area, ) from h3fake2._cy import ( @@ -42,12 +47,12 @@ from h3fake2._cy import ( # compact, # uncompact, # num_hexagons, - mean_hex_area, - cell_area, - line, + # mean_hex_area, + # cell_area, + # line, # is_res_class_iii, - get_pentagon_indexes, - get_res0_indexes, + # get_pentagon_indexes, + # get_res0_indexes, # center_child, get_faces, experimental_h3_to_local_ij, diff --git a/src/h3/_cy/cells.pxd b/src/h3/_cy/cells.pxd index 4e2d441..0dd3aa6 100644 --- a/src/h3/_cy/cells.pxd +++ b/src/h3/_cy/cells.pxd @@ -14,12 +14,12 @@ cpdef H3int center_child(H3int h, res=*) except 0 cpdef H3int[:] compact(const H3int[:] hu) cpdef H3int[:] uncompact(const H3int[:] hc, int res) cpdef int64_t num_hexagons(int resolution) except -1 -# cpdef double mean_hex_area(int resolution, unit=*) except -1 -# cpdef double cell_area(H3int h, unit=*) except -1 -# cpdef H3int[:] line(H3int start, H3int end) +cpdef double mean_hex_area(int resolution, unit=*) except -1 +cpdef double cell_area(H3int h, unit=*) except -1 +cpdef H3int[:] line(H3int start, H3int end) cpdef bool is_res_class_iii(H3int h) -# cpdef H3int[:] get_pentagon_indexes(int res) -# cpdef H3int[:] get_res0_indexes() +cpdef H3int[:] get_pentagon_indexes(int res) +cpdef H3int[:] get_res0_indexes() # cpdef get_faces(H3int h) # cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except * # cpdef H3int experimental_local_ij_to_h3(H3int origin, int i, int j) except 0 diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 265e4c3..9cfcbb2 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -281,87 +281,104 @@ cpdef int64_t num_hexagons(int resolution) except -1: return num_cells -# cpdef double mean_hex_area(int resolution, unit='km^2') except -1: -# check_res(resolution) +cpdef double mean_hex_area(int resolution, unit='km^2') except -1: + cdef: + h3lib.H3Error err + double area -# area = h3lib.hexAreaKm2(resolution) + check_res(resolution) -# # todo: multiple units -# convert = { -# 'km^2': 1.0, -# 'm^2': 1000*1000.0 -# } + err = h3lib.getHexagonAreaAvgKm2(resolution, &area) -# try: -# area *= convert[unit] -# except: -# raise H3ValueError('Unknown unit: {}'.format(unit)) + # todo: multiple units + convert = { + 'km^2': 1.0, + 'm^2': 1000*1000.0 + } -# return area + try: + area *= convert[unit] + except: + raise H3ValueError('Unknown unit: {}'.format(unit)) + + return area -# cpdef double cell_area(H3int h, unit='km^2') except -1: -# check_cell(h) +cpdef double cell_area(H3int h, unit='km^2') except -1: + cdef: + h3lib.H3Error err + double area -# if unit == 'rads^2': -# area = h3lib.cellAreaRads2(h) -# elif unit == 'km^2': -# area = h3lib.cellAreaKm2(h) -# elif unit == 'm^2': -# area = h3lib.cellAreaM2(h) -# else: -# raise H3ValueError('Unknown unit: {}'.format(unit)) + check_cell(h) -# return area + if unit == 'rads^2': + err = h3lib.cellAreaRads2(h, &area) + elif unit == 'km^2': + err = h3lib.cellAreaKm2(h, &area) + elif unit == 'm^2': + err = h3lib.cellAreaM2(h, &area) + else: + raise H3ValueError('Unknown unit: {}'.format(unit)) + + return area -# cpdef H3int[:] line(H3int start, H3int end): -# check_cell(start) -# check_cell(end) +cpdef H3int[:] line(H3int start, H3int end): + cdef: + h3lib.H3Error err + int64_t n -# n = h3lib.h3LineSize(start, end) + check_cell(start) + check_cell(end) -# if n < 0: -# s = "Couldn't find line between cells {} and {}" -# s = s.format(hex(start), hex(end)) -# raise H3ValueError(s) + err = h3lib.gridPathCellsSize(start, end, &n) -# ptr = create_ptr(n) -# flag = h3lib.h3Line(start, end, ptr) -# mv = create_mv(ptr, n) + if err: + s = "Couldn't find line between cells {} and {}" + s = s.format(hex(start), hex(end)) + raise H3ValueError(s) -# if flag != 0: -# s = "Couldn't find line between cells {} and {}" -# s = s.format(hex(start), hex(end)) -# raise H3ValueError(s) + ptr = create_ptr(n) + err = h3lib.gridPathCells(start, end, ptr) + mv = create_mv(ptr, n) -# return mv + if err: + s = "Couldn't find line between cells {} and {}" + s = s.format(hex(start), hex(end)) + raise H3ValueError(s) + + return mv cpdef bool is_res_class_iii(H3int h): return h3lib.isResClassIII(h) == 1 -# cpdef H3int[:] get_pentagon_indexes(int res): -# check_res(res) +cpdef H3int[:] get_pentagon_indexes(int res): + cdef: + h3lib.H3Error err -# n = h3lib.pentagonIndexCount() + check_res(res) -# ptr = create_ptr(n) -# h3lib.getPentagonIndexes(res, ptr) -# mv = create_mv(ptr, n) + n = h3lib.pentagonCount() -# return mv + ptr = create_ptr(n) + err = h3lib.getPentagons(res, ptr) + mv = create_mv(ptr, n) + + return mv -# cpdef H3int[:] get_res0_indexes(): -# n = h3lib.res0IndexCount() +cpdef H3int[:] get_res0_indexes(): + cdef: + h3lib.H3Error err -# ptr = create_ptr(n) -# h3lib.getRes0Indexes(ptr) -# mv = create_mv(ptr, n) + n = h3lib.res0CellCount() -# return mv + ptr = create_ptr(n) + err = h3lib.getRes0Cells(ptr) + mv = create_mv(ptr, n) + return mv # cpdef get_faces(H3int h): # check_cell(h) diff --git a/src/h3/_cy/h3lib.pxd b/src/h3/_cy/h3lib.pxd index 633af81..2061b43 100644 --- a/src/h3/_cy/h3lib.pxd +++ b/src/h3/_cy/h3lib.pxd @@ -60,7 +60,21 @@ cdef extern from "h3api.h": const int res ) nogil - H3Error getNumCells(int res, int64_t *out) + H3Error getNumCells(int res, int64_t *out) nogil + int pentagonCount() nogil + int res0CellCount() nogil + H3Error getPentagons(int res, H3Index *out) nogil + H3Error getRes0Cells(H3Index *out) nogil + + H3Error gridPathCellsSize(H3Index start, H3Index end, int64_t *size) nogil + H3Error gridPathCells(H3Index start, H3Index end, H3Index *out) nogil + + H3Error getHexagonAreaAvgKm2(int res, double *out) nogil + H3Error getHexagonAreaAvgM2(int res, double *out) nogil + + H3Error cellAreaRads2(H3Index h, double *out) nogil + H3Error cellAreaKm2(H3Index h, double *out) nogil + H3Error cellAreaM2(H3Index h, double *out) nogil # ctypedef struct GeoBoundary: # int num_verts "numVerts" @@ -122,14 +136,6 @@ cdef extern from "h3api.h": # void h3ToString(H3Index h, char *str, size_t sz) - # int pentagonIndexCount() - - # void getPentagonIndexes(int res, H3Index *out) - - # int res0IndexCount() - - # void getRes0Indexes(H3Index *out) - # int h3IndexesAreNeighbors(H3Index origin, H3Index destination) # H3Index getH3UnidirectionalEdge(H3Index origin, H3Index destination) @@ -144,22 +150,12 @@ cdef extern from "h3api.h": # void getH3UnidirectionalEdgeBoundary(H3Index edge, GeoBoundary *gb) - # int h3LineSize(H3Index start, H3Index end) - # int h3Line(H3Index start, H3Index end, H3Index *out) - # int maxFaceCount(H3Index h3) # void h3GetFaces(H3Index h3, int *out) # int experimentalH3ToLocalIj(H3Index origin, H3Index h3, CoordIJ *out) # int experimentalLocalIjToH3(H3Index origin, const CoordIJ *ij, H3Index *out) - # double hexAreaKm2(int res) nogil - # double hexAreaM2(int res) nogil - - # double cellAreaRads2(H3Index h) nogil - # double cellAreaKm2(H3Index h) nogil - # double cellAreaM2(H3Index h) nogil - # double edgeLengthKm(int res) nogil # double edgeLengthM(int res) nogil diff --git a/tests/test_cells_and_edges.py b/tests/test_cells_and_edges.py index 014f519..851dda5 100644 --- a/tests/test_cells_and_edges.py +++ b/tests/test_cells_and_edges.py @@ -269,11 +269,11 @@ def test_num_hexagons(): def test_hex_area(): expected_in_km2 = { - 0: 4250546.848, - 1: 607220.9782, - 2: 86745.85403, - 9: 0.1053325, - 15: 9e-07, + 0: 4357449.416078381, + 1: 609788.441794133, + 2: 86801.780398997, + 9: 0.105332513, + 15: 8.95311e-07, } out = { From f2253cfb30e8b3925f0b3df29e176c8508884a5a Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Wed, 25 May 2022 11:14:55 -0700 Subject: [PATCH 03/60] get_faces (#248) * initial pass * thoughts * the results of thinking * this commit is associated with the previous commits * nogil * i'm a bill * remove the typical nonsense --- src/h3/_cy/__init__.py | 3 ++- src/h3/_cy/cells.pxd | 2 +- src/h3/_cy/cells.pyx | 26 +++++++++++++++----------- src/h3/_cy/h3lib.pxd | 6 +++--- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/h3/_cy/__init__.py b/src/h3/_cy/__init__.py index cfe34c7..d8460bf 100644 --- a/src/h3/_cy/__init__.py +++ b/src/h3/_cy/__init__.py @@ -32,6 +32,7 @@ from .cells import ( line, mean_hex_area, cell_area, + get_faces, ) from h3fake2._cy import ( @@ -54,7 +55,7 @@ from h3fake2._cy import ( # get_pentagon_indexes, # get_res0_indexes, # center_child, - get_faces, + # get_faces, experimental_h3_to_local_ij, experimental_local_ij_to_h3, ) diff --git a/src/h3/_cy/cells.pxd b/src/h3/_cy/cells.pxd index 0dd3aa6..8ca4313 100644 --- a/src/h3/_cy/cells.pxd +++ b/src/h3/_cy/cells.pxd @@ -20,6 +20,6 @@ cpdef H3int[:] line(H3int start, H3int end) cpdef bool is_res_class_iii(H3int h) cpdef H3int[:] get_pentagon_indexes(int res) cpdef H3int[:] get_res0_indexes() -# cpdef get_faces(H3int h) +cpdef get_faces(H3int h) # cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except * # cpdef H3int experimental_local_ij_to_h3(H3int origin, int i, int j) except 0 diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 9cfcbb2..fdc2e7e 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -380,22 +380,26 @@ cpdef H3int[:] get_res0_indexes(): return mv -# cpdef get_faces(H3int h): -# check_cell(h) +cpdef get_faces(H3int h): + cdef: + h3lib.H3Error err + int n -# n = h3lib.maxFaceCount(h) + check_cell(h) -# cdef int* ptr = stdlib.calloc(n, sizeof(int)) -# if (n > 0) and (not ptr): -# raise MemoryError() + err = h3lib.maxFaceCount(h, &n) #ignore error for now -# h3lib.h3GetFaces(h, ptr) + cdef int* ptr = stdlib.calloc(n, sizeof(int)) + if (n > 0) and (not ptr): + raise MemoryError() -# faces = ptr -# faces = {f for f in faces if f >= 0} -# stdlib.free(ptr) + err = h3lib.getIcosahedronFaces(h, ptr) # handle error? -# return faces + faces = ptr + faces = {f for f in faces if f >= 0} + stdlib.free(ptr) + + return faces # cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except *: diff --git a/src/h3/_cy/h3lib.pxd b/src/h3/_cy/h3lib.pxd index 2061b43..a5aeb77 100644 --- a/src/h3/_cy/h3lib.pxd +++ b/src/h3/_cy/h3lib.pxd @@ -76,6 +76,9 @@ cdef extern from "h3api.h": H3Error cellAreaKm2(H3Index h, double *out) nogil H3Error cellAreaM2(H3Index h, double *out) nogil + H3Error maxFaceCount(H3Index h, int *out) nogil + H3Error getIcosahedronFaces(H3Index h3, int *out) nogil + # ctypedef struct GeoBoundary: # int num_verts "numVerts" # GeoCoord verts[10] # MAX_CELL_BNDRY_VERTS @@ -150,9 +153,6 @@ cdef extern from "h3api.h": # void getH3UnidirectionalEdgeBoundary(H3Index edge, GeoBoundary *gb) - # int maxFaceCount(H3Index h3) - # void h3GetFaces(H3Index h3, int *out) - # int experimentalH3ToLocalIj(H3Index origin, H3Index h3, CoordIJ *out) # int experimentalLocalIjToH3(H3Index origin, const CoordIJ *ij, H3Index *out) From 7abaf49b195cea067e10e982b255d7996bc6ba53 Mon Sep 17 00:00:00 2001 From: Isaac Brodsky Date: Wed, 25 May 2022 17:09:36 -0700 Subject: [PATCH 04/60] v4: local ij functions to v4 cython (#240) * move local ij functions to v4 implementation * revert some changes and add gridPathCells * swap implementation * revert gridPathCells --- src/h3/_cy/__init__.py | 6 +++-- src/h3/_cy/cells.pxd | 4 ++-- src/h3/_cy/cells.pyx | 51 +++++++++++++++++++++--------------------- src/h3/_cy/h3lib.pxd | 23 +++++++++---------- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/h3/_cy/__init__.py b/src/h3/_cy/__init__.py index d8460bf..d378be8 100644 --- a/src/h3/_cy/__init__.py +++ b/src/h3/_cy/__init__.py @@ -33,6 +33,8 @@ from .cells import ( mean_hex_area, cell_area, get_faces, + experimental_h3_to_local_ij, + experimental_local_ij_to_h3, ) from h3fake2._cy import ( @@ -56,8 +58,8 @@ from h3fake2._cy import ( # get_res0_indexes, # center_child, # get_faces, - experimental_h3_to_local_ij, - experimental_local_ij_to_h3, + # experimental_h3_to_local_ij, + # experimental_local_ij_to_h3, ) # from .edges import ( diff --git a/src/h3/_cy/cells.pxd b/src/h3/_cy/cells.pxd index 8ca4313..ee6c2a7 100644 --- a/src/h3/_cy/cells.pxd +++ b/src/h3/_cy/cells.pxd @@ -21,5 +21,5 @@ cpdef bool is_res_class_iii(H3int h) cpdef H3int[:] get_pentagon_indexes(int res) cpdef H3int[:] get_res0_indexes() cpdef get_faces(H3int h) -# cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except * -# cpdef H3int experimental_local_ij_to_h3(H3int origin, int i, int j) except 0 +cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except * +cpdef H3int experimental_local_ij_to_h3(H3int origin, int i, int j) except 0 diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index fdc2e7e..0bd99bd 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -402,39 +402,38 @@ cpdef get_faces(H3int h): return faces -# cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except *: -# cdef: -# int flag -# h3lib.CoordIJ c +cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except *: + cdef: + int flag + h3lib.CoordIJ c -# check_cell(origin) -# check_cell(h) + check_cell(origin) + check_cell(h) -# flag = h3lib.experimentalH3ToLocalIj(origin, h, &c) + flag = h3lib.cellToLocalIj(origin, h, 0, &c) -# if flag != 0: -# s = "Couldn't find local (i,j) between cells {} and {}." -# s = s.format(hex(origin), hex(h)) -# raise H3ValueError(s) + if flag != 0: + s = "Couldn't find local (i,j) between cells {} and {}." + s = s.format(hex(origin), hex(h)) + raise H3ValueError(s) -# return c.i, c.j + return c.i, c.j +cpdef H3int experimental_local_ij_to_h3(H3int origin, int i, int j) except 0: + cdef: + int flag + h3lib.CoordIJ c + H3int out -# cpdef H3int experimental_local_ij_to_h3(H3int origin, int i, int j) except 0: -# cdef: -# int flag -# h3lib.CoordIJ c -# H3int out + check_cell(origin) -# check_cell(origin) + c.i, c.j = i, j -# c.i, c.j = i, j + flag = h3lib.localIjToCell(origin, &c, 0, &out) -# flag = h3lib.experimentalLocalIjToH3(origin, &c, &out) + if flag != 0: + s = "Couldn't find cell at local ({},{}) from cell {}." + s = s.format(i, j, hex(origin)) + raise H3ValueError(s) -# if flag != 0: -# s = "Couldn't find cell at local ({},{}) from cell {}." -# s = s.format(i, j, hex(origin)) -# raise H3ValueError(s) - -# return out + return out diff --git a/src/h3/_cy/h3lib.pxd b/src/h3/_cy/h3lib.pxd index a5aeb77..05013b9 100644 --- a/src/h3/_cy/h3lib.pxd +++ b/src/h3/_cy/h3lib.pxd @@ -1,9 +1,8 @@ -from libc cimport stdint from cpython cimport bool -from libc.stdint cimport int64_t +from libc.stdint cimport uint64_t, int64_t, uint32_t -ctypedef stdint.uint64_t H3int -ctypedef stdint.uint32_t H3Error +ctypedef uint64_t H3int +ctypedef uint32_t H3Error ctypedef basestring H3str cdef extern from "h3api.h": @@ -11,12 +10,16 @@ cdef extern from "h3api.h": cdef int H3_VERSION_MINOR cdef int H3_VERSION_PATCH - ctypedef stdint.uint64_t H3Index + ctypedef uint64_t H3Index ctypedef struct LatLng: double lat # in radians double lng # in radians + ctypedef struct CoordIJ: + int i + int j + int isValidCell(H3Index h) nogil int isPentagon(H3Index h) nogil int isResClassIII(H3Index h) nogil @@ -79,6 +82,9 @@ cdef extern from "h3api.h": H3Error maxFaceCount(H3Index h, int *out) nogil H3Error getIcosahedronFaces(H3Index h3, int *out) nogil + H3Error cellToLocalIj(H3Index origin, H3Index h3, uint32_t mode, CoordIJ *out) nogil + H3Error localIjToCell(H3Index origin, const CoordIJ *ij, uint32_t mode, H3Index *out) nogil + # ctypedef struct GeoBoundary: # int num_verts "numVerts" # GeoCoord verts[10] # MAX_CELL_BNDRY_VERTS @@ -111,10 +117,6 @@ cdef extern from "h3api.h": # LinkedGeoLoop *_data_last "last" # not needed in Cython bindings # LinkedGeoPolygon *next - # ctypedef struct CoordIJ: - # int i - # int j - # void h3ToGeoBoundary(H3Index h3, GeoBoundary *gp) # int hexRange(H3Index origin, int k, H3Index *out) @@ -153,9 +155,6 @@ cdef extern from "h3api.h": # void getH3UnidirectionalEdgeBoundary(H3Index edge, GeoBoundary *gb) - # int experimentalH3ToLocalIj(H3Index origin, H3Index h3, CoordIJ *out) - # int experimentalLocalIjToH3(H3Index origin, const CoordIJ *ij, H3Index *out) - # double edgeLengthKm(int res) nogil # double edgeLengthM(int res) nogil From 610d648bfee25e55b96133f1038bffc0e81bff62 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Wed, 25 May 2022 21:45:26 -0700 Subject: [PATCH 05/60] ring (#251) --- src/h3/_cy/__init__.py | 38 ++++--------------- src/h3/_cy/cells.pxd | 4 +- src/h3/_cy/cells.pyx | 86 ++++++++++++++++++++++-------------------- src/h3/_cy/h3lib.pxd | 7 ++-- 4 files changed, 58 insertions(+), 77 deletions(-) diff --git a/src/h3/_cy/__init__.py b/src/h3/_cy/__init__.py index d378be8..6bce5bf 100644 --- a/src/h3/_cy/__init__.py +++ b/src/h3/_cy/__init__.py @@ -18,50 +18,26 @@ from .cells import ( is_pentagon, get_base_cell, resolution, - is_res_class_iii, + parent, distance, disk, - parent, + ring, children, - center_child, compact, uncompact, num_hexagons, - get_pentagon_indexes, - get_res0_indexes, - line, mean_hex_area, cell_area, + line, + is_res_class_iii, + get_pentagon_indexes, + get_res0_indexes, + center_child, get_faces, experimental_h3_to_local_ij, experimental_local_ij_to_h3, ) -from h3fake2._cy import ( - # is_cell, - # is_pentagon, - # get_base_cell, - # resolution, - # parent, - # distance, - # disk, - ring, - # children, - # compact, - # uncompact, - # num_hexagons, - # mean_hex_area, - # cell_area, - # line, - # is_res_class_iii, - # get_pentagon_indexes, - # get_res0_indexes, - # center_child, - # get_faces, - # experimental_h3_to_local_ij, - # experimental_local_ij_to_h3, -) - # from .edges import ( from h3fake2._cy import ( are_neighbors, diff --git a/src/h3/_cy/cells.pxd b/src/h3/_cy/cells.pxd index ee6c2a7..1071938 100644 --- a/src/h3/_cy/cells.pxd +++ b/src/h3/_cy/cells.pxd @@ -6,8 +6,8 @@ cpdef int get_base_cell(H3int h) except -1 cpdef int resolution(H3int h) except -1 cpdef int distance(H3int h1, H3int h2) except -1 cpdef H3int[:] disk(H3int h, int k) -# cpdef H3int[:] _ring_fallback(H3int h, int k) -# cpdef H3int[:] ring(H3int h, int k) +cpdef H3int[:] _ring_fallback(H3int h, int k) +cpdef H3int[:] ring(H3int h, int k) cpdef H3int parent(H3int h, res=*) except 0 cpdef H3int[:] children(H3int h, res=*) cpdef H3int center_child(H3int h, res=*) except 0 diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 0bd99bd..4453e38 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -85,61 +85,67 @@ cpdef H3int[:] disk(H3int h, int k): return mv -# cpdef H3int[:] _ring_fallback(H3int h, int k): -# """ -# `ring` tries to call `h3lib.hexRing` first; if that fails, we call -# this function, which relies on `h3lib.kRingDistances`. +cpdef H3int[:] _ring_fallback(H3int h, int k): + """ + `ring` tries to call `h3lib.hexRing` first; if that fails, we call + this function, which relies on `h3lib.kRingDistances`. -# Failures for `h3lib.hexRing` happen when the algortihm runs into a pentagon. -# """ -# check_cell(h) -# check_distance(k) + Failures for `h3lib.hexRing` happen when the algortihm runs into a pentagon. + """ + cdef: + int64_t n + h3lib.H3Error err -# n = h3lib.maxKringSize(k) -# # array of h3 cells -# ptr = create_ptr(n) + check_cell(h) + check_distance(k) -# # array of cell distances from `h` -# dist_ptr = stdlib.calloc(n, sizeof(int)) -# if dist_ptr is NULL: -# raise MemoryError() + err = h3lib.maxGridDiskSize(k, &n) + # array of h3 cells + ptr = create_ptr(n) -# h3lib.kRingDistances(h, k, ptr, dist_ptr) + # array of cell distances from `h` + dist_ptr = stdlib.calloc(n, sizeof(int)) + if dist_ptr is NULL: + raise MemoryError() -# distances = dist_ptr -# distances.callback_free_data = stdlib.free + err = h3lib.gridDiskDistances(h, k, ptr, dist_ptr) -# for i,v in enumerate(distances): -# if v != k: -# ptr[i] = 0 + distances = dist_ptr + distances.callback_free_data = stdlib.free -# mv = create_mv(ptr, n) + for i,v in enumerate(distances): + if v != k: + ptr[i] = 0 -# return mv + mv = create_mv(ptr, n) -# cpdef H3int[:] ring(H3int h, int k): -# """ Return cells at grid distance `== k` from `h`. -# Collection is "hollow" for k >= 1. -# """ -# check_cell(h) -# check_distance(k) + return mv -# n = 6*k if k > 0 else 1 -# ptr = create_ptr(n) +cpdef H3int[:] ring(H3int h, int k): + """ Return cells at grid distance `== k` from `h`. + Collection is "hollow" for k >= 1. + """ + cdef: + h3lib.H3Error err -# flag = h3lib.hexRing(h, k, ptr) + check_cell(h) + check_distance(k) -# # if we drop into the failure state, we might be tempted to not create -# # this mv, but creating the mv is exactly what guarantees that we'll free -# # the memory. context manager would be better here, if we can figure out -# # how to do that -# mv = create_mv(ptr, n) + n = 6*k if k > 0 else 1 + ptr = create_ptr(n) -# if flag != 0: -# mv = _ring_fallback(h, k) + err = h3lib.gridRingUnsafe(h, k, ptr) -# return mv + # if we drop into the failure state, we might be tempted to not create + # this mv, but creating the mv is exactly what guarantees that we'll free + # the memory. context manager would be better here, if we can figure out + # how to do that + mv = create_mv(ptr, n) + if err: + mv = _ring_fallback(h, k) + + return mv cpdef H3int parent(H3int h, res=None) except 0: cdef: diff --git a/src/h3/_cy/h3lib.pxd b/src/h3/_cy/h3lib.pxd index 05013b9..aa84b2b 100644 --- a/src/h3/_cy/h3lib.pxd +++ b/src/h3/_cy/h3lib.pxd @@ -85,6 +85,9 @@ cdef extern from "h3api.h": H3Error cellToLocalIj(H3Index origin, H3Index h3, uint32_t mode, CoordIJ *out) nogil H3Error localIjToCell(H3Index origin, const CoordIJ *ij, uint32_t mode, H3Index *out) nogil + H3Error gridDiskDistances(H3Index origin, int k, H3Index *out, int *distances) nogil + H3Error gridRingUnsafe(H3Index origin, int k, H3Index *out) nogil + # ctypedef struct GeoBoundary: # int num_verts "numVerts" # GeoCoord verts[10] # MAX_CELL_BNDRY_VERTS @@ -125,10 +128,6 @@ cdef extern from "h3api.h": # int hexRanges(H3Index *h3Set, int length, int k, H3Index *out) - # void kRingDistances(H3Index origin, int k, H3Index *out, int *distances) - - # int hexRing(H3Index origin, int k, H3Index *out) - # int maxPolyfillSize(const GeoPolygon *geoPolygon, int res) # void polyfill(const GeoPolygon *geoPolygon, int res, H3Index *out) From 51e73fe01b38ae53289d49d692e233e6b16eba3b Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Thu, 26 May 2022 14:15:35 -0400 Subject: [PATCH 06/60] Uncomment cython example, and exclude sdist from wheels test temporarilty (#252) * uncomment cython test * exclude make_sdist temporarily from the wheels tests --- .github/workflows/wheels.yml | 3 ++- tests/cython_example.pyx | 28 ++++++++++++------------- tests/test_cython.py | 40 ++++++++++++++++++------------------ 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index 71f47d5..dd6bfbc 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -168,7 +168,8 @@ jobs: path: wheelhouse/*.whl upload_all: - needs: [make_sdist, make_cibw_v1_wheels, make_cibw_v2_wheels] + # needs: [make_sdist, make_cibw_v1_wheels, make_cibw_v2_wheels] + needs: [make_cibw_v1_wheels, make_cibw_v2_wheels] runs-on: ubuntu-latest if: github.event_name == 'release' && github.event.action == 'published' diff --git a/tests/cython_example.pyx b/tests/cython_example.pyx index c19fe21..e8d2859 100644 --- a/tests/cython_example.pyx +++ b/tests/cython_example.pyx @@ -1,18 +1,18 @@ -# from cython cimport boundscheck, wraparound +from cython cimport boundscheck, wraparound -# from h3._cy.h3lib cimport H3int -# from h3._cy.geo cimport geo_to_h3 +from h3._cy.h3lib cimport H3int +from h3._cy.geo cimport geo_to_h3 -# @boundscheck(False) -# @wraparound(False) -# cpdef void geo_to_h3_vect( -# const double[:] lat, -# const double[:] lng, -# int res, -# H3int[:] out -# ): +@boundscheck(False) +@wraparound(False) +cpdef void geo_to_h3_vect( + const double[:] lat, + const double[:] lng, + int res, + H3int[:] out +): -# cdef Py_ssize_t i + cdef Py_ssize_t i -# for i in range(len(lat)): -# out[i] = geo_to_h3(lat[i], lng[i], res) + for i in range(len(lat)): + out[i] = geo_to_h3(lat[i], lng[i], res) diff --git a/tests/test_cython.py b/tests/test_cython.py index 0acf307..30566b1 100644 --- a/tests/test_cython.py +++ b/tests/test_cython.py @@ -1,28 +1,28 @@ -# import numpy as np +import numpy as np -# # Avoid checking for import-error here because cython_example may not have -# # been compiled yet. -# try: -# from .cython_example import geo_to_h3_vect # pylint: disable=import-error -# except ImportError: -# geo_to_h3_vect = None +# Avoid checking for import-error here because cython_example may not have +# been compiled yet. +try: + from .cython_example import geo_to_h3_vect # pylint: disable=import-error +except ImportError: + geo_to_h3_vect = None -# np.random.seed(0) +np.random.seed(0) -# def test_cython_api(): -# if geo_to_h3_vect is None: -# print("Not running Cython test because cython example was not compiled") -# return +def test_cython_api(): + if geo_to_h3_vect is None: + print("Not running Cython test because cython example was not compiled") + return -# N = 100000 + N = 100000 -# lats, lngs = np.random.uniform(0, 90, N), np.random.uniform(0, 90, N) -# res = 9 + lats, lngs = np.random.uniform(0, 90, N), np.random.uniform(0, 90, N) + res = 9 -# lats = np.array(lats, dtype=np.float64) -# lngs = np.array(lngs, dtype=np.float64) -# out = np.zeros(len(lats), dtype="uint64") -# geo_to_h3_vect(lats, lngs, res, out) + lats = np.array(lats, dtype=np.float64) + lngs = np.array(lngs, dtype=np.float64) + out = np.zeros(len(lats), dtype="uint64") + geo_to_h3_vect(lats, lngs, res, out) -# assert out[0] == 617284541015654399 + assert out[0] == 617284541015654399 From c5333321e92b77847685b3dbd80692e5f9d65713 Mon Sep 17 00:00:00 2001 From: Isaac Brodsky Date: Tue, 31 May 2022 19:01:51 -0700 Subject: [PATCH 07/60] v4: polygonToCells (#254) --- src/h3/_cy/__init__.py | 12 +- src/h3/_cy/geo.pyx | 259 +++++++++++++++++++++-------------------- src/h3/_cy/h3lib.pxd | 25 ++-- 3 files changed, 149 insertions(+), 147 deletions(-) diff --git a/src/h3/_cy/__init__.py b/src/h3/_cy/__init__.py index 6bce5bf..db15af6 100644 --- a/src/h3/_cy/__init__.py +++ b/src/h3/_cy/__init__.py @@ -54,9 +54,9 @@ from h3fake2._cy import ( from .geo import ( geo_to_h3, h3_to_geo, - # polyfill_polygon, - # polyfill_geojson, - # polyfill, + polyfill_polygon, + polyfill_geojson, + polyfill, # cell_boundary, # edge_boundary, # point_dist, @@ -65,9 +65,9 @@ from .geo import ( from h3fake2._cy import ( # geo_to_h3, # h3_to_geo, - polyfill_polygon, - polyfill_geojson, - polyfill, + # polyfill_polygon, + # polyfill_geojson, + # polyfill, cell_boundary, edge_boundary, point_dist, diff --git a/src/h3/_cy/geo.pyx b/src/h3/_cy/geo.pyx index fe6c93b..870594b 100644 --- a/src/h3/_cy/geo.pyx +++ b/src/h3/_cy/geo.pyx @@ -10,6 +10,7 @@ from .util cimport ( coord2deg, ) from libc cimport stdlib +from libc.stdint cimport uint64_t from .util import H3ValueError @@ -42,180 +43,182 @@ cpdef (double, double) h3_to_geo(H3int h) except *: return coord2deg(c) -# cdef h3lib.Geofence make_geofence(geos, bool lnglat_order=False) except *: -# """ +cdef h3lib.GeoLoop make_geoloop(geos, bool lnglat_order=False) except *: + """ -# The returned `Geofence` must be freed with a call to `free_geofence`. + The returned `GeoLoop` must be freed with a call to `free_geoloop`. -# Parameters -# ---------- -# geos : list or tuple -# GeoFence: A sequence of >= 3 (lat, lng) pairs where the last -# element may or may not be same as the first (to form a closed loop). -# The order of the pairs may be either clockwise or counterclockwise. -# lnglat_order : bool -# If True, assume coordinate pairs like (lng, lat) -# If False, assume coordinate pairs like (lat, lng) -# """ -# cdef: -# h3lib.Geofence gf + Parameters + ---------- + geos : list or tuple + GeoLoop: A sequence of >= 3 (lat, lng) pairs where the last + element may or may not be same as the first (to form a closed loop). + The order of the pairs may be either clockwise or counterclockwise. + lnglat_order : bool + If True, assume coordinate pairs like (lng, lat) + If False, assume coordinate pairs like (lat, lng) + """ + cdef: + h3lib.GeoLoop gl -# gf.numVerts = len(geos) + gl.numVerts = len(geos) -# gf.verts = stdlib.calloc(gf.numVerts, sizeof(h3lib.GeoCoord)) + gl.verts = stdlib.calloc(gl.numVerts, sizeof(h3lib.LatLng)) -# if lnglat_order: -# latlng = (g[::-1] for g in geos) -# else: -# latlng = geos + if lnglat_order: + latlng = (g[::-1] for g in geos) + else: + latlng = geos -# for i, (lat, lng) in enumerate(latlng): -# gf.verts[i] = deg2coord(lat, lng) + for i, (lat, lng) in enumerate(latlng): + gl.verts[i] = deg2coord(lat, lng) -# return gf + return gl -# cdef free_geofence(h3lib.Geofence* gf): -# stdlib.free(gf.verts) -# gf.verts = NULL +cdef free_geoloop(h3lib.GeoLoop* gl): + stdlib.free(gl.verts) + gl.verts = NULL -# cdef class GeoPolygon: -# cdef: -# h3lib.GeoPolygon gp +cdef class GeoPolygon: + cdef: + h3lib.GeoPolygon gp -# def __cinit__(self, outer, holes=None, bool lnglat_order=False): -# """ + def __cinit__(self, outer, holes=None, bool lnglat_order=False): + """ -# Parameters -# ---------- -# outer : list or tuple -# GeoFence -# A GeoFence is a sequence of >= 3 (lat, lng) pairs where the last -# element may or may not be same as the first (to form a closed loop). -# The order of the pairs may be either clockwise or counterclockwise. -# holes : list or tuple -# A sequence of GeoFences -# lnglat_order : bool -# If True, assume coordinate pairs like (lng, lat) -# If False, assume coordinate pairs like (lat, lng) + Parameters + ---------- + outer : list or tuple + GeoLoop + A GeoLoop is a sequence of >= 3 (lat, lng) pairs where the last + element may or may not be same as the first (to form a closed loop). + The order of the pairs may be either clockwise or counterclockwise. + holes : list or tuple + A sequence of GeoLoops + lnglat_order : bool + If True, assume coordinate pairs like (lng, lat) + If False, assume coordinate pairs like (lat, lng) -# """ -# if holes is None: -# holes = [] + """ + if holes is None: + holes = [] -# self.gp.geofence = make_geofence(outer, lnglat_order) -# self.gp.numHoles = len(holes) -# self.gp.holes = NULL + self.gp.geoloop = make_geoloop(outer, lnglat_order) + self.gp.numHoles = len(holes) + self.gp.holes = NULL -# if len(holes) > 0: -# self.gp.holes = stdlib.calloc(len(holes), sizeof(h3lib.Geofence)) -# for i, hole in enumerate(holes): -# self.gp.holes[i] = make_geofence(hole, lnglat_order) + if len(holes) > 0: + self.gp.holes = stdlib.calloc(len(holes), sizeof(h3lib.GeoLoop)) + for i, hole in enumerate(holes): + self.gp.holes[i] = make_geoloop(hole, lnglat_order) -# def __dealloc__(self): -# free_geofence(&self.gp.geofence) + def __dealloc__(self): + free_geoloop(&self.gp.geoloop) -# for i in range(self.gp.numHoles): -# free_geofence(&self.gp.holes[i]) + for i in range(self.gp.numHoles): + free_geoloop(&self.gp.holes[i]) -# stdlib.free(self.gp.holes) + stdlib.free(self.gp.holes) -# def polyfill_polygon(outer, int res, holes=None, bool lnglat_order=False): -# """ Set of hexagons whose center is contained in a polygon. +def polyfill_polygon(outer, int res, holes=None, bool lnglat_order=False): + """ Set of hexagons whose center is contained in a polygon. -# The polygon is defined as in the GeoJson standard, with an exterior -# LinearRing `outer` and a list of LinearRings `holes`, which define any -# holes in the polygon. + The polygon is defined as in the GeoJson standard, with an exterior + LinearRing `outer` and a list of LinearRings `holes`, which define any + holes in the polygon. -# Each LinearRing may be in clockwise or counter-clockwise order -# (right-hand rule or not), and may or may not be a closed loop (where the last -# element is equal to the first). -# The GeoJSON spec requires the right-hand rule, and a closed loop, but -# this function will work with any input format. + Each LinearRing may be in clockwise or counter-clockwise order + (right-hand rule or not), and may or may not be a closed loop (where the last + element is equal to the first). + The GeoJSON spec requires the right-hand rule, and a closed loop, but + this function will work with any input format. -# Parameters -# ---------- -# outer : list or tuple -# A LinearRing, a sequence of (lat/lng) or (lng/lat) pairs -# res : int -# The resolution of the output hexagons -# holes : list or tuple -# A collection of LinearRings, describing any holes in the polygon -# lnglat_order : bool -# If True, assume coordinate pairs like (lng, lat) -# If False, assume coordinate pairs like (lat, lng) -# """ + Parameters + ---------- + outer : list or tuple + A LinearRing, a sequence of (lat/lng) or (lng/lat) pairs + res : int + The resolution of the output hexagons + holes : list or tuple + A collection of LinearRings, describing any holes in the polygon + lnglat_order : bool + If True, assume coordinate pairs like (lng, lat) + If False, assume coordinate pairs like (lat, lng) + """ + cdef: + uint64_t n -# check_res(res) -# gp = GeoPolygon(outer, holes=holes, lnglat_order=lnglat_order) + check_res(res) + gp = GeoPolygon(outer, holes=holes, lnglat_order=lnglat_order) -# n = h3lib.maxPolyfillSize(&gp.gp, res) -# ptr = create_ptr(n) + h3lib.maxPolygonToCellsSize(&gp.gp, res, 0, &n) + ptr = create_ptr(n) -# h3lib.polyfill(&gp.gp, res, ptr) -# mv = create_mv(ptr, n) + h3lib.polygonToCells(&gp.gp, res, 0, ptr) + mv = create_mv(ptr, n) -# return mv + return mv -# def polyfill_geojson(geojson, int res): -# """ Set of hexagons whose center is contained in a GeoJson Polygon object. +def polyfill_geojson(geojson, int res): + """ Set of hexagons whose center is contained in a GeoJson Polygon object. -# The polygon is defined exactly as in the GeoJson standard, so -# `geojson` should be a dictionary like: -# { -# 'type': 'Polygon', -# 'coordinates': [...] -# } + The polygon is defined exactly as in the GeoJson standard, so + `geojson` should be a dictionary like: + { + 'type': 'Polygon', + 'coordinates': [...] + } -# 'coordinates' should be a list of LinearRings, where the first ring describes -# the exterior boundary of the Polygon, and any subsequent LinearRings -# describe holes in the polygon. + 'coordinates' should be a list of LinearRings, where the first ring describes + the exterior boundary of the Polygon, and any subsequent LinearRings + describe holes in the polygon. -# Note that we don't provide an option for the order of the coordinates, -# as the GeoJson standard requires them to be in lng/lat order. + Note that we don't provide an option for the order of the coordinates, + as the GeoJson standard requires them to be in lng/lat order. -# Parameters -# ---------- -# geojson : dict -# res : int -# The resolution of the output hexagons -# """ + Parameters + ---------- + geojson : dict + res : int + The resolution of the output hexagons + """ -# # todo: this one could handle multipolygons... + # todo: this one could handle multipolygons... -# if geojson['type'] != 'Polygon': -# raise ValueError('Only Polygon GeoJSON supported') + if geojson['type'] != 'Polygon': + raise ValueError('Only Polygon GeoJSON supported') -# coords = geojson['coordinates'] + coords = geojson['coordinates'] -# out = polyfill_polygon(coords[0], res, holes=coords[1:], lnglat_order=True) + out = polyfill_polygon(coords[0], res, holes=coords[1:], lnglat_order=True) -# return out + return out -# def polyfill(dict geojson, int res, bool geo_json_conformant=False): -# """ Light wrapper around `polyfill_geojson` to provide backward compatibility. -# """ +def polyfill(dict geojson, int res, bool geo_json_conformant=False): + """ Light wrapper around `polyfill_geojson` to provide backward compatibility. + """ -# try: -# gj_type = geojson['type'] -# except KeyError: -# raise KeyError("`geojson` dict must have key 'type'.") from None + try: + gj_type = geojson['type'] + except KeyError: + raise KeyError("`geojson` dict must have key 'type'.") from None -# if gj_type != 'Polygon': -# raise ValueError('Only Polygon GeoJSON supported') + if gj_type != 'Polygon': + raise ValueError('Only Polygon GeoJSON supported') -# if geo_json_conformant: -# out = polyfill_geojson(geojson, res) -# else: -# coords = geojson['coordinates'] -# out = polyfill_polygon(coords[0], res, holes=coords[1:], lnglat_order=False) + if geo_json_conformant: + out = polyfill_geojson(geojson, res) + else: + coords = geojson['coordinates'] + out = polyfill_polygon(coords[0], res, holes=coords[1:], lnglat_order=False) -# return out + return out # def cell_boundary(H3int h, bool geo_json=False): diff --git a/src/h3/_cy/h3lib.pxd b/src/h3/_cy/h3lib.pxd index aa84b2b..440391a 100644 --- a/src/h3/_cy/h3lib.pxd +++ b/src/h3/_cy/h3lib.pxd @@ -20,6 +20,15 @@ cdef extern from "h3api.h": int i int j + ctypedef struct GeoLoop: + int numVerts + LatLng *verts + + ctypedef struct GeoPolygon: + GeoLoop geoloop + int numHoles + GeoLoop *holes + int isValidCell(H3Index h) nogil int isPentagon(H3Index h) nogil int isResClassIII(H3Index h) nogil @@ -88,19 +97,13 @@ cdef extern from "h3api.h": H3Error gridDiskDistances(H3Index origin, int k, H3Index *out, int *distances) nogil H3Error gridRingUnsafe(H3Index origin, int k, H3Index *out) nogil + H3Error maxPolygonToCellsSize(const GeoPolygon *geoPolygon, int res, uint32_t flags, uint64_t *count) + H3Error polygonToCells(const GeoPolygon *geoPolygon, int res, uint32_t flags, H3Index *out) + # ctypedef struct GeoBoundary: # int num_verts "numVerts" # GeoCoord verts[10] # MAX_CELL_BNDRY_VERTS - # ctypedef struct Geofence: - # int numVerts - # GeoCoord *verts - - # ctypedef struct GeoPolygon: - # Geofence geofence - # int numHoles - # Geofence *holes - # ctypedef struct GeoMultiPolygon: # int numPolygons # GeoPolygon *polygons @@ -128,10 +131,6 @@ cdef extern from "h3api.h": # int hexRanges(H3Index *h3Set, int length, int k, H3Index *out) - # int maxPolyfillSize(const GeoPolygon *geoPolygon, int res) - - # void polyfill(const GeoPolygon *geoPolygon, int res, H3Index *out) - # void h3SetToLinkedGeo(const H3Index *h3Set, const int numHexes, LinkedGeoPolygon *out) # void destroyLinkedPolygon(LinkedGeoPolygon *polygon) From dc9c011dde04df701fe22df429e16d9980fd146b Mon Sep 17 00:00:00 2001 From: Isaac Brodsky Date: Wed, 1 Jun 2022 14:42:02 -0700 Subject: [PATCH 08/60] v4: geo boundary, set to multi polygon (#255) * v4: cell boundary * v4: h3_set_to_multi_polygon --- src/h3/_cy/__init__.py | 14 +---- src/h3/_cy/geo.pxd | 6 +-- src/h3/_cy/geo.pyx | 92 +++++++++++++++---------------- src/h3/_cy/h3lib.pxd | 57 ++++++++++---------- src/h3/_cy/to_multipoly.pyx | 104 ++++++++++++++++++------------------ 5 files changed, 132 insertions(+), 141 deletions(-) diff --git a/src/h3/_cy/__init__.py b/src/h3/_cy/__init__.py index db15af6..c684997 100644 --- a/src/h3/_cy/__init__.py +++ b/src/h3/_cy/__init__.py @@ -57,24 +57,12 @@ from .geo import ( polyfill_polygon, polyfill_geojson, polyfill, - # cell_boundary, - # edge_boundary, - # point_dist, -) - -from h3fake2._cy import ( - # geo_to_h3, - # h3_to_geo, - # polyfill_polygon, - # polyfill_geojson, - # polyfill, cell_boundary, edge_boundary, point_dist, ) -# from .to_multipoly import ( -from h3fake2._cy import ( +from .to_multipoly import ( h3_set_to_multi_polygon ) diff --git a/src/h3/_cy/geo.pxd b/src/h3/_cy/geo.pxd index 37e32a0..1a2c1c3 100644 --- a/src/h3/_cy/geo.pxd +++ b/src/h3/_cy/geo.pxd @@ -2,6 +2,6 @@ from .h3lib cimport H3int cpdef H3int geo_to_h3(double lat, double lng, int res) except 1 cpdef (double, double) h3_to_geo(H3int h) except * -# cpdef double point_dist( -# double lat1, double lng1, -# double lat2, double lng2, unit=*) except -1 +cpdef double point_dist( + double lat1, double lng1, + double lat2, double lng2, unit=*) except -1 diff --git a/src/h3/_cy/geo.pyx b/src/h3/_cy/geo.pyx index 870594b..7a737e2 100644 --- a/src/h3/_cy/geo.pyx +++ b/src/h3/_cy/geo.pyx @@ -221,66 +221,66 @@ def polyfill(dict geojson, int res, bool geo_json_conformant=False): return out -# def cell_boundary(H3int h, bool geo_json=False): -# """Compose an array of geo-coordinates that outlines a hexagonal cell""" -# cdef: -# h3lib.GeoBoundary gb +def cell_boundary(H3int h, bool geo_json=False): + """Compose an array of geo-coordinates that outlines a hexagonal cell""" + cdef: + h3lib.CellBoundary gb -# check_cell(h) + check_cell(h) -# h3lib.h3ToGeoBoundary(h, &gb) + h3lib.cellToBoundary(h, &gb) -# verts = tuple( -# coord2deg(gb.verts[i]) -# for i in range(gb.num_verts) -# ) + verts = tuple( + coord2deg(gb.verts[i]) + for i in range(gb.num_verts) + ) -# if geo_json: -# #lat/lng -> lng/lat and last point same as first -# verts += (verts[0],) -# verts = tuple(v[::-1] for v in verts) + if geo_json: + #lat/lng -> lng/lat and last point same as first + verts += (verts[0],) + verts = tuple(v[::-1] for v in verts) -# return verts + return verts -# def edge_boundary(H3int edge, bool geo_json=False): -# """ Returns the GeoBoundary containing the coordinates of the edge -# """ -# cdef: -# h3lib.GeoBoundary gb +def edge_boundary(H3int edge, bool geo_json=False): + """ Returns the CellBoundary containing the coordinates of the edge + """ + cdef: + h3lib.CellBoundary gb -# check_edge(edge) + check_edge(edge) -# h3lib.getH3UnidirectionalEdgeBoundary(edge, &gb) + h3lib.directedEdgeToBoundary(edge, &gb) -# # todo: move this verts transform into the GeoBoundary object -# verts = tuple( -# coord2deg(gb.verts[i]) -# for i in range(gb.num_verts) -# ) + # todo: move this verts transform into the CellBoundary object + verts = tuple( + coord2deg(gb.verts[i]) + for i in range(gb.num_verts) + ) -# if geo_json: -# #lat/lng -> lng/lat and last point same as first -# verts += (verts[0],) -# verts = tuple(v[::-1] for v in verts) + if geo_json: + #lat/lng -> lng/lat and last point same as first + verts += (verts[0],) + verts = tuple(v[::-1] for v in verts) -# return verts + return verts -# cpdef double point_dist( -# double lat1, double lng1, -# double lat2, double lng2, unit='km') except -1: +cpdef double point_dist( + double lat1, double lng1, + double lat2, double lng2, unit='km') except -1: -# a = deg2coord(lat1, lng1) -# b = deg2coord(lat2, lng2) + a = deg2coord(lat1, lng1) + b = deg2coord(lat2, lng2) -# if unit == 'rads': -# d = h3lib.pointDistRads(&a, &b) -# elif unit == 'km': -# d = h3lib.pointDistKm(&a, &b) -# elif unit == 'm': -# d = h3lib.pointDistM(&a, &b) -# else: -# raise H3ValueError('Unknown unit: {}'.format(unit)) + if unit == 'rads': + d = h3lib.distanceRads(&a, &b) + elif unit == 'km': + d = h3lib.distanceKm(&a, &b) + elif unit == 'm': + d = h3lib.distanceM(&a, &b) + else: + raise H3ValueError('Unknown unit: {}'.format(unit)) -# return d + return d diff --git a/src/h3/_cy/h3lib.pxd b/src/h3/_cy/h3lib.pxd index 440391a..c3efea8 100644 --- a/src/h3/_cy/h3lib.pxd +++ b/src/h3/_cy/h3lib.pxd @@ -16,10 +16,29 @@ cdef extern from "h3api.h": double lat # in radians double lng # in radians + ctypedef struct CellBoundary: + int num_verts "numVerts" + LatLng verts[10] # MAX_CELL_BNDRY_VERTS + ctypedef struct CoordIJ: int i int j + ctypedef struct LinkedLatLng: + LatLng data "vertex" + LinkedLatLng *next + + # renaming these for clarity + ctypedef struct LinkedGeoLoop: + LinkedLatLng *data "first" + LinkedLatLng *_data_last "last" # not needed in Cython bindings + LinkedGeoLoop *next + + ctypedef struct LinkedGeoPolygon: + LinkedGeoLoop *data "first" + LinkedGeoLoop *_data_last "last" # not needed in Cython bindings + LinkedGeoPolygon *next + ctypedef struct GeoLoop: int numVerts LatLng *verts @@ -97,34 +116,24 @@ cdef extern from "h3api.h": H3Error gridDiskDistances(H3Index origin, int k, H3Index *out, int *distances) nogil H3Error gridRingUnsafe(H3Index origin, int k, H3Index *out) nogil + H3Error cellToBoundary(H3Index h3, CellBoundary *gp) nogil + + H3Error directedEdgeToBoundary(H3Index edge, CellBoundary *gb) nogil + + double distanceRads(const LatLng *a, const LatLng *b) nogil + double distanceKm(const LatLng *a, const LatLng *b) nogil + double distanceM(const LatLng *a, const LatLng *b) nogil + + H3Error cellsToLinkedMultiPolygon(const H3Index *h3Set, const int numHexes, LinkedGeoPolygon *out) + void destroyLinkedMultiPolygon(LinkedGeoPolygon *polygon) + H3Error maxPolygonToCellsSize(const GeoPolygon *geoPolygon, int res, uint32_t flags, uint64_t *count) H3Error polygonToCells(const GeoPolygon *geoPolygon, int res, uint32_t flags, H3Index *out) - # ctypedef struct GeoBoundary: - # int num_verts "numVerts" - # GeoCoord verts[10] # MAX_CELL_BNDRY_VERTS - # ctypedef struct GeoMultiPolygon: # int numPolygons # GeoPolygon *polygons - # ctypedef struct LinkedGeoCoord: - # GeoCoord data "vertex" - # LinkedGeoCoord *next - - # # renaming these for clarity - # ctypedef struct LinkedGeoLoop: - # LinkedGeoCoord *data "first" - # LinkedGeoCoord *_data_last "last" # not needed in Cython bindings - # LinkedGeoLoop *next - - # ctypedef struct LinkedGeoPolygon: - # LinkedGeoLoop *data "first" - # LinkedGeoLoop *_data_last "last" # not needed in Cython bindings - # LinkedGeoPolygon *next - - # void h3ToGeoBoundary(H3Index h3, GeoBoundary *gp) - # int hexRange(H3Index origin, int k, H3Index *out) # int hexRangeDistances(H3Index origin, int k, H3Index *out, int *distances) @@ -151,15 +160,9 @@ cdef extern from "h3api.h": # void getH3UnidirectionalEdgesFromHexagon(H3Index origin, H3Index *edges) - # void getH3UnidirectionalEdgeBoundary(H3Index edge, GeoBoundary *gb) - # double edgeLengthKm(int res) nogil # double edgeLengthM(int res) nogil # double exactEdgeLengthRads(H3Index edge) nogil # double exactEdgeLengthKm(H3Index edge) nogil # double exactEdgeLengthM(H3Index edge) nogil - - # double pointDistRads(const GeoCoord *a, const GeoCoord *b) nogil - # double pointDistKm(const GeoCoord *a, const GeoCoord *b) nogil - # double pointDistM(const GeoCoord *a, const GeoCoord *b) nogil diff --git a/src/h3/_cy/to_multipoly.pyx b/src/h3/_cy/to_multipoly.pyx index 296a932..7a421f7 100644 --- a/src/h3/_cy/to_multipoly.pyx +++ b/src/h3/_cy/to_multipoly.pyx @@ -1,75 +1,75 @@ -# cimport h3lib -# from h3lib cimport H3int -# from .util cimport check_cell, coord2deg +cimport h3lib +from h3lib cimport H3int +from .util cimport check_cell, coord2deg -# # todo: it's driving me crazy that these three functions are all essentially the same linked list walker... -# # grumble: no way to do iterators in with cdef functions! -# cdef walk_polys(const h3lib.LinkedGeoPolygon* L): -# out = [] -# while L: -# out += [walk_loops(L.data)] -# L = L.next +# todo: it's driving me crazy that these three functions are all essentially the same linked list walker... +# grumble: no way to do iterators in with cdef functions! +cdef walk_polys(const h3lib.LinkedGeoPolygon* L): + out = [] + while L: + out += [walk_loops(L.data)] + L = L.next -# return out + return out -# cdef walk_loops(const h3lib.LinkedGeoLoop* L): -# out = [] -# while L: -# out += [walk_coords(L.data)] -# L = L.next +cdef walk_loops(const h3lib.LinkedGeoLoop* L): + out = [] + while L: + out += [walk_coords(L.data)] + L = L.next -# return out + return out -# cdef walk_coords(const h3lib.LinkedGeoCoord* L): -# out = [] -# while L: -# out += [coord2deg(L.data)] -# L = L.next +cdef walk_coords(const h3lib.LinkedLatLng* L): + out = [] + while L: + out += [coord2deg(L.data)] + L = L.next -# return out + return out -# # todo: tuples instead of lists? -# def _to_multi_polygon(const H3int[:] hexes): -# cdef: -# h3lib.LinkedGeoPolygon polygon +# todo: tuples instead of lists? +def _to_multi_polygon(const H3int[:] hexes): + cdef: + h3lib.LinkedGeoPolygon polygon -# for h in hexes: -# check_cell(h) + for h in hexes: + check_cell(h) -# h3lib.h3SetToLinkedGeo(&hexes[0], len(hexes), &polygon) + h3lib.cellsToLinkedMultiPolygon(&hexes[0], len(hexes), &polygon) -# out = walk_polys(&polygon) + out = walk_polys(&polygon) -# # we're still responsible for cleaning up the passed in `polygon`, -# # but not a problem here, since it is stack allocated -# h3lib.destroyLinkedPolygon(&polygon) + # we're still responsible for cleaning up the passed in `polygon`, + # but not a problem here, since it is stack allocated + h3lib.destroyLinkedMultiPolygon(&polygon) -# return out + return out -# def _geojson_loop(loop): -# """ Swap lat/lng order and close loop. -# """ -# loop = [e[::-1] for e in loop] -# loop += [loop[0]] +def _geojson_loop(loop): + """ Swap lat/lng order and close loop. + """ + loop = [e[::-1] for e in loop] + loop += [loop[0]] -# return loop + return loop -# def h3_set_to_multi_polygon(const H3int[:] hexes, geo_json=False): -# # todo: gotta be a more elegant way to handle these... -# if len(hexes) == 0: -# return [] +def h3_set_to_multi_polygon(const H3int[:] hexes, geo_json=False): + # todo: gotta be a more elegant way to handle these... + if len(hexes) == 0: + return [] -# multipoly = _to_multi_polygon(hexes) + multipoly = _to_multi_polygon(hexes) -# if geo_json: -# multipoly = [ -# [_geojson_loop(loop) for loop in poly] -# for poly in multipoly -# ] + if geo_json: + multipoly = [ + [_geojson_loop(loop) for loop in poly] + for poly in multipoly + ] -# return multipoly + return multipoly From 75fb4c7046a3b14bf6b413dc6a0709579189c1e9 Mon Sep 17 00:00:00 2001 From: Isaac Brodsky Date: Thu, 2 Jun 2022 15:44:24 -0700 Subject: [PATCH 09/60] v4: convert edge from h3fake2 (#253) * v4: convert edge from h3fake2 * on error not neighbors * except * * check for neighbor error --- src/h3/_cy/__init__.py | 3 +- src/h3/_cy/edges.pxd | 20 ++--- src/h3/_cy/edges.pyx | 165 ++++++++++++++++++++++++----------------- src/h3/_cy/h3lib.pxd | 31 ++++---- 4 files changed, 119 insertions(+), 100 deletions(-) diff --git a/src/h3/_cy/__init__.py b/src/h3/_cy/__init__.py index c684997..bb4dfc0 100644 --- a/src/h3/_cy/__init__.py +++ b/src/h3/_cy/__init__.py @@ -38,8 +38,7 @@ from .cells import ( experimental_local_ij_to_h3, ) -# from .edges import ( -from h3fake2._cy import ( +from .edges import ( are_neighbors, edge, is_edge, diff --git a/src/h3/_cy/edges.pxd b/src/h3/_cy/edges.pxd index cdcf4d0..bf4c5c4 100644 --- a/src/h3/_cy/edges.pxd +++ b/src/h3/_cy/edges.pxd @@ -1,11 +1,11 @@ -# from .h3lib cimport bool, H3int +from .h3lib cimport bool, H3int -# cpdef bool are_neighbors(H3int h1, H3int h2) -# cpdef H3int edge(H3int origin, H3int destination) except 1 -# cpdef bool is_edge(H3int e) -# cpdef H3int edge_origin(H3int e) except 1 -# cpdef H3int edge_destination(H3int e) except 1 -# cpdef (H3int, H3int) edge_cells(H3int e) except * -# cpdef H3int[:] edges_from_cell(H3int origin) -# cpdef double mean_edge_length(int resolution, unit=*) except -1 -# cpdef double edge_length(H3int e, unit=*) except -1 +cpdef bool are_neighbors(H3int h1, H3int h2) +cpdef H3int edge(H3int origin, H3int destination) except * +cpdef bool is_edge(H3int e) +cpdef H3int edge_origin(H3int e) except 1 +cpdef H3int edge_destination(H3int e) except 1 +cpdef (H3int, H3int) edge_cells(H3int e) except * +cpdef H3int[:] edges_from_cell(H3int origin) +cpdef double mean_edge_length(int resolution, unit=*) except -1 +cpdef double edge_length(H3int e, unit=*) except -1 diff --git a/src/h3/_cy/edges.pyx b/src/h3/_cy/edges.pyx index 27397d9..716c96a 100644 --- a/src/h3/_cy/edges.pyx +++ b/src/h3/_cy/edges.pyx @@ -1,100 +1,125 @@ -# cimport h3lib -# from .h3lib cimport bool, H3int +cimport h3lib +from .h3lib cimport bool, H3int -# from .util cimport ( -# check_cell, -# check_edge, -# check_res, -# create_ptr, -# create_mv, -# ) +from .util cimport ( + check_cell, + check_edge, + check_res, + create_ptr, + create_mv, +) -# from .util import H3ValueError +from .util import H3ValueError -# cpdef bool are_neighbors(H3int h1, H3int h2): -# check_cell(h1) -# check_cell(h2) +cpdef bool are_neighbors(H3int h1, H3int h2): + cdef: + int out -# return h3lib.h3IndexesAreNeighbors(h1, h2) == 1 + check_cell(h1) + check_cell(h2) + + error = h3lib.areNeighborCells(h1, h2, &out) + if error != 0: + return False + return out == 1 -# cpdef H3int edge(H3int origin, H3int destination) except 1: -# check_cell(origin) -# check_cell(destination) +cpdef H3int edge(H3int origin, H3int destination) except *: + cdef: + int neighbor_out + H3int out -# if h3lib.h3IndexesAreNeighbors(origin, destination) != 1: -# s = 'Cells are not neighbors: {} and {}' -# s = s.format(hex(origin), hex(destination)) -# raise H3ValueError(s) + check_cell(origin) + check_cell(destination) -# return h3lib.getH3UnidirectionalEdge(origin, destination) + error = h3lib.areNeighborCells(origin, destination, &neighbor_out) + if error != 0 or neighbor_out != 1: + s = 'Cells are not neighbors: {} and {}' + s = s.format(hex(origin), hex(destination)) + raise H3ValueError(s) + + h3lib.cellsToDirectedEdge(origin, destination, &out) + return out -# cpdef bool is_edge(H3int e): -# return h3lib.h3UnidirectionalEdgeIsValid(e) == 1 +cpdef bool is_edge(H3int e): + return h3lib.isValidDirectedEdge(e) == 1 -# cpdef H3int edge_origin(H3int e) except 1: -# # without the check, with an invalid input, the function will just return 0 -# check_edge(e) +cpdef H3int edge_origin(H3int e) except 1: + cdef: + H3int out -# return h3lib.getOriginH3IndexFromUnidirectionalEdge(e) + # without the check, with an invalid input, the function will just return 0 + check_edge(e) -# cpdef H3int edge_destination(H3int e) except 1: -# check_edge(e) + h3lib.getDirectedEdgeOrigin(e, &out) + return out -# return h3lib.getDestinationH3IndexFromUnidirectionalEdge(e) +cpdef H3int edge_destination(H3int e) except 1: + cdef: + H3int out -# cpdef (H3int, H3int) edge_cells(H3int e) except *: -# check_edge(e) + check_edge(e) -# return edge_origin(e), edge_destination(e) + h3lib.getDirectedEdgeDestination(e, &out) + return out -# cpdef H3int[:] edges_from_cell(H3int origin): -# """ Returns the 6 (or 5 for pentagons) directed edges -# for the given origin cell -# """ -# check_cell(origin) +cpdef (H3int, H3int) edge_cells(H3int e) except *: + check_edge(e) -# ptr = create_ptr(6) -# h3lib.getH3UnidirectionalEdgesFromHexagon(origin, ptr) -# mv = create_mv(ptr, 6) + return edge_origin(e), edge_destination(e) -# return mv +cpdef H3int[:] edges_from_cell(H3int origin): + """ Returns the 6 (or 5 for pentagons) directed edges + for the given origin cell + """ + check_cell(origin) + + ptr = create_ptr(6) + h3lib.originToDirectedEdges(origin, ptr) + mv = create_mv(ptr, 6) + + return mv -# cpdef double mean_edge_length(int resolution, unit='km') except -1: -# check_res(resolution) +cpdef double mean_edge_length(int resolution, unit='km') except -1: + cdef: + double length -# length = h3lib.edgeLengthKm(resolution) + check_res(resolution) -# # todo: multiple units -# convert = { -# 'km': 1.0, -# 'm': 1000.0 -# } + h3lib.getHexagonEdgeLengthAvgKm(resolution, &length) -# try: -# length *= convert[unit] -# except: -# raise H3ValueError('Unknown unit: {}'.format(unit)) + # todo: multiple units + convert = { + 'km': 1.0, + 'm': 1000.0 + } -# return length + try: + length *= convert[unit] + except: + raise H3ValueError('Unknown unit: {}'.format(unit)) + + return length -# cpdef double edge_length(H3int e, unit='km') except -1: -# check_edge(e) +cpdef double edge_length(H3int e, unit='km') except -1: + cdef: + double length + check_edge(e) -# # todo: maybe kick this logic up to the python level -# # it might be a little cleaner, because we can do the "switch statement" -# # with a dict, but would require exposing more C functions + # todo: maybe kick this logic up to the python level + # it might be a little cleaner, because we can do the "switch statement" + # with a dict, but would require exposing more C functions -# if unit == 'rads': -# length = h3lib.exactEdgeLengthRads(e) -# elif unit == 'km': -# length = h3lib.exactEdgeLengthKm(e) -# elif unit == 'm': -# length = h3lib.exactEdgeLengthM(e) -# else: -# raise H3ValueError('Unknown unit: {}'.format(unit)) + if unit == 'rads': + h3lib.exactEdgeLengthRads(e, &length) + elif unit == 'km': + h3lib.exactEdgeLengthKm(e, &length) + elif unit == 'm': + h3lib.exactEdgeLengthM(e, &length) + else: + raise H3ValueError('Unknown unit: {}'.format(unit)) -# return length + return length diff --git a/src/h3/_cy/h3lib.pxd b/src/h3/_cy/h3lib.pxd index c3efea8..ba8a4e2 100644 --- a/src/h3/_cy/h3lib.pxd +++ b/src/h3/_cy/h3lib.pxd @@ -116,8 +116,20 @@ cdef extern from "h3api.h": H3Error gridDiskDistances(H3Index origin, int k, H3Index *out, int *distances) nogil H3Error gridRingUnsafe(H3Index origin, int k, H3Index *out) nogil - H3Error cellToBoundary(H3Index h3, CellBoundary *gp) nogil + H3Error areNeighborCells(H3Index origin, H3Index destination, int *out) nogil + H3Error cellsToDirectedEdge(H3Index origin, H3Index destination, H3Index *out) nogil + H3Error getDirectedEdgeOrigin(H3Index edge, H3Index *out) nogil + H3Error getDirectedEdgeDestination(H3Index edge, H3Index *out) nogil + H3Error originToDirectedEdges(H3Index origin, H3Index *edges) nogil + H3Error getHexagonEdgeLengthAvgKm(int res, double *out) nogil + H3Error getHexagonEdgeLengthAvgM(int res, double *out) nogil + + H3Error exactEdgeLengthRads(H3Index edge, double *out) nogil + H3Error exactEdgeLengthKm(H3Index edge, double *out) nogil + H3Error exactEdgeLengthM(H3Index edge, double *out) nogil + + H3Error cellToBoundary(H3Index h3, CellBoundary *gp) nogil H3Error directedEdgeToBoundary(H3Index edge, CellBoundary *gb) nogil double distanceRads(const LatLng *a, const LatLng *b) nogil @@ -148,21 +160,4 @@ cdef extern from "h3api.h": # void h3ToString(H3Index h, char *str, size_t sz) - # int h3IndexesAreNeighbors(H3Index origin, H3Index destination) - - # H3Index getH3UnidirectionalEdge(H3Index origin, H3Index destination) - - # H3Index getOriginH3IndexFromUnidirectionalEdge(H3Index edge) - - # H3Index getDestinationH3IndexFromUnidirectionalEdge(H3Index edge) - # void getH3IndexesFromUnidirectionalEdge(H3Index edge, H3Index *originDestination) - - # void getH3UnidirectionalEdgesFromHexagon(H3Index origin, H3Index *edges) - - # double edgeLengthKm(int res) nogil - # double edgeLengthM(int res) nogil - - # double exactEdgeLengthRads(H3Index edge) nogil - # double exactEdgeLengthKm(H3Index edge) nogil - # double exactEdgeLengthM(H3Index edge) nogil From 075da765394683988610a6fb02415acf24e5f490 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Thu, 2 Jun 2022 20:10:41 -0400 Subject: [PATCH 10/60] Fix sdist build (#239) * make sure `make_sdist` CI job still broken * reproduce locally * update h3lib * still seems broken * try sdist on mac and ubuntu * pyest! * debugging * ensurepath * Disable building docs * just build sdist once * colon Co-authored-by: Isaac Brodsky --- .github/workflows/wheels.yml | 58 ++++++++++++++++++++---------------- CMakeLists.txt | 1 + makefile | 1 - src/h3lib | 2 +- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index dd6bfbc..a6607e4 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -18,34 +18,43 @@ on: - published jobs: - # make_sdist: - # name: SDist - # if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.draft }} - # runs-on: ubuntu-latest + make_sdist: + name: "SDist: ${{ matrix.os }}" + if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.draft }} + runs-on: ${{ matrix.os }} - # steps: - # - uses: actions/checkout@v3 - # with: - # submodules: recursive + strategy: + matrix: + os: [ubuntu-latest] - # - name: Make sdist - # run: | - # pipx run build --sdist + steps: + - uses: actions/checkout@v3 + with: + submodules: recursive - # - name: Install from sdist - # run: | - # pip install --upgrade pip setuptools wheel - # pip install git+https://github.com/ajfriend/h3fake2.git - # cp dist/h3-*.tar.gz h3.tar.gz - # pip install h3.tar.gz[all] + - name: Setup Python + uses: actions/setup-python@v3.1.2 - # - name: Test sdist - # run: pytest --cov=h3 --full-trace + - name: Make sdist + run: | + python --version + pipx --version + pipx run build --sdist - # - name: Upload artifacts to GitHub - # uses: actions/upload-artifact@v3 - # with: - # path: ./dist + - name: Install from sdist + run: | + pip install --upgrade pip setuptools wheel + pip install git+https://github.com/ajfriend/h3fake2.git pytest + cp dist/h3-*.tar.gz h3.tar.gz + pip install h3.tar.gz[all] + + - name: Test sdist + run: pytest --cov=h3 --full-trace + + - name: Upload artifacts to GitHub + uses: actions/upload-artifact@v3 + with: + path: ./dist make_cibw_v1_wheels: name: "cibuildwheel v1: ${{ matrix.name }}" @@ -168,8 +177,7 @@ jobs: path: wheelhouse/*.whl upload_all: - # needs: [make_sdist, make_cibw_v1_wheels, make_cibw_v2_wheels] - needs: [make_cibw_v1_wheels, make_cibw_v2_wheels] + needs: [make_sdist, make_cibw_v1_wheels, make_cibw_v2_wheels] runs-on: ubuntu-latest if: github.event_name == 'release' && github.event.action == 'published' diff --git a/CMakeLists.txt b/CMakeLists.txt index 00d9fe4..436ea65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ set(BUILD_BENCHMARKS OFF CACHE BOOL "" FORCE) set(BUILD_FILTERS OFF CACHE BOOL "" FORCE) set(BUILD_GENERATORS OFF CACHE BOOL "" FORCE) set(BUILD_TESTING OFF CACHE BOOL "" FORCE) +set(ENABLE_DOCS OFF CACHE BOOL "" FORCE) # Build the core library as static set(BUILD_SHARED_LIBS OFF) diff --git a/makefile b/makefile index 7c2845e..ac2e463 100644 --- a/makefile +++ b/makefile @@ -7,7 +7,6 @@ build-docs: open: open docs/_build/html/index.html - init: purge git submodule update --init python -m venv env diff --git a/src/h3lib b/src/h3lib index 294c20e..a36c614 160000 --- a/src/h3lib +++ b/src/h3lib @@ -1 +1 @@ -Subproject commit 294c20ed7d447122d5ab37260896d4ef7579ebfb +Subproject commit a36c614b477fb5409a0a87e0111d347ce550e40d From cfbd892780978dfba70747465069c0b28ca3f22f Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Thu, 2 Jun 2022 21:11:17 -0400 Subject: [PATCH 11/60] remove h3fake2 transitional library (#257) --- .github/workflows/coverage-lint.yml | 1 - .github/workflows/tests.yml | 1 - .github/workflows/wheels.yml | 6 +----- makefile | 1 - tests/h3fake2_errors.py | 15 --------------- tests/test_cells_and_edges.py | 2 +- tests/test_length_area.py | 2 +- tests/test_polyfill.py | 2 +- 8 files changed, 4 insertions(+), 26 deletions(-) delete mode 100644 tests/h3fake2_errors.py diff --git a/.github/workflows/coverage-lint.yml b/.github/workflows/coverage-lint.yml index 5110236..6d33368 100644 --- a/.github/workflows/coverage-lint.yml +++ b/.github/workflows/coverage-lint.yml @@ -23,7 +23,6 @@ jobs: - name: Install from source run: | pip install --upgrade pip setuptools wheel - pip install git+https://github.com/ajfriend/h3fake2.git pip install .[all] - name: Lint diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0b9883d..072e21c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -44,7 +44,6 @@ jobs: - name: Install from source run: | pip install --upgrade pip setuptools wheel - pip install git+https://github.com/ajfriend/h3fake2.git pip install .[all] - name: Tests diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index a6607e4..a02f51d 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -44,7 +44,7 @@ jobs: - name: Install from sdist run: | pip install --upgrade pip setuptools wheel - pip install git+https://github.com/ajfriend/h3fake2.git pytest + pip install pytest cp dist/h3-*.tar.gz h3.tar.gz pip install h3.tar.gz[all] @@ -84,8 +84,6 @@ jobs: CIBW_TEST_COMMAND: pytest {project}/tests CIBW_ARCHS_LINUX: auto aarch64 CIBW_BUILD: ${{ matrix.build }} - CIBW_BEFORE_BUILD: pip install git+https://github.com/ajfriend/h3fake2.git - CIBW_BEFORE_TEST: pip install git+https://github.com/ajfriend/h3fake2.git - name: Check with Twine run: | @@ -164,8 +162,6 @@ jobs: CIBW_ARCHS_MACOS: x86_64 arm64 CIBW_BUILD: ${{ matrix.build }} CIBW_SKIP: ${{ matrix.skip }} - CIBW_BEFORE_BUILD: pip install git+https://github.com/ajfriend/h3fake2.git - CIBW_BEFORE_TEST: pip install git+https://github.com/ajfriend/h3fake2.git - name: Check with Twine run: | diff --git a/makefile b/makefile index ac2e463..44eceb8 100644 --- a/makefile +++ b/makefile @@ -11,7 +11,6 @@ init: purge git submodule update --init python -m venv env env/bin/pip install --upgrade pip wheel setuptools - env/bin/pip install git+https://github.com/ajfriend/h3fake2.git env/bin/pip install .[all] env/bin/pip install -r requirements.in diff --git a/tests/h3fake2_errors.py b/tests/h3fake2_errors.py deleted file mode 100644 index da56887..0000000 --- a/tests/h3fake2_errors.py +++ /dev/null @@ -1,15 +0,0 @@ -# flake8: noqa -""" -Some of the tests expect to catch certain errors. -As we transition, we'll have errors of each type. -pytest functions can accept tuples of errors to check for either one. -""" - -import h3 -import h3fake2 - -H3ValueError = (h3.H3ValueError, h3fake2.H3ValueError) -H3CellError = (h3.H3CellError, h3fake2.H3CellError) -H3ResolutionError = (h3.H3ResolutionError, h3fake2.H3ResolutionError) -H3EdgeError = (h3.H3EdgeError, h3fake2.H3EdgeError) -H3DistanceError = (h3.H3DistanceError, h3fake2.H3DistanceError) diff --git a/tests/test_cells_and_edges.py b/tests/test_cells_and_edges.py index 851dda5..a90bb0a 100644 --- a/tests/test_cells_and_edges.py +++ b/tests/test_cells_and_edges.py @@ -1,7 +1,7 @@ import h3 import pytest -from .h3fake2_errors import ( +from h3 import ( H3ValueError, H3CellError, H3ResolutionError, diff --git a/tests/test_length_area.py b/tests/test_length_area.py index 2a1b5d7..8236199 100644 --- a/tests/test_length_area.py +++ b/tests/test_length_area.py @@ -1,7 +1,7 @@ import h3 import pytest -from .h3fake2_errors import H3ValueError +from h3 import H3ValueError def approx2(a, b): diff --git a/tests/test_polyfill.py b/tests/test_polyfill.py index a02560d..7c35bb6 100644 --- a/tests/test_polyfill.py +++ b/tests/test_polyfill.py @@ -2,7 +2,7 @@ import h3 import itertools import pytest -from .h3fake2_errors import H3ResolutionError +from h3 import H3ResolutionError def reverse(loop): From 572a09b08c5a7de5440346bebc79effcf0e0b36a Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 20:10:10 -0700 Subject: [PATCH 12/60] initial commit --- src/h3/_cy/CMakeLists.txt | 3 ++ src/h3/_cy/cells.pyx | 8 ++-- src/h3/_cy/memory.pxd | 14 ++++++ src/h3/_cy/memory.pyx | 99 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 src/h3/_cy/memory.pxd create mode 100644 src/h3/_cy/memory.pyx diff --git a/src/h3/_cy/CMakeLists.txt b/src/h3/_cy/CMakeLists.txt index 386557c..2103b48 100644 --- a/src/h3/_cy/CMakeLists.txt +++ b/src/h3/_cy/CMakeLists.txt @@ -22,6 +22,7 @@ add_cython_file(geo) add_cython_file(cells) add_cython_file(edges) add_cython_file(to_multipoly) +add_cython_file(memory) # Include pyx and pxd files in distribution for use by Cython API install( @@ -35,6 +36,8 @@ install( h3lib.pxd util.pxd util.pyx + memory.pxd + memory.pyx DESTINATION src/h3/_cy ) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 4453e38..c280a77 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -11,6 +11,8 @@ from .util cimport ( empty_memory_view, # want to drop this import if possible ) +from .memory cimport H3MemoryManager + from .util import H3ValueError, H3ResolutionError # todo: add notes about Cython exception handling @@ -78,9 +80,9 @@ cpdef H3int[:] disk(H3int h, int k): # ignoring error for now err = h3lib.maxGridDiskSize(k, &n) - ptr = create_ptr(n) # todo: return a "smart" pointer that knows its length? - err = h3lib.gridDisk(h, k, ptr) # ignoring error again! - mv = create_mv(ptr, n) + hmm = H3MemoryManager(n) + err = h3lib.gridDisk(h, k, hmm.get_ptr()) + mv = hmm.create_mv() return mv diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd new file mode 100644 index 0000000..8e058fd --- /dev/null +++ b/src/h3/_cy/memory.pxd @@ -0,0 +1,14 @@ +from .h3lib cimport H3int + +cdef class H3MemoryManager: + cdef: + size_t n + H3int* ptr + + cdef H3int* get_ptr(self) + cdef H3int[:] create_mv(self) + + +""" +todo: read: https://cython.readthedocs.io/en/latest/src/tutorial/pxd_files.html +""" diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx new file mode 100644 index 0000000..69fce9c --- /dev/null +++ b/src/h3/_cy/memory.pyx @@ -0,0 +1,99 @@ +from libc cimport stdlib +from cython.view cimport array +from .h3lib cimport H3int + + +cdef size_t move_nonzeros(H3int* a, size_t n): + """ Move nonzero elements to front of array `a` of length `n`. + Return the number of nonzero elements. + + Loop invariant: Everything *before* `i` or *after* `j` is "done". + Move `i` and `j` inwards until they equal, and exit. + You can move `i` forward until there's a zero in front of it. + You can move `j` backward until there's a nonzero to the left of it. + Anything to the right of `j` is "junk" that can be reallocated. + + | a | b | 0 | c | d | ... | + ^ ^ + i j + + + | a | b | d | c | d | ... | + ^ ^ + i j + """ + cdef: + size_t i = 0 + size_t j = n + + while i < j: + if a[j-1] == 0: + j -= 1 + continue + + if a[i] != 0: + i += 1 + continue + + # if we're here, we know: + # a[i] == 0 + # a[j-1] != 0 + # i < j + # so we can swap! (actually, move a[j-1] -> a[i]) + a[i] = a[j-1] + j -= 1 + + return i + + +cdef H3int[:] empty_memory_view(): + # there's gotta be a better way to do this... + # create an empty cython.view.array? + cdef: + H3int a[1] + + return (a)[:0] + + +cdef class H3MemoryManager: + def __cinit__(self, size_t n): + self.n = n + self.ptr = stdlib.calloc(self.n, sizeof(H3int)) + + if (self.n > 0) and (not self.ptr): + raise MemoryError() + + cdef H3int* get_ptr(self): + return self.ptr + + cdef H3int[:] create_mv(self): + cdef: + array x + + self.n = move_nonzeros(self.ptr, self.n) + if self.n <= 0: + stdlib.free(self.ptr) + return empty_memory_view() + + self.ptr = stdlib.realloc(self.ptr, self.n*sizeof(H3int)) + + if self.ptr is NULL: + raise MemoryError() + + x = self.ptr + x.callback_free_data = stdlib.free + + # responsibility for the memory moves from this object to the array/memoryview + self.ptr = NULL + self.n = 0 + + return x + + def __dealloc__(self): + # If the memory now belongs elsewhere with a memoryview, this pointer + # should be null, and deallocing on NULL is fine. + # If the pointer is not null, then this means the MemoryManager + # has not given the memory away to another object. + stdlib.free(self.ptr) + + From 34b7eed770b96e94158b527efef6eab362e16eb0 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 20:15:36 -0700 Subject: [PATCH 13/60] hmmmmmmm --- src/h3/_cy/cells.pyx | 2 +- src/h3/_cy/memory.pxd | 1 - src/h3/_cy/memory.pyx | 11 ++++------- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index c280a77..6cc4841 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -81,7 +81,7 @@ cpdef H3int[:] disk(H3int h, int k): err = h3lib.maxGridDiskSize(k, &n) hmm = H3MemoryManager(n) - err = h3lib.gridDisk(h, k, hmm.get_ptr()) + err = h3lib.gridDisk(h, k, hmm.ptr) mv = hmm.create_mv() return mv diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index 8e058fd..5dc5b2a 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -5,7 +5,6 @@ cdef class H3MemoryManager: size_t n H3int* ptr - cdef H3int* get_ptr(self) cdef H3int[:] create_mv(self) diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index 69fce9c..b051d73 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -63,9 +63,6 @@ cdef class H3MemoryManager: if (self.n > 0) and (not self.ptr): raise MemoryError() - cdef H3int* get_ptr(self): - return self.ptr - cdef H3int[:] create_mv(self): cdef: array x @@ -90,10 +87,10 @@ cdef class H3MemoryManager: return x def __dealloc__(self): - # If the memory now belongs elsewhere with a memoryview, this pointer - # should be null, and deallocing on NULL is fine. - # If the pointer is not null, then this means the MemoryManager - # has not given the memory away to another object. + # If the memory has been handed off to a memoryview, this pointer + # should be NULL, and deallocing on NULL is fine. + # If the pointer is *not* NULL, then this means the MemoryManager + # has is still responsible for the memory (it hasn't given the memory away to another object). stdlib.free(self.ptr) From 0daa79be3e20904a9bf9cccc4bfabe8037c160dd Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 20:27:15 -0700 Subject: [PATCH 14/60] H3Index -> H3int --- src/h3/_cy/h3lib.pxd | 105 +++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/src/h3/_cy/h3lib.pxd b/src/h3/_cy/h3lib.pxd index ba8a4e2..c3b9417 100644 --- a/src/h3/_cy/h3lib.pxd +++ b/src/h3/_cy/h3lib.pxd @@ -1,7 +1,6 @@ from cpython cimport bool from libc.stdint cimport uint64_t, int64_t, uint32_t -ctypedef uint64_t H3int ctypedef uint32_t H3Error ctypedef basestring H3str @@ -10,7 +9,7 @@ cdef extern from "h3api.h": cdef int H3_VERSION_MINOR cdef int H3_VERSION_PATCH - ctypedef uint64_t H3Index + ctypedef uint64_t H3int "H3Index" ctypedef struct LatLng: double lat # in radians @@ -48,45 +47,45 @@ cdef extern from "h3api.h": int numHoles GeoLoop *holes - int isValidCell(H3Index h) nogil - int isPentagon(H3Index h) nogil - int isResClassIII(H3Index h) nogil - int isValidDirectedEdge(H3Index edge) nogil + int isValidCell(H3int h) nogil + int isPentagon(H3int h) nogil + int isResClassIII(H3int h) nogil + int isValidDirectedEdge(H3int edge) nogil double degsToRads(double degrees) nogil double radsToDegs(double radians) nogil - int getResolution(H3Index h) nogil - int getBaseCellNumber(H3Index h) nogil + int getResolution(H3int h) nogil + int getBaseCellNumber(H3int h) nogil - H3Error latLngToCell(const LatLng *g, int res, H3Index *out) nogil - H3Error cellToLatLng(H3Index h, LatLng *) nogil - H3Error gridDistance(H3Index h1, H3Index h2, int64_t *distance) nogil + H3Error latLngToCell(const LatLng *g, int res, H3int *out) nogil + H3Error cellToLatLng(H3int h, LatLng *) nogil + H3Error gridDistance(H3int h1, H3int h2, int64_t *distance) nogil H3Error maxGridDiskSize(int k, int64_t *out) nogil # num/out/N? - H3Error gridDisk(H3Index h, int k, H3Index *out) nogil + H3Error gridDisk(H3int h, int k, H3int *out) nogil - H3Error cellToParent( H3Index h, int parentRes, H3Index *parent) nogil - H3Error cellToCenterChild(H3Index h, int childRes, H3Index *child) nogil + H3Error cellToParent( H3int h, int parentRes, H3int *parent) nogil + H3Error cellToCenterChild(H3int h, int childRes, H3int *child) nogil - H3Error cellToChildrenSize(H3Index h, int childRes, int64_t *num) nogil # num/out/N? - H3Error cellToChildren( H3Index h, int childRes, H3Index *children) nogil + H3Error cellToChildrenSize(H3int h, int childRes, int64_t *num) nogil # num/out/N? + H3Error cellToChildren( H3int h, int childRes, H3int *children) nogil H3Error compactCells( - const H3Index *cells_u, - H3Index *cells_c, + const H3int *cells_u, + H3int *cells_c, const int num_u ) nogil H3Error uncompactCellsSize( - const H3Index *cells_c, + const H3int *cells_c, const int64_t num_c, const int res, int64_t *num_u ) nogil H3Error uncompactCells( - const H3Index *cells_c, + const H3int *cells_c, const int num_c, - H3Index *cells_u, + H3int *cells_u, const int num_u, const int res ) nogil @@ -94,70 +93,70 @@ cdef extern from "h3api.h": H3Error getNumCells(int res, int64_t *out) nogil int pentagonCount() nogil int res0CellCount() nogil - H3Error getPentagons(int res, H3Index *out) nogil - H3Error getRes0Cells(H3Index *out) nogil + H3Error getPentagons(int res, H3int *out) nogil + H3Error getRes0Cells(H3int *out) nogil - H3Error gridPathCellsSize(H3Index start, H3Index end, int64_t *size) nogil - H3Error gridPathCells(H3Index start, H3Index end, H3Index *out) nogil + H3Error gridPathCellsSize(H3int start, H3int end, int64_t *size) nogil + H3Error gridPathCells(H3int start, H3int end, H3int *out) nogil H3Error getHexagonAreaAvgKm2(int res, double *out) nogil H3Error getHexagonAreaAvgM2(int res, double *out) nogil - H3Error cellAreaRads2(H3Index h, double *out) nogil - H3Error cellAreaKm2(H3Index h, double *out) nogil - H3Error cellAreaM2(H3Index h, double *out) nogil + H3Error cellAreaRads2(H3int h, double *out) nogil + H3Error cellAreaKm2(H3int h, double *out) nogil + H3Error cellAreaM2(H3int h, double *out) nogil - H3Error maxFaceCount(H3Index h, int *out) nogil - H3Error getIcosahedronFaces(H3Index h3, int *out) nogil + H3Error maxFaceCount(H3int h, int *out) nogil + H3Error getIcosahedronFaces(H3int h3, int *out) nogil - H3Error cellToLocalIj(H3Index origin, H3Index h3, uint32_t mode, CoordIJ *out) nogil - H3Error localIjToCell(H3Index origin, const CoordIJ *ij, uint32_t mode, H3Index *out) nogil + H3Error cellToLocalIj(H3int origin, H3int h3, uint32_t mode, CoordIJ *out) nogil + H3Error localIjToCell(H3int origin, const CoordIJ *ij, uint32_t mode, H3int *out) nogil - H3Error gridDiskDistances(H3Index origin, int k, H3Index *out, int *distances) nogil - H3Error gridRingUnsafe(H3Index origin, int k, H3Index *out) nogil + H3Error gridDiskDistances(H3int origin, int k, H3int *out, int *distances) nogil + H3Error gridRingUnsafe(H3int origin, int k, H3int *out) nogil - H3Error areNeighborCells(H3Index origin, H3Index destination, int *out) nogil - H3Error cellsToDirectedEdge(H3Index origin, H3Index destination, H3Index *out) nogil - H3Error getDirectedEdgeOrigin(H3Index edge, H3Index *out) nogil - H3Error getDirectedEdgeDestination(H3Index edge, H3Index *out) nogil - H3Error originToDirectedEdges(H3Index origin, H3Index *edges) nogil + H3Error areNeighborCells(H3int origin, H3int destination, int *out) nogil + H3Error cellsToDirectedEdge(H3int origin, H3int destination, H3int *out) nogil + H3Error getDirectedEdgeOrigin(H3int edge, H3int *out) nogil + H3Error getDirectedEdgeDestination(H3int edge, H3int *out) nogil + H3Error originToDirectedEdges(H3int origin, H3int *edges) nogil H3Error getHexagonEdgeLengthAvgKm(int res, double *out) nogil H3Error getHexagonEdgeLengthAvgM(int res, double *out) nogil - H3Error exactEdgeLengthRads(H3Index edge, double *out) nogil - H3Error exactEdgeLengthKm(H3Index edge, double *out) nogil - H3Error exactEdgeLengthM(H3Index edge, double *out) nogil + H3Error exactEdgeLengthRads(H3int edge, double *out) nogil + H3Error exactEdgeLengthKm(H3int edge, double *out) nogil + H3Error exactEdgeLengthM(H3int edge, double *out) nogil - H3Error cellToBoundary(H3Index h3, CellBoundary *gp) nogil - H3Error directedEdgeToBoundary(H3Index edge, CellBoundary *gb) nogil + H3Error cellToBoundary(H3int h3, CellBoundary *gp) nogil + H3Error directedEdgeToBoundary(H3int edge, CellBoundary *gb) nogil double distanceRads(const LatLng *a, const LatLng *b) nogil double distanceKm(const LatLng *a, const LatLng *b) nogil double distanceM(const LatLng *a, const LatLng *b) nogil - H3Error cellsToLinkedMultiPolygon(const H3Index *h3Set, const int numHexes, LinkedGeoPolygon *out) + H3Error cellsToLinkedMultiPolygon(const H3int *h3Set, const int numHexes, LinkedGeoPolygon *out) void destroyLinkedMultiPolygon(LinkedGeoPolygon *polygon) H3Error maxPolygonToCellsSize(const GeoPolygon *geoPolygon, int res, uint32_t flags, uint64_t *count) - H3Error polygonToCells(const GeoPolygon *geoPolygon, int res, uint32_t flags, H3Index *out) + H3Error polygonToCells(const GeoPolygon *geoPolygon, int res, uint32_t flags, H3int *out) # ctypedef struct GeoMultiPolygon: # int numPolygons # GeoPolygon *polygons - # int hexRange(H3Index origin, int k, H3Index *out) + # int hexRange(H3int origin, int k, H3int *out) - # int hexRangeDistances(H3Index origin, int k, H3Index *out, int *distances) + # int hexRangeDistances(H3int origin, int k, H3int *out, int *distances) - # int hexRanges(H3Index *h3Set, int length, int k, H3Index *out) + # int hexRanges(H3int *h3Set, int length, int k, H3int *out) - # void h3SetToLinkedGeo(const H3Index *h3Set, const int numHexes, LinkedGeoPolygon *out) + # void h3SetToLinkedGeo(const H3int *h3Set, const int numHexes, LinkedGeoPolygon *out) # void destroyLinkedPolygon(LinkedGeoPolygon *polygon) - # H3Index stringToH3(const char *str) + # H3int stringToH3(const char *str) - # void h3ToString(H3Index h, char *str, size_t sz) + # void h3ToString(H3int h, char *str, size_t sz) - # void getH3IndexesFromUnidirectionalEdge(H3Index edge, H3Index *originDestination) + # void getH3intesFromUnidirectionalEdge(H3int edge, H3int *originDestination) From ffb8c8a3c1b835c0d0a1ddfdabf76fcd41a475f9 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 20:39:09 -0700 Subject: [PATCH 15/60] hmm --- src/h3/_cy/cells.pyx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 6cc4841..9ca5ce3 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -92,7 +92,7 @@ cpdef H3int[:] _ring_fallback(H3int h, int k): `ring` tries to call `h3lib.hexRing` first; if that fails, we call this function, which relies on `h3lib.kRingDistances`. - Failures for `h3lib.hexRing` happen when the algortihm runs into a pentagon. + Failures for `h3lib.hexRing` happen when the algorithm runs into a pentagon. """ cdef: int64_t n @@ -102,24 +102,23 @@ cpdef H3int[:] _ring_fallback(H3int h, int k): check_distance(k) err = h3lib.maxGridDiskSize(k, &n) - # array of h3 cells - ptr = create_ptr(n) + hmm = H3MemoryManager(n) # array of cell distances from `h` dist_ptr = stdlib.calloc(n, sizeof(int)) if dist_ptr is NULL: raise MemoryError() - err = h3lib.gridDiskDistances(h, k, ptr, dist_ptr) + err = h3lib.gridDiskDistances(h, k, hmm.ptr, dist_ptr) distances = dist_ptr distances.callback_free_data = stdlib.free for i,v in enumerate(distances): if v != k: - ptr[i] = 0 + hmm.ptr[i] = 0 - mv = create_mv(ptr, n) + mv = hmm.create_mv() return mv From 6f62aab5c69e4a915dea1513ead30e3a02075392 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 21:07:08 -0700 Subject: [PATCH 16/60] int_mv rides into town --- src/h3/_cy/memory.pxd | 6 ++++++ src/h3/_cy/memory.pyx | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index 5dc5b2a..83ac1fb 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -8,6 +8,12 @@ cdef class H3MemoryManager: cdef H3int[:] create_mv(self) +cdef int[:] int_mv(size_t n) + + """ todo: read: https://cython.readthedocs.io/en/latest/src/tutorial/pxd_files.html + +todo: should i be allocating with python memory functions? +https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html """ diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index b051d73..1393a0e 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -94,3 +94,21 @@ cdef class H3MemoryManager: stdlib.free(self.ptr) +""" +Can someone please swoop in and find a much cleaner way to do all of this Cython memory management baloney? +""" +cdef int[:] int_mv(size_t n): + cdef: + int* ptr + array arr + + if n <= 0: + raise MemoryError() + ptr = stdlib.calloc(n, sizeof(int)) + if ptr is NULL: + raise MemoryError() + + arr = ptr + arr.callback_free_data = stdlib.free + + return arr From f8a65051d80ae0d882f8c6c60e3e4d1197a9d9a3 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 21:22:50 -0700 Subject: [PATCH 17/60] the mystery of the `int[:] distances` --- src/h3/_cy/cells.pyx | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 9ca5ce3..5b48a3a 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -11,7 +11,7 @@ from .util cimport ( empty_memory_view, # want to drop this import if possible ) -from .memory cimport H3MemoryManager +from .memory cimport H3MemoryManager, int_mv from .util import H3ValueError, H3ResolutionError @@ -97,6 +97,7 @@ cpdef H3int[:] _ring_fallback(H3int h, int k): cdef: int64_t n h3lib.H3Error err + int[:] distances ## todo: weird, this needs to be specified to avoid errors. cython bug? check_cell(h) check_distance(k) @@ -104,15 +105,9 @@ cpdef H3int[:] _ring_fallback(H3int h, int k): err = h3lib.maxGridDiskSize(k, &n) hmm = H3MemoryManager(n) - # array of cell distances from `h` - dist_ptr = stdlib.calloc(n, sizeof(int)) - if dist_ptr is NULL: - raise MemoryError() - - err = h3lib.gridDiskDistances(h, k, hmm.ptr, dist_ptr) - - distances = dist_ptr - distances.callback_free_data = stdlib.free + # parallel array of cell distances from `h` + distances = int_mv(n) + err = h3lib.gridDiskDistances(h, k, hmm.ptr, &distances[0]) for i,v in enumerate(distances): if v != k: From c38be89b26f27e1a24799de1977d6b04d56d6e7e Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 21:42:37 -0700 Subject: [PATCH 18/60] ring and children --- src/h3/_cy/cells.pyx | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 5b48a3a..8e04e22 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -106,6 +106,9 @@ cpdef H3int[:] _ring_fallback(H3int h, int k): hmm = H3MemoryManager(n) # parallel array of cell distances from `h` + + # idea: instead of a memoryview object, have a my_mv object that can return a ptr, but does the correct logic when it has length 0 + # then can always just return the pointer, instead of the weird &mv[0] syntax distances = int_mv(n) err = h3lib.gridDiskDistances(h, k, hmm.ptr, &distances[0]) @@ -128,18 +131,14 @@ cpdef H3int[:] ring(H3int h, int k): check_distance(k) n = 6*k if k > 0 else 1 - ptr = create_ptr(n) + hmm = H3MemoryManager(n) - err = h3lib.gridRingUnsafe(h, k, ptr) - - # if we drop into the failure state, we might be tempted to not create - # this mv, but creating the mv is exactly what guarantees that we'll free - # the memory. context manager would be better here, if we can figure out - # how to do that - mv = create_mv(ptr, n) + err = h3lib.gridRingUnsafe(h, k, hmm.ptr) if err: mv = _ring_fallback(h, k) + else: + mv = hmm.create_mv() return mv @@ -165,9 +164,8 @@ cpdef H3int parent(H3int h, res=None) except 0: cpdef H3int[:] children(H3int h, res=None): cdef: - H3int child h3lib.H3Error err - int64_t N + int64_t n check_cell(h) @@ -179,11 +177,11 @@ cpdef H3int[:] children(H3int h, res=None): raise H3ResolutionError(msg) check_res(res) - err = h3lib.cellToChildrenSize(h, res, &N) + err = h3lib.cellToChildrenSize(h, res, &n) - ptr = create_ptr(N) - err = h3lib.cellToChildren(h, res, ptr) - mv = create_mv(ptr, N) + hmm = H3MemoryManager(n) + err = h3lib.cellToChildren(h, res, hmm.ptr) + mv = hmm.create_mv() return mv From b0b0472f75218d2805e888e2e96565764cd4cbdf Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 21:54:33 -0700 Subject: [PATCH 19/60] mysteries abound! --- src/h3/_cy/cells.pyx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 8e04e22..da496c5 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -222,14 +222,18 @@ cpdef H3int[:] compact(const H3int[:] hu): for h in hu: ## todo: should we have an array version? would that be faster? check_cell(h) - ptr = create_ptr(len(hu)) - err = h3lib.compactCells(&hu[0], ptr, len(hu)) - mv = create_mv(ptr, len(hu)) + cdef size_t n = len(hu) + hmm = H3MemoryManager(n) + err = h3lib.compactCells(&hu[0], hmm.ptr, n) if err: # todo: additional error processing raise H3ValueError('Could not compact set of hexagons!') + # todo: mystery: why does this break in the tests when you move it before the error check? + # ideally, the create_mv method would be robust to borked data... + mv = hmm.create_mv() + return mv From fd8a9c261656e45fb6d56bae1c014305162d663f Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 21:56:45 -0700 Subject: [PATCH 20/60] thinking thoughts --- src/h3/_cy/cells.pyx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index da496c5..ccfbd21 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -208,6 +208,8 @@ cpdef H3int center_child(H3int h, res=None) except 0: cpdef H3int[:] compact(const H3int[:] hu): + # todo: fix this with my own Cython object "wrapper" class? + # everything has a .ptr interface? # todo: the Clib can handle 0-len arrays because it **avoids** # dereferencing the pointer, but Cython's syntax of # `&hu[0]` **requires** a dereference. For Cython, checking for array From b74f7ef584e5f20440ac21a8603242271959bc89 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 22:02:01 -0700 Subject: [PATCH 21/60] so, check this out: this works --- src/h3/_cy/cells.pyx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index ccfbd21..ab7e92e 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -249,7 +249,7 @@ cpdef H3int[:] uncompact(const H3int[:] hc, int res): # note: open to better ideas! cdef: h3lib.H3Error err - int64_t N + int64_t n if len(hc) == 0: return empty_memory_view() @@ -258,21 +258,22 @@ cpdef H3int[:] uncompact(const H3int[:] hc, int res): check_cell(h) # ignoring error for now - err = h3lib.uncompactCellsSize(&hc[0], len(hc), res, &N) + err = h3lib.uncompactCellsSize(&hc[0], len(hc), res, &n) - ptr = create_ptr(N) + hmm = H3MemoryManager(n) err = h3lib.uncompactCells( &hc[0], len(hc), - ptr, - N, + hmm.ptr, + hmm.n, res ) - mv = create_mv(ptr, N) if err: raise H3ValueError('Could not uncompact set of hexagons!') + mv = hmm.create_mv() + return mv From 5e02924c6e00dbb8590262eb4b0e0e815ed2f9fa Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 22:04:51 -0700 Subject: [PATCH 22/60] and this one gives an error. the game is afoot. --- src/h3/_cy/cells.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index ab7e92e..f75a990 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -269,11 +269,11 @@ cpdef H3int[:] uncompact(const H3int[:] hc, int res): res ) + mv = hmm.create_mv() + if err: raise H3ValueError('Could not uncompact set of hexagons!') - mv = hmm.create_mv() - return mv From f70f1163bb6bbb238f9a65f366e33e69756c80e1 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 22:06:33 -0700 Subject: [PATCH 23/60] good ol' fashioned detective work will catch the crook --- src/h3/_cy/cells.pyx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index f75a990..16298ba 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -269,11 +269,13 @@ cpdef H3int[:] uncompact(const H3int[:] hc, int res): res ) - mv = hmm.create_mv() - if err: raise H3ValueError('Could not uncompact set of hexagons!') + # todo: again! doesn't work if i have this before the error check! + # why isn't hmm.create_mv() robust to failures before it? + mv = hmm.create_mv() + return mv From b787da973f45a35f03850fa63aeccef352038395 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 22:09:48 -0700 Subject: [PATCH 24/60] just good taste, really --- src/h3/_cy/cells.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 16298ba..ec5de04 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -262,7 +262,7 @@ cpdef H3int[:] uncompact(const H3int[:] hc, int res): hmm = H3MemoryManager(n) err = h3lib.uncompactCells( - &hc[0], + &hc[0], # todo: symmetry here with the wrapper object might be nice. hc.ptr / hc.n len(hc), hmm.ptr, hmm.n, From 582973012717b92cd68085bfc312c93baeb32c1b Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 22:14:00 -0700 Subject: [PATCH 25/60] line --- src/h3/_cy/cells.pyx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index ec5de04..c1d45c1 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -347,15 +347,17 @@ cpdef H3int[:] line(H3int start, H3int end): s = s.format(hex(start), hex(end)) raise H3ValueError(s) - ptr = create_ptr(n) - err = h3lib.gridPathCells(start, end, ptr) - mv = create_mv(ptr, n) + hmm = H3MemoryManager(n) + err = h3lib.gridPathCells(start, end, hmm.ptr) if err: s = "Couldn't find line between cells {} and {}" s = s.format(hex(start), hex(end)) raise H3ValueError(s) + # todo: probably here too? + mv = hmm.create_mv() + return mv cpdef bool is_res_class_iii(H3int h): From f4471d3a2a6000522ca9f602107cd062438e94df Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 22:15:54 -0700 Subject: [PATCH 26/60] oh boy. didn't actually break on this one. --- src/h3/_cy/cells.pyx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index c1d45c1..b847eff 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -350,14 +350,14 @@ cpdef H3int[:] line(H3int start, H3int end): hmm = H3MemoryManager(n) err = h3lib.gridPathCells(start, end, hmm.ptr) + # todo: probably here too? + mv = hmm.create_mv() + if err: s = "Couldn't find line between cells {} and {}" s = s.format(hex(start), hex(end)) raise H3ValueError(s) - # todo: probably here too? - mv = hmm.create_mv() - return mv cpdef bool is_res_class_iii(H3int h): From 0354013a51bddc9095cece918f62a1406ab7140d Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 22:16:11 -0700 Subject: [PATCH 27/60] but back to what's good, just to be safe --- src/h3/_cy/cells.pyx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index b847eff..c1d45c1 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -350,14 +350,14 @@ cpdef H3int[:] line(H3int start, H3int end): hmm = H3MemoryManager(n) err = h3lib.gridPathCells(start, end, hmm.ptr) - # todo: probably here too? - mv = hmm.create_mv() - if err: s = "Couldn't find line between cells {} and {}" s = s.format(hex(start), hex(end)) raise H3ValueError(s) + # todo: probably here too? + mv = hmm.create_mv() + return mv cpdef bool is_res_class_iii(H3int h): From 2ceaa5c493e4c98701231dee26a0d327fd219d68 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 22:19:45 -0700 Subject: [PATCH 28/60] these work --- src/h3/_cy/cells.pyx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index c1d45c1..a537159 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -372,9 +372,9 @@ cpdef H3int[:] get_pentagon_indexes(int res): n = h3lib.pentagonCount() - ptr = create_ptr(n) - err = h3lib.getPentagons(res, ptr) - mv = create_mv(ptr, n) + hmm = H3MemoryManager(n) + err = h3lib.getPentagons(res, hmm.ptr) + mv = hmm.create_mv() return mv @@ -385,9 +385,9 @@ cpdef H3int[:] get_res0_indexes(): n = h3lib.res0CellCount() - ptr = create_ptr(n) - err = h3lib.getRes0Cells(ptr) - mv = create_mv(ptr, n) + hmm = H3MemoryManager(n) + err = h3lib.getRes0Cells(hmm.ptr) + mv = hmm.create_mv() return mv From f2fd2b7ca513e1a82420a9173ba743192a4c9737 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 22:29:04 -0700 Subject: [PATCH 29/60] get_faces --- src/h3/_cy/cells.pyx | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index a537159..f749464 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -391,26 +391,26 @@ cpdef H3int[:] get_res0_indexes(): return mv +# oh, this is returning a set?? +# todo: convert to int[:]? cpdef get_faces(H3int h): cdef: h3lib.H3Error err int n + int[:] faces ## todo: weird, this needs to be specified to avoid errors. cython bug? check_cell(h) err = h3lib.maxFaceCount(h, &n) #ignore error for now - cdef int* ptr = stdlib.calloc(n, sizeof(int)) - if (n > 0) and (not ptr): - raise MemoryError() + faces = int_mv(n) + err = h3lib.getIcosahedronFaces(h, &faces[0]) # handle error? - err = h3lib.getIcosahedronFaces(h, ptr) # handle error? + # todo: wait? do faces start from 0 or 1? + # we could do this check/processing in the int_mv object + out = {f for f in faces if f >= 0} - faces = ptr - faces = {f for f in faces if f >= 0} - stdlib.free(ptr) - - return faces + return out cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except *: From 9669632af5b1474b6c95fececbd5db69bdd267b0 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 22:43:01 -0700 Subject: [PATCH 30/60] cells.pyx is moved over --- src/h3/_cy/cells.pyx | 12 ++++++------ src/h3/_cy/memory.pxd | 2 +- src/h3/_cy/memory.pyx | 1 + 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index f749464..f363d95 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -6,15 +6,15 @@ from .util cimport ( check_cell, check_res, check_distance, - create_ptr, - create_mv, - empty_memory_view, # want to drop this import if possible ) - -from .memory cimport H3MemoryManager, int_mv - from .util import H3ValueError, H3ResolutionError +from .memory cimport ( + H3MemoryManager, + int_mv, + empty_memory_view, # todo: want to get rid of this! +) + # todo: add notes about Cython exception handling diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index 83ac1fb..235c06e 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -9,7 +9,7 @@ cdef class H3MemoryManager: cdef int[:] int_mv(size_t n) - +cdef H3int[:] empty_memory_view() """ todo: read: https://cython.readthedocs.io/en/latest/src/tutorial/pxd_files.html diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index 1393a0e..1625bca 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -47,6 +47,7 @@ cdef size_t move_nonzeros(H3int* a, size_t n): cdef H3int[:] empty_memory_view(): + # todo: get rid of this? # there's gotta be a better way to do this... # create an empty cython.view.array? cdef: From f918c91b81458258bae71ab152fab2364e670b78 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 22:47:16 -0700 Subject: [PATCH 31/60] i think that covers edges.pyx --- src/h3/_cy/edges.pyx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/h3/_cy/edges.pyx b/src/h3/_cy/edges.pyx index 716c96a..45fd711 100644 --- a/src/h3/_cy/edges.pyx +++ b/src/h3/_cy/edges.pyx @@ -5,10 +5,10 @@ from .util cimport ( check_cell, check_edge, check_res, - create_ptr, - create_mv, ) +from .memory cimport H3MemoryManager + from .util import H3ValueError cpdef bool are_neighbors(H3int h1, H3int h2): @@ -75,9 +75,9 @@ cpdef H3int[:] edges_from_cell(H3int origin): """ check_cell(origin) - ptr = create_ptr(6) - h3lib.originToDirectedEdges(origin, ptr) - mv = create_mv(ptr, 6) + hmm = H3MemoryManager(6) + h3lib.originToDirectedEdges(origin, hmm.ptr) + mv = hmm.create_mv() return mv From cec9c84fa5a141c48715317b3b6614c466d44e18 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 23:07:01 -0700 Subject: [PATCH 32/60] so i can get this far, but i run into issues when i try to convert `polyfill_polygon`. some memory error. not sure what's happening just yet. --- src/h3/_cy/geo.pyx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/h3/_cy/geo.pyx b/src/h3/_cy/geo.pyx index 7a737e2..725bd25 100644 --- a/src/h3/_cy/geo.pyx +++ b/src/h3/_cy/geo.pyx @@ -1,5 +1,9 @@ +from libc cimport stdlib +from libc.stdint cimport uint64_t + cimport h3lib -from h3lib cimport bool, H3int +from h3lib cimport bool, H3int, H3Error + from .util cimport ( check_cell, check_edge, @@ -9,11 +13,10 @@ from .util cimport ( deg2coord, coord2deg, ) -from libc cimport stdlib -from libc.stdint cimport uint64_t - from .util import H3ValueError +from .memory cimport H3MemoryManager + cpdef H3int geo_to_h3(double lat, double lng, int res) except 1: cdef: @@ -27,6 +30,9 @@ cpdef H3int geo_to_h3(double lat, double lng, int res) except 1: # todo: just ignoring the error for now, check in the future err = h3lib.latLngToCell(&c, res, &out) + if err: + raise H3ValueError() + return out @@ -39,6 +45,8 @@ cpdef (double, double) h3_to_geo(H3int h) except *: check_cell(h) err = h3lib.cellToLatLng(h, &c) + if err: + raise H3ValueError() return coord2deg(c) @@ -63,6 +71,7 @@ cdef h3lib.GeoLoop make_geoloop(geos, bool lnglat_order=False) except *: gl.numVerts = len(geos) + # todo: need for memory management gl.verts = stdlib.calloc(gl.numVerts, sizeof(h3lib.LatLng)) if lnglat_order: From 12b6370f09dd6d35102bf184fe99caf08bf69523 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 4 Jun 2022 23:18:42 -0700 Subject: [PATCH 33/60] even with the error checking... --- src/h3/_cy/geo.pyx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/h3/_cy/geo.pyx b/src/h3/_cy/geo.pyx index 725bd25..0436184 100644 --- a/src/h3/_cy/geo.pyx +++ b/src/h3/_cy/geo.pyx @@ -160,14 +160,21 @@ def polyfill_polygon(outer, int res, holes=None, bool lnglat_order=False): """ cdef: uint64_t n + H3Error err check_res(res) gp = GeoPolygon(outer, holes=holes, lnglat_order=lnglat_order) - h3lib.maxPolygonToCellsSize(&gp.gp, res, 0, &n) + err = h3lib.maxPolygonToCellsSize(&gp.gp, res, 0, &n) + if err: + raise H3ValueError() + ptr = create_ptr(n) - h3lib.polygonToCells(&gp.gp, res, 0, ptr) + err = h3lib.polygonToCells(&gp.gp, res, 0, ptr) + if err: + raise H3ValueError() + mv = create_mv(ptr, n) return mv From 03a27e71c91870e235f239c334adbde340dea20e Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Mon, 6 Jun 2022 01:11:02 -0700 Subject: [PATCH 34/60] notes --- src/h3/_cy/memory.pxd | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index 235c06e..afe8b9e 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -16,4 +16,11 @@ todo: read: https://cython.readthedocs.io/en/latest/src/tutorial/pxd_files.html todo: should i be allocating with python memory functions? https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html + +## things i'm not happy about with this current situation + +- there's gotta be a cleaner way to create a empty_memory_view() +- abolish any appearance of &thing[0]. (i.e., identical interfaces) +- can i make the interface for all these memory views identical? +- also, what's going on with the segfault stuff when we `.create_mv()` before checking an error? why isn't it robust to that? """ From 95c9affd90a141a1959bf9313aba94bd4ef103bc Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Mon, 6 Jun 2022 19:37:42 -0400 Subject: [PATCH 35/60] Remove more sdist files (#259) * remove scripts/examples/apps * BUILD_FUZZERS OFF * ENABLE_FORMAT off * turn off all the things (and sort options) * mac attack * see if there's a build time difference * back to the future --- CMakeLists.txt | 21 ++++++++++++++++----- MANIFEST.in | 3 --- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 436ea65..85ea71c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,11 +7,22 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_BUILD_TYPE Release) # Avoid building tooling we won't need for release -set(BUILD_BENCHMARKS OFF CACHE BOOL "" FORCE) -set(BUILD_FILTERS OFF CACHE BOOL "" FORCE) -set(BUILD_GENERATORS OFF CACHE BOOL "" FORCE) -set(BUILD_TESTING OFF CACHE BOOL "" FORCE) -set(ENABLE_DOCS OFF CACHE BOOL "" FORCE) +# See all options with `cmake -LA` in an `h3/build` directory, +# or at https://h3geo.org/docs/next/core-library/compilation-options/ +macro(turn_off option_name) + set(${option_name} OFF CACHE BOOL "" FORCE) +endmacro() +turn_off(BUILD_ALLOC_TESTS) +turn_off(BUILD_BENCHMARKS) +turn_off(BUILD_FILTERS) +turn_off(BUILD_FUZZERS) +turn_off(BUILD_GENERATORS) +turn_off(BUILD_TESTING) +turn_off(ENABLE_COVERAGE) +turn_off(ENABLE_DOCS) +turn_off(ENABLE_FORMAT) +turn_off(ENABLE_LIBFUZZER) +turn_off(ENABLE_LINTING) # Build the core library as static set(BUILD_SHARED_LIBS OFF) diff --git a/MANIFEST.in b/MANIFEST.in index 47de939..6ccde1f 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -19,9 +19,6 @@ include src/h3lib/README.md include src/h3lib/VERSION include src/h3lib/CMakeLists.txt -graft src/h3lib/scripts -graft src/h3lib/examples graft src/h3lib/cmake graft src/h3lib/src/h3lib -graft src/h3lib/src/apps exclude MANIFEST.in From 20a6a29dae19bb22c6b61b24d77f56b70c00d63a Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Thu, 28 Jul 2022 15:47:09 -0400 Subject: [PATCH 36/60] New error system (#260) * starting to play around with new error system * move enum to h3lib.pxd * this structure is weird :( * error system gets its own file * go back to using H3ErrorCodes * extern the H3ErrorCodes * remove H3Index in favor of just H3int * " to ' * import ordering * something * exception hierarchy * formatting * errors list * remove H3ResolutionError * remove H3EdgeError * remove H3CellError * remove H3DistanceError * H3FailedError * domain error * H3UnrecognizedException * most of em done * remove H3ValueError * remove some checks * ideas ideas * document what the function raises? * thoughts * lint * one half idea for DRYing (kinda) * potentially make the E_SUCCESS path faster * typos * maple syrup * cimport H3ErrorCodes as ec * clean up * whitespace * clarify cases * trying new error idiom * thanks comment * notes * applied idiom a few times now * alternative idiom? * handling some more potential errors * idioms and ideas * clean * removing check * interesting allocation example * lint * just trying to beat 2.7 for sport right now * works? * bow to lint * using enum values directly! * now that's a hierarchy * remove raise_with_msg * ij funcs * lint * remove some checks * remove err * comment * cleaned up `ring`, and i think i fixed a memory leak bug * think i fixed another memory leak bug * another memory leak bug * memory leaks! * clean * removing some comments * meh * H3Exception shouldn't be appearing as a concrete object * renaming some base exceptions * clean up * don't have a test for this anyway * documented * puttering around * words * backing up this nonsense with an airtight argument * air toight * todos * more words * skepticism * rewording * editing * ordering * tests on the error codes * more error code tests * note * address some comments * we raise! * lint * comments --- src/h3/__init__.py | 25 +++- src/h3/_cy/CMakeLists.txt | 2 + src/h3/_cy/__init__.py | 28 ++++- src/h3/_cy/cells.pyx | 202 ++++++++++++++--------------- src/h3/_cy/edges.pyx | 55 ++++---- src/h3/_cy/error_system.pxd | 3 + src/h3/_cy/error_system.pyx | 231 ++++++++++++++++++++++++++++++++++ src/h3/_cy/geo.pyx | 18 +-- src/h3/_cy/h3lib.pxd | 143 +++++++++++---------- src/h3/_cy/util.pyx | 30 ++--- src/h3/api/_api_template.py | 2 +- tests/test_cells_and_edges.py | 116 +++++++++-------- tests/test_error_codes.py | 67 ++++++++++ tests/test_h3.py | 5 +- tests/test_length_area.py | 10 +- tests/test_polyfill.py | 6 +- 16 files changed, 643 insertions(+), 300 deletions(-) create mode 100644 src/h3/_cy/error_system.pxd create mode 100644 src/h3/_cy/error_system.pyx create mode 100644 tests/test_error_codes.py diff --git a/src/h3/__init__.py b/src/h3/__init__.py index 717f9cb..a507e3b 100644 --- a/src/h3/__init__.py +++ b/src/h3/__init__.py @@ -8,9 +8,26 @@ from .api import basic_str as h3 from ._cy import ( + UnknownH3ErrorCode, + H3BaseException, + + H3GridNavigationError, + H3MemoryError, H3ValueError, - H3CellError, - H3ResolutionError, - H3EdgeError, - H3DistanceError, + + H3FailedError, + H3DomainError, + H3LatLngDomainError, + H3ResDomainError, + H3CellInvalidError, + H3DirEdgeInvalidError, + H3UndirEdgeInvalidError, + H3VertexInvalidError, + H3PentagonError, + H3DuplicateInputError, + H3NotNeighborsError, + H3ResMismatchError, + H3MemoryAllocError, + H3MemoryBoundsError, + H3OptionInvalidError, ) diff --git a/src/h3/_cy/CMakeLists.txt b/src/h3/_cy/CMakeLists.txt index 386557c..57a3b21 100644 --- a/src/h3/_cy/CMakeLists.txt +++ b/src/h3/_cy/CMakeLists.txt @@ -22,6 +22,7 @@ add_cython_file(geo) add_cython_file(cells) add_cython_file(edges) add_cython_file(to_multipoly) +add_cython_file(error_system) # Include pyx and pxd files in distribution for use by Cython API install( @@ -35,6 +36,7 @@ install( h3lib.pxd util.pxd util.pyx + error_system.pyx DESTINATION src/h3/_cy ) diff --git a/src/h3/_cy/__init__.py b/src/h3/_cy/__init__.py index bb4dfc0..8496c05 100644 --- a/src/h3/_cy/__init__.py +++ b/src/h3/_cy/__init__.py @@ -70,9 +70,29 @@ from .util import ( hex2int, int2hex, from_iter, +) + +from .error_system import ( + UnknownH3ErrorCode, + H3BaseException, + + H3GridNavigationError, + H3MemoryError, H3ValueError, - H3CellError, - H3ResolutionError, - H3EdgeError, - H3DistanceError, + + H3FailedError, + H3DomainError, + H3LatLngDomainError, + H3ResDomainError, + H3CellInvalidError, + H3DirEdgeInvalidError, + H3UndirEdgeInvalidError, + H3VertexInvalidError, + H3PentagonError, + H3DuplicateInputError, + H3NotNeighborsError, + H3ResMismatchError, + H3MemoryAllocError, + H3MemoryBoundsError, + H3OptionInvalidError, ) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 4453e38..f6abe99 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -1,5 +1,5 @@ cimport h3lib -from .h3lib cimport bool, int64_t, H3int +from .h3lib cimport bool, int64_t, H3int, H3ErrorCodes from libc cimport stdlib from .util cimport ( @@ -11,7 +11,10 @@ from .util cimport ( empty_memory_view, # want to drop this import if possible ) -from .util import H3ValueError, H3ResolutionError +from .error_system cimport ( + check_for_error, + check_for_error_msg, +) # todo: add notes about Cython exception handling @@ -51,17 +54,15 @@ cpdef int distance(H3int h1, H3int h2) except -1: """ cdef: int64_t distance - h3lib.H3Error err check_cell(h1) check_cell(h2) - err = h3lib.gridDistance(h1, h2, &distance) - if err: - # todo: do error handling later - s = 'Cells are too far apart to compute distance: {} and {}' - s = s.format(hex(h1), hex(h2)) - raise H3ValueError(s) + check_for_error( + h3lib.gridDistance(h1, h2, &distance) + ) # todo: should this raise a distance error? + + # s = 'Cells are too far apart to compute distance: {} and {}' return distance @@ -70,17 +71,19 @@ cpdef H3int[:] disk(H3int h, int k): """ cdef: int64_t n - h3lib.H3Error err check_cell(h) check_distance(k) - # ignoring error for now - err = h3lib.maxGridDiskSize(k, &n) + check_for_error( + h3lib.maxGridDiskSize(k, &n) + ) ptr = create_ptr(n) # todo: return a "smart" pointer that knows its length? - err = h3lib.gridDisk(h, k, ptr) # ignoring error again! + + err = h3lib.gridDisk(h, k, ptr) mv = create_mv(ptr, n) + check_for_error(err) # needs to be after, to ensure we clean up memory return mv @@ -94,19 +97,20 @@ cpdef H3int[:] _ring_fallback(H3int h, int k): """ cdef: int64_t n - h3lib.H3Error err check_cell(h) check_distance(k) - err = h3lib.maxGridDiskSize(k, &n) + check_for_error( + h3lib.maxGridDiskSize(k, &n) + ) # array of h3 cells ptr = create_ptr(n) # array of cell distances from `h` dist_ptr = stdlib.calloc(n, sizeof(int)) if dist_ptr is NULL: - raise MemoryError() + raise MemoryError() #todo: H3 memory error? err = h3lib.gridDiskDistances(h, k, ptr, dist_ptr) @@ -119,15 +123,15 @@ cpdef H3int[:] _ring_fallback(H3int h, int k): mv = create_mv(ptr, n) + check_for_error(err) # note: need to move this down here to make sure ptr gets freed + # again, easier with a memory manager object + return mv cpdef H3int[:] ring(H3int h, int k): """ Return cells at grid distance `== k` from `h`. Collection is "hollow" for k >= 1. """ - cdef: - h3lib.H3Error err - check_cell(h) check_distance(k) @@ -136,33 +140,33 @@ cpdef H3int[:] ring(H3int h, int k): err = h3lib.gridRingUnsafe(h, k, ptr) - # if we drop into the failure state, we might be tempted to not create - # this mv, but creating the mv is exactly what guarantees that we'll free - # the memory. context manager would be better here, if we can figure out - # how to do that - mv = create_mv(ptr, n) - - if err: + if err == H3ErrorCodes.E_SUCCESS: + mv = create_mv(ptr, n) + elif err == H3ErrorCodes.E_PENTAGON: + mv = create_mv(ptr, n) # need to create this to guarantee memory is freed. + # better done with a Cython object, i think. mv = _ring_fallback(h, k) + else: + mv = create_mv(ptr, n) # need to create this to guarantee memory is freed. + # definitely better done with a Cython "memory manager" object. + check_for_error(err) return mv cpdef H3int parent(H3int h, res=None) except 0: cdef: H3int parent - h3lib.H3Error err - check_cell(h) + check_cell(h) # todo: do we want to check for validity here? or leave correctness to the user? if res is None: res = resolution(h) - 1 - if res > resolution(h): + + err = h3lib.cellToParent(h, res, &parent) + if err: msg = 'Invalid parent resolution {} for cell {}.' msg = msg.format(res, hex(h)) - raise H3ResolutionError(msg) - - check_res(res) - err = h3lib.cellToParent(h, res, &parent) + check_for_error_msg(err, msg) return parent @@ -170,24 +174,23 @@ cpdef H3int parent(H3int h, res=None) except 0: cpdef H3int[:] children(H3int h, res=None): cdef: H3int child - h3lib.H3Error err int64_t N check_cell(h) if res is None: res = resolution(h) + 1 - if res < resolution(h): + + err = h3lib.cellToChildrenSize(h, res, &N) + if err: msg = 'Invalid child resolution {} for cell {}.' msg = msg.format(res, hex(h)) - raise H3ResolutionError(msg) - - check_res(res) - err = h3lib.cellToChildrenSize(h, res, &N) + check_for_error_msg(err, msg) ptr = create_ptr(N) err = h3lib.cellToChildren(h, res, ptr) mv = create_mv(ptr, N) + check_for_error(err) # needs to be after, to ensure memory is freed! return mv @@ -195,19 +198,17 @@ cpdef H3int[:] children(H3int h, res=None): cpdef H3int center_child(H3int h, res=None) except 0: cdef: H3int child - h3lib.H3Error err check_cell(h) if res is None: res = resolution(h) + 1 - if res < resolution(h): + + err = h3lib.cellToCenterChild(h, res, &child) + if err: msg = 'Invalid child resolution {} for cell {}.' msg = msg.format(res, hex(h)) - raise H3ResolutionError(msg) - - check_res(res) - err = h3lib.cellToCenterChild(h, res, &child) + check_for_error_msg(err, msg) return child @@ -219,8 +220,6 @@ cpdef H3int[:] compact(const H3int[:] hu): # `&hu[0]` **requires** a dereference. For Cython, checking for array # length of zero and returning early seems like the easiest solution. # note: open to better ideas! - cdef: - h3lib.H3Error err if len(hu) == 0: return empty_memory_view() @@ -232,9 +231,8 @@ cpdef H3int[:] compact(const H3int[:] hu): err = h3lib.compactCells(&hu[0], ptr, len(hu)) mv = create_mv(ptr, len(hu)) - if err: - # todo: additional error processing - raise H3ValueError('Could not compact set of hexagons!') + # todo: fix weird error ordering due to memory freeing... + check_for_error(err) return mv @@ -248,7 +246,6 @@ cpdef H3int[:] uncompact(const H3int[:] hc, int res): # length of zero and returning early seems like the easiest solution. # note: open to better ideas! cdef: - h3lib.H3Error err int64_t N if len(hc) == 0: @@ -270,31 +267,30 @@ cpdef H3int[:] uncompact(const H3int[:] hc, int res): ) mv = create_mv(ptr, N) - if err: - raise H3ValueError('Could not uncompact set of hexagons!') + # todo: fix weird error ordering due to memory freeing... + check_for_error(err) return mv cpdef int64_t num_hexagons(int resolution) except -1: - check_res(resolution) cdef: - h3lib.H3Error err int64_t num_cells - err = h3lib.getNumCells(resolution, &num_cells) + check_for_error( + h3lib.getNumCells(resolution, &num_cells) + ) return num_cells cpdef double mean_hex_area(int resolution, unit='km^2') except -1: cdef: - h3lib.H3Error err double area - check_res(resolution) - - err = h3lib.getHexagonAreaAvgKm2(resolution, &area) + check_for_error( + h3lib.getHexagonAreaAvgKm2(resolution, &area) + ) # todo: multiple units convert = { @@ -305,18 +301,15 @@ cpdef double mean_hex_area(int resolution, unit='km^2') except -1: try: area *= convert[unit] except: - raise H3ValueError('Unknown unit: {}'.format(unit)) + raise ValueError('Unknown unit: {}'.format(unit)) return area cpdef double cell_area(H3int h, unit='km^2') except -1: cdef: - h3lib.H3Error err double area - check_cell(h) - if unit == 'rads^2': err = h3lib.cellAreaRads2(h, &area) elif unit == 'km^2': @@ -324,34 +317,35 @@ cpdef double cell_area(H3int h, unit='km^2') except -1: elif unit == 'm^2': err = h3lib.cellAreaM2(h, &area) else: - raise H3ValueError('Unknown unit: {}'.format(unit)) + raise ValueError('Unknown unit: {}'.format(unit)) + + check_for_error(err) return area +cdef could_not_find_line(err, start, end): + msg = "Couldn't find line between cells {} and {}" + msg = msg.format(hex(start), hex(end)) + + check_for_error_msg(err, msg) + cpdef H3int[:] line(H3int start, H3int end): cdef: - h3lib.H3Error err int64_t n - check_cell(start) - check_cell(end) - + # todo: can we segfault here with invalid inputs? + # Can we trust the c library to validate the start/end cells? + # probably applies to all size/work pairs of functions... err = h3lib.gridPathCellsSize(start, end, &n) - if err: - s = "Couldn't find line between cells {} and {}" - s = s.format(hex(start), hex(end)) - raise H3ValueError(s) + could_not_find_line(err, start, end) ptr = create_ptr(n) err = h3lib.gridPathCells(start, end, ptr) mv = create_mv(ptr, n) - if err: - s = "Couldn't find line between cells {} and {}" - s = s.format(hex(start), hex(end)) - raise H3ValueError(s) + could_not_find_line(err, start, end) return mv @@ -360,46 +354,45 @@ cpdef bool is_res_class_iii(H3int h): cpdef H3int[:] get_pentagon_indexes(int res): - cdef: - h3lib.H3Error err - - check_res(res) - n = h3lib.pentagonCount() + # todo note: this is the tricky situation where we need the memory manager ptr = create_ptr(n) err = h3lib.getPentagons(res, ptr) mv = create_mv(ptr, n) + check_for_error(err) + return mv cpdef H3int[:] get_res0_indexes(): - cdef: - h3lib.H3Error err - n = h3lib.res0CellCount() + # todo note: this is the tricky situation where we need the memory manager ptr = create_ptr(n) err = h3lib.getRes0Cells(ptr) mv = create_mv(ptr, n) + check_for_error(err) + return mv cpdef get_faces(H3int h): cdef: - h3lib.H3Error err int n - check_cell(h) - - err = h3lib.maxFaceCount(h, &n) #ignore error for now + check_for_error( + h3lib.maxFaceCount(h, &n) + ) cdef int* ptr = stdlib.calloc(n, sizeof(int)) if (n > 0) and (not ptr): raise MemoryError() - err = h3lib.getIcosahedronFaces(h, ptr) # handle error? + check_for_error( + h3lib.getIcosahedronFaces(h, ptr) + ) faces = ptr faces = {f for f in faces if f >= 0} @@ -410,36 +403,27 @@ cpdef get_faces(H3int h): cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except *: cdef: - int flag h3lib.CoordIJ c - check_cell(origin) - check_cell(h) - - flag = h3lib.cellToLocalIj(origin, h, 0, &c) - - if flag != 0: - s = "Couldn't find local (i,j) between cells {} and {}." - s = s.format(hex(origin), hex(h)) - raise H3ValueError(s) + err = h3lib.cellToLocalIj(origin, h, 0, &c) + if err: + msg = "Couldn't find local (i,j) between cells {} and {}." + msg = msg.format(hex(origin), hex(h)) + check_for_error_msg(err, msg) return c.i, c.j cpdef H3int experimental_local_ij_to_h3(H3int origin, int i, int j) except 0: cdef: - int flag h3lib.CoordIJ c H3int out - check_cell(origin) - c.i, c.j = i, j - flag = h3lib.localIjToCell(origin, &c, 0, &out) - - if flag != 0: - s = "Couldn't find cell at local ({},{}) from cell {}." - s = s.format(i, j, hex(origin)) - raise H3ValueError(s) + err = h3lib.localIjToCell(origin, &c, 0, &out) + if err: + msg = "Couldn't find cell at local ({},{}) from cell {}." + msg = msg.format(i, j, hex(origin)) + check_for_error_msg(err, msg) return out diff --git a/src/h3/_cy/edges.pyx b/src/h3/_cy/edges.pyx index 716c96a..2df0795 100644 --- a/src/h3/_cy/edges.pyx +++ b/src/h3/_cy/edges.pyx @@ -2,25 +2,25 @@ cimport h3lib from .h3lib cimport bool, H3int from .util cimport ( - check_cell, - check_edge, - check_res, create_ptr, create_mv, ) -from .util import H3ValueError +from .error_system cimport check_for_error +# todo: make bint cpdef bool are_neighbors(H3int h1, H3int h2): cdef: int out - check_cell(h1) - check_cell(h2) + err = h3lib.areNeighborCells(h1, h2, &out) - error = h3lib.areNeighborCells(h1, h2, &out) - if error != 0: + # note: we are intentionally not raising an error here, and just + # returning false. + # todo: is this choice consistent across the Python and C libs? + if err: return False + return out == 1 @@ -29,16 +29,10 @@ cpdef H3int edge(H3int origin, H3int destination) except *: int neighbor_out H3int out - check_cell(origin) - check_cell(destination) + check_for_error( + h3lib.cellsToDirectedEdge(origin, destination, &out) + ) - error = h3lib.areNeighborCells(origin, destination, &neighbor_out) - if error != 0 or neighbor_out != 1: - s = 'Cells are not neighbors: {} and {}' - s = s.format(hex(origin), hex(destination)) - raise H3ValueError(s) - - h3lib.cellsToDirectedEdge(origin, destination, &out) return out @@ -49,34 +43,36 @@ cpdef H3int edge_origin(H3int e) except 1: cdef: H3int out - # without the check, with an invalid input, the function will just return 0 - check_edge(e) + check_for_error( + h3lib.getDirectedEdgeOrigin(e, &out) + ) - h3lib.getDirectedEdgeOrigin(e, &out) return out cpdef H3int edge_destination(H3int e) except 1: cdef: H3int out - check_edge(e) + check_for_error( + h3lib.getDirectedEdgeDestination(e, &out) + ) - h3lib.getDirectedEdgeDestination(e, &out) return out cpdef (H3int, H3int) edge_cells(H3int e) except *: - check_edge(e) - return edge_origin(e), edge_destination(e) cpdef H3int[:] edges_from_cell(H3int origin): """ Returns the 6 (or 5 for pentagons) directed edges for the given origin cell """ - check_cell(origin) ptr = create_ptr(6) - h3lib.originToDirectedEdges(origin, ptr) + + check_for_error( + h3lib.originToDirectedEdges(origin, ptr) + ) + mv = create_mv(ptr, 6) return mv @@ -86,8 +82,6 @@ cpdef double mean_edge_length(int resolution, unit='km') except -1: cdef: double length - check_res(resolution) - h3lib.getHexagonEdgeLengthAvgKm(resolution, &length) # todo: multiple units @@ -99,7 +93,7 @@ cpdef double mean_edge_length(int resolution, unit='km') except -1: try: length *= convert[unit] except: - raise H3ValueError('Unknown unit: {}'.format(unit)) + raise ValueError('Unknown unit: {}'.format(unit)) return length @@ -107,7 +101,6 @@ cpdef double mean_edge_length(int resolution, unit='km') except -1: cpdef double edge_length(H3int e, unit='km') except -1: cdef: double length - check_edge(e) # todo: maybe kick this logic up to the python level # it might be a little cleaner, because we can do the "switch statement" @@ -120,6 +113,6 @@ cpdef double edge_length(H3int e, unit='km') except -1: elif unit == 'm': h3lib.exactEdgeLengthM(e, &length) else: - raise H3ValueError('Unknown unit: {}'.format(unit)) + raise ValueError('Unknown unit: {}'.format(unit)) return length diff --git a/src/h3/_cy/error_system.pxd b/src/h3/_cy/error_system.pxd new file mode 100644 index 0000000..af8c709 --- /dev/null +++ b/src/h3/_cy/error_system.pxd @@ -0,0 +1,3 @@ +from .h3lib cimport H3Error +cdef check_for_error(H3Error err) +cdef check_for_error_msg(H3Error err, str msg) diff --git a/src/h3/_cy/error_system.pyx b/src/h3/_cy/error_system.pyx new file mode 100644 index 0000000..799e4f2 --- /dev/null +++ b/src/h3/_cy/error_system.pyx @@ -0,0 +1,231 @@ +""" +Exceptions from the h3-py library have three possible sources: + +- the Python code +- the Cython code +- the underlying H3 C library code + +The Python and Cython `h3-py` code will only raise standard Python +built-in exceptions; **no custom** exception classes will be used. + +Conversely, many functions in the H3 C library return a `uint32_t` +error code (aliased as type `H3Error`). +When these errors happen (and `h3-py` can't recover from them internally), +they are passed up to the Python/Cython code, where their +`uint32_t` error values are converted to **custom** Python exception types. +These custom exception classes all inherit from `H3BaseException`. + +There is a 1-1 correspondence between the concrete subclasses of +`H3BaseException` and the H3 C library `H3ErrorCodes` values. +The correspondence is intentional, so that the user can refer to the +H3 C library documentation on these errors. + +The (`uint32_t` <-> Exception) correspondence should be clear from +the names of each error/exception, but the explicit mapping is given by +a dictionary in the code below. + +Note that some "abstract" subclasses of `H3BaseException` are also included to +group the exceptions by type. (We say "abstract" because Python has no easy +way to make true abstract exception classes.) + +These "abstract" exceptions will never be raised directly by `h3-py`, but they +allow the user to catch general groups of errors. +Note that `h3-py` will only ever directly raise +the "concrete" exception classes. + +Summarizing, all exceptions originating from the C library inherit from +`H3BaseException`, which has both "abstract" and "concrete" subclasses. + +**Abstract classes**: + +- H3BaseException +- H3ValueError +- H3MemoryError +- H3GridNavigationError + +**Concrete classes**: + +- H3FailedError +- H3DomainError +- H3LatLngDomainError +- H3ResDomainError +- H3CellInvalidError +- H3DirEdgeInvalidError +- H3UndirEdgeInvalidError +- H3VertexInvalidError +- H3PentagonError +- H3DuplicateInputError +- H3NotNeighborsError +- H3ResMismatchError +- H3MemoryAllocError +- H3MemoryBoundsError +- H3OptionInvalidError + + +# TODO: add tests verifying that concrete exception classes have the right error codes associated with them +""" + +from contextlib import contextmanager + +from .h3lib cimport ( + H3Error, + + # H3ErrorCodes enum values + E_SUCCESS, + E_FAILED, + E_DOMAIN, + E_LATLNG_DOMAIN, + E_RES_DOMAIN, + E_CELL_INVALID, + E_DIR_EDGE_INVALID, + E_UNDIR_EDGE_INVALID, + E_VERTEX_INVALID, + E_PENTAGON, + E_DUPLICATE_INPUT, + E_NOT_NEIGHBORS, + E_RES_MISMATCH, + E_MEMORY, + E_MEMORY_BOUNDS, + E_OPTION_INVALID, +) + +@contextmanager +def _the_error(obj): + """ + Syntactic maple syrup for grouping exception definitions. + The associated `with` statement ends up as a not-half-bad + approximation to a valid sentence fragment. + + This provides sort of a "pretend scope", in that it allows for + block indentation which helps to visually indicate the "scope" + of the `... as e` statement. Just note that Python doesn't treat the + `with` block as a "true" separate scope. + + Note that this doesn't actually do anything context-manager-y, outside + of the variable assignment and block indentation. + """ + yield obj + + +# +# Base exception for C library error codes +# +class H3BaseException(Exception): + """ Base H3 exception class. + + Concrete subclasses of this class correspond to specific + error codes from the C library. + + Base/abstract subclasses will have `h3_error_code = None`, while + concrete subclasses will have `h3_error_code` equal to their associated + C library error code. + """ + h3_error_code = None + + +# +# A few "abstract" exceptions; organizational. +# +with _the_error(H3BaseException) as e: + class H3ValueError(e, ValueError): ... + class H3MemoryError(e, MemoryError): ... + class H3GridNavigationError(e, RuntimeError): ... + + +# +# Concrete exceptions +# +class UnknownH3ErrorCode(H3BaseException): + """ + Indicates that the h3-py Python bindings have received an + unrecognized error code from the C library. + + This should never happen. Please report if you get this error. + + Note that this exception is *outside* of the + H3BaseException class hierarchy. + """ + pass + +with _the_error(H3BaseException) as e: + class H3FailedError(e): ... + +with _the_error(H3GridNavigationError) as e: + class H3PentagonError(e): ... + +with _the_error(H3MemoryError) as e: + class H3MemoryAllocError(e): ... + class H3MemoryBoundsError(e): ... + +with _the_error(H3ValueError) as e: + class H3DomainError(e): ... + class H3LatLngDomainError(e): ... + class H3ResDomainError(e): ... + class H3CellInvalidError(e): ... + class H3DirEdgeInvalidError(e): ... + class H3UndirEdgeInvalidError(e): ... + class H3VertexInvalidError(e): ... + class H3DuplicateInputError(e): ... + class H3NotNeighborsError(e): ... + class H3ResMismatchError(e): ... + class H3OptionInvalidError(e): ... + + +""" +This defines a mapping between uint32_t error codes and concrete Python +exception classes. +Note that we intentionally omit E_SUCCESS, as it isn't an actual error. +""" +error_mapping = { + E_FAILED: H3FailedError, + E_DOMAIN: H3DomainError, + E_LATLNG_DOMAIN: H3LatLngDomainError, + E_RES_DOMAIN: H3ResDomainError, + E_CELL_INVALID: H3CellInvalidError, + E_DIR_EDGE_INVALID: H3DirEdgeInvalidError, + E_UNDIR_EDGE_INVALID: H3UndirEdgeInvalidError, + E_VERTEX_INVALID: H3VertexInvalidError, + E_PENTAGON: H3PentagonError, + E_DUPLICATE_INPUT: H3DuplicateInputError, + E_NOT_NEIGHBORS: H3NotNeighborsError, + E_RES_MISMATCH: H3ResMismatchError, + E_MEMORY: H3MemoryAllocError, + E_MEMORY_BOUNDS: H3MemoryBoundsError, + E_OPTION_INVALID: H3OptionInvalidError, +} + +# Go back and modify the class definitions so that each concrete exception +# stores its associated error code. +for code, ex in error_mapping.items(): + ex.h3_error_code = code + + +# +# Helper functions +# + +# TODO: Move the helpers to util? +# TODO: Unclear how/where to expose these functions. cdef/cpdef? + +cdef code_to_exception(H3Error err): + if err == E_SUCCESS: + return None + elif err in error_mapping: + return error_mapping[err] + else: + raise UnknownH3ErrorCode(err) + +cdef check_for_error(H3Error err): + ex = code_to_exception(err) + if ex: + raise ex + +# todo: There's no easy way to do `*args` in `cdef` functions, but I'm also +# not sure this even needs to be a Cython `cdef` function at all, or that +# any of the other helper functions need to be in Cython. +# todo: Revisit after we've played with this a bit. +# todo: also: maybe the extra messages aren't that much more helpful... +cdef check_for_error_msg(H3Error err, str msg): + ex = code_to_exception(err) + if ex: + raise ex(msg) diff --git a/src/h3/_cy/geo.pyx b/src/h3/_cy/geo.pyx index 7a737e2..b40039b 100644 --- a/src/h3/_cy/geo.pyx +++ b/src/h3/_cy/geo.pyx @@ -12,20 +12,19 @@ from .util cimport ( from libc cimport stdlib from libc.stdint cimport uint64_t -from .util import H3ValueError +from .error_system cimport check_for_error cpdef H3int geo_to_h3(double lat, double lng, int res) except 1: cdef: h3lib.LatLng c H3int out - h3lib.H3Error err - check_res(res) c = deg2coord(lat, lng) - # todo: just ignoring the error for now, check in the future - err = h3lib.latLngToCell(&c, res, &out) + check_for_error( + h3lib.latLngToCell(&c, res, &out) + ) return out @@ -34,11 +33,14 @@ cpdef (double, double) h3_to_geo(H3int h) except *: """Map an H3 cell into its centroid geo-coordinate (lat/lng)""" cdef: h3lib.LatLng c - h3lib.H3Error err check_cell(h) + # todo: think about: if you give this an invalid cell, should it still return a lat/lng? + # idea: safe and unsafe APIs? - err = h3lib.cellToLatLng(h, &c) + check_for_error( + h3lib.cellToLatLng(h, &c) + ) return coord2deg(c) @@ -281,6 +283,6 @@ cpdef double point_dist( elif unit == 'm': d = h3lib.distanceM(&a, &b) else: - raise H3ValueError('Unknown unit: {}'.format(unit)) + raise ValueError('Unknown unit: {}'.format(unit)) return d diff --git a/src/h3/_cy/h3lib.pxd b/src/h3/_cy/h3lib.pxd index ba8a4e2..0ff65e3 100644 --- a/src/h3/_cy/h3lib.pxd +++ b/src/h3/_cy/h3lib.pxd @@ -1,23 +1,40 @@ from cpython cimport bool -from libc.stdint cimport uint64_t, int64_t, uint32_t +from libc.stdint cimport uint32_t, uint64_t, int64_t -ctypedef uint64_t H3int -ctypedef uint32_t H3Error -ctypedef basestring H3str +ctypedef basestring H3str # todo: do we really need this str one? -cdef extern from "h3api.h": +cdef extern from 'h3api.h': cdef int H3_VERSION_MAJOR cdef int H3_VERSION_MINOR cdef int H3_VERSION_PATCH - ctypedef uint64_t H3Index + ctypedef uint64_t H3int 'H3Index' + + ctypedef uint32_t H3Error + ctypedef enum H3ErrorCodes: + E_SUCCESS = 0 + E_FAILED = 1 + E_DOMAIN = 2 + E_LATLNG_DOMAIN = 3 + E_RES_DOMAIN = 4 + E_CELL_INVALID = 5 + E_DIR_EDGE_INVALID = 6 + E_UNDIR_EDGE_INVALID = 7 + E_VERTEX_INVALID = 8 + E_PENTAGON = 9 + E_DUPLICATE_INPUT = 10 + E_NOT_NEIGHBORS = 11 + E_RES_MISMATCH = 12 + E_MEMORY = 13 + E_MEMORY_BOUNDS = 14 + E_OPTION_INVALID = 15 ctypedef struct LatLng: double lat # in radians double lng # in radians ctypedef struct CellBoundary: - int num_verts "numVerts" + int num_verts 'numVerts' LatLng verts[10] # MAX_CELL_BNDRY_VERTS ctypedef struct CoordIJ: @@ -25,18 +42,18 @@ cdef extern from "h3api.h": int j ctypedef struct LinkedLatLng: - LatLng data "vertex" + LatLng data 'vertex' LinkedLatLng *next # renaming these for clarity ctypedef struct LinkedGeoLoop: - LinkedLatLng *data "first" - LinkedLatLng *_data_last "last" # not needed in Cython bindings + LinkedLatLng *data 'first' + LinkedLatLng *_data_last 'last' # not needed in Cython bindings LinkedGeoLoop *next ctypedef struct LinkedGeoPolygon: - LinkedGeoLoop *data "first" - LinkedGeoLoop *_data_last "last" # not needed in Cython bindings + LinkedGeoLoop *data 'first' + LinkedGeoLoop *_data_last 'last' # not needed in Cython bindings LinkedGeoPolygon *next ctypedef struct GeoLoop: @@ -48,45 +65,45 @@ cdef extern from "h3api.h": int numHoles GeoLoop *holes - int isValidCell(H3Index h) nogil - int isPentagon(H3Index h) nogil - int isResClassIII(H3Index h) nogil - int isValidDirectedEdge(H3Index edge) nogil + int isValidCell(H3int h) nogil + int isPentagon(H3int h) nogil + int isResClassIII(H3int h) nogil + int isValidDirectedEdge(H3int edge) nogil double degsToRads(double degrees) nogil double radsToDegs(double radians) nogil - int getResolution(H3Index h) nogil - int getBaseCellNumber(H3Index h) nogil + int getResolution(H3int h) nogil + int getBaseCellNumber(H3int h) nogil - H3Error latLngToCell(const LatLng *g, int res, H3Index *out) nogil - H3Error cellToLatLng(H3Index h, LatLng *) nogil - H3Error gridDistance(H3Index h1, H3Index h2, int64_t *distance) nogil + H3Error latLngToCell(const LatLng *g, int res, H3int *out) nogil + H3Error cellToLatLng(H3int h, LatLng *) nogil + H3Error gridDistance(H3int h1, H3int h2, int64_t *distance) nogil H3Error maxGridDiskSize(int k, int64_t *out) nogil # num/out/N? - H3Error gridDisk(H3Index h, int k, H3Index *out) nogil + H3Error gridDisk(H3int h, int k, H3int *out) nogil - H3Error cellToParent( H3Index h, int parentRes, H3Index *parent) nogil - H3Error cellToCenterChild(H3Index h, int childRes, H3Index *child) nogil + H3Error cellToParent( H3int h, int parentRes, H3int *parent) nogil + H3Error cellToCenterChild(H3int h, int childRes, H3int *child) nogil - H3Error cellToChildrenSize(H3Index h, int childRes, int64_t *num) nogil # num/out/N? - H3Error cellToChildren( H3Index h, int childRes, H3Index *children) nogil + H3Error cellToChildrenSize(H3int h, int childRes, int64_t *num) nogil # num/out/N? + H3Error cellToChildren( H3int h, int childRes, H3int *children) nogil H3Error compactCells( - const H3Index *cells_u, - H3Index *cells_c, + const H3int *cells_u, + H3int *cells_c, const int num_u ) nogil H3Error uncompactCellsSize( - const H3Index *cells_c, + const H3int *cells_c, const int64_t num_c, const int res, int64_t *num_u ) nogil H3Error uncompactCells( - const H3Index *cells_c, + const H3int *cells_c, const int num_c, - H3Index *cells_u, + H3int *cells_u, const int num_u, const int res ) nogil @@ -94,70 +111,70 @@ cdef extern from "h3api.h": H3Error getNumCells(int res, int64_t *out) nogil int pentagonCount() nogil int res0CellCount() nogil - H3Error getPentagons(int res, H3Index *out) nogil - H3Error getRes0Cells(H3Index *out) nogil + H3Error getPentagons(int res, H3int *out) nogil + H3Error getRes0Cells(H3int *out) nogil - H3Error gridPathCellsSize(H3Index start, H3Index end, int64_t *size) nogil - H3Error gridPathCells(H3Index start, H3Index end, H3Index *out) nogil + H3Error gridPathCellsSize(H3int start, H3int end, int64_t *size) nogil + H3Error gridPathCells(H3int start, H3int end, H3int *out) nogil H3Error getHexagonAreaAvgKm2(int res, double *out) nogil H3Error getHexagonAreaAvgM2(int res, double *out) nogil - H3Error cellAreaRads2(H3Index h, double *out) nogil - H3Error cellAreaKm2(H3Index h, double *out) nogil - H3Error cellAreaM2(H3Index h, double *out) nogil + H3Error cellAreaRads2(H3int h, double *out) nogil + H3Error cellAreaKm2(H3int h, double *out) nogil + H3Error cellAreaM2(H3int h, double *out) nogil - H3Error maxFaceCount(H3Index h, int *out) nogil - H3Error getIcosahedronFaces(H3Index h3, int *out) nogil + H3Error maxFaceCount(H3int h, int *out) nogil + H3Error getIcosahedronFaces(H3int h3, int *out) nogil - H3Error cellToLocalIj(H3Index origin, H3Index h3, uint32_t mode, CoordIJ *out) nogil - H3Error localIjToCell(H3Index origin, const CoordIJ *ij, uint32_t mode, H3Index *out) nogil + H3Error cellToLocalIj(H3int origin, H3int h3, uint32_t mode, CoordIJ *out) nogil + H3Error localIjToCell(H3int origin, const CoordIJ *ij, uint32_t mode, H3int *out) nogil - H3Error gridDiskDistances(H3Index origin, int k, H3Index *out, int *distances) nogil - H3Error gridRingUnsafe(H3Index origin, int k, H3Index *out) nogil + H3Error gridDiskDistances(H3int origin, int k, H3int *out, int *distances) nogil + H3Error gridRingUnsafe(H3int origin, int k, H3int *out) nogil - H3Error areNeighborCells(H3Index origin, H3Index destination, int *out) nogil - H3Error cellsToDirectedEdge(H3Index origin, H3Index destination, H3Index *out) nogil - H3Error getDirectedEdgeOrigin(H3Index edge, H3Index *out) nogil - H3Error getDirectedEdgeDestination(H3Index edge, H3Index *out) nogil - H3Error originToDirectedEdges(H3Index origin, H3Index *edges) nogil + H3Error areNeighborCells(H3int origin, H3int destination, int *out) nogil + H3Error cellsToDirectedEdge(H3int origin, H3int destination, H3int *out) nogil + H3Error getDirectedEdgeOrigin(H3int edge, H3int *out) nogil + H3Error getDirectedEdgeDestination(H3int edge, H3int *out) nogil + H3Error originToDirectedEdges(H3int origin, H3int *edges) nogil H3Error getHexagonEdgeLengthAvgKm(int res, double *out) nogil H3Error getHexagonEdgeLengthAvgM(int res, double *out) nogil - H3Error exactEdgeLengthRads(H3Index edge, double *out) nogil - H3Error exactEdgeLengthKm(H3Index edge, double *out) nogil - H3Error exactEdgeLengthM(H3Index edge, double *out) nogil + H3Error exactEdgeLengthRads(H3int edge, double *out) nogil + H3Error exactEdgeLengthKm(H3int edge, double *out) nogil + H3Error exactEdgeLengthM(H3int edge, double *out) nogil - H3Error cellToBoundary(H3Index h3, CellBoundary *gp) nogil - H3Error directedEdgeToBoundary(H3Index edge, CellBoundary *gb) nogil + H3Error cellToBoundary(H3int h3, CellBoundary *gp) nogil + H3Error directedEdgeToBoundary(H3int edge, CellBoundary *gb) nogil double distanceRads(const LatLng *a, const LatLng *b) nogil double distanceKm(const LatLng *a, const LatLng *b) nogil double distanceM(const LatLng *a, const LatLng *b) nogil - H3Error cellsToLinkedMultiPolygon(const H3Index *h3Set, const int numHexes, LinkedGeoPolygon *out) + H3Error cellsToLinkedMultiPolygon(const H3int *h3Set, const int numHexes, LinkedGeoPolygon *out) void destroyLinkedMultiPolygon(LinkedGeoPolygon *polygon) H3Error maxPolygonToCellsSize(const GeoPolygon *geoPolygon, int res, uint32_t flags, uint64_t *count) - H3Error polygonToCells(const GeoPolygon *geoPolygon, int res, uint32_t flags, H3Index *out) + H3Error polygonToCells(const GeoPolygon *geoPolygon, int res, uint32_t flags, H3int *out) # ctypedef struct GeoMultiPolygon: # int numPolygons # GeoPolygon *polygons - # int hexRange(H3Index origin, int k, H3Index *out) + # int hexRange(H3int origin, int k, H3int *out) - # int hexRangeDistances(H3Index origin, int k, H3Index *out, int *distances) + # int hexRangeDistances(H3int origin, int k, H3int *out, int *distances) - # int hexRanges(H3Index *h3Set, int length, int k, H3Index *out) + # int hexRanges(H3int *h3Set, int length, int k, H3int *out) - # void h3SetToLinkedGeo(const H3Index *h3Set, const int numHexes, LinkedGeoPolygon *out) + # void h3SetToLinkedGeo(const H3int *h3Set, const int numHexes, LinkedGeoPolygon *out) # void destroyLinkedPolygon(LinkedGeoPolygon *polygon) - # H3Index stringToH3(const char *str) + # H3int stringToH3(const char *str) - # void h3ToString(H3Index h, char *str, size_t sz) + # void h3ToString(H3int h, char *str, size_t sz) - # void getH3IndexesFromUnidirectionalEdge(H3Index edge, H3Index *originDestination) + # void getH3intesFromUnidirectionalEdge(H3int edge, H3int *originDestination) diff --git a/src/h3/_cy/util.pyx b/src/h3/_cy/util.pyx index bbb63e4..fff0216 100644 --- a/src/h3/_cy/util.pyx +++ b/src/h3/_cy/util.pyx @@ -4,6 +4,13 @@ from .h3lib cimport H3int, H3str, isValidCell, isValidDirectedEdge cimport h3lib +from .error_system import ( + H3ResDomainError, + H3DomainError, + H3DirEdgeInvalidError, + H3CellInvalidError, +) + cdef h3lib.LatLng deg2coord(double lat, double lng) nogil: cdef: @@ -48,21 +55,6 @@ cpdef H3str int2hex(H3int x): return '{:x}'.format(x) -class H3ValueError(ValueError): - pass - -class H3CellError(H3ValueError): - pass - -class H3EdgeError(H3ValueError): - pass - -class H3ResolutionError(H3ValueError): - pass - -class H3DistanceError(H3ValueError): - pass - cdef check_cell(H3int h): """ Check if valid H3 "cell" (hexagon or pentagon). @@ -78,19 +70,19 @@ cdef check_cell(H3int h): `str` inputs. """ if isValidCell(h) == 0: - raise H3CellError('Integer is not a valid H3 cell: {}'.format(hex(h))) + raise H3CellInvalidError('Integer is not a valid H3 cell: {}'.format(hex(h))) cdef check_edge(H3int e): if isValidDirectedEdge(e) == 0: - raise H3EdgeError('Integer is not a valid H3 edge: {}'.format(hex(e))) + raise H3DirEdgeInvalidError('Integer is not a valid H3 edge: {}'.format(hex(e))) cdef check_res(int res): if (res < 0) or (res > 15): - raise H3ResolutionError(res) + raise H3ResDomainError(res) cdef check_distance(int k): if k < 0: - raise H3DistanceError( + raise H3DomainError( 'Grid distances must be nonnegative. Received: {}'.format(k) ) diff --git a/src/h3/api/_api_template.py b/src/h3/api/_api_template.py index 499c040..fa9082e 100644 --- a/src/h3/api/_api_template.py +++ b/src/h3/api/_api_template.py @@ -254,7 +254,7 @@ class _API_FUNCTIONS(object): path between the cells in the graph formed by connecting adjacent cells. - This function will return an H3ValueError if the + This function will raise an exception if the cells are too far apart to compute the distance. Parameters diff --git a/tests/test_cells_and_edges.py b/tests/test_cells_and_edges.py index a90bb0a..ccca342 100644 --- a/tests/test_cells_and_edges.py +++ b/tests/test_cells_and_edges.py @@ -2,11 +2,12 @@ import h3 import pytest from h3 import ( - H3ValueError, - H3CellError, - H3ResolutionError, - H3EdgeError, - H3DistanceError, + H3FailedError, + H3ResDomainError, + H3DomainError, + H3ResMismatchError, + H3CellInvalidError, + H3NotNeighborsError, ) @@ -61,12 +62,12 @@ def test4(): def test_k_ring_distance(): - with pytest.raises(H3DistanceError): + with pytest.raises(H3DomainError): h3.k_ring('8928308280fffff', -10) def test_hex_ring_distance(): - with pytest.raises(H3DistanceError): + with pytest.raises(H3DomainError): h3.hex_ring('8928308280fffff', -10) @@ -114,7 +115,7 @@ def test8(): assert not h3.h3_is_valid(h_bad) # other methods should validate and raise exception if bad input - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.h3_get_resolution(h_bad) @@ -130,15 +131,34 @@ def test_parent(): assert h3.h3_to_parent(h, 8) == '8828308281fffff' assert h3.h3_to_parent(h, 9) == h - with pytest.raises(H3ResolutionError): + with pytest.raises(H3ResMismatchError): h3.h3_to_parent(h, 10) +def test_parent_err(): + # Test 1 + h = '8075fffffffffff' # geo_to_h3(0,0,0) + + with pytest.raises(H3ResDomainError): + h3.h3_to_parent(h) + + # Test 2 + try: + h3.h3_to_parent(h) + except Exception as e: + msg = str(e) + + # todo: revist this weird formatting stuff + expected = 'Invalid parent resolution -1 for cell {}.' + expected = expected.format(hex(h3.string_to_h3(h))) + + assert msg == expected + + def test_children(): h = '8928308280fffff' - # one above should raise an exception - with pytest.raises(H3ResolutionError): + with pytest.raises(H3ResDomainError): h3.h3_to_children(h, 8) # same resolution is set of just cell itself @@ -160,7 +180,7 @@ def test_children(): # finest resolution cell should return error for children h = '8f04ccb2c45e225' - with pytest.raises(H3ResolutionError): + with pytest.raises(H3ResDomainError): h3.h3_to_children(h) @@ -168,7 +188,7 @@ def test_center_child(): h = '8928308280fffff' # one above should raise an exception - with pytest.raises(H3ResolutionError): + with pytest.raises(H3ResDomainError): h3.h3_to_center_child(h, 8) # same resolution should be same cell @@ -180,7 +200,7 @@ def test_center_child(): # finest resolution hex should return error for child h = '8f04ccb2c45e225' - with pytest.raises(H3ResolutionError): + with pytest.raises(H3ResDomainError): h3.h3_to_center_child(h) @@ -196,10 +216,14 @@ def test_distance(): def test_distance_error(): + """ Two valid cells, but they are too far apart compute the distance + + todo: make sure this raises a E_TOO_FAR error (when we add it in the future) + """ h1 = '8353b0fffffffff' h2 = '835804fffffffff' - with pytest.raises(H3ValueError): + with pytest.raises(H3FailedError): h3.h3_distance(h1, h2) @@ -351,38 +375,38 @@ def test_edge_boundary(): def test_validation(): h = '8a28308280fffff' # invalid cell - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.h3_get_base_cell(h) - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.h3_get_resolution(h) - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.h3_to_parent(h, 9) - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.h3_distance(h, h) - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.k_ring(h, 1) - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.hex_ring(h, 1) - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.h3_to_children(h, 11) - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.compact({h}) - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.uncompact({h}, 10) def test_validation2(): h = '8928308280fffff' - with pytest.raises(H3ResolutionError): + with pytest.raises(H3ResDomainError): h3.h3_to_children(h, 17) assert not h3.h3_indexes_are_neighbors(h, h) @@ -391,40 +415,37 @@ def test_validation2(): def test_validation_geo(): h = '8a28308280fffff' # invalid cell - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.h3_to_geo(h) - with pytest.raises(H3ResolutionError): + with pytest.raises(H3ResDomainError): h3.geo_to_h3(0, 0, 17) - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.h3_to_geo_boundary(h) - with pytest.raises(H3CellError): - h3.h3_indexes_are_neighbors(h, h) + # note: this won't raise an exception on bad input, but it does + # *correctly* say that two invalid indexes are not neighbors + assert not h3.h3_indexes_are_neighbors(h, h) def test_edges(): h = '8928308280fffff' - with pytest.raises(H3ValueError): + with pytest.raises(H3NotNeighborsError): h3.get_h3_unidirectional_edge(h, h) h2 = h3.hex_ring(h, 2).pop() - with pytest.raises(H3ValueError): + with pytest.raises(H3NotNeighborsError): h3.get_h3_unidirectional_edge(h, h2) e_bad = '14928308280ffff1' assert not h3.h3_unidirectional_edge_is_valid(e_bad) - with pytest.raises(H3EdgeError): - h3.get_origin_h3_index_from_unidirectional_edge(e_bad) - - with pytest.raises(H3EdgeError): - h3.get_destination_h3_index_from_unidirectional_edge(e_bad) - - with pytest.raises(H3EdgeError): - h3.get_h3_indexes_from_unidirectional_edge(e_bad) + # note: won't raise an error on bad input + h3.get_origin_h3_index_from_unidirectional_edge(e_bad) + h3.get_destination_h3_index_from_unidirectional_edge(e_bad) + h3.get_h3_indexes_from_unidirectional_edge(e_bad) def test_line(): @@ -523,7 +544,7 @@ def test_uncompact_cell_input(): # inputting a single cell string can raise weird errors. # Ensure we get a reasonably helpful answer - with pytest.raises(H3CellError): + with pytest.raises(H3CellInvalidError): h3.uncompact('8001fffffffffff', 1) @@ -554,13 +575,6 @@ def test_get_res0_indexes(): assert sub < out -def test_get_faces_invalid(): - h = '8a28308280fffff' # invalid cell - - with pytest.raises(H3CellError): - h3.h3_get_faces(h) - - def test_get_faces(): h = '804dfffffffffff' expected = {2, 3, 7, 8, 12} @@ -583,7 +597,9 @@ def test_to_local_ij_error(): # error if we cross a face nb = h3.hex_ring(h, k=2) - with pytest.raises(H3ValueError): + + # todo: should this be the E_TOO_FAR guy? + with pytest.raises(H3FailedError): [h3.experimental_h3_to_local_ij(h, p) for p in nb] # should be fine if we do not cross a face @@ -599,7 +615,7 @@ def test_from_local_ij_error(): baddies = [(1, -1), (-1, 1), (-1, -1)] for i, j in baddies: - with pytest.raises(H3ValueError): + with pytest.raises(H3FailedError): h3.experimental_local_ij_to_h3(h, i, j) # inverting output should give good data diff --git a/tests/test_error_codes.py b/tests/test_error_codes.py new file mode 100644 index 0000000..d8229b3 --- /dev/null +++ b/tests/test_error_codes.py @@ -0,0 +1,67 @@ +import pytest + +import h3 + +# todo: maybe check the `check_for_error` function behavior directly? + + +h3_exceptions = { + # h3.UnknownH3ErrorCode + h3.H3BaseException: None, + + h3.H3GridNavigationError: None, + h3.H3MemoryError: None, + h3.H3ValueError: None, + + h3.H3FailedError: 1, + h3.H3DomainError: 2, + h3.H3LatLngDomainError: 3, + h3.H3ResDomainError: 4, + h3.H3CellInvalidError: 5, + h3.H3DirEdgeInvalidError: 6, + h3.H3UndirEdgeInvalidError: 7, + h3.H3VertexInvalidError: 8, + h3.H3PentagonError: 9, + h3.H3DuplicateInputError: 10, + h3.H3NotNeighborsError: 11, + h3.H3ResMismatchError: 12, + h3.H3MemoryAllocError: 13, + h3.H3MemoryBoundsError: 14, + h3.H3OptionInvalidError: 15, +} + + +def test_error_codes_match(): + """ + Should match the error codes given in `h3api.h.in` in the core C lib. + """ + + for err, code in h3_exceptions.items(): + assert err.h3_error_code == code + + +def test_unknown(): + weird_code = 1234 + + with pytest.raises(h3.UnknownH3ErrorCode) as excinfo: + raise h3.UnknownH3ErrorCode(weird_code) + err = excinfo.value + + assert isinstance(err, h3.UnknownH3ErrorCode) + assert err.args == (weird_code,) + + +def test_atributes(): + # errors always have an `h3_error_code` attribute + for err in h3_exceptions: + x = err.h3_error_code + assert (x is None) or (x > 0) + + # UnknownH3ErrorCode is a bit of a special case + weird_code = 1234 + with pytest.raises(h3.UnknownH3ErrorCode) as excinfo: + raise h3.UnknownH3ErrorCode(weird_code) + err = excinfo.value + + assert err.h3_error_code is None + assert err.args == (weird_code,) diff --git a/tests/test_h3.py b/tests/test_h3.py index 09a1e7d..73b1869 100644 --- a/tests/test_h3.py +++ b/tests/test_h3.py @@ -877,6 +877,7 @@ def test_h3_is_pentagon(): def test_h3_indexes_are_neighbors(): assert h3.h3_indexes_are_neighbors('8928308280fffff', '8928308280bffff') + assert not h3.h3_indexes_are_neighbors('821c07fffffffff', '8928308280fffff') @@ -884,7 +885,7 @@ def test_get_h3_unidirectional_edge(): out = h3.get_h3_unidirectional_edge('8928308280fffff', '8928308280bffff') assert h3.h3_unidirectional_edge_is_valid(out) - with pytest.raises(ValueError): + with pytest.raises(h3.H3NotNeighborsError): h3.get_h3_unidirectional_edge('821c07fffffffff', '8928308280fffff') @@ -982,5 +983,5 @@ def test_h3_line(): assert out == expected - with pytest.raises(ValueError): + with pytest.raises(h3.H3ResMismatchError): h3.h3_line(h1, '8001fffffffffff') diff --git a/tests/test_length_area.py b/tests/test_length_area.py index 8236199..b16ef90 100644 --- a/tests/test_length_area.py +++ b/tests/test_length_area.py @@ -1,8 +1,6 @@ import h3 import pytest -from h3 import H3ValueError - def approx2(a, b): if len(a) != len(b): @@ -103,13 +101,13 @@ def test_bad_units(): assert h3.h3_is_valid(h) assert h3.h3_unidirectional_edge_is_valid(e) - with pytest.raises(H3ValueError): + with pytest.raises(ValueError): h3.cell_area(h, unit='foot-pounds') - with pytest.raises(H3ValueError): - h3.exact_edge_length(h, unit='foot-pounds') + with pytest.raises(ValueError): + h3.exact_edge_length(e, unit='foot-pounds') - with pytest.raises(H3ValueError): + with pytest.raises(ValueError): h3.point_dist((0, 0), (0, 0), unit='foot-pounds') diff --git a/tests/test_polyfill.py b/tests/test_polyfill.py index 7c35bb6..58faaff 100644 --- a/tests/test_polyfill.py +++ b/tests/test_polyfill.py @@ -2,7 +2,7 @@ import h3 import itertools import pytest -from h3 import H3ResolutionError +from h3 import H3ResDomainError def reverse(loop): @@ -211,10 +211,10 @@ def test_resolution(): 'coordinates': [[]], } - with pytest.raises(H3ResolutionError): + with pytest.raises(H3ResDomainError): h3.polyfill(d, -1) - with pytest.raises(H3ResolutionError): + with pytest.raises(H3ResDomainError): h3.polyfill(d, 16) From d12658336bc6d0dad225dedb1a0eb311a1f87010 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 17:33:21 -0700 Subject: [PATCH 37/60] Not sure why, but self.ptr = NULL fixes the bug --- src/h3/_cy/geo.pyx | 16 +++++----------- src/h3/_cy/memory.pyx | 3 +++ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/h3/_cy/geo.pyx b/src/h3/_cy/geo.pyx index e229bb2..b793670 100644 --- a/src/h3/_cy/geo.pyx +++ b/src/h3/_cy/geo.pyx @@ -169,17 +169,11 @@ def polyfill_polygon(outer, int res, holes=None, bool lnglat_order=False): h3lib.maxPolygonToCellsSize(&gp.gp, res, 0, &n) ) - ptr = create_ptr(n) - err = h3lib.polygonToCells(&gp.gp, res, 0, ptr) - mv = create_mv(ptr, n) - check_for_error(err) - - # todo: I don't understand why this code below breaks the tests. - # hmm = H3MemoryManager(n) - # check_for_error( - # h3lib.polygonToCells(&gp.gp, res, 0, hmm.ptr) - # ) - # mv = hmm.create_mv() + hmm = H3MemoryManager(n) + check_for_error( + h3lib.polygonToCells(&gp.gp, res, 0, hmm.ptr) + ) + mv = hmm.create_mv() return mv diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index 1625bca..df1d38b 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -71,6 +71,8 @@ cdef class H3MemoryManager: self.n = move_nonzeros(self.ptr, self.n) if self.n <= 0: stdlib.free(self.ptr) + self.ptr = NULL + self.n = 0 return empty_memory_view() self.ptr = stdlib.realloc(self.ptr, self.n*sizeof(H3int)) @@ -93,6 +95,7 @@ cdef class H3MemoryManager: # If the pointer is *not* NULL, then this means the MemoryManager # has is still responsible for the memory (it hasn't given the memory away to another object). stdlib.free(self.ptr) + self.ptr = NULL """ From 24eef340b3f03277fc371bc6195dae1fa115d37c Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 17:46:32 -0700 Subject: [PATCH 38/60] remove create_ptr and create_mv --- src/h3/_cy/geo.pyx | 2 -- src/h3/_cy/util.pxd | 6 ------ src/h3/_cy/util.pyx | 22 ++-------------------- 3 files changed, 2 insertions(+), 28 deletions(-) diff --git a/src/h3/_cy/geo.pyx b/src/h3/_cy/geo.pyx index b793670..619ec53 100644 --- a/src/h3/_cy/geo.pyx +++ b/src/h3/_cy/geo.pyx @@ -8,8 +8,6 @@ from .util cimport ( check_cell, check_edge, check_res, - create_ptr, - create_mv, deg2coord, coord2deg, ) diff --git a/src/h3/_cy/util.pxd b/src/h3/_cy/util.pxd index 6a34b75..750316b 100644 --- a/src/h3/_cy/util.pxd +++ b/src/h3/_cy/util.pxd @@ -6,13 +6,7 @@ cdef (double, double) coord2deg(LatLng c) nogil cpdef H3int hex2int(H3str h) except? 0 cpdef H3str int2hex(H3int x) -cdef H3int* create_ptr(size_t n) except? NULL -cdef H3int[:] create_mv(H3int* ptr, size_t n) - - cdef check_cell(H3int h) cdef check_edge(H3int e) cdef check_res(int res) cdef check_distance(int k) - -cdef H3int[:] empty_memory_view() # want to drop this if possible diff --git a/src/h3/_cy/util.pyx b/src/h3/_cy/util.pyx index fff0216..de5c298 100644 --- a/src/h3/_cy/util.pyx +++ b/src/h3/_cy/util.pyx @@ -11,6 +11,8 @@ from .error_system import ( H3CellInvalidError, ) +from .memory cimport H3MemoryManager + cdef h3lib.LatLng deg2coord(double lat, double lng) nogil: cdef: @@ -96,26 +98,6 @@ cdef H3int* create_ptr(size_t n) except? NULL: return ptr -cdef H3int[:] create_mv(H3int* ptr, size_t n): - cdef: - array x - - n = move_nonzeros(ptr, n) - if n <= 0: - stdlib.free(ptr) - return empty_memory_view() - - ptr = stdlib.realloc(ptr, n*sizeof(H3int)) - - if ptr is NULL: - raise MemoryError() - - x = ptr - x.callback_free_data = stdlib.free - - return x - - cpdef H3int[:] from_iter(hexes): """ hexes needs to be an iterable that knows its size... or should we have it match the np.fromiter function, which infers if not available? From 85ab47dae517323f6c07030beb6e197a117d635a Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:16:58 -0700 Subject: [PATCH 39/60] meh --- src/h3/_cy/memory.pxd | 1 + src/h3/_cy/memory.pyx | 14 +++++++++++ src/h3/_cy/util.pyx | 58 ++++--------------------------------------- 3 files changed, 20 insertions(+), 53 deletions(-) diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index afe8b9e..bc78c0a 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -10,6 +10,7 @@ cdef class H3MemoryManager: cdef int[:] int_mv(size_t n) cdef H3int[:] empty_memory_view() +cdef H3int[:] simple_mv(size_t n) """ todo: read: https://cython.readthedocs.io/en/latest/src/tutorial/pxd_files.html diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index df1d38b..c1e905d 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -56,6 +56,20 @@ cdef H3int[:] empty_memory_view(): return (a)[:0] +cdef H3int[:] simple_mv(size_t n): + if n == 0: + return empty_memory_view() + + ptr = stdlib.calloc(n, sizeof(H3int)) + if not ptr: + raise MemoryError() + + x = ptr + x.callback_free_data = stdlib.free + + return x + + cdef class H3MemoryManager: def __cinit__(self, size_t n): self.n = n diff --git a/src/h3/_cy/util.pyx b/src/h3/_cy/util.pyx index de5c298..5eec9cd 100644 --- a/src/h3/_cy/util.pyx +++ b/src/h3/_cy/util.pyx @@ -11,7 +11,11 @@ from .error_system import ( H3CellInvalidError, ) -from .memory cimport H3MemoryManager +from .memory cimport ( + H3MemoryManager, + empty_memory_view, + simple_mv, +) cdef h3lib.LatLng deg2coord(double lat, double lng) nogil: @@ -117,55 +121,3 @@ cpdef H3int[:] from_iter(hexes): x[i] = h return x - - -cdef size_t move_nonzeros(H3int* a, size_t n): - """ Move nonzero elements to front of array `a` of length `n`. - Return the number of nonzero elements. - - Loop invariant: Everything *before* `i` or *after* `j` is "done". - Move `i` and `j` inwards until they equal, and exit. - You can move `i` forward until there's a zero in front of it. - You can move `j` backward until there's a nonzero to the left of it. - Anything to the right of `j` is "junk" that can be reallocated. - - | a | b | 0 | c | d | ... | - ^ ^ - i j - - - | a | b | d | c | d | ... | - ^ ^ - i j - """ - cdef: - size_t i = 0 - size_t j = n - - while i < j: - if a[j-1] == 0: - j -= 1 - continue - - if a[i] != 0: - i += 1 - continue - - # if we're here, we know: - # a[i] == 0 - # a[j-1] != 0 - # i < j - # so we can swap! (actually, move a[j-1] -> a[i]) - a[i] = a[j-1] - j -= 1 - - return i - - -cdef H3int[:] empty_memory_view(): - # there's gotta be a better way to do this... - # create an empty cython.view.array? - cdef: - H3int a[1] - - return (a)[:0] From 4d90557d2465b1b8bb41649a216e346a1837eb40 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:22:49 -0700 Subject: [PATCH 40/60] bah --- src/h3/_cy/util.pyx | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/h3/_cy/util.pyx b/src/h3/_cy/util.pyx index 5eec9cd..96f5626 100644 --- a/src/h3/_cy/util.pyx +++ b/src/h3/_cy/util.pyx @@ -93,7 +93,6 @@ cdef check_distance(int k): ) -## todo: can i turn these two into a context manager? cdef H3int* create_ptr(size_t n) except? NULL: cdef H3int* ptr = stdlib.calloc(n, sizeof(H3int)) if (n > 0) and (not ptr): @@ -101,15 +100,9 @@ cdef H3int* create_ptr(size_t n) except? NULL: return ptr - -cpdef H3int[:] from_iter(hexes): - """ hexes needs to be an iterable that knows its size... - or should we have it match the np.fromiter function, which infers if not available? - """ +cdef H3int[:] foo(size_t n): cdef: array x - size_t n - n = len(hexes) if n == 0: return empty_memory_view() @@ -117,6 +110,20 @@ cpdef H3int[:] from_iter(hexes): x = create_ptr(n) x.callback_free_data = stdlib.free + return x + + +cpdef H3int[:] from_iter(hexes): + """ hexes needs to be an iterable that knows its size... + or should we have it match the np.fromiter function, which infers if not available? + """ + cdef: + # array x # this needs to be commented out to avoid an error + size_t n + n = len(hexes) + + x = foo(n) + for i,h in enumerate(hexes): x[i] = h From b2307b004006d1a1e1955fdeb57843e58fd915ff Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:25:59 -0700 Subject: [PATCH 41/60] works! --- src/h3/_cy/memory.pyx | 3 +++ src/h3/_cy/util.pyx | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index c1e905d..9f9bdf3 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -57,6 +57,9 @@ cdef H3int[:] empty_memory_view(): cdef H3int[:] simple_mv(size_t n): + cdef: + array x + if n == 0: return empty_memory_view() diff --git a/src/h3/_cy/util.pyx b/src/h3/_cy/util.pyx index 96f5626..5ad36b5 100644 --- a/src/h3/_cy/util.pyx +++ b/src/h3/_cy/util.pyx @@ -122,7 +122,7 @@ cpdef H3int[:] from_iter(hexes): size_t n n = len(hexes) - x = foo(n) + x = simple_mv(n) for i,h in enumerate(hexes): x[i] = h From e945f1fd4c1b106dd4fc1bd08375bab9a343f040 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:27:32 -0700 Subject: [PATCH 42/60] clean up --- src/h3/_cy/util.pyx | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/src/h3/_cy/util.pyx b/src/h3/_cy/util.pyx index 5ad36b5..073841d 100644 --- a/src/h3/_cy/util.pyx +++ b/src/h3/_cy/util.pyx @@ -11,11 +11,7 @@ from .error_system import ( H3CellInvalidError, ) -from .memory cimport ( - H3MemoryManager, - empty_memory_view, - simple_mv, -) +from .memory cimport simple_mv cdef h3lib.LatLng deg2coord(double lat, double lng) nogil: @@ -93,26 +89,6 @@ cdef check_distance(int k): ) -cdef H3int* create_ptr(size_t n) except? NULL: - cdef H3int* ptr = stdlib.calloc(n, sizeof(H3int)) - if (n > 0) and (not ptr): - raise MemoryError() - - return ptr - -cdef H3int[:] foo(size_t n): - cdef: - array x - - if n == 0: - return empty_memory_view() - - x = create_ptr(n) - x.callback_free_data = stdlib.free - - return x - - cpdef H3int[:] from_iter(hexes): """ hexes needs to be an iterable that knows its size... or should we have it match the np.fromiter function, which infers if not available? From e8b245747deade1b4ef0068861326c3243023123 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:29:21 -0700 Subject: [PATCH 43/60] works --- src/h3/_cy/util.pyx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/h3/_cy/util.pyx b/src/h3/_cy/util.pyx index 073841d..76779e0 100644 --- a/src/h3/_cy/util.pyx +++ b/src/h3/_cy/util.pyx @@ -96,9 +96,8 @@ cpdef H3int[:] from_iter(hexes): cdef: # array x # this needs to be commented out to avoid an error size_t n - n = len(hexes) - x = simple_mv(n) + x = simple_mv(len(hexes)) for i,h in enumerate(hexes): x[i] = h From 8d2acb90c0343c61af8525cb3ba40003d70bba72 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:32:40 -0700 Subject: [PATCH 44/60] clean --- src/h3/_cy/util.pyx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/h3/_cy/util.pyx b/src/h3/_cy/util.pyx index 76779e0..976fac0 100644 --- a/src/h3/_cy/util.pyx +++ b/src/h3/_cy/util.pyx @@ -93,9 +93,7 @@ cpdef H3int[:] from_iter(hexes): """ hexes needs to be an iterable that knows its size... or should we have it match the np.fromiter function, which infers if not available? """ - cdef: - # array x # this needs to be commented out to avoid an error - size_t n + # cdef array x # this needs to be commented out to avoid an error x = simple_mv(len(hexes)) From 7307e97ca51847490067a9591e1319c3a818bb01 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:35:25 -0700 Subject: [PATCH 45/60] maybe we don't need this? --- src/h3/_cy/memory.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index 9f9bdf3..989a0e0 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -57,8 +57,8 @@ cdef H3int[:] empty_memory_view(): cdef H3int[:] simple_mv(size_t n): - cdef: - array x + # cdef: + # array x if n == 0: return empty_memory_view() From 28dc3698b206f5b97875ced65e0dc76a6a26ff04 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:36:06 -0700 Subject: [PATCH 46/60] comments --- src/h3/_cy/cells.pyx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 4abcbe1..19b910c 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -225,8 +225,6 @@ cpdef H3int[:] compact(const H3int[:] hu): h3lib.compactCells(&hu[0], hmm.ptr, n) ) - # todo: mystery: why does this break in the tests when you move it before the error check? - # ideally, the create_mv method would be robust to borked data... mv = hmm.create_mv() return mv From 7afc536caeb9bbae1d6c2f0cd251e09a99501eb2 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:36:18 -0700 Subject: [PATCH 47/60] line --- src/h3/_cy/cells.pyx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 19b910c..d4930a0 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -224,7 +224,6 @@ cpdef H3int[:] compact(const H3int[:] hu): check_for_error( h3lib.compactCells(&hu[0], hmm.ptr, n) ) - mv = hmm.create_mv() return mv From 55c4343396f53bcd23ffc66b1652be9d49e79865 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:45:28 -0700 Subject: [PATCH 48/60] nice and clean --- src/h3/_cy/cells.pyx | 6 ++---- src/h3/_cy/geo.pyx | 3 --- src/h3/_cy/memory.pxd | 1 - src/h3/_cy/util.pyx | 2 -- 4 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index d4930a0..7e961cf 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -1,6 +1,5 @@ cimport h3lib from .h3lib cimport bool, int64_t, H3int, H3ErrorCodes -from libc cimport stdlib from .util cimport ( check_cell, @@ -16,7 +15,6 @@ from .error_system cimport ( from .memory cimport ( H3MemoryManager, int_mv, - empty_memory_view, # todo: want to get rid of this! ) # todo: add notes about Cython exception handling @@ -214,7 +212,7 @@ cpdef H3int[:] compact(const H3int[:] hu): # note: open to better ideas! if len(hu) == 0: - return empty_memory_view() + return H3MemoryManager(0).create_mv() for h in hu: ## todo: should we have an array version? would that be faster? check_cell(h) @@ -242,7 +240,7 @@ cpdef H3int[:] uncompact(const H3int[:] hc, int res): if len(hc) == 0: - return empty_memory_view() + return H3MemoryManager(0).create_mv() for h in hc: check_cell(h) diff --git a/src/h3/_cy/geo.pyx b/src/h3/_cy/geo.pyx index 619ec53..ef77259 100644 --- a/src/h3/_cy/geo.pyx +++ b/src/h3/_cy/geo.pyx @@ -12,9 +12,6 @@ from .util cimport ( coord2deg, ) -from libc cimport stdlib -from libc.stdint cimport uint64_t - from .error_system cimport check_for_error from .memory cimport H3MemoryManager diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index bc78c0a..8c5b17f 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -9,7 +9,6 @@ cdef class H3MemoryManager: cdef int[:] int_mv(size_t n) -cdef H3int[:] empty_memory_view() cdef H3int[:] simple_mv(size_t n) """ diff --git a/src/h3/_cy/util.pyx b/src/h3/_cy/util.pyx index 976fac0..b026801 100644 --- a/src/h3/_cy/util.pyx +++ b/src/h3/_cy/util.pyx @@ -1,5 +1,3 @@ -from libc cimport stdlib -from cython.view cimport array from .h3lib cimport H3int, H3str, isValidCell, isValidDirectedEdge cimport h3lib From 7811697188eea571eb4954eee6f875d5d2f1c1ab Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:49:01 -0700 Subject: [PATCH 49/60] mv_from_iter should be under memory module --- src/h3/_cy/__init__.py | 5 ++++- src/h3/_cy/memory.pxd | 1 + src/h3/_cy/memory.pyx | 14 ++++++++++++++ src/h3/_cy/util.pyx | 17 ----------------- src/h3/api/basic_int/_binding.py | 2 +- src/h3/api/basic_str/_binding.py | 2 +- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/h3/_cy/__init__.py b/src/h3/_cy/__init__.py index 8496c05..8b83289 100644 --- a/src/h3/_cy/__init__.py +++ b/src/h3/_cy/__init__.py @@ -69,7 +69,10 @@ from .util import ( c_version, hex2int, int2hex, - from_iter, +) + +from .memory import ( + mv_from_iter, ) from .error_system import ( diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index 8c5b17f..91e6de0 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -10,6 +10,7 @@ cdef class H3MemoryManager: cdef int[:] int_mv(size_t n) cdef H3int[:] simple_mv(size_t n) +cpdef H3int[:] mv_from_iter(hexes) """ todo: read: https://cython.readthedocs.io/en/latest/src/tutorial/pxd_files.html diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index 989a0e0..afc353b 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -133,3 +133,17 @@ cdef int[:] int_mv(size_t n): arr.callback_free_data = stdlib.free return arr + + +cpdef H3int[:] mv_from_iter(hexes): + """ hexes needs to be an iterable that knows its size... + or should we have it match the np.fromiter function, which infers if not available? + """ + # cdef array x # this needs to be commented out to avoid an error + + x = simple_mv(len(hexes)) + + for i,h in enumerate(hexes): + x[i] = h + + return x diff --git a/src/h3/_cy/util.pyx b/src/h3/_cy/util.pyx index b026801..16c6df4 100644 --- a/src/h3/_cy/util.pyx +++ b/src/h3/_cy/util.pyx @@ -9,9 +9,6 @@ from .error_system import ( H3CellInvalidError, ) -from .memory cimport simple_mv - - cdef h3lib.LatLng deg2coord(double lat, double lng) nogil: cdef: h3lib.LatLng c @@ -85,17 +82,3 @@ cdef check_distance(int k): raise H3DomainError( 'Grid distances must be nonnegative. Received: {}'.format(k) ) - - -cpdef H3int[:] from_iter(hexes): - """ hexes needs to be an iterable that knows its size... - or should we have it match the np.fromiter function, which infers if not available? - """ - # cdef array x # this needs to be commented out to avoid an error - - x = simple_mv(len(hexes)) - - for i,h in enumerate(hexes): - x[i] = h - - return x diff --git a/src/h3/api/basic_int/_binding.py b/src/h3/api/basic_int/_binding.py index a232287..3f46a5a 100644 --- a/src/h3/api/basic_int/_binding.py +++ b/src/h3/api/basic_int/_binding.py @@ -24,7 +24,7 @@ def _id(x): def _in_collection(hexes): it = list(hexes) - return _cy.from_iter(it) + return _cy.mv_from_iter(it) _binding = _API_FUNCTIONS( diff --git a/src/h3/api/basic_str/_binding.py b/src/h3/api/basic_str/_binding.py index 765fdbb..a3c33d4 100644 --- a/src/h3/api/basic_str/_binding.py +++ b/src/h3/api/basic_str/_binding.py @@ -21,7 +21,7 @@ from .._api_template import _API_FUNCTIONS def _in_collection(hexes): it = [_cy.hex2int(h) for h in hexes] - return _cy.from_iter(it) + return _cy.mv_from_iter(it) def _out_unordered(mv): From 8a8813339cafb0fbef880452b21f589502ad62e9 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:54:17 -0700 Subject: [PATCH 50/60] move around --- src/h3/_cy/memory.pxd | 1 - src/h3/_cy/memory.pyx | 34 +++++++++++++++++----------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index 91e6de0..c733302 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -9,7 +9,6 @@ cdef class H3MemoryManager: cdef int[:] int_mv(size_t n) -cdef H3int[:] simple_mv(size_t n) cpdef H3int[:] mv_from_iter(hexes) """ diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index afc353b..d609b73 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -56,23 +56,6 @@ cdef H3int[:] empty_memory_view(): return (a)[:0] -cdef H3int[:] simple_mv(size_t n): - # cdef: - # array x - - if n == 0: - return empty_memory_view() - - ptr = stdlib.calloc(n, sizeof(H3int)) - if not ptr: - raise MemoryError() - - x = ptr - x.callback_free_data = stdlib.free - - return x - - cdef class H3MemoryManager: def __cinit__(self, size_t n): self.n = n @@ -135,6 +118,23 @@ cdef int[:] int_mv(size_t n): return arr +cdef H3int[:] simple_mv(size_t n): + # cdef: + # array x + + if n == 0: + return empty_memory_view() + + ptr = stdlib.calloc(n, sizeof(H3int)) + if not ptr: + raise MemoryError() + + x = ptr + x.callback_free_data = stdlib.free + + return x + + cpdef H3int[:] mv_from_iter(hexes): """ hexes needs to be an iterable that knows its size... or should we have it match the np.fromiter function, which infers if not available? From 11aa2eb14b4a607d18fa8daeb590575b0a4b387a Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 18:58:47 -0700 Subject: [PATCH 51/60] rename to iter_to_mv --- src/h3/_cy/__init__.py | 2 +- src/h3/_cy/cells.pyx | 24 ++++++++++++------------ src/h3/_cy/edges.pyx | 2 +- src/h3/_cy/geo.pyx | 2 +- src/h3/_cy/memory.pxd | 6 +++--- src/h3/_cy/memory.pyx | 4 ++-- src/h3/api/basic_int/_binding.py | 2 +- src/h3/api/basic_str/_binding.py | 2 +- 8 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/h3/_cy/__init__.py b/src/h3/_cy/__init__.py index 8b83289..e61bb4e 100644 --- a/src/h3/_cy/__init__.py +++ b/src/h3/_cy/__init__.py @@ -72,7 +72,7 @@ from .util import ( ) from .memory import ( - mv_from_iter, + iter_to_mv, ) from .error_system import ( diff --git a/src/h3/_cy/cells.pyx b/src/h3/_cy/cells.pyx index 7e961cf..2a05cd2 100644 --- a/src/h3/_cy/cells.pyx +++ b/src/h3/_cy/cells.pyx @@ -84,7 +84,7 @@ cpdef H3int[:] disk(H3int h, int k): check_for_error( h3lib.gridDisk(h, k, hmm.ptr) ) - mv = hmm.create_mv() + mv = hmm.to_mv() return mv @@ -120,7 +120,7 @@ cpdef H3int[:] _ring_fallback(H3int h, int k): if v != k: hmm.ptr[i] = 0 - mv = hmm.create_mv() + mv = hmm.to_mv() return mv @@ -137,7 +137,7 @@ cpdef H3int[:] ring(H3int h, int k): if err: mv = _ring_fallback(h, k) else: - mv = hmm.create_mv() + mv = hmm.to_mv() return mv @@ -178,7 +178,7 @@ cpdef H3int[:] children(H3int h, res=None): check_for_error( h3lib.cellToChildren(h, res, hmm.ptr) ) - mv = hmm.create_mv() + mv = hmm.to_mv() return mv @@ -212,7 +212,7 @@ cpdef H3int[:] compact(const H3int[:] hu): # note: open to better ideas! if len(hu) == 0: - return H3MemoryManager(0).create_mv() + return H3MemoryManager(0).to_mv() for h in hu: ## todo: should we have an array version? would that be faster? check_cell(h) @@ -222,7 +222,7 @@ cpdef H3int[:] compact(const H3int[:] hu): check_for_error( h3lib.compactCells(&hu[0], hmm.ptr, n) ) - mv = hmm.create_mv() + mv = hmm.to_mv() return mv @@ -240,7 +240,7 @@ cpdef H3int[:] uncompact(const H3int[:] hc, int res): if len(hc) == 0: - return H3MemoryManager(0).create_mv() + return H3MemoryManager(0).to_mv() for h in hc: check_cell(h) @@ -261,8 +261,8 @@ cpdef H3int[:] uncompact(const H3int[:] hc, int res): ) # todo: again! doesn't work if i have this before the error check! - # why isn't hmm.create_mv() robust to failures before it? - mv = hmm.create_mv() + # why isn't hmm.to_mv() robust to failures before it? + mv = hmm.to_mv() return mv @@ -341,7 +341,7 @@ cpdef H3int[:] line(H3int start, H3int end): could_not_find_line(err, start, end) # todo: probably here too? - mv = hmm.create_mv() + mv = hmm.to_mv() return mv @@ -356,7 +356,7 @@ cpdef H3int[:] get_pentagon_indexes(int res): check_for_error( h3lib.getPentagons(res, hmm.ptr) ) - mv = hmm.create_mv() + mv = hmm.to_mv() return mv @@ -368,7 +368,7 @@ cpdef H3int[:] get_res0_indexes(): check_for_error( h3lib.getRes0Cells(hmm.ptr) ) - mv = hmm.create_mv() + mv = hmm.to_mv() return mv diff --git a/src/h3/_cy/edges.pyx b/src/h3/_cy/edges.pyx index c3445eb..7f1701e 100644 --- a/src/h3/_cy/edges.pyx +++ b/src/h3/_cy/edges.pyx @@ -66,7 +66,7 @@ cpdef H3int[:] edges_from_cell(H3int origin): hmm = H3MemoryManager(6) h3lib.originToDirectedEdges(origin, hmm.ptr) - mv = hmm.create_mv() + mv = hmm.to_mv() return mv diff --git a/src/h3/_cy/geo.pyx b/src/h3/_cy/geo.pyx index ef77259..f294b12 100644 --- a/src/h3/_cy/geo.pyx +++ b/src/h3/_cy/geo.pyx @@ -168,7 +168,7 @@ def polyfill_polygon(outer, int res, holes=None, bool lnglat_order=False): check_for_error( h3lib.polygonToCells(&gp.gp, res, 0, hmm.ptr) ) - mv = hmm.create_mv() + mv = hmm.to_mv() return mv diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index c733302..12554fc 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -5,11 +5,11 @@ cdef class H3MemoryManager: size_t n H3int* ptr - cdef H3int[:] create_mv(self) + cdef H3int[:] to_mv(self) cdef int[:] int_mv(size_t n) -cpdef H3int[:] mv_from_iter(hexes) +cpdef H3int[:] iter_to_mv(hexes) """ todo: read: https://cython.readthedocs.io/en/latest/src/tutorial/pxd_files.html @@ -22,5 +22,5 @@ https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html - there's gotta be a cleaner way to create a empty_memory_view() - abolish any appearance of &thing[0]. (i.e., identical interfaces) - can i make the interface for all these memory views identical? -- also, what's going on with the segfault stuff when we `.create_mv()` before checking an error? why isn't it robust to that? +- also, what's going on with the segfault stuff when we `.to_mv()` before checking an error? why isn't it robust to that? """ diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index d609b73..6cae150 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -64,7 +64,7 @@ cdef class H3MemoryManager: if (self.n > 0) and (not self.ptr): raise MemoryError() - cdef H3int[:] create_mv(self): + cdef H3int[:] to_mv(self): cdef: array x @@ -135,7 +135,7 @@ cdef H3int[:] simple_mv(size_t n): return x -cpdef H3int[:] mv_from_iter(hexes): +cpdef H3int[:] iter_to_mv(hexes): """ hexes needs to be an iterable that knows its size... or should we have it match the np.fromiter function, which infers if not available? """ diff --git a/src/h3/api/basic_int/_binding.py b/src/h3/api/basic_int/_binding.py index 3f46a5a..adfdb6e 100644 --- a/src/h3/api/basic_int/_binding.py +++ b/src/h3/api/basic_int/_binding.py @@ -24,7 +24,7 @@ def _id(x): def _in_collection(hexes): it = list(hexes) - return _cy.mv_from_iter(it) + return _cy.iter_to_mv(it) _binding = _API_FUNCTIONS( diff --git a/src/h3/api/basic_str/_binding.py b/src/h3/api/basic_str/_binding.py index a3c33d4..e5b563b 100644 --- a/src/h3/api/basic_str/_binding.py +++ b/src/h3/api/basic_str/_binding.py @@ -21,7 +21,7 @@ from .._api_template import _API_FUNCTIONS def _in_collection(hexes): it = [_cy.hex2int(h) for h in hexes] - return _cy.mv_from_iter(it) + return _cy.iter_to_mv(it) def _out_unordered(mv): From bf5fb8a079b46484d6470f24872e831f769b65e1 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 23:25:28 -0700 Subject: [PATCH 52/60] memory allocator options --- src/h3/_cy/geo.pyx | 17 +++++++--- src/h3/_cy/memory.pxd | 3 -- src/h3/_cy/memory.pyx | 74 +++++++++++++++++++++++++++++++++++++------ 3 files changed, 76 insertions(+), 18 deletions(-) diff --git a/src/h3/_cy/geo.pyx b/src/h3/_cy/geo.pyx index f294b12..f6a5221 100644 --- a/src/h3/_cy/geo.pyx +++ b/src/h3/_cy/geo.pyx @@ -1,4 +1,3 @@ -from libc cimport stdlib from libc.stdint cimport uint64_t cimport h3lib @@ -16,6 +15,14 @@ from .error_system cimport check_for_error from .memory cimport H3MemoryManager +# TODO: We might be OK with taking the GIL for the functions in this module +from libc.stdlib cimport ( + # malloc as h3_malloc, # not used + calloc as h3_calloc, + realloc as h3_realloc, + free as h3_free, +) + cpdef H3int geo_to_h3(double lat, double lng, int res) except 1: cdef: @@ -68,7 +75,7 @@ cdef h3lib.GeoLoop make_geoloop(geos, bool lnglat_order=False) except *: gl.numVerts = len(geos) # todo: need for memory management - gl.verts = stdlib.calloc(gl.numVerts, sizeof(h3lib.LatLng)) + gl.verts = h3_calloc(gl.numVerts, sizeof(h3lib.LatLng)) if lnglat_order: latlng = (g[::-1] for g in geos) @@ -82,7 +89,7 @@ cdef h3lib.GeoLoop make_geoloop(geos, bool lnglat_order=False) except *: cdef free_geoloop(h3lib.GeoLoop* gl): - stdlib.free(gl.verts) + h3_free(gl.verts) gl.verts = NULL @@ -115,7 +122,7 @@ cdef class GeoPolygon: self.gp.holes = NULL if len(holes) > 0: - self.gp.holes = stdlib.calloc(len(holes), sizeof(h3lib.GeoLoop)) + self.gp.holes = h3_calloc(len(holes), sizeof(h3lib.GeoLoop)) for i, hole in enumerate(holes): self.gp.holes[i] = make_geoloop(hole, lnglat_order) @@ -126,7 +133,7 @@ cdef class GeoPolygon: for i in range(self.gp.numHoles): free_geoloop(&self.gp.holes[i]) - stdlib.free(self.gp.holes) + h3_free(self.gp.holes) def polyfill_polygon(outer, int res, holes=None, bool lnglat_order=False): diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index 12554fc..0d1d568 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -14,9 +14,6 @@ cpdef H3int[:] iter_to_mv(hexes) """ todo: read: https://cython.readthedocs.io/en/latest/src/tutorial/pxd_files.html -todo: should i be allocating with python memory functions? -https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html - ## things i'm not happy about with this current situation - there's gotta be a cleaner way to create a empty_memory_view() diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index 6cae150..246801a 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -1,7 +1,61 @@ -from libc cimport stdlib from cython.view cimport array from .h3lib cimport H3int +""" +### Memory allocation options + +We have a few options for the memory allocation functions. +There's a trade-off between using the Python allocators which let Python +track memory usage and offers some optimizations vs the system +allocators, which do not need to acquire the GIL. +""" + +""" +System allocation functions. These do not acquire the GIL. +""" +from libc.stdlib cimport ( + # malloc as h3_malloc, # not used + calloc as h3_calloc, + realloc as h3_realloc, + free as h3_free, +) + + +""" +PyMem_Raw* functions should just be wrappers around system allocators +also given in libc.stdlib. These functions do not acquire the GIL. + +Note that these do not have a calloc function until py 3.5 and Cython 3.0, +so we would need to zero-out memory manually. + +https://python.readthedocs.io/en/stable/c-api/memory.html#raw-memory-interface +""" +# from cpython.mem cimport ( +# PyMem_RawMalloc as h3_malloc, +# # PyMem_RawCalloc as h3_calloc, # only in py3.5 (and cython 3.0?) +# PyMem_RawRealloc as h3_realloc, +# PyMem_RawFree as h3_free, +# ) + + +""" +These functions use the Python allocator (instead of the system allocator), +which offers some optimizations for Python, and allows Python to track +memory usage. However, these functions must acquire the GIL. + +Note that these do not have a calloc function until py 3.5 and Cython 3.0, +so we would need to zero-out memory manually. + +https://cython.readthedocs.io/en/stable/src/tutorial/memory_allocation.html +https://python.readthedocs.io/en/stable/c-api/memory.html#memory-interface +""" +# from cpython.mem cimport ( +# PyMem_Malloc as h3_malloc, +# # PyMem_Calloc as h3_calloc, # only in Python >=3.5 (and Cython >=3.0?) +# PyMem_Realloc as h3_realloc, +# PyMem_Free as h3_free, +# ) + cdef size_t move_nonzeros(H3int* a, size_t n): """ Move nonzero elements to front of array `a` of length `n`. @@ -59,7 +113,7 @@ cdef H3int[:] empty_memory_view(): cdef class H3MemoryManager: def __cinit__(self, size_t n): self.n = n - self.ptr = stdlib.calloc(self.n, sizeof(H3int)) + self.ptr = h3_calloc(self.n, sizeof(H3int)) if (self.n > 0) and (not self.ptr): raise MemoryError() @@ -70,18 +124,18 @@ cdef class H3MemoryManager: self.n = move_nonzeros(self.ptr, self.n) if self.n <= 0: - stdlib.free(self.ptr) + h3_free(self.ptr) self.ptr = NULL self.n = 0 return empty_memory_view() - self.ptr = stdlib.realloc(self.ptr, self.n*sizeof(H3int)) + self.ptr = h3_realloc(self.ptr, self.n*sizeof(H3int)) if self.ptr is NULL: raise MemoryError() x = self.ptr - x.callback_free_data = stdlib.free + x.callback_free_data = h3_free # responsibility for the memory moves from this object to the array/memoryview self.ptr = NULL @@ -94,7 +148,7 @@ cdef class H3MemoryManager: # should be NULL, and deallocing on NULL is fine. # If the pointer is *not* NULL, then this means the MemoryManager # has is still responsible for the memory (it hasn't given the memory away to another object). - stdlib.free(self.ptr) + h3_free(self.ptr) self.ptr = NULL @@ -108,12 +162,12 @@ cdef int[:] int_mv(size_t n): if n <= 0: raise MemoryError() - ptr = stdlib.calloc(n, sizeof(int)) + ptr = h3_calloc(n, sizeof(int)) if ptr is NULL: raise MemoryError() arr = ptr - arr.callback_free_data = stdlib.free + arr.callback_free_data = h3_free return arr @@ -125,12 +179,12 @@ cdef H3int[:] simple_mv(size_t n): if n == 0: return empty_memory_view() - ptr = stdlib.calloc(n, sizeof(H3int)) + ptr = h3_calloc(n, sizeof(H3int)) if not ptr: raise MemoryError() x = ptr - x.callback_free_data = stdlib.free + x.callback_free_data = h3_free return x From b8f53dbd805bea299c2307dcfa67cfae7c130ffb Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 23:46:07 -0700 Subject: [PATCH 53/60] _remove_zeros helper method --- src/h3/_cy/memory.pxd | 2 ++ src/h3/_cy/memory.pyx | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index 0d1d568..685059c 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -6,11 +6,13 @@ cdef class H3MemoryManager: H3int* ptr cdef H3int[:] to_mv(self) + cdef _remove_zeros(self) # not sure i really want this exposed here cdef int[:] int_mv(size_t n) cpdef H3int[:] iter_to_mv(hexes) + """ todo: read: https://cython.readthedocs.io/en/latest/src/tutorial/pxd_files.html diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index 246801a..f5619e2 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -118,22 +118,25 @@ cdef class H3MemoryManager: if (self.n > 0) and (not self.ptr): raise MemoryError() - cdef H3int[:] to_mv(self): - cdef: - array x - + cdef _remove_zeros(self): self.n = move_nonzeros(self.ptr, self.n) if self.n <= 0: h3_free(self.ptr) self.ptr = NULL self.n = 0 - return empty_memory_view() self.ptr = h3_realloc(self.ptr, self.n*sizeof(H3int)) - - if self.ptr is NULL: + if not self.ptr: raise MemoryError() + cdef H3int[:] to_mv(self): + cdef: + array x + + self._remove_zeros() + if self.n == 0: + return empty_memory_view() + x = self.ptr x.callback_free_data = h3_free @@ -149,7 +152,6 @@ cdef class H3MemoryManager: # If the pointer is *not* NULL, then this means the MemoryManager # has is still responsible for the memory (it hasn't given the memory away to another object). h3_free(self.ptr) - self.ptr = NULL """ From e1f56db442d784db854283ba8f1e56c864ad0383 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sat, 30 Jul 2022 23:55:59 -0700 Subject: [PATCH 54/60] little cleanup --- src/h3/_cy/memory.pxd | 1 - src/h3/_cy/memory.pyx | 29 +++++++++++++++++------------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index 685059c..68d6380 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -6,7 +6,6 @@ cdef class H3MemoryManager: H3int* ptr cdef H3int[:] to_mv(self) - cdef _remove_zeros(self) # not sure i really want this exposed here cdef int[:] int_mv(size_t n) diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index f5619e2..f2837ce 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -110,6 +110,21 @@ cdef H3int[:] empty_memory_view(): return (a)[:0] +cdef (H3int*, size_t) _remove_zeros(H3int* ptr, size_t n): + n = move_nonzeros(ptr, n) + if n <= 0: + h3_free(ptr) + ptr = NULL + n = 0 + + ptr = h3_realloc(ptr, n*sizeof(H3int)) + if not ptr: + raise MemoryError() + + return ptr, n + + + cdef class H3MemoryManager: def __cinit__(self, size_t n): self.n = n @@ -118,22 +133,12 @@ cdef class H3MemoryManager: if (self.n > 0) and (not self.ptr): raise MemoryError() - cdef _remove_zeros(self): - self.n = move_nonzeros(self.ptr, self.n) - if self.n <= 0: - h3_free(self.ptr) - self.ptr = NULL - self.n = 0 - - self.ptr = h3_realloc(self.ptr, self.n*sizeof(H3int)) - if not self.ptr: - raise MemoryError() - cdef H3int[:] to_mv(self): cdef: array x - self._remove_zeros() + self.ptr, self.n = _remove_zeros(self.ptr, self.n) + if self.n == 0: return empty_memory_view() From bfa13ed6d172f1c19cf86f10cea5d669dbf26fe5 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sun, 31 Jul 2022 00:04:46 -0700 Subject: [PATCH 55/60] add method: _create_mv --- src/h3/_cy/memory.pxd | 1 + src/h3/_cy/memory.pyx | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index 68d6380..672c8f2 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -6,6 +6,7 @@ cdef class H3MemoryManager: H3int* ptr cdef H3int[:] to_mv(self) + cdef H3int[:] _create_mv(self) cdef int[:] int_mv(size_t n) diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index f2837ce..f17b323 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -123,22 +123,26 @@ cdef (H3int*, size_t) _remove_zeros(H3int* ptr, size_t n): return ptr, n +""" +TODO: think about the case when n=0 +"Requesting zero elements or elements of size zero bytes returns a distinct non-NULL pointer if possible, as if PyMem_RawCalloc(1, 1) had been called instead." +do we not actually need to special case it? +""" cdef class H3MemoryManager: def __cinit__(self, size_t n): self.n = n self.ptr = h3_calloc(self.n, sizeof(H3int)) + # todo: avoid n=0 special case because of allocation non-null pointer guarantee? if (self.n > 0) and (not self.ptr): raise MemoryError() - cdef H3int[:] to_mv(self): + cdef H3int[:] _create_mv(self): cdef: array x - self.ptr, self.n = _remove_zeros(self.ptr, self.n) - if self.n == 0: return empty_memory_view() @@ -151,6 +155,10 @@ cdef class H3MemoryManager: return x + cdef H3int[:] to_mv(self): + self.ptr, self.n = _remove_zeros(self.ptr, self.n) + return self._create_mv() + def __dealloc__(self): # If the memory has been handed off to a memoryview, this pointer # should be NULL, and deallocing on NULL is fine. From 49e114732b636d7217639729d63715a7829bf224 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sun, 31 Jul 2022 00:10:27 -0700 Subject: [PATCH 56/60] don't need the n==0 case --- src/h3/_cy/memory.pyx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index f17b323..073d7e2 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -112,7 +112,7 @@ cdef H3int[:] empty_memory_view(): cdef (H3int*, size_t) _remove_zeros(H3int* ptr, size_t n): n = move_nonzeros(ptr, n) - if n <= 0: + if n == 0: h3_free(ptr) ptr = NULL n = 0 @@ -135,8 +135,7 @@ cdef class H3MemoryManager: self.n = n self.ptr = h3_calloc(self.n, sizeof(H3int)) - # todo: avoid n=0 special case because of allocation non-null pointer guarantee? - if (self.n > 0) and (not self.ptr): + if not self.ptr: raise MemoryError() cdef H3int[:] _create_mv(self): From 865326af426f81fcda484190df07eed52841e845 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sun, 31 Jul 2022 00:10:51 -0700 Subject: [PATCH 57/60] another comment on n==0 --- src/h3/_cy/memory.pyx | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index 073d7e2..d6496ab 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -123,12 +123,6 @@ cdef (H3int*, size_t) _remove_zeros(H3int* ptr, size_t n): return ptr, n -""" -TODO: think about the case when n=0 -"Requesting zero elements or elements of size zero bytes returns a distinct non-NULL pointer if possible, as if PyMem_RawCalloc(1, 1) had been called instead." -do we not actually need to special case it? - -""" cdef class H3MemoryManager: def __cinit__(self, size_t n): From 2d8f4167eab26615136e1b8bd1d52285252feded Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sun, 31 Jul 2022 00:15:41 -0700 Subject: [PATCH 58/60] mv is better than x --- src/h3/_cy/memory.pyx | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index d6496ab..dbdb6f1 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -134,19 +134,19 @@ cdef class H3MemoryManager: cdef H3int[:] _create_mv(self): cdef: - array x + array mv if self.n == 0: return empty_memory_view() - x = self.ptr - x.callback_free_data = h3_free + mv = self.ptr + mv.callback_free_data = h3_free # responsibility for the memory moves from this object to the array/memoryview self.ptr = NULL self.n = 0 - return x + return mv cdef H3int[:] to_mv(self): self.ptr, self.n = _remove_zeros(self.ptr, self.n) @@ -182,7 +182,7 @@ cdef int[:] int_mv(size_t n): cdef H3int[:] simple_mv(size_t n): # cdef: - # array x + # array mv if n == 0: return empty_memory_view() @@ -191,10 +191,10 @@ cdef H3int[:] simple_mv(size_t n): if not ptr: raise MemoryError() - x = ptr - x.callback_free_data = h3_free + mv = ptr + mv.callback_free_data = h3_free - return x + return mv cpdef H3int[:] iter_to_mv(hexes): @@ -203,9 +203,9 @@ cpdef H3int[:] iter_to_mv(hexes): """ # cdef array x # this needs to be commented out to avoid an error - x = simple_mv(len(hexes)) + mv = simple_mv(len(hexes)) for i,h in enumerate(hexes): - x[i] = h + mv[i] = h - return x + return mv From 3b3f9272f46f351ebc9d8cbd66803810a472c3d8 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sun, 31 Jul 2022 00:33:56 -0700 Subject: [PATCH 59/60] organize --- src/h3/_cy/memory.pxd | 4 +++- src/h3/_cy/memory.pyx | 26 +++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index 672c8f2..4f10c05 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -6,7 +6,10 @@ cdef class H3MemoryManager: H3int* ptr cdef H3int[:] to_mv(self) + + # private methods cdef H3int[:] _create_mv(self) + cdef _remove_zeros(self) cdef int[:] int_mv(size_t n) @@ -18,7 +21,6 @@ todo: read: https://cython.readthedocs.io/en/latest/src/tutorial/pxd_files.html ## things i'm not happy about with this current situation -- there's gotta be a cleaner way to create a empty_memory_view() - abolish any appearance of &thing[0]. (i.e., identical interfaces) - can i make the interface for all these memory views identical? - also, what's going on with the segfault stuff when we `.to_mv()` before checking an error? why isn't it robust to that? diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index dbdb6f1..b02c5f0 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -110,20 +110,6 @@ cdef H3int[:] empty_memory_view(): return (a)[:0] -cdef (H3int*, size_t) _remove_zeros(H3int* ptr, size_t n): - n = move_nonzeros(ptr, n) - if n == 0: - h3_free(ptr) - ptr = NULL - n = 0 - - ptr = h3_realloc(ptr, n*sizeof(H3int)) - if not ptr: - raise MemoryError() - - return ptr, n - - cdef class H3MemoryManager: def __cinit__(self, size_t n): self.n = n @@ -132,6 +118,16 @@ cdef class H3MemoryManager: if not self.ptr: raise MemoryError() + cdef _remove_zeros(self): + self.n = move_nonzeros(self.ptr, self.n) + if self.n == 0: + h3_free(self.ptr) + self.ptr = NULL + + self.ptr = h3_realloc(self.ptr, self.n*sizeof(H3int)) + if not self.ptr: + raise MemoryError() + cdef H3int[:] _create_mv(self): cdef: array mv @@ -149,7 +145,7 @@ cdef class H3MemoryManager: return mv cdef H3int[:] to_mv(self): - self.ptr, self.n = _remove_zeros(self.ptr, self.n) + self._remove_zeros() return self._create_mv() def __dealloc__(self): From 50c649c6b62b3ba77966b11f439cab178a8c8684 Mon Sep 17 00:00:00 2001 From: AJ Friend Date: Sun, 31 Jul 2022 00:43:26 -0700 Subject: [PATCH 60/60] avoiding superfluous methods in the memory manager, because I'm not sure there's a way to make them private --- src/h3/_cy/memory.pxd | 5 ---- src/h3/_cy/memory.pyx | 59 +++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/h3/_cy/memory.pxd b/src/h3/_cy/memory.pxd index 4f10c05..31aecd0 100644 --- a/src/h3/_cy/memory.pxd +++ b/src/h3/_cy/memory.pxd @@ -7,11 +7,6 @@ cdef class H3MemoryManager: cdef H3int[:] to_mv(self) - # private methods - cdef H3int[:] _create_mv(self) - cdef _remove_zeros(self) - - cdef int[:] int_mv(size_t n) cpdef H3int[:] iter_to_mv(hexes) diff --git a/src/h3/_cy/memory.pyx b/src/h3/_cy/memory.pyx index b02c5f0..0e406b4 100644 --- a/src/h3/_cy/memory.pyx +++ b/src/h3/_cy/memory.pyx @@ -110,43 +110,46 @@ cdef H3int[:] empty_memory_view(): return (a)[:0] +cdef _remove_zeros(H3MemoryManager x): + x.n = move_nonzeros(x.ptr, x.n) + if x.n == 0: + h3_free(x.ptr) + x.ptr = NULL + + x.ptr = h3_realloc(x.ptr, x.n*sizeof(H3int)) + if not x.ptr: + raise MemoryError() + + +cdef H3int[:] _create_mv(H3MemoryManager x): + cdef: + array mv + + if x.n == 0: + return empty_memory_view() + + mv = x.ptr + mv.callback_free_data = h3_free + + # responsibility for the memory moves from this object to the array/memoryview + x.ptr = NULL + x.n = 0 + + return mv + + cdef class H3MemoryManager: def __cinit__(self, size_t n): self.n = n self.ptr = h3_calloc(self.n, sizeof(H3int)) + # todo: do we actually have a memory leak when n = 0 because of the non-null pointer thing? if not self.ptr: raise MemoryError() - cdef _remove_zeros(self): - self.n = move_nonzeros(self.ptr, self.n) - if self.n == 0: - h3_free(self.ptr) - self.ptr = NULL - - self.ptr = h3_realloc(self.ptr, self.n*sizeof(H3int)) - if not self.ptr: - raise MemoryError() - - cdef H3int[:] _create_mv(self): - cdef: - array mv - - if self.n == 0: - return empty_memory_view() - - mv = self.ptr - mv.callback_free_data = h3_free - - # responsibility for the memory moves from this object to the array/memoryview - self.ptr = NULL - self.n = 0 - - return mv - cdef H3int[:] to_mv(self): - self._remove_zeros() - return self._create_mv() + _remove_zeros(self) + return _create_mv(self) def __dealloc__(self): # If the memory has been handed off to a memoryview, this pointer