From 88f2d542b5ac1bbe8dc2a8fce610bbf3375ec1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20BRIOL?= Date: Thu, 23 Nov 2023 13:26:46 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20Fix=20issue=20with=20single=20po?= =?UTF-8?q?int=20bounding=20box=20calculation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pyinterp/core/lib/geohash/int64.cpp | 6 ++++++ src/pyinterp/tests/core/test_geohash.py | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/pyinterp/core/lib/geohash/int64.cpp b/src/pyinterp/core/lib/geohash/int64.cpp index 3e9e8516..88cbb9ef 100644 --- a/src/pyinterp/core/lib/geohash/int64.cpp +++ b/src/pyinterp/core/lib/geohash/int64.cpp @@ -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; diff --git a/src/pyinterp/tests/core/test_geohash.py b/src/pyinterp/tests/core/test_geohash.py index 0d717171..70855c26 100644 --- a/src/pyinterp/tests/core/test_geohash.py +++ b/src/pyinterp/tests/core/test_geohash.py @@ -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], @@ -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)