Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace _out_unordered and _out_ordered with _out_collection #339

Merged
merged 18 commits into from
Mar 10, 2024
Merged
2 changes: 1 addition & 1 deletion src/h3/_cy/cells.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ cpdef get_icosahedron_faces(H3int h):

# 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}
out = [f for f in faces if f >= 0]

return out

Expand Down
23 changes: 11 additions & 12 deletions src/h3/api/basic_int/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
_in_scalar,
_out_scalar,
_in_collection,
_out_unordered,
_out_ordered,
_out_collection,
)


Expand Down Expand Up @@ -273,7 +272,7 @@ def grid_disk(h, k=1):
"""
mv = _cy.grid_disk(_in_scalar(h), k)

return _out_unordered(mv)
return _out_collection(mv)


def grid_ring(h, k=1):
Expand All @@ -293,7 +292,7 @@ def grid_ring(h, k=1):
"""
mv = _cy.grid_ring(_in_scalar(h), k)

return _out_unordered(mv)
return _out_collection(mv)


def cell_to_children(h, res=None):
Expand All @@ -313,7 +312,7 @@ def cell_to_children(h, res=None):
"""
mv = _cy.cell_to_children(_in_scalar(h), res)

return _out_unordered(mv)
return _out_collection(mv)


# todo: nogil for expensive C operation?
Expand All @@ -335,7 +334,7 @@ def compact_cells(cells):
hu = _in_collection(cells)
hc = _cy.compact_cells(hu)

return _out_unordered(hc)
return _out_collection(hc)


def uncompact_cells(cells, res):
Expand Down Expand Up @@ -363,7 +362,7 @@ def uncompact_cells(cells, res):
hc = _in_collection(cells)
hu = _cy.uncompact_cells(hc, res)

return _out_unordered(hu)
return _out_collection(hu)


def h3shape_to_cells(h3shape, res):
Expand Down Expand Up @@ -410,7 +409,7 @@ def h3shape_to_cells(h3shape, res):
else:
raise ValueError('Unrecognized type: ' + str(type(h3shape)))

return _out_unordered(mv)
return _out_collection(mv)


def cells_to_h3shape(cells, tight=True):
Expand Down Expand Up @@ -643,7 +642,7 @@ def origin_to_directed_edges(origin):
"""
mv = _cy.origin_to_directed_edges(_in_scalar(origin))

return _out_unordered(mv)
return _out_collection(mv)


def directed_edge_to_boundary(edge):
Expand All @@ -667,7 +666,7 @@ def grid_path_cells(start, end):
"""
mv = _cy.grid_path_cells(_in_scalar(start), _in_scalar(end))

return _out_ordered(mv)
return _out_collection(mv)


def is_res_class_III(h):
Expand Down Expand Up @@ -715,7 +714,7 @@ def get_pentagons(res):
"""
mv = _cy.get_pentagons(res)

return _out_unordered(mv)
return _out_collection(mv)


