Skip to content

Commit

Permalink
Remove gj (#316)
Browse files Browse the repository at this point in the history
* Remove geojson logic from cell_to_boundary and directed_edge_to_boundary

* fix a warning about return values in tests from pytest

* Remove geo_json logic from cells_to_multi_polygon

* remove some unused tests

* add back in polygons_to_cells
  • Loading branch information
ajfriend authored Mar 26, 2023
1 parent cfc6ba3 commit 0ae6115
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 127 deletions.
14 changes: 2 additions & 12 deletions src/h3/_cy/geo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def polygons_to_cells(polygons, int res):
return hmm.to_mv()


def cell_to_boundary(H3int h, bool geo_json=False):
def cell_to_boundary(H3int h):
"""Compose an array of geo-coordinates that outlines a hexagonal cell"""
cdef:
h3lib.CellBoundary gb
Expand All @@ -206,15 +206,10 @@ def cell_to_boundary(H3int h, bool geo_json=False):
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)

return verts


def directed_edge_to_boundary(H3int edge, bool geo_json=False):
def directed_edge_to_boundary(H3int edge):
""" Returns the CellBoundary containing the coordinates of the edge
"""
cdef:
Expand All @@ -230,11 +225,6 @@ def directed_edge_to_boundary(H3int edge, bool geo_json=False):
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)

return verts


Expand Down
17 changes: 1 addition & 16 deletions src/h3/_cy/to_multipoly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,11 @@ def _to_multi_polygon(const H3int[:] cells):
return out


def _geojson_loop(loop):
""" Swap lat/lng order and close loop.
"""
loop = [e[::-1] for e in loop]
loop += [loop[0]]

return loop


def cells_to_multi_polygon(const H3int[:] cells, geo_json=False):
def cells_to_multi_polygon(const H3int[:] cells):
# todo: gotta be a more elegant way to handle these...
if len(cells) == 0:
return []

multipoly = _to_multi_polygon(cells)

if geo_json:
multipoly = [
[_geojson_loop(loop) for loop in poly]
for poly in multipoly
]

return multipoly
16 changes: 5 additions & 11 deletions src/h3/api/_api_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,25 +272,19 @@ def grid_distance(self, h1, h2):

return d

def cell_to_boundary(self, h, geo_json=False):
def cell_to_boundary(self, h):
"""
Return tuple of lat/lng pairs describing the cell boundary.
Parameters
----------
h : H3Cell
geo_json : bool, optional
If ``True``, return output in GeoJson format:
lng/lat pairs (opposite order), and
have the last pair be the same as the first.
If ``False`` (default), return lat/lng pairs, with the last
pair distinct from the first.
Returns
-------
tuple of (float, float) tuples
tuple of (lat, lng) tuples
"""
return _cy.cell_to_boundary(self._in_scalar(h), geo_json)
return _cy.cell_to_boundary(self._in_scalar(h))

def grid_disk(self, h, k=1):
"""
Expand Down Expand Up @@ -625,8 +619,8 @@ def origin_to_directed_edges(self, origin):

return self._out_unordered(mv)

def directed_edge_to_boundary(self, edge, geo_json=False):
return _cy.directed_edge_to_boundary(self._in_scalar(edge), geo_json=geo_json)
def directed_edge_to_boundary(self, edge):
return _cy.directed_edge_to_boundary(self._in_scalar(edge))

def grid_path_cells(self, start, end):
"""
Expand Down
42 changes: 14 additions & 28 deletions tests/test_cells_and_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,6 @@ def test3():
assert approx2(out, expected)


def test4():
expected = (
(-122.41719971841658, 37.775197782893386),
(-122.41612835779264, 37.77688044840226),
(-122.4173879761762, 37.778385004930925),
(-122.41971895414807, 37.77820687262238),
(-122.42079024541877, 37.77652420699321),
(-122.4195306280734, 37.775019673792606),
(-122.41719971841658, 37.775197782893386)
)

out = h3.cell_to_boundary('8928308280fffff', geo_json=True)
assert approx2(out, expected)


def test_grid_disk_distance():
with pytest.raises(H3DomainError):
h3.grid_disk('8928308280fffff', -10)
Expand Down Expand Up @@ -228,10 +213,9 @@ def test_distance_error():
h3.grid_distance(h1, h2)


def test_compact_cells():

