Skip to content
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
10 changes: 10 additions & 0 deletions docs/examples/COG.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@
"rds"
]
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"# Check rioxarray accessor representation\n",
"rds.rio"
]
},
{
"cell_type": "code",
"execution_count": 5,
Expand Down
31 changes: 31 additions & 0 deletions docs/getting_started/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@ Why use :func:`rioxarray.open_rasterio` instead of `xarray.open_rasterio`?
6. It loads raster metadata into the attributes.
7. `xarray.open_rasterio` is deprecated (since v0.20.0)

rio string representation
--------------------------

The rio accessor has a string representation, this can help you check quickly the more relevant attributes of your raster:

.. code-block:: python

import rioxarray

xds = rioxarray.open_rasterio("my.tif")
xds.rio

Wich gives:

.. code-block::

rioxarray accessor (.rio) | RasterArray
Attributes:
count: 1
crs: 32611
rasterio_dtype: float64
nodata: Unset
transform: | 14.26, 9.26, 305827.93|
| 9.26,-14.26, 5223236.60|
| 0.00, 0.00, 1.00|
height: 10
width: 10
blockxsize: 10
blockysize: 10
bounds: (305827.93, 5223236.6, 305997.93, 5223406.6)


Introductory Information
--------------------------
Expand Down
50 changes: 50 additions & 0 deletions rioxarray/raster_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,3 +1099,53 @@ def to_rasterio_dataset(self) -> Generator[DatasetReader, None, None]:
self.to_raster(memfile.name)
with memfile.open() as src_ds:
yield src_ds

def _to_repr(self) -> list[str]:
"""Function representing the DataArray. Not set directly in __repr__ as it is used also in Datasets."""
# Manage chunks
try:
pref_chunks = self._obj.encoding["preferred_chunks"]
blockxsize = pref_chunks["x"]
blockysize = pref_chunks["y"]
except KeyError:
blockxsize = None
blockysize = None

# Never leave CRS empty
crs = self.crs
if crs is None:
crs = "Unprojected"
else:
crs = crs.to_epsg()

# Never leave nodata empty
nodata = self.encoded_nodata if self.encoded_nodata is not None else self.nodata

if nodata is None:
nodata = "Unset"

# Create representation dict
repr_dict = {
"count": self.count,
"crs": crs,
"rasterio_dtype": self._obj.encoding.get("rasterio_dtype"),
"nodata": nodata,
"transform": self.transform(),
"height": self.height,
"width": self.width,
"blockxsize": blockxsize,
"blockysize": blockysize,
"gcps": self.get_gcps(),
"rpcs": self.get_rpcs(),
"bounds": self.bounds(),
}

return [f"{key}: {val}" for key, val in repr_dict.items() if val is not None]

def __repr__(self) -> str:
repr_list = [
f"rioxarray accessor (.rio) | {self.__class__.__name__}",
"Attributes:",
] + [f"\t{val}" for val in self._to_repr()]

return "\n".join(repr_list)
12 changes: 12 additions & 0 deletions rioxarray/raster_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,15 @@ def to_raster(
compute=compute,
**profile_kwargs,
)

def __repr__(self) -> str:
repr_list = [
f"rioxarray accessor (.rio) | {self.__class__.__name__}",
"Variables:",
]

for var in self.vars:
repr_list += [f"\tName: '{var}'", f"\t'{var}' attributes:"]
repr_list += [f"\t\t{val}" for val in self._obj[var].rio._to_repr()]

return "\n".join(repr_list)
26 changes: 26 additions & 0 deletions test/integration/test_integration__io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import glob
import io
import itertools
import logging
Expand Down Expand Up @@ -1612,3 +1613,28 @@ def test_reading_writing_rpcs(tmp_path):
assert (
dst.rpcs is not None
), "Existing RPCs in dst raster (through rpc attribute)"


def test_repr_nc():
"""Simple test of __repr__ just testing the function doesn't fail"""
input_datasets = list(glob.glob(os.path.join(TEST_INPUT_DATA_DIR, "*.nc")))
assert len(input_datasets) > 0
for input_ds in input_datasets:
print(input_ds)
ds = rioxarray.open_rasterio(input_ds)
if isinstance(ds, xarray.DataArray):
ds = ds.to_dataset(name="nc_ds")
str_arr = str(ds.rio)
print(str_arr)
assert "rioxarray accessor (.rio) | RasterDataset" in str_arr


def test_repr_tifs():
"""Simple test of __repr__ just testing the function doesn't fail"""
input_arrays = list(glob.glob(os.path.join(TEST_INPUT_DATA_DIR, "*.tif")))
assert len(input_arrays) > 0
for input_array in input_arrays:
print(input_array)
str_arr = str(rioxarray.open_rasterio(input_array).rio)
print(str_arr)
assert "rioxarray accessor (.rio) | RasterArray" in str_arr
Loading