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

ENH: implement __repr__ for the index #65

Merged
merged 3 commits into from
Apr 22, 2024
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
114 changes: 87 additions & 27 deletions doc/source/extract_pts.ipynb

Large diffs are not rendered by default.

142 changes: 86 additions & 56 deletions doc/source/geopandas.ipynb

Large diffs are not rendered by default.

313 changes: 212 additions & 101 deletions doc/source/indexing.ipynb

Large diffs are not rendered by default.

44 changes: 32 additions & 12 deletions doc/source/intro.ipynb

Large diffs are not rendered by default.

164 changes: 111 additions & 53 deletions doc/source/io.ipynb

Large diffs are not rendered by default.

158 changes: 109 additions & 49 deletions doc/source/projections.ipynb

Large diffs are not rendered by default.

458 changes: 234 additions & 224 deletions doc/source/zonal_stats.ipynb

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions xvec/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,20 @@ def _repr_inline_(self, max_width: int) -> str:

srs = _format_crs(self.crs, max_width=max_width)
return f"{self.__class__.__name__} (crs={srs})"

def __repr__(self) -> str:
srs = _format_crs(self.crs)
shape = self._index.index.shape[0]
if shape < 10:
wkts = [repr(g) for g in self._index.index]
else:
wkts = [repr(g) for g in self._index.index[:4]]
wkts.append("...")
wkts = wkts + [repr(g) for g in self._index.index[-4:]]

if len(wkts) == 1:
return f"GeometryIndex([{wkts[0]}], crs={srs})"
joined = "\n ".join(wkts[1:])
return f"GeometryIndex(\n[{wkts[0]}\n {joined}],\ncrs={srs})".replace(
"\n", "\n" + " " * 4
)
21 changes: 21 additions & 0 deletions xvec/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,24 @@ def test_repr_inline(geom_dataset, geom_dataset_no_crs):
actual = geom_dataset_no_crs.xindexes["geom"]._repr_inline_(70)
expected = "GeometryIndex (crs=None)"
assert actual == expected


def test_repr(geom_dataset, geom_dataset_no_crs):
actual = repr(geom_dataset.xindexes["geom"])
expected = (
"GeometryIndex(\n [<POINT (1 2)>\n <POINT (3 4)>],\n crs=EPSG:26915)"
)
assert actual == expected

actual = repr(geom_dataset_no_crs.xindexes["geom"])
expected = "GeometryIndex(\n [<POINT (1 2)>\n <POINT (3 4)>],\n crs=None)"
assert actual == expected

single = geom_dataset.sel(geom=[shapely.Point(1, 2)])
actual = repr(single.xindexes["geom"])
expected = "GeometryIndex([<POINT (1 2)>], crs=EPSG:26915)"
assert actual == expected

long = xr.concat([geom_dataset] * 10, dim="geom")
actual = repr(long.xindexes["geom"])
expected = "GeometryIndex(\n [<POINT (1 2)>\n <POINT (3 4)>\n <POINT (1 2)>\n <POINT (3 4)>\n ...\n <POINT (1 2)>\n <POINT (3 4)>\n <POINT (1 2)>\n <POINT (3 4)>],\n crs=EPSG:26915)"
Loading