File: test_hilbert_curve.py

package info (click to toggle)
python-geopandas 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,752 kB
  • sloc: python: 26,021; makefile: 147; sh: 25
file content (76 lines) | stat: -rw-r--r-- 2,087 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import numpy as np

from shapely.geometry import Point
from shapely.wkt import loads

import geopandas

import pytest
from pandas.testing import assert_series_equal


def test_hilbert_distance():
    # test the actual Hilbert Code algorithm against some hardcoded values
    geoms = geopandas.GeoSeries.from_wkt(
        [
            "POINT (0 0)",
            "POINT (1 1)",
            "POINT (1 0)",
            "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))",
        ]
    )
    result = geoms.hilbert_distance(total_bounds=(0, 0, 1, 1), level=2)
    assert result.tolist() == [0, 10, 15, 2]

    result = geoms.hilbert_distance(total_bounds=(0, 0, 1, 1), level=3)
    assert result.tolist() == [0, 42, 63, 10]

    result = geoms.hilbert_distance(total_bounds=(0, 0, 1, 1), level=16)
    assert result.tolist() == [0, 2863311530, 4294967295, 715827882]


@pytest.fixture
def geoseries_points():
    p1 = Point(1, 2)
    p2 = Point(2, 3)
    p3 = Point(3, 4)
    p4 = Point(4, 1)
    return geopandas.GeoSeries([p1, p2, p3, p4])


def test_hilbert_distance_level(geoseries_points):
    with pytest.raises(ValueError):
        geoseries_points.hilbert_distance(level=20)


def test_specified_total_bounds(geoseries_points):
    result = geoseries_points.hilbert_distance(
        total_bounds=geoseries_points.total_bounds
    )
    expected = geoseries_points.hilbert_distance()
    assert_series_equal(result, expected)


@pytest.mark.parametrize(
    "empty",
    [
        None,
        loads("POLYGON EMPTY"),
    ],
)
def test_empty(geoseries_points, empty):
    s = geoseries_points
    s.iloc[-1] = empty
    with pytest.raises(
        ValueError, match="cannot be computed on a GeoSeries with empty"
    ):
        s.hilbert_distance()


def test_zero_width():
    # special case of all points on the same line -> avoid warnings because
    # of division by 0 and introducing NaN
    s = geopandas.GeoSeries([Point(0, 0), Point(0, 2), Point(0, 1)])
    with np.errstate(all="raise"):
        result = s.hilbert_distance()
    assert np.array(result).argsort().tolist() == [0, 2, 1]