Skip to content

Commit

Permalink
🩹 Fix issue with single point bounding box
Browse files Browse the repository at this point in the history
calculation
  • Loading branch information
fbriol committed Nov 23, 2023
1 parent 0a65593 commit 88f2d54
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/pyinterp/core/lib/geohash/int64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ auto grid_properties(const geodetic::Box &box, const uint32_t precision)
auto hash_sw = encode(box.min_corner(), precision);
auto box_sw = bounding_box(hash_sw, precision);
auto box_ne = bounding_box(encode(box.max_corner(), precision), precision);

// Special case: the box is a single point
if (box_sw == box_ne) {
return std::make_tuple(hash_sw, 1, 1);
}

auto lon_offset = box.max_corner().lon() == 180 ? 1 : 0;
auto lat_offset = box.max_corner().lat() == 90 ? 1 : 0;

Expand Down
10 changes: 9 additions & 1 deletion src/pyinterp/tests/core/test_geohash.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy
import pytest

from ...core import GeoHash, geohash
from ...core import GeoHash, geodetic, geohash

testcases = [['77mkh2hcj7mz', -26.015434642, -26.173663656],
['wthnssq3w00x', 29.291182895, 118.331595326],
Expand Down Expand Up @@ -69,6 +69,14 @@ def test_bounding_boxes():
with pytest.raises(MemoryError):
geohash.bounding_boxes(precision=12)

# Special case: the given polygon represents a single geohash
polygon = geodetic.Polygon.read_wkt(
'POLYGON((-158.4 -68.4,-158.4 -67.6,-157.6 -67.6,-157.6 -68.4,'
'-158.4 -68.4))')
bboxes = geohash.bounding_boxes(polygon, precision=3)
assert len(bboxes) == 1
assert bboxes[0] == b'07z'


def test_bounding_zoom():
bboxes = geohash.bounding_boxes(precision=1)
Expand Down

0 comments on commit 88f2d54

Please sign in to comment.