def test_select_on_transposed_dataarray(self): x = np.linspace(-3, 7, 53) y = np.linspace(-5, 8, 89) z = np.exp(-1 * (x**2 + y[:, np.newaxis]**2)) array = xr.DataArray(z, coords=[y, x], dims=['x', 'y']) img = Image(array)[1:3] self.assertEqual(img['z'], Image(array.sel(x=slice(1, 3)))['z'])
def test_read_pattern_path_as_pattern_as_str_with_list_of_urlpaths(): pytest.importorskip('rasterio') cat = intake.open_catalog(os.path.join(here, 'data', 'catalog.yaml')) colors = cat.pattern_tiff_source_path_pattern_as_str() da = colors.read() assert da.shape == (2, 3, 64, 64) assert len(da.color) == 2 assert set(da.color.data) == set(['red', 'green']) assert da.sel(color='red').shape == (3, 64, 64) rgb = {'red': [204, 17, 17], 'green': [17, 204, 17]} for color, values in rgb.items(): for i, v in enumerate(values): assert (da.sel(color=color).sel(band=i + 1).values == v).all()
def test_rasterio_vrt_network(self): import rasterio url = "https://storage.googleapis.com/\ gcp-public-data-landsat/LC08/01/047/027/\ LC08_L1TP_047027_20130421_20170310_01_T1/\ LC08_L1TP_047027_20130421_20170310_01_T1_B4.TIF" env = rasterio.Env( GDAL_DISABLE_READDIR_ON_OPEN="EMPTY_DIR", CPL_VSIL_CURL_USE_HEAD=False, CPL_VSIL_CURL_ALLOWED_EXTENSIONS="TIF", ) with env: with rasterio.open(url) as src: with rasterio.vrt.WarpedVRT(src, crs="epsg:4326") as vrt: expected_shape = (vrt.width, vrt.height) expected_crs = vrt.crs expected_res = vrt.res # Value of single pixel in center of image lon, lat = vrt.xy(vrt.width // 2, vrt.height // 2) expected_val = next(vrt.sample([(lon, lat)])) with xr.open_rasterio(vrt) as da: actual_shape = (da.sizes["x"], da.sizes["y"]) actual_crs = da.crs actual_res = da.res actual_val = da.sel(dict(x=lon, y=lat), method="nearest").data assert_equal(actual_shape, expected_shape) assert_equal(actual_crs, expected_crs) assert_equal(actual_res, expected_res) assert_equal(expected_val, actual_val)
def test_select_on_transposed_dataarray(self): x = np.linspace(-3, 7, 53) y = np.linspace(-5, 8, 89) z = np.exp(-1*(x**2 + y[:, np.newaxis]**2)) array = xr.DataArray(z, coords=[y, x], dims=['x', 'y']) img = Image(array)[1:3] self.assertEqual(img['z'], Image(array.sel(x=slice(1, 3)))['z'])
def index(da, indexers): from xarray.core.indexing import remap_label_indexers if not isinstance(da, xr.DataArray): raise TypeError(f"Expected DataArray. Received {type(da).__name__}") pos_indexers, new_indexes = remap_label_indexers(da, indexers) dask_indexers = list(pos_indexers.values()) # TODO: avoid the sel. That could be slow indexed = da.sel(**indexers).copy(data=dask_safeslice(da.data, dask_indexers)) return indexed
def test_rasterio_vrt(self): # tmp_file default crs is UTM: CRS({'init': 'epsg:32618'} with create_tmp_geotiff() as (tmp_file, expected): with rasterio.open(tmp_file) as src: with rasterio.vrt.WarpedVRT(src, crs="epsg:4326") as vrt: expected_shape = (vrt.width, vrt.height) expected_crs = vrt.crs expected_res = vrt.res # Value of single pixel in center of image lon, lat = vrt.xy(vrt.width // 2, vrt.height // 2) expected_val = next(vrt.sample([(lon, lat)])) with xr.open_rasterio(vrt) as da: actual_shape = (da.sizes["x"], da.sizes["y"]) actual_crs = da.crs actual_res = da.res actual_val = da.sel(dict(x=lon, y=lat), method="nearest").data assert actual_crs == expected_crs assert actual_res == expected_res assert actual_shape == expected_shape assert expected_val.all() == actual_val.all()
def slice_dataarray(da): return da.sel(lat=site[1].lat.unique(), lon=site[1].lon.unique(), method='nearest')