|
| 1 | +import geopandas |
| 2 | + |
| 3 | +import dask_geopandas |
| 4 | + |
| 5 | +import pytest |
| 6 | +from geopandas.testing import assert_geodataframe_equal |
| 7 | + |
| 8 | +try: |
| 9 | + import pyogrio # noqa: F401 |
| 10 | + |
| 11 | + PYOGRIO = True |
| 12 | +except ImportError: |
| 13 | + PYOGRIO = False |
| 14 | + |
| 15 | +BACKENDS = ["arrow", "file", "parquet"] |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(params=BACKENDS) |
| 19 | +def backend(request): |
| 20 | + param = request.param |
| 21 | + if not PYOGRIO and param == "file": |
| 22 | + pytest.skip("Unable to import pyogrio for file backend") |
| 23 | + return param |
| 24 | + |
| 25 | + |
| 26 | +def from_arrow_backend(path, tmp_path, npartitions): |
| 27 | + df = geopandas.read_file(path) |
| 28 | + basedir = tmp_path / "dataset" |
| 29 | + basedir.mkdir() |
| 30 | + ddf = dask_geopandas.from_geopandas(df, npartitions=npartitions) |
| 31 | + for i, part in enumerate(ddf.partitions): |
| 32 | + part.compute().to_feather(basedir / f"data.{i}.feather") |
| 33 | + return dask_geopandas.read_feather(basedir) |
| 34 | + |
| 35 | + |
| 36 | +def from_file_backend(path, tmp_path, npartitions): |
| 37 | + return dask_geopandas.read_file(path, npartitions=npartitions) |
| 38 | + |
| 39 | + |
| 40 | +def from_parquet_backend(path, tmp_path, npartitions): |
| 41 | + ddf = dask_geopandas.from_geopandas( |
| 42 | + geopandas.read_file(path), npartitions=npartitions |
| 43 | + ) |
| 44 | + basedir = tmp_path / "dataset" |
| 45 | + ddf.to_parquet(basedir) |
| 46 | + return dask_geopandas.read_parquet(basedir) |
| 47 | + |
| 48 | + |
| 49 | +def get_from_backend(backend, data_path, tmp_path, npartitions=4): |
| 50 | + if backend == "arrow": |
| 51 | + ddf = from_arrow_backend(data_path, tmp_path, npartitions) |
| 52 | + elif backend == "file": |
| 53 | + ddf = from_file_backend(data_path, tmp_path, npartitions) |
| 54 | + elif backend == "parquet": |
| 55 | + ddf = from_parquet_backend(data_path, tmp_path, npartitions) |
| 56 | + else: |
| 57 | + raise ValueError() |
| 58 | + return ddf |
| 59 | + |
| 60 | + |
| 61 | +def test_spatial_shuffle_integration(backend, naturalearth_lowres, tmp_path): |
| 62 | + ddf = get_from_backend(backend, naturalearth_lowres, tmp_path) |
| 63 | + new_idx = ddf.hilbert_distance() |
| 64 | + expected = ddf.compute().set_index(new_idx.compute()) |
| 65 | + |
| 66 | + result = ddf.spatial_shuffle() |
| 67 | + # Sort because the index is shuffled |
| 68 | + assert_geodataframe_equal(result.compute().sort_index(), expected.sort_index()) |
0 commit comments