Skip to content

Commit 2b06287

Browse files
GH5005 fix documentation on open_rasterio (#5021)
* GH5005 fix documentation on open_rasterio * Example gets the transformation from attrs instead of da.transform * Doctest example in open_rasterio * Doctest for open_rasterio, reverting commit * Applying blackdoc * Remove unnecessary imports Co-authored-by: keewis <[email protected]> * Getting transform from attribute Co-authored-by: keewis <[email protected]> Co-authored-by: keewis <[email protected]>
1 parent 061bc8e commit 2b06287

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

xarray/backends/rasterio_.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,46 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, loc
174174
175175
You can generate 2D coordinates from the file's attributes with::
176176
177-
from affine import Affine
178-
da = xr.open_rasterio('path_to_file.tif')
179-
transform = Affine.from_gdal(*da.attrs['transform'])
180-
nx, ny = da.sizes['x'], da.sizes['y']
181-
x, y = np.meshgrid(np.arange(nx)+0.5, np.arange(ny)+0.5) * transform
177+
>>> from affine import Affine
178+
>>> da = xr.open_rasterio(
179+
... "https://github.com/mapbox/rasterio/raw/master/tests/data/RGB.byte.tif"
180+
... )
181+
>>> da
182+
<xarray.DataArray (band: 3, y: 718, x: 791)>
183+
[1703814 values with dtype=uint8]
184+
Coordinates:
185+
* band (band) int64 1 2 3
186+
* y (y) float64 2.827e+06 2.826e+06 2.826e+06 ... 2.612e+06 2.612e+06
187+
* x (x) float64 1.021e+05 1.024e+05 1.027e+05 ... 3.389e+05 3.392e+05
188+
Attributes:
189+
transform: (300.0379266750948, 0.0, 101985.0, 0.0, -300.041782729805...
190+
crs: +init=epsg:32618
191+
res: (300.0379266750948, 300.041782729805)
192+
is_tiled: 0
193+
nodatavals: (0.0, 0.0, 0.0)
194+
scales: (1.0, 1.0, 1.0)
195+
offsets: (0.0, 0.0, 0.0)
196+
AREA_OR_POINT: Area
197+
>>> transform = Affine(*da.attrs["transform"])
198+
>>> transform
199+
Affine(300.0379266750948, 0.0, 101985.0,
200+
0.0, -300.041782729805, 2826915.0)
201+
>>> nx, ny = da.sizes["x"], da.sizes["y"]
202+
>>> x, y = transform * np.meshgrid(np.arange(nx) + 0.5, np.arange(ny) + 0.5)
203+
>>> x
204+
array([[102135.01896334, 102435.05689001, 102735.09481669, ...,
205+
338564.90518331, 338864.94310999, 339164.98103666],
206+
[102135.01896334, 102435.05689001, 102735.09481669, ...,
207+
338564.90518331, 338864.94310999, 339164.98103666],
208+
[102135.01896334, 102435.05689001, 102735.09481669, ...,
209+
338564.90518331, 338864.94310999, 339164.98103666],
210+
...,
211+
[102135.01896334, 102435.05689001, 102735.09481669, ...,
212+
338564.90518331, 338864.94310999, 339164.98103666],
213+
[102135.01896334, 102435.05689001, 102735.09481669, ...,
214+
338564.90518331, 338864.94310999, 339164.98103666],
215+
[102135.01896334, 102435.05689001, 102735.09481669, ...,
216+
338564.90518331, 338864.94310999, 339164.98103666]])
182217
183218
Parameters
184219
----------

0 commit comments

Comments
 (0)