Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle larger than 32bit distances when creating Hilbert distance and spatially partitioning. #219

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dask_geopandas/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def hilbert_distance(self, total_bounds=None, level=16):
_hilbert_distance,
total_bounds=total_bounds,
level=level,
meta=pd.Series([], name="hilbert_distance", dtype="uint32"),
meta=pd.Series([], name="hilbert_distance", dtype=np.int64),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be an option provided by the user, rather than assuming int64. The default should be uint32 for backwards compatability.

)

return distances
Expand Down
4 changes: 3 additions & 1 deletion dask_geopandas/hilbert_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def _hilbert_distance(gdf, total_bounds=None, level=16):
# Compute distance along hilbert curve
distances = _encode(level, x, y)

return pd.Series(distances, index=gdf.index, name="hilbert_distance")
return pd.Series(
distances, index=gdf.index, name="hilbert_distance", dtype=np.int64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't distances already be a uint32? I suspect that the changes will need to be at a lower level (maybe in _encode), and then when we're constructing a Series here the dtype will just be the dtype of whatever ndarray is passed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right, i had missed that this is actually an unsigned int. The reason for the overflow is actually that the value is greater than the max value for int but not uint, and dask simply isn't respecting the dtype.

)


def _continuous_to_discrete_coords(bounds, level, total_bounds):
Expand Down
16 changes: 16 additions & 0 deletions dask_geopandas/tests/test_spatial_partitioning.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import numpy as np
import pytest

import geopandas
from geopandas.testing import assert_geodataframe_equal, assert_geoseries_equal
from shapely.geometry import Point

import dask_geopandas
from dask_geopandas.hilbert_distance import _hilbert_distance


def test_propagate_on_geometry_access():
Expand Down Expand Up @@ -60,3 +63,16 @@ def test_cx():
assert len(subset) == 0
expected = df.cx[-200:-190, 300:400]
assert_geodataframe_equal(subset.compute(), expected)


def test_geopandas_handles_large_hilbert_distances():
df = geopandas.GeoDataFrame(
{"geometry": [Point(-103152.516, -8942.156), Point(118914.500, 1010032.562)]}
)

# make sure we have values greater than 32bits
dist = _hilbert_distance(df)
assert ((dist > np.iinfo(np.int32).max) | (dist < np.iinfo(np.int32).min)).any()

ddf = dask_geopandas.from_geopandas(df, npartitions=1)
ddf.spatial_shuffle()