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

Fix GeoDataset pickling #304

Merged
merged 2 commits into from
Dec 19, 2021
Merged
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
9 changes: 9 additions & 0 deletions tests/datasets/test_geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the MIT License.

import os
import pickle
from pathlib import Path
from typing import Dict

Expand Down Expand Up @@ -121,6 +122,14 @@ def test_str(self, dataset: GeoDataset) -> None:
assert "bbox: BoundingBox" in out
assert "size: 1" in out

def test_picklable(self, dataset: GeoDataset) -> None:
x = pickle.dumps(dataset)
y = pickle.loads(x)
assert dataset.crs == y.crs
assert dataset.res == y.res
assert len(dataset) == len(y)
assert dataset.bounds == y.bounds

def test_abstract(self) -> None:
with pytest.raises(TypeError, match="Can't instantiate abstract class"):
GeoDataset() # type: ignore[abstract]
Expand Down
35 changes: 35 additions & 0 deletions torchgeo/datasets/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,41 @@ def __str__(self) -> str:
bbox: {self.bounds}
size: {len(self)}"""

# NOTE: This hack should be removed once the following issue is fixed:
# https://github.com/Toblerity/rtree/issues/87

def __getstate__(
self,
) -> Tuple[
Dict[Any, Any],
List[Tuple[int, Tuple[float, float, float, float, float, float], str]],
]:
"""Define how instances are pickled.

Returns:
the state necessary to unpickle the instance
"""
objects = self.index.intersection(self.index.bounds, objects=True)
tuples = [(item.id, item.bounds, item.object) for item in objects]
return self.__dict__, tuples

def __setstate__(
self,
state: Tuple[
Dict[Any, Any],
List[Tuple[int, Tuple[float, float, float, float, float, float], str]],
],
) -> None:
"""Define how to unpickle an instance.

Args:
state: the state of the instance when it was pickled
"""
attrs, tuples = state
self.__dict__.update(attrs)
for item in tuples:
self.index.insert(*item)

@property
def bounds(self) -> BoundingBox:
"""Bounds of the index.
Expand Down