def get_res0_cells():
Expand All @@ -732,7 +731,7 @@ def get_res0_cells():
"""
mv = _cy.get_res0_cells()

return _out_unordered(mv)
return _out_collection(mv)


def cell_to_center_child(h, res=None):
Expand Down
3 changes: 1 addition & 2 deletions src/h3/api/basic_int/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ def _in_collection(cells):
return _cy.iter_to_mv(it)


_out_unordered = set
_out_ordered = list
_out_collection = list
8 changes: 1 addition & 7 deletions src/h3/api/basic_str/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,5 @@ def _in_collection(cells):
return _cy.iter_to_mv(it)


def _out_unordered(mv):
# todo: should this be an (immutable) frozenset?
return set(_cy.int_to_str(h) for h in mv)


def _out_ordered(mv):
# todo: should this be an (immutable) tuple?
def _out_collection(mv):
return list(_cy.int_to_str(h) for h in mv)
3 changes: 1 addition & 2 deletions src/h3/api/memview_int/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ def _id(x):
_in_scalar = _id
_out_scalar = _id
_in_collection = _id
_out_unordered = _id
_out_ordered = _id
_out_collection = _id
3 changes: 1 addition & 2 deletions src/h3/api/numpy_int/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ def _in_collection(x):
return np.asarray(x, dtype='uint64')


_out_unordered = _in_collection
_out_ordered = _in_collection
_out_collection = _in_collection
26 changes: 21 additions & 5 deletions tests/polyfill/test_h3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ def __geo_interface__(self):
return self.dictionary


def same_set(a, b):
ajfriend marked this conversation as resolved.
Show resolved Hide resolved
"""Test if two collections are the same if taken as sets"""
set_a = set(a)
set_b = set(b)

assert len(a) == len(b) == len(set_a) == len(set_b)

return set_a == set_b


def latlng_open():
return [
[37.813, -122.408],
Expand Down Expand Up @@ -111,9 +121,12 @@ def test_polyfill_with_hole():
out = h3.h3shape_to_cells(poly, res=9)
assert len(out) == 1214

foo = lambda x: h3.h3shape_to_cells(h3.H3Poly(x), 9)
# todo: foo = lambda x: h3.H3Poly(x).to_cells(9)
assert out == foo(sf_7x7) - foo(sf_hole1)
foo = lambda x: set(h3.h3shape_to_cells(h3.H3Poly(x), 9))

assert same_set(
out,
foo(sf_7x7) - foo(sf_hole1)
)


def test_polyfill_with_two_holes():
Expand All @@ -122,8 +135,11 @@ def test_polyfill_with_two_holes():
out = h3.h3shape_to_cells(poly, 9)
assert len(out) == 1172

foo = lambda x: h3.h3shape_to_cells(h3.H3Poly(x), 9)
assert out == foo(sf_7x7) - (foo(sf_hole1) | foo(sf_hole2))
foo = lambda x: set(h3.h3shape_to_cells(h3.H3Poly(x), 9))
assert same_set(
out,
foo(sf_7x7) - (foo(sf_hole1) | foo(sf_hole2))
)


def test_polyfill_geo_json_compliant():
Expand Down
29 changes: 21 additions & 8 deletions tests/polyfill/test_polyfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
from h3 import H3ResDomainError


def same_set(a, b):
"""Test if two collections are the same if taken as sets"""
set_a = set(a)
set_b = set(b)

assert len(a) == len(b) == len(set_a) == len(set_b)

return set_a == set_b


def get_us_box_coords():

# big center chunk of the US in lat/lng order
Expand Down Expand Up @@ -72,7 +82,7 @@ def test_h3shape_to_cells():
poly = h3.H3Poly(maine)
out = h3.h3shape_to_cells(poly, 3)

assert out == expected
assert same_set(out, expected)


def test_h3shape_to_cells2():
Expand Down Expand Up @@ -100,20 +110,23 @@ def test_h3shape_to_cells_holes():

for res in 1, 2, 3, 4, 5:
cells_all = h3.h3shape_to_cells(h3.H3Poly(outer), res)
cells_holes = h3.h3shape_to_cells(h3.H3Poly(outer, hole1, hole2), res=res)
cells_holes = set(h3.h3shape_to_cells(h3.H3Poly(outer, hole1, hole2), res=res))

cells_1 = h3.h3shape_to_cells(h3.H3Poly(hole1), res)
cells_2 = h3.h3shape_to_cells(h3.H3Poly(hole2), res)
cells_1 = set(h3.h3shape_to_cells(h3.H3Poly(hole1), res))
cells_2 = set(h3.h3shape_to_cells(h3.H3Poly(hole2), res))

assert len(cells_all) == len(cells_holes) + len(cells_1) + len(cells_2)
assert cells_all == set.union(cells_holes, cells_1, cells_2)
assert same_set(
cells_all,
set.union(cells_holes, cells_1, cells_2)
)


def test_resolution():
poly = h3.H3Poly([])

assert h3.h3shape_to_cells(poly, 0) == set()
assert h3.h3shape_to_cells(poly, 15) == set()
assert h3.h3shape_to_cells(poly, 0) == []
assert h3.h3shape_to_cells(poly, 15) == []

with pytest.raises(H3ResDomainError):
h3.h3shape_to_cells(poly, -1)
Expand Down Expand Up @@ -158,4 +171,4 @@ def test_cells_to_geo():
assert len(coord[0]) == 7
assert coord[0][0] == coord[0][-1]

assert h3.geo_to_cells(geo, res) == {h}
assert h3.geo_to_cells(geo, res) == [h]
4 changes: 2 additions & 2 deletions tests/polyfill/test_polygon_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ def test_repr():
a = '8928308280fffff'
b = h3.grid_ring(a, 5).pop()

cells1 = h3.grid_ring(b, 2) | {a}
cells2 = cells1 | {b}
cells1 = h3.grid_ring(b, 2) + [a]
cells2 = cells1 + [b]

mpoly1 = h3.cells_to_h3shape(cells1)
mpoly2 = h3.cells_to_h3shape(cells2)
Expand Down
23 changes: 18 additions & 5 deletions tests/polyfill/test_to_multipoly.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import h3


def same_set(a, b):
"""Test if two collections are the same if taken as sets"""
set_a = set(a)
set_b = set(b)

assert len(a) == len(b) == len(set_a) == len(set_b)

return set_a == set_b


def test_cells_to_h3shape():
h = '8928308280fffff'
cells = h3.grid_disk(h, 1)
Expand All @@ -11,7 +21,7 @@ def test_cells_to_h3shape():
poly2 = h3.H3Poly(poly.outer, *poly.holes)
out = h3.h3shape_to_cells(poly2, 9)

assert out == cells
assert same_set(out, cells)


def test_cells_to_h3shape_tight():
Expand All @@ -22,22 +32,25 @@ def test_cells_to_h3shape_tight():
poly2 = h3.H3Poly(poly.outer, *poly.holes)
out = h3.h3shape_to_cells(poly2, 9)

assert out == cells
assert same_set(out, cells)


def test_2_polys():
h = '8928308280fffff'
cells = h3.grid_ring(h, 2)
cells = cells | {h}
cells = cells + [h]
# cells should be a center hex, and the 2-ring around it
# (with the 1-ring being absent)

mpoly = h3.cells_to_h3shape(cells)

out = [
h3.h3shape_to_cells(poly, 9)
set(h3.h3shape_to_cells(poly, 9))
for poly in mpoly
]

assert set.union(*out) == cells
assert same_set(
set.union(*out),
cells
)
assert set(map(len, out)) == {1, 12}
28 changes: 19 additions & 9 deletions tests/test_basic_int.py → tests/test_apis/test_basic_int.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import h3.api.basic_int as h3


def same_set(a, b):
"""Test if two collections are the same if taken as sets"""
set_a = set(a)
set_b = set(b)

assert len(a) == len(b) == len(set_a) == len(set_b)

return set_a == set_b


def test_int_output():
lat = 37.7752702151959
lng = -122.418307270836
Expand All @@ -10,39 +20,39 @@ def test_int_output():


def test_grid_disk():
expected = {
expected = [
617700169957507071,
617700169957769215,
617700169958031359,
617700169958293503,
617700169961177087,
617700169964847103,
617700169965109247,
}
]

out = h3.grid_disk(617700169958293503, 1)
assert out == expected
assert same_set(out, expected)


def test_compact_cells():
h = 617700169958293503
cells = h3.cell_to_children(h)

assert h3.compact_cells(cells) == {h}
assert h3.compact_cells(cells) == [h]


def test_get_icosahedron_faces():
h = 577832942814887935
expected = {2, 3, 7, 8, 12}
expected = [2, 3, 7, 8, 12]
out = h3.get_icosahedron_faces(h)
assert out == expected
assert same_set(out, expected)

h = 579873636396040191
expected = {13}
expected = [13]
out = h3.get_icosahedron_faces(h)
assert out == expected

h = 579768083279773695
expected = {16, 15}
expected = [16, 15]
out = h3.get_icosahedron_faces(h)
assert out == expected
assert same_set(out, expected)
Loading