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
55 changes: 55 additions & 0 deletions docs/getting_started/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,61 @@ 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

Which gives here (and for DataArrays):

.. code-block::

rioxarray.RasterDataArray: (y[latitude (m)]: 10, x[latitude (m)]: 10, z[band]: 1)
Profile:
count: 1
crs: 32605
dtype: float32
nodata: 0.0
transform:
| 10.00, 0.00, 346860.00|
| 0.00,-10.00, 6392220.00|
| 0.00, 0.00, 1.00|
height: 22547
width: 21710
blockxsize: 256
blockysize: 256
bounds: (346860.0, 6166750.0, 563960.0, 6392220.0)

For Datasets, it looks like:

.. code-block::

rioxarray.RasterDataset
Dimensions: y[latitude (m)]: 10, x[latitude (m)]: 10, z[time]: 2
Data variables:
blue y[latitude (m)]: 10, x[latitude (m)]: 10, z[time]: 2
green y[latitude (m)]: 10, x[latitude (m)]: 10, z[time]: 2
Profile:
count: 2
crs: 32722
dtype: float64
nodata: nan
transform:
| 3.00, 0.00, 466266.00|
| 0.00,-3.00, 8084700.00|
| 0.00, 0.00, 1.00|
height: 10
width: 10
blockxsize: 10
blockysize: 10
bounds: (466266.0, 8084670.0, 466296.0, 8084700.0)

Introductory Information
--------------------------
Expand Down
37 changes: 37 additions & 0 deletions docs/getting_started/switching_from_rasterio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,43 @@ Beware, ``xarray`` comes also with gotchas! You can see some of them in `the ded

``rasterio`` Dataset and xarray Dataset are two completely different things! Please be careful with these overlapping names.

Quickly check your raster metadata
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

With ``rasterio``, you can rapidly print your dataset profile to grasp your dataset information (a bit like ``gdalinfo``):

.. code-block:: python

>>> with rasterio.open(r"my_raster.tif") as ds:
>>> print(ds.profile)

{'driver': 'GTiff', 'dtype': 'float32', 'nodata': 0.0, 'width': 21710, 'height': 22547, 'count': 1, 'crs': CRS.from_wkt('PROJCS["WGS 84 / UTM zone 5N",...]]'),
'transform': Affine(10.0, 0.0, 346860.0, 0.0, -10.0, 6392220.0), 'blockxsize': 256, 'blockysize': 256, 'tiled': True, 'compress': 'deflate', 'interleave': 'band'}

With rioxarry, you just have to print the accessor itself to have similar ionformation:

.. code-block:: python

>>> xda = rioxarray.open_rasterio(r"my_raster.tif")
>>> print(xda.rio)

rioxarray.RasterDataArray: (y[latitude (m)]: 10, x[latitude (m)]: 10, z[band]: 1)
Profile:
count: 1
crs: 32605
dtype: float32
nodata: 0.0
transform:
| 10.00, 0.00, 346860.00|
| 0.00,-10.00, 6392220.00|
| 0.00, 0.00, 1.00|
height: 22547
width: 21710
blockxsize: 256
blockysize: 256
bounds: (346860.0, 6166750.0, 563960.0, 6392220.0)


Equivalences between ``rasterio`` and ``rioxarray``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
72 changes: 72 additions & 0 deletions rioxarray/raster_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,3 +1099,75 @@ 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, from_ds: bool = False, uniform_ds: bool = True) -> list[str]:
"""
Function representing the DataArray. Not set directly in __repr__ as it is used also in Datasets.

Parameters
----------
from_ds: bool
repr of a DataArray from a Dataset
uniform_ds: bool
the dataset is uniform, meaning all contained DataArrays have the same data
"""

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

# Create representation dict
# NOTE: height, width and count values are already present in the dimensions repr, so don't set them again
repr_dict = {}

# If we have a native DataArray or a Dataset with uniform coordinates
if not from_ds or (from_ds and uniform_ds):
repr_dict.update(
{
"crs": crs,
"transform": f"\n{self.transform()}",
"gcps": self.get_gcps(),
"rpcs": self.get_rpcs(),
"bounds": self.bounds(),
}
)

# If we have a native DataArray, this is not useful in case of a Dataset (because it may vary between variables)
if not from_ds:
# 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 nodata empty
nodata = (
self.encoded_nodata if self.encoded_nodata is not None else self.nodata
)

if nodata is None:
nodata = "Unset"

repr_dict.update(
{
"nodata": nodata,
"blockxsize": blockxsize,
"blockysize": blockysize,
"dtype": self._obj.encoding.get("rasterio_dtype"),
}
)

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

def __repr__(self) -> str:
repr_list = [
f"rioxarray.RasterDataArray: ({self._dims_unit_to_repr()})",
"Profile:",
] + ["\t\t\t".join(f"\t{val}".splitlines(True)) for val in self._to_repr()]

return "\n".join(repr_list)
29 changes: 29 additions & 0 deletions rioxarray/raster_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import os
from collections import defaultdict
from collections.abc import Iterable, Mapping
from typing import Any, Literal, Optional, Union
from uuid import uuid4
Expand Down Expand Up @@ -583,3 +584,31 @@ def to_raster(
compute=compute,
**profile_kwargs,
)

def __repr__(self) -> str:
# Add dimension data to repr
repr_list = [
"rioxarray.RasterDataset",
f"Dimensions: {self._dims_unit_to_repr()}",
"Data variables:",
]

# Retrieve data from variables
da_info = defaultdict(list)
for var in self.vars:
repr_list += [f"\t{var}\t{self._obj[var].rio._dims_unit_to_repr()}"]
da_info["crs"].append(self._obj[var].rio.crs)
da_info["tf"].append(self._obj[var].rio.transform())

# Check ds uniformity (same CRS and transform across the arrays)
uniform_ds = len(set(da_info["crs"])) == 1 and len(set(da_info["tf"])) == 1

# Create repr
repr_list += ["Profile:"] + [
"\t\t".join(f"\t{r}".splitlines(True))
for r in self._obj[self.vars[0]].rio._to_repr(
from_ds=True, uniform_ds=uniform_ds
)
]

return "\n".join(repr_list)
15 changes: 15 additions & 0 deletions rioxarray/rioxarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,3 +1132,18 @@ def get_rpcs(self) -> Optional[RPC]:

self._rpcs = RPC(**json_rpcs)
return self._rpcs

def _dims_unit_to_repr(self):
dims = self._obj.sizes

if self.crs is not None:
dim_unit = " (°)" if self.crs.is_geographic else " (m)"
else:
dim_unit = None

dims_str = f"{self.y_dim}[latitude{dim_unit}]: {self.height}, {self.x_dim}[latitude{dim_unit}]: {self.width}"
if len(dims) == 3:
last_key = [k for k in dims.keys() if k not in (self.y_dim, self.x_dim)][0]
dims_str += f", z[{last_key}]: {dims[last_key]}"

return dims_str
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 str_arr.startswith("rioxarray.RasterDataset")


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 str_arr.startswith("rioxarray.RasterDataArray")
Loading