def get_maine_cells():
# lat/lngs for State of Maine
maine = h3.Polygon([
poly = h3.Polygon([
(45.137451890638886, -67.13734351262877),
(44.8097, -66.96466),
(44.3252, -68.03252),
Expand All @@ -255,23 +239,25 @@ def test_compact_cells():
])

res = 5
h_uncomp = h3.polygon_to_cells(maine, res=res)
h_comp = h3.compact_cells(h_uncomp)

expected = {'852b114ffffffff', '852b189bfffffff', '852b1163fffffff', '842ba9bffffffff', '842bad3ffffffff', '852ba9cffffffff', '842badbffffffff', '852b1e8bfffffff', '852a346ffffffff', '842b1e3ffffffff', '852b116ffffffff', '842b185ffffffff', '852b1bdbfffffff', '852bad47fffffff', '852ba9c3fffffff', '852b106bfffffff', '852a30d3fffffff', '842b1edffffffff', '852b12a7fffffff', '852b1027fffffff', '842baddffffffff', '852a349bfffffff', '852b1227fffffff', '852a3473fffffff', '852b117bfffffff', '842ba99ffffffff', '852a341bfffffff', '852ba9d3fffffff', '852b1067fffffff', '852a3463fffffff', '852baca7fffffff', '852b116bfffffff', '852b1c6bfffffff', '852a3493fffffff', '852ba9dbfffffff', '852b180bfffffff', '842bad7ffffffff', '852b1063fffffff', '842ba93ffffffff', '852a3693fffffff', '852ba977fffffff', '852b1e9bfffffff', '852bad53fffffff', '852b100ffffffff', '852b102bfffffff', '852a3413fffffff', '852ba8b7fffffff', '852bad43fffffff', '852b1c6ffffffff', '852a340bfffffff', '852b103bfffffff', '852b1813fffffff', '852b12affffffff', '842a34dffffffff', '852b1873fffffff', '852b106ffffffff', '852b115bfffffff', '852baca3fffffff', '852b114bfffffff', '852b1143fffffff', '852a348bfffffff', '852a30d7fffffff', '852b181bfffffff', '842a345ffffffff', '852b1e8ffffffff', '852b1883fffffff', '852b1147fffffff', '852a3483fffffff', '852b12a3fffffff', '852a346bfffffff', '852ba9d7fffffff', '842b18dffffffff', '852b188bfffffff', '852a36a7fffffff', '852bacb3fffffff', '852b187bfffffff', '852bacb7fffffff', '842b1ebffffffff', '842b1e5ffffffff', '852ba8a7fffffff', '842bad9ffffffff', '852a36b7fffffff', '852a347bfffffff', '832b13fffffffff', '852ba9c7fffffff', '832b1afffffffff', '842ba91ffffffff', '852bad57fffffff', '852ba8affffffff', '852b1803fffffff', '842b1e7ffffffff', '852bad4ffffffff', '852b102ffffffff', '852b1077fffffff', '852b1237fffffff', '852b1153fffffff', '852a3697fffffff', '852a36b3fffffff', '842bad1ffffffff', '842b1e1ffffffff', '852b186bfffffff', '852b1023fffffff'} # noqa
cells_uncomp = h3.polygon_to_cells(poly, res=res)

assert h_comp == expected
# the expected result from h3.compact_cells(cells_uncomp)
cells_comp = {'852b114ffffffff', '852b189bfffffff', '852b1163fffffff', '842ba9bffffffff', '842bad3ffffffff', '852ba9cffffffff', '842badbffffffff', '852b1e8bfffffff', '852a346ffffffff', '842b1e3ffffffff', '852b116ffffffff', '842b185ffffffff', '852b1bdbfffffff', '852bad47fffffff', '852ba9c3fffffff', '852b106bfffffff', '852a30d3fffffff', '842b1edffffffff', '852b12a7fffffff', '852b1027fffffff', '842baddffffffff', '852a349bfffffff', '852b1227fffffff', '852a3473fffffff', '852b117bfffffff', '842ba99ffffffff', '852a341bfffffff', '852ba9d3fffffff', '852b1067fffffff', '852a3463fffffff', '852baca7fffffff', '852b116bfffffff', '852b1c6bfffffff', '852a3493fffffff', '852ba9dbfffffff', '852b180bfffffff', '842bad7ffffffff', '852b1063fffffff', '842ba93ffffffff', '852a3693fffffff', '852ba977fffffff', '852b1e9bfffffff', '852bad53fffffff', '852b100ffffffff', '852b102bfffffff', '852a3413fffffff', '852ba8b7fffffff', '852bad43fffffff', '852b1c6ffffffff', '852a340bfffffff', '852b103bfffffff', '852b1813fffffff', '852b12affffffff', '842a34dffffffff', '852b1873fffffff', '852b106ffffffff', '852b115bfffffff', '852baca3fffffff', '852b114bfffffff', '852b1143fffffff', '852a348bfffffff', '852a30d7fffffff', '852b181bfffffff', '842a345ffffffff', '852b1e8ffffffff', '852b1883fffffff', '852b1147fffffff', '852a3483fffffff', '852b12a3fffffff', '852a346bfffffff', '852ba9d7fffffff', '842b18dffffffff', '852b188bfffffff', '852a36a7fffffff', '852bacb3fffffff', '852b187bfffffff', '852bacb7fffffff', '842b1ebffffffff', '842b1e5ffffffff', '852ba8a7fffffff', '842bad9ffffffff', '852a36b7fffffff', '852a347bfffffff', '832b13fffffffff', '852ba9c7fffffff', '832b1afffffffff', '842ba91ffffffff', '852bad57fffffff', '852ba8affffffff', '852b1803fffffff', '842b1e7ffffffff', '852bad4ffffffff', '852b102ffffffff', '852b1077fffffff', '852b1237fffffff', '852b1153fffffff', '852a3697fffffff', '852a36b3fffffff', '842bad1ffffffff', '842b1e1ffffffff', '852b186bfffffff', '852b1023fffffff'} # noqa

return h_uncomp, h_comp, res
return cells_uncomp, cells_comp, res


def test_uncompact_cells():
def test_compact_cells():
cells_uncomp, cells_comp, _ = get_maine_cells()
out = h3.compact_cells(cells_uncomp)

h_uncomp, h_comp, res = test_compact_cells()
assert out == cells_comp

out = h3.uncompact_cells(h_comp, res)

assert out == h_uncomp
def test_uncompact_cells():
cells_uncomp, cells_comp, res = get_maine_cells()
out = h3.uncompact_cells(cells_comp, res)
assert out == cells_uncomp


def test_get_num_cells():
Expand Down
34 changes: 0 additions & 34 deletions tests/test_h3.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,6 @@ def test_cell_to_boundary():
assert o == approx(e)


def test_cell_to_boundary_geo_json():
out = h3.cell_to_boundary('85283473fffffff', True)

expected = [
[-121.91508032705622, 37.271355866731895],
[-121.86222328902491, 37.353926450852256],
[-121.9235499963016, 37.42834118609435],
[-122.0377349642703, 37.42012867767778],
[-122.09042892904395, 37.33755608435298],
[-122.02910130919, 37.26319797461824],
[-121.91508032705622, 37.271355866731895],
]

assert len(out) == len(expected)

for o, e in zip(out, expected):
assert o == approx(e)


def test_grid_disk():
h = '8928308280fffff'
out = h3.grid_disk(h, 1)
Expand Down Expand Up @@ -179,12 +160,6 @@ def test_polyfill():
assert '89283095edbffff' in out


# def test_polyfill_bogus_geo_json():
# with pytest.raises(ValueError):
# bad_geo = {'type': 'whatwhat'}
# h3.polyfill(bad_geo, 9)


def test_polyfill_with_hole():
poly = h3.Polygon(sf_7x7, sf_hole1)

Expand Down Expand Up @@ -586,15 +561,6 @@ def test_origin_to_directed_edges():
assert len(h3_uni_edge_pentagon) == 5


def test_directed_edge_to_boundary():
e = '11928308280fffff'
boundary = h3.directed_edge_to_boundary(e)
assert len(boundary) == 2

boundary_geo_json = h3.directed_edge_to_boundary(e, True)
assert len(boundary_geo_json) == 3


def test_grid_distance():
h = '89283082993ffff'

Expand Down
26 changes: 0 additions & 26 deletions tests/test_polyfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,32 +151,6 @@ def test_polygon_to_cells_holes():
assert cells_all == set.union(cells_holes, cells_1, cells_2)


# def test_polyfill_geojson():
# outer, hole1, hole2 = get_us_box_coords(order='lnglat')

# d = {
# 'type': 'Polygon',
# 'coordinates': [outer],
# }

# out = h3.polyfill_geojson(d, 5)

# assert len(out) == 7063


# def test_polyfill():
# outer, hole1, hole2 = get_us_box_coords(order='lnglat')

# d = {
# 'type': 'Polygon',
# 'coordinates': [outer],
# }

# out = h3.polyfill(d, 5, geo_json_conformant=True)

# assert len(out) == 7063


def test_input_format():
""" Test that `polygon_to_cells` can take in polygon inputs
where the LinearRings may or may not follow the right hand rule,
Expand Down

0 comments on commit 0ae6115

Please sign in to comment.