Skip to content

Commit

Permalink
ENH: implement __repr__ for the index (#65)
Browse files Browse the repository at this point in the history
* implement __repr__ showing geoms

* execute notebooks

* backwards compat
  • Loading branch information
martinfleis authored Apr 22, 2024
1 parent 4e808e8 commit 2e55fbb
Show file tree
Hide file tree
Showing 9 changed files with 909 additions and 522 deletions.
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)"

0 comments on commit 2e55fbb

Please sign in to comment.