From 660eb7e9b38d27da0c8bae5cf1ff3f027a499a6b Mon Sep 17 00:00:00 2001 From: Gabriel Abrahao Date: Wed, 10 Mar 2021 18:15:00 -0300 Subject: [PATCH 1/7] GH5005 fix documentation on open_rasterio --- xarray/backends/rasterio_.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/backends/rasterio_.py b/xarray/backends/rasterio_.py index 51f0599e8e0..4d8d5c072ca 100644 --- a/xarray/backends/rasterio_.py +++ b/xarray/backends/rasterio_.py @@ -176,7 +176,7 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, loc from affine import Affine da = xr.open_rasterio('path_to_file.tif') - transform = Affine.from_gdal(*da.attrs['transform']) + transform = Affine(*da.transform) nx, ny = da.sizes['x'], da.sizes['y'] x, y = np.meshgrid(np.arange(nx)+0.5, np.arange(ny)+0.5) * transform From 262b8e3ecc05e7a5b62c3871cfaed3de532e95fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Medeiros=20Abrah=C3=A3o?= <30908904+gabriel-abrahao@users.noreply.github.com> Date: Wed, 10 Mar 2021 20:12:15 -0300 Subject: [PATCH 2/7] Example gets the transformation from attrs instead of da.transform --- xarray/backends/rasterio_.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/backends/rasterio_.py b/xarray/backends/rasterio_.py index 4d8d5c072ca..10b3a550318 100644 --- a/xarray/backends/rasterio_.py +++ b/xarray/backends/rasterio_.py @@ -176,7 +176,7 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, loc from affine import Affine da = xr.open_rasterio('path_to_file.tif') - transform = Affine(*da.transform) + transform = Affine(*da.attrs['transform']) nx, ny = da.sizes['x'], da.sizes['y'] x, y = np.meshgrid(np.arange(nx)+0.5, np.arange(ny)+0.5) * transform From 44b21946e5b00ed55d321201ff29b9a17d1b4251 Mon Sep 17 00:00:00 2001 From: Gabriel Abrahao Date: Mon, 15 Mar 2021 00:02:06 -0300 Subject: [PATCH 3/7] Doctest example in open_rasterio --- test.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 00000000000..61dc1647645 --- /dev/null +++ b/test.py @@ -0,0 +1,90 @@ +#%% +def testing(filename, parse_coordinates=None, chunks=None, cache=None, lock=None): + """Open a file with rasterio (experimental). + + This should work with any file that rasterio can open (most often: + geoTIFF). The x and y coordinates are generated automatically from the + file's geoinformation, shifted to the center of each pixel (see + `"PixelIsArea" Raster Space + `_ + for more information). + + You can generate 2D coordinates from the file's attributes with:: + + >>> from affine import Affine + >>> import xarray as xr + >>> import numpy as np + >>> da = xr.open_rasterio('https://github.com/mapbox/rasterio/raw/master/tests/data/RGB.byte.tif') + >>> da + + [1703814 values with dtype=uint8] + Coordinates: + * band (band) int64 1 2 3 + * y (y) float64 2.827e+06 2.826e+06 2.826e+06 ... 2.612e+06 2.612e+06 + * x (x) float64 1.021e+05 1.024e+05 1.027e+05 ... 3.389e+05 3.392e+05 + Attributes: + transform: (300.0379266750948, 0.0, 101985.0, 0.0, -300.041782729805... + crs: +init=epsg:32618 + res: (300.0379266750948, 300.041782729805) + is_tiled: 0 + nodatavals: (0.0, 0.0, 0.0) + scales: (1.0, 1.0, 1.0) + offsets: (0.0, 0.0, 0.0) + AREA_OR_POINT: Area + >>> transform = Affine(*da.transform) + >>> transform + Affine(300.0379266750948, 0.0, 101985.0, + 0.0, -300.041782729805, 2826915.0) + >>> nx, ny = da.sizes['x'], da.sizes['y'] + >>> x, y = transform * np.meshgrid(np.arange(nx)+0.5, np.arange(ny)+0.5) + >>> x + array([[102135.01896334, 102435.05689001, 102735.09481669, ..., + 338564.90518331, 338864.94310999, 339164.98103666], + [102135.01896334, 102435.05689001, 102735.09481669, ..., + 338564.90518331, 338864.94310999, 339164.98103666], + [102135.01896334, 102435.05689001, 102735.09481669, ..., + 338564.90518331, 338864.94310999, 339164.98103666], + ..., + [102135.01896334, 102435.05689001, 102735.09481669, ..., + 338564.90518331, 338864.94310999, 339164.98103666], + [102135.01896334, 102435.05689001, 102735.09481669, ..., + 338564.90518331, 338864.94310999, 339164.98103666], + [102135.01896334, 102435.05689001, 102735.09481669, ..., + 338564.90518331, 338864.94310999, 339164.98103666]]) + + Parameters + ---------- + filename : str, rasterio.DatasetReader, or rasterio.WarpedVRT + Path to the file to open. Or already open rasterio dataset. + parse_coordinates : bool, optional + Whether to parse the x and y coordinates out of the file's + ``transform`` attribute or not. The default is to automatically + parse the coordinates only if they are rectilinear (1D). + It can be useful to set ``parse_coordinates=False`` + if your files are very large or if you don't need the coordinates. + chunks : int, tuple or dict, optional + Chunk sizes along each dimension, e.g., ``5``, ``(5, 5)`` or + ``{'x': 5, 'y': 5}``. If chunks is provided, it used to load the new + DataArray into a dask array. + cache : bool, optional + If True, cache data loaded from the underlying datastore in memory as + NumPy arrays when accessed to avoid reading from the underlying data- + store multiple times. Defaults to True unless you specify the `chunks` + argument to use dask, in which case it defaults to False. + lock : False, True or threading.Lock, optional + If chunks is provided, this argument is passed on to + :py:func:`dask.array.from_array`. By default, a global lock is + used to avoid issues with concurrent access to the same file when using + dask's multithreaded backend. + + Returns + ------- + data : DataArray + The newly created DataArray. + """ + import rasterio + from rasterio.vrt import WarpedVRT + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file From 648b0a609f025456d17ec487acbb705196cf931e Mon Sep 17 00:00:00 2001 From: Gabriel Abrahao Date: Mon, 15 Mar 2021 00:08:31 -0300 Subject: [PATCH 4/7] Doctest for open_rasterio, reverting commit --- test.py | 90 ------------------------------------ xarray/backends/rasterio_.py | 45 ++++++++++++++++-- 2 files changed, 40 insertions(+), 95 deletions(-) delete mode 100644 test.py diff --git a/test.py b/test.py deleted file mode 100644 index 61dc1647645..00000000000 --- a/test.py +++ /dev/null @@ -1,90 +0,0 @@ -#%% -def testing(filename, parse_coordinates=None, chunks=None, cache=None, lock=None): - """Open a file with rasterio (experimental). - - This should work with any file that rasterio can open (most often: - geoTIFF). The x and y coordinates are generated automatically from the - file's geoinformation, shifted to the center of each pixel (see - `"PixelIsArea" Raster Space - `_ - for more information). - - You can generate 2D coordinates from the file's attributes with:: - - >>> from affine import Affine - >>> import xarray as xr - >>> import numpy as np - >>> da = xr.open_rasterio('https://github.com/mapbox/rasterio/raw/master/tests/data/RGB.byte.tif') - >>> da - - [1703814 values with dtype=uint8] - Coordinates: - * band (band) int64 1 2 3 - * y (y) float64 2.827e+06 2.826e+06 2.826e+06 ... 2.612e+06 2.612e+06 - * x (x) float64 1.021e+05 1.024e+05 1.027e+05 ... 3.389e+05 3.392e+05 - Attributes: - transform: (300.0379266750948, 0.0, 101985.0, 0.0, -300.041782729805... - crs: +init=epsg:32618 - res: (300.0379266750948, 300.041782729805) - is_tiled: 0 - nodatavals: (0.0, 0.0, 0.0) - scales: (1.0, 1.0, 1.0) - offsets: (0.0, 0.0, 0.0) - AREA_OR_POINT: Area - >>> transform = Affine(*da.transform) - >>> transform - Affine(300.0379266750948, 0.0, 101985.0, - 0.0, -300.041782729805, 2826915.0) - >>> nx, ny = da.sizes['x'], da.sizes['y'] - >>> x, y = transform * np.meshgrid(np.arange(nx)+0.5, np.arange(ny)+0.5) - >>> x - array([[102135.01896334, 102435.05689001, 102735.09481669, ..., - 338564.90518331, 338864.94310999, 339164.98103666], - [102135.01896334, 102435.05689001, 102735.09481669, ..., - 338564.90518331, 338864.94310999, 339164.98103666], - [102135.01896334, 102435.05689001, 102735.09481669, ..., - 338564.90518331, 338864.94310999, 339164.98103666], - ..., - [102135.01896334, 102435.05689001, 102735.09481669, ..., - 338564.90518331, 338864.94310999, 339164.98103666], - [102135.01896334, 102435.05689001, 102735.09481669, ..., - 338564.90518331, 338864.94310999, 339164.98103666], - [102135.01896334, 102435.05689001, 102735.09481669, ..., - 338564.90518331, 338864.94310999, 339164.98103666]]) - - Parameters - ---------- - filename : str, rasterio.DatasetReader, or rasterio.WarpedVRT - Path to the file to open. Or already open rasterio dataset. - parse_coordinates : bool, optional - Whether to parse the x and y coordinates out of the file's - ``transform`` attribute or not. The default is to automatically - parse the coordinates only if they are rectilinear (1D). - It can be useful to set ``parse_coordinates=False`` - if your files are very large or if you don't need the coordinates. - chunks : int, tuple or dict, optional - Chunk sizes along each dimension, e.g., ``5``, ``(5, 5)`` or - ``{'x': 5, 'y': 5}``. If chunks is provided, it used to load the new - DataArray into a dask array. - cache : bool, optional - If True, cache data loaded from the underlying datastore in memory as - NumPy arrays when accessed to avoid reading from the underlying data- - store multiple times. Defaults to True unless you specify the `chunks` - argument to use dask, in which case it defaults to False. - lock : False, True or threading.Lock, optional - If chunks is provided, this argument is passed on to - :py:func:`dask.array.from_array`. By default, a global lock is - used to avoid issues with concurrent access to the same file when using - dask's multithreaded backend. - - Returns - ------- - data : DataArray - The newly created DataArray. - """ - import rasterio - from rasterio.vrt import WarpedVRT - -if __name__ == "__main__": - import doctest - doctest.testmod() \ No newline at end of file diff --git a/xarray/backends/rasterio_.py b/xarray/backends/rasterio_.py index 10b3a550318..71340b7c9b2 100644 --- a/xarray/backends/rasterio_.py +++ b/xarray/backends/rasterio_.py @@ -174,11 +174,46 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, loc You can generate 2D coordinates from the file's attributes with:: - from affine import Affine - da = xr.open_rasterio('path_to_file.tif') - transform = Affine(*da.attrs['transform']) - nx, ny = da.sizes['x'], da.sizes['y'] - x, y = np.meshgrid(np.arange(nx)+0.5, np.arange(ny)+0.5) * transform + >>> from affine import Affine + >>> import xarray as xr + >>> import numpy as np + >>> da = xr.open_rasterio('https://github.com/mapbox/rasterio/raw/master/tests/data/RGB.byte.tif') + >>> da + + [1703814 values with dtype=uint8] + Coordinates: + * band (band) int64 1 2 3 + * y (y) float64 2.827e+06 2.826e+06 2.826e+06 ... 2.612e+06 2.612e+06 + * x (x) float64 1.021e+05 1.024e+05 1.027e+05 ... 3.389e+05 3.392e+05 + Attributes: + transform: (300.0379266750948, 0.0, 101985.0, 0.0, -300.041782729805... + crs: +init=epsg:32618 + res: (300.0379266750948, 300.041782729805) + is_tiled: 0 + nodatavals: (0.0, 0.0, 0.0) + scales: (1.0, 1.0, 1.0) + offsets: (0.0, 0.0, 0.0) + AREA_OR_POINT: Area + >>> transform = Affine(*da.transform) + >>> transform + Affine(300.0379266750948, 0.0, 101985.0, + 0.0, -300.041782729805, 2826915.0) + >>> nx, ny = da.sizes['x'], da.sizes['y'] + >>> x, y = transform * np.meshgrid(np.arange(nx)+0.5, np.arange(ny)+0.5) + >>> x + array([[102135.01896334, 102435.05689001, 102735.09481669, ..., + 338564.90518331, 338864.94310999, 339164.98103666], + [102135.01896334, 102435.05689001, 102735.09481669, ..., + 338564.90518331, 338864.94310999, 339164.98103666], + [102135.01896334, 102435.05689001, 102735.09481669, ..., + 338564.90518331, 338864.94310999, 339164.98103666], + ..., + [102135.01896334, 102435.05689001, 102735.09481669, ..., + 338564.90518331, 338864.94310999, 339164.98103666], + [102135.01896334, 102435.05689001, 102735.09481669, ..., + 338564.90518331, 338864.94310999, 339164.98103666], + [102135.01896334, 102435.05689001, 102735.09481669, ..., + 338564.90518331, 338864.94310999, 339164.98103666]]) Parameters ---------- From bdc3e1091620addb7c4841f10d513d1d6e792553 Mon Sep 17 00:00:00 2001 From: Gabriel Abrahao Date: Mon, 15 Mar 2021 00:21:36 -0300 Subject: [PATCH 5/7] Applying blackdoc --- xarray/backends/rasterio_.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/xarray/backends/rasterio_.py b/xarray/backends/rasterio_.py index 71340b7c9b2..35ab42c6e19 100644 --- a/xarray/backends/rasterio_.py +++ b/xarray/backends/rasterio_.py @@ -177,7 +177,9 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, loc >>> from affine import Affine >>> import xarray as xr >>> import numpy as np - >>> da = xr.open_rasterio('https://github.com/mapbox/rasterio/raw/master/tests/data/RGB.byte.tif') + >>> da = xr.open_rasterio( + ... "https://github.com/mapbox/rasterio/raw/master/tests/data/RGB.byte.tif" + ... ) >>> da [1703814 values with dtype=uint8] @@ -198,8 +200,8 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, loc >>> transform Affine(300.0379266750948, 0.0, 101985.0, 0.0, -300.041782729805, 2826915.0) - >>> nx, ny = da.sizes['x'], da.sizes['y'] - >>> x, y = transform * np.meshgrid(np.arange(nx)+0.5, np.arange(ny)+0.5) + >>> nx, ny = da.sizes["x"], da.sizes["y"] + >>> x, y = transform * np.meshgrid(np.arange(nx) + 0.5, np.arange(ny) + 0.5) >>> x array([[102135.01896334, 102435.05689001, 102735.09481669, ..., 338564.90518331, 338864.94310999, 339164.98103666], From 3f052e4c33dd67ba173b0368fdbe35da37074da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Medeiros=20Abrah=C3=A3o?= <30908904+gabriel-abrahao@users.noreply.github.com> Date: Mon, 15 Mar 2021 10:43:07 -0300 Subject: [PATCH 6/7] Remove unnecessary imports Co-authored-by: keewis --- xarray/backends/rasterio_.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/xarray/backends/rasterio_.py b/xarray/backends/rasterio_.py index 35ab42c6e19..6b5ac6a798e 100644 --- a/xarray/backends/rasterio_.py +++ b/xarray/backends/rasterio_.py @@ -175,8 +175,6 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, loc You can generate 2D coordinates from the file's attributes with:: >>> from affine import Affine - >>> import xarray as xr - >>> import numpy as np >>> da = xr.open_rasterio( ... "https://github.com/mapbox/rasterio/raw/master/tests/data/RGB.byte.tif" ... ) From f4758510e68608cefdfce9ca314855a99e705d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Medeiros=20Abrah=C3=A3o?= <30908904+gabriel-abrahao@users.noreply.github.com> Date: Mon, 15 Mar 2021 10:46:56 -0300 Subject: [PATCH 7/7] Getting transform from attribute Co-authored-by: keewis --- xarray/backends/rasterio_.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/backends/rasterio_.py b/xarray/backends/rasterio_.py index 6b5ac6a798e..06b964fdc46 100644 --- a/xarray/backends/rasterio_.py +++ b/xarray/backends/rasterio_.py @@ -194,7 +194,7 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, loc scales: (1.0, 1.0, 1.0) offsets: (0.0, 0.0, 0.0) AREA_OR_POINT: Area - >>> transform = Affine(*da.transform) + >>> transform = Affine(*da.attrs["transform"]) >>> transform Affine(300.0379266750948, 0.0, 101985.0, 0.0, -300.041782729805, 2826915.0)