def do_comparison(concession_name): print("comparing: ", concession_name) dirname = 'C:\\Users\\ME\\Dropbox\\HCSA_GIS\\' + concession_name + '\\' assessment_file = dirname + concession_name + '_all_class.remapped.tif' class_file = dirname + concession_name + 'FINAL_classified_by_ensemble_rf.tif' file = glob.glob(assessment_file) assessment = rx.open_rasterio(file[0]) file = glob.glob(class_file) predict_data = rx.open_rasterio(file[0]) y = get_classes(assessment.data, 'clas') y = y.iloc[:, 0] y_hat = get_classes(predict_data.data, 'clas') y_hat = y_hat.iloc[:, 0] y_mixed = map_to_mixed_classes(y) y_2class = map_to_2class(y) df = y_mixed.to_frame(name='mixed').join( y_2class.to_frame(name='2class')).join(y_hat.to_frame(name='y_hat')) print("BEFORE drop no data: ", df.size) df = drop_no_data(df) print("AFTER drop no data: ", df.size) # train_df.dropna(inplace=True) indices_to_keep = ~df.isin([np.nan, np.inf, -np.inf]).any(1) df = df[indices_to_keep] print("AFTER intrmediate drop: ", df.size) show_results(df.loc[:, 'mixed'], df.loc[:, 'y_hat']) show_results(df.loc[:, '2class'], df.loc[:, 'y_hat']) indices_to_keep = ~df.isin([8, 13, 14, 18, 8.0, 13.0, 14.0, 18.0]).any(1) df = df[indices_to_keep] print("AFTER LAST drop: ", df.size) show_results(df.loc[:, 'mixed'], df.loc[:, 'y_hat']) print('END_COMPARISON')
def test_writing_gcps(tmp_path): """ Test writing gcps to a tiff file. """ tiffname = tmp_path / "test.tif" tiffname2 = tmp_path / "test_written.tif" gdal_gcps = _create_gdal_gcps() with rasterio.open( tiffname, mode="w", height=800, width=800, count=3, dtype=np.uint8, driver="GTiff", ) as source: source.gcps = gdal_gcps with rioxarray.open_rasterio(tiffname) as darr: darr.rio.to_raster(tiffname2, driver="GTIFF") with rioxarray.open_rasterio(tiffname2) as darr: assert "gcps" in darr.coords["spatial_ref"].attrs _check_rio_gcps(darr, *gdal_gcps)
def test_http_url(): # more examples urls here # http://download.osgeo.org/geotiff/samples/ url = ( "https://github.com/corteva/rioxarray/blob/master/" "test/test_data/input/cog.tif?raw=true" ) with rioxarray.open_rasterio(url) as actual: assert actual.shape == (1, 500, 500) # make sure chunking works with rioxarray.open_rasterio(url, chunks=(1, 256, 256)) as actual: assert isinstance(actual.data, dask.array.Array)
def __init__(self, parent, image_spec: Dict[str, Any], **kwargs): self.figure = Figure(constrained_layout=True) FigureCanvas.__init__(self, self.figure) self.spec = image_spec self.setParent(parent) FigureCanvas.setSizePolicy(self, QSizePolicy.Ignored, QSizePolicy.Ignored) FigureCanvas.setContentsMargins(self, 0, 0, 0, 0) FigureCanvas.updateGeometry(self) self.axes: Axes = self.figure.add_subplot(111) self.axes.get_xaxis().set_visible(False) self.axes.get_yaxis().set_visible(False) self.figure.set_constrained_layout_pads(w_pad=0., h_pad=0.) self.image: xa.DataArray = rio.open_rasterio(self.spec['path']) self.xdim = self.image.dims[-1] self.ydim = self.image.dims[-2] self.classes = [('Unlabeled', [1.0, 1.0, 1.0, 0.5]) ] + self.format_labels(self.spec.get('classes', [])) if self.classes == None: cmap = "jet" else: cmap = ListedColormap([item[1] for item in self.classes]) self.plot: AxesImage = self.axes.imshow(self.image.squeeze().values, alpha=1.0, aspect='auto', cmap=cmap) self._mousepress = self.plot.figure.canvas.mpl_connect( 'button_press_event', self.onMouseClick)
def plotMap(self, show=True): ''' Plot the map''' #print(self.mapUrl) option = '?list_dir=no' vsiCurl = f'/vsicurl/{option}&url={self.mapUrl}' # da = rioxarray.open_rasterio(vsiCurl, overview_level=3, parse_coordinates=True, chunks=dict(band=1, y=512, x=512), masked=False).squeeze('band') img = da.hvplot.image(rasterize=True, cmap='gray', x='x', y='y', aspect='equal', frame_width=400, title=os.path.basename(self.mapUrl)).opts( active_tools=['box_select']) self.box.source = img bounds = hv.DynamicMap(lambda bounds: hv.Bounds(bounds), streams=[self.box]).opts(color='red') if show: mapview = pn.Column(img * bounds) else: mapview = None return mapview
def raster2masked_xr(raster_path, valid_range=None): """converts raster to array and masks invalid values. Parameters ----------- raster_path: string a path to the shp boundary valid_range: takes in a tuple Returns ----------- xr : xarray object xarray with masked values outside valid range xr_counts : pandas data frame a count of all unique elements, helps locate possible artifacts """ # open raster as rxr xr_object = rxr.open_rasterio(raster_path, masked=True, parse_coordinates=False) # mask values if valid_range is not None: mask = ((xr_object < valid_range[0]) | (xr_object > valid_range[1])) xr_object = xr_object.where(~xr.where(mask, True, False)) # generate data frame of unique values and counts xr_df = xr_object.to_dataframe(name='unique values') xr_counts = xr_df.value_counts() return xr_object, xr_counts
def plot_raster(ax, tif_path, cmap='viridis', levels=None, colors=None, clip_extent=None): """Plot raster with vectors/labels """ # Open raster ds = rioxarray.open_rasterio(tif_path, mask_and_scale=True) if clip_extent is not None: left, right, bottom, top = clip_extent ds = ds.rio.clip_box( minx=left, miny=bottom, maxx=right, maxy=top, ) # Check raster CRS with rasterio.open(tif_path) as da: crs_code = da.crs.to_epsg() if crs_code == 4326: crs = ccrs.PlateCarree() else: crs = ccrs.epsg(crs_code) # Plot raster if levels is not None and colors is not None: ds.plot(ax=ax, levels=levels, colors=colors, transform=crs) else: ds.plot(ax=ax, cmap=cmap, transform=crs) return ax
def open_raster(path, crs=None, crop_bound=None, **kwargs): """Opens, reprojects, clips, and squeezes a raster file Parameters ---------- path: str the path to a raster file crs: str or int (optional) the CRS to convert the raster to as an EPSG code crop_bound (optional): the geometry to clip the data to kwargs (optional): any keyword argument accepted by `rioxarray.open_rasterio` Returns ------- xarray.DataArray the reprojected, clipped, and squeezed array """ kwargs.setdefault("masked", True) xda = rxr.open_rasterio(path, **kwargs) # Reproject to CRS if crs is not None and xda.rio.crs != crs: xda = xda.rio.reproject(crs) # Reproject to CRS if crop_bound is not None: if crop_bound.crs != xda.rio.crs: crop_bound = crop_bound.to_crs(xda.rio.crs) xda = xda.rio.clip(crop_bound, drop=True, from_disk=True) return xda.squeeze()
def inspect_tile( infile: Union[str, Path, xarray.DataArray], tile_shape: Tuple[int, int] = (8192, 8192), subtile_shape: Tuple[int, int] = (512, 512), ) -> TileInfo: with rioxarray.open_rasterio(infile).sel( band=1, drop=True) if not isinstance( infile, xarray.DataArray) else infile as da: shape = tuple(da.shape) if not divisible_without_remainder(tile_shape[0], subtile_shape[0]): raise ValueError( f"Shapes unaligned (v): {tile_shape[0], subtile_shape[0]}") if not divisible_without_remainder(tile_shape[1], subtile_shape[1]): raise ValueError( f"Shapes unaligned (h): {tile_shape[1], subtile_shape[1]}") subtiles = ( math.ceil(shape[0] / subtile_shape[0]), math.ceil(shape[1] / subtile_shape[1]), ) return TileInfo(size=shape, subtiles=subtiles)
def test_pickle_rasterio(): # regression test for https://github.com/pydata/xarray/issues/2121 with create_tmp_geotiff() as (tmp_file, expected): with rioxarray.open_rasterio(tmp_file) as rioda: temp = pickle.dumps(rioda) with pickle.loads(temp) as actual: assert_equal(actual, rioda)
def get_vic_polygon_working(s_var, geom_data, sd, ed): poly_geojson = Polygon(json.loads(geom_data)) shape_obj = shapely.geometry.asShape(poly_geojson) try: nuts = gpd.GeoDataFrame({'geometry': json.loads(geom_data)}) nuts.head() d = xarray.open_mfdataset(os.path.join(cfg.data['path'], s_var + "_final.nc"), chunks={'time': 10}) # d = d.assign_coords(longitude=(((d.lon + 180) % 360) - 180)).sortby('lon') # nuts_mask_poly = regionmask.Regions_cls(name='nuts_mask', numbers=list(range(0, 37)), names=list(nuts.id), # abbrevs=list(nuts.ccode), # outlines=list(nuts.geometry.values[i] for i in range(0, 37))) ds = rioxarray.open_rasterio( os.path.join(cfg.data['path'], s_var + "_final.nc"), masked=True, chunks=True, ) clipped = ds.rio.clip(geometries=poly_geojson, crs=4326) except Exception as e: print(e) print(clipped)
def clip_tif_wrt_tif(inpath, outpath, to_copy_from_path): """ :param inpath: path to tif that is to be clipped :param outpath: output path for tif :param to_copy_from_path: tif to copy from :return: outpath """ poly = get_polygon(to_copy_from_path).wkt geom = mapping(loads(poly)) rds = rioxarray.open_rasterio(inpath, parse_coordinates=False) try: masked = rds.rio.clip([geom], "EPSG:4326", drop=True) masked.rio.to_raster(outpath) rds.close() os.remove(inpath) except: rds.close() print("error clipping img:") print("{}".format(inpath)) base_path = os.path.split(inpath[0])[0] shutil.rmtree(base_path) return outpath
def process_modis(country): """ Process the modis data by converting from .hdf to .tif. """ path = os.path.join(DATA_RAW, 'modis', '.hdf') all_paths = glob.glob(path + '/*.hdf') #[:1] for path in all_paths: filename_out = os.path.basename(path)[:-4] + ".tif" directory = os.path.join(DATA_RAW, 'modis', '.tif') path_out = os.path.join(directory, filename_out) if not os.path.exists(os.path.dirname(path_out)): os.makedirs(os.path.dirname(path_out)) if not os.path.exists(path_out): print('Working on : {}'.format(path)) modis_pre = rxr.open_rasterio(path, masked=True) modis_pre = modis_pre.rio.reproject("EPSG:4326") modis_pre.Percent_Tree_Cover.rio.to_raster(path_out, crs='epsg:4326') return
def calculate_stats(typ: str, sat: str, date: str, db_handler): if sat == 'modis': mosaic = glob(os.path.join(const.INTERMEDIATE_TIF_MODIS, date, 'modis_composite*.tif'))[0] elif sat == 'viirs': mosaic = glob(os.path.join(const.OUTPUT_TIF_VIIRS, date.split('.')[0], f'{date}.tif'))[0] else: return gdf = gpd.read_file(glob(os.path.join(f'aoi', typ,'*.shp'))[0]) gdf = gdf.to_crs('EPSG:3153') with rioxr.open_rasterio(mosaic) as src: src = src.rio.reproject('EPSG:3153', resolution=const.RES[sat]) for _, row in gdf.iterrows(): if typ == 'watersheds': name = "_".join(row.basinName.replace('.','').split(" ")) else: name = "_".join(row.WSDG_NAME.replace('.','').split(" ")) clipped = src.rio.clip([row.geometry], drop=False, all_touched=True) nodata = (((clipped.data > 100)&(clipped.data != 255)).sum()) below = ((clipped.data <= 20).sum()) snow = (((clipped.data <= 100)&(clipped.data > 20)).sum()) area = nodata + below + snow prepare = { 'sat': sat, 'name': name, 'date_': date, 'coverage': (snow/area)*100, 'nodata': (nodata/area)*100, 'below_threshold': (below/area)*100 } db_handler.insert(**prepare)
def test_open_rasterio_mask_chunk_clip(): with rioxarray.open_rasterio( os.path.join(TEST_COMPARE_DATA_DIR, "small_dem_3m_merged.tif"), masked=True, chunks=True, default_name="dem", ) as xdi: assert xdi.name == "dem" assert str(xdi.dtype) == "float64" assert str(xdi.data.dtype) == "float64" assert str(type(xdi.data)) == "<class 'dask.array.core.Array'>" assert xdi.chunks == ((1, ), (245, ), (574, )) assert np.isnan(xdi.values).sum() == 52119 test_encoding = dict(xdi.encoding) assert test_encoding.pop("source").endswith("small_dem_3m_merged.tif") assert test_encoding == {"_FillValue": 0.0} attrs = dict(xdi.attrs) assert_almost_equal( tuple(xdi.rio._cached_transform())[:6], (3.0, 0.0, 425047.68381405267, 0.0, -3.0, 4615780.040546387), ) assert attrs == { "grid_mapping": "spatial_ref", "add_offset": 0.0, "scale_factor": 1.0, } # get subset for testing subset = xdi.isel(x=slice(150, 160), y=slice(100, 150)) comp_subset = subset.isel(x=slice(1, None), y=slice(1, None)) # add transform for test comp_subset.rio.write_transform() geometries = [{ "type": "Polygon", "coordinates": [[ [subset.x.values[0], subset.y.values[-1]], [subset.x.values[0], subset.y.values[0]], [subset.x.values[-1], subset.y.values[0]], [subset.x.values[-1], subset.y.values[-1]], [subset.x.values[0], subset.y.values[-1]], ]], }] # test data array clipped = xdi.rio.clip(geometries, comp_subset.rio.crs) _assert_xarrays_equal(clipped, comp_subset) test_encoding = dict(clipped.encoding) assert test_encoding.pop("source").endswith("small_dem_3m_merged.tif") assert test_encoding == {"_FillValue": 0.0} # test dataset clipped_ds = xdi.to_dataset(name="test_data").rio.clip( geometries, subset.rio.crs) comp_subset_ds = comp_subset.to_dataset(name="test_data") _assert_xarrays_equal(clipped_ds, comp_subset_ds) test_encoding = dict(clipped.encoding) assert test_encoding.pop("source").endswith("small_dem_3m_merged.tif") assert test_encoding == {"_FillValue": 0.0}
def test_no_mask_and_scale(): with rioxarray.open_rasterio(os.path.join(TEST_INPUT_DATA_DIR, "tmmx_20190121.nc"), masked=True) as rds: assert np.nanmin(rds.air_temperature.values) == 287 assert np.nanmax(rds.air_temperature.values) == 821 test_encoding = dict(rds.air_temperature.encoding) source = test_encoding.pop("source") assert source.startswith("netcdf:") and source.endswith( "tmmx_20190121.nc:air_temperature") assert test_encoding == { "_FillValue": 32767.0, "missing_value": 32767, } attrs = rds.air_temperature.attrs assert attrs == { "_Unsigned": "true", "add_offset": 220.0, "coordinates": "day", "coordinate_system": "WGS84,EPSG:4326", "description": "Daily Maximum Temperature", "dimensions": "lon lat time", "grid_mapping": "crs", "long_name": "tmmx", "scale_factor": 0.1, "standard_name": "tmmx", "units": "K", }
def gridtrack(Longitude='', Latitude='', geotif='', geotif_name='', geotif_epsg=''): import pygmt import rioxarray import pandas as pd Longitude = Longitude Latitude = Latitude geotif = geotif geotif_name = geotif_name geotif_epsg = geotif_epsg df = pd.DataFrame(Longitude) df['Latitude'] = pd.DataFrame(Latitude) df.columns = ['Longitude', 'Latitude'] rds = rioxarray.open_rasterio(geotif) rds.rio.set_crs(geotif_epsg) rds = rds.rio.reproject("epsg:4326") rds = rds.squeeze('band') rds = rds.astype(float) df = pygmt.grdtrack(df, rds, newcolname=geotif_name) raster_vals = df[geotif_name].values return raster_vals del df, Latitude, Longitude del rds
def test_open_group_filter__missing(): with rioxarray.open_rasterio( os.path.join(TEST_INPUT_DATA_DIR, "PLANET_SCOPE_3D.nc"), variable="blue", group=["non-existent"], ) as rds: assert list(rds.data_vars) == []
def get_dataset(self, key, info): """Load a dataset.""" if self._polarization != key["polarization"]: return logger.debug('Reading %s.', key['name']) if key['name'] in ['longitude', 'latitude', 'altitude']: logger.debug('Constructing coordinate arrays.') arrays = dict() arrays['longitude'], arrays['latitude'], arrays[ 'altitude'] = self.get_lonlatalts() data = arrays[key['name']] data.attrs.update(info) else: data = rioxarray.open_rasterio(self.filename, lock=False, chunks=(1, CHUNK_SIZE, CHUNK_SIZE)).squeeze() data = data.assign_coords(x=np.arange(len(data.coords['x'])), y=np.arange(len(data.coords['y']))) data = self._calibrate_and_denoise(data, key) data.attrs.update(info) data.attrs.update({'platform_name': self._mission_id}) data = self._change_quantity(data, key['quantity']) return data
def join_files(file_list): ''' a method for merging raster/tif data along the band dimension using rioxarray. this method does not save data to storage. ''' import rioxarray as rxr import xarray as xr # initialize band names var_names = [] # loop over bands, append to a rasterio xarray for file in file_list: xmin,ymin,xmax,ymax=tuple(gdal_corners(file)) cda=rxr.open_rasterio(file,chunks=(1,1200,1200)) cda=cda.sel(x=slice(xmin,xmax),y=slice(ymax,ymin)) if file == file_list[0]: da = cda else: da = xr.concat([da, cda], "band") # extract frequency / polarization band var_name = file.split('_')[5] var_name = var_name[0:2] + var_name[5:] var_names.append(var_name) # change band names for reading da = da.assign_coords({'band' : var_names}) return da
def mk_mask(region_fp, raster_fp, output_fp): """ 在指定区域运行SWAT,需准备相应的数据有,气象强迫数据(降雨,相对湿度,短波辐射,风速,温度): 在气象数据的栅格数据中,以区域内每个栅格作为站点提取气象强迫数据序列。 ------------------------------------------------------- parameters: returns: """ shapefile = gpd.GeoDataFrame.from_file(region_fp) # 读取矢量文件 raster_data = rioxr.open_rasterio(raster_fp)[0] # 读取栅格文件 raster_data.rio.set_crs(shapefile.crs) # 统一坐标系 clipped = raster_data.rio.clip(shapefile['geometry'], drop=False) # 以矢量边界剪切栅格数据 mask = np.where(clipped == clipped._FillValue, np.nan, 1) da = xr.DataArray(mask, dims=('lat', 'lon'), coords={ "lat": np.arange(54.95, 15.05 - 0.01, -0.1), "lon": np.arange(70.05, 139.95 + 0.01, 0.1) }) ds = xr.Dataset({'mask': da}) ds.to_netcdf(output_fp) return
def test_merge_arrays__res(): dem_test = os.path.join(TEST_INPUT_DATA_DIR, "MODIS_ARRAY.nc") with open_rasterio(dem_test, masked=True) as rds: rds.attrs = { "_FillValue": rds.rio.nodata, "grid_mapping": "spatial_ref", "crs": rds.attrs["crs"], } arrays = [ rds.isel(x=slice(100), y=slice(100)), rds.isel(x=slice(100, 200), y=slice(100, 200)), rds.isel(x=slice(100), y=slice(100, 200)), rds.isel(x=slice(100, 200), y=slice(100)), ] merged = merge_arrays(arrays, res=(300, 300)) assert_almost_equal( merged.rio.bounds(), (-7274009.649486291, 5003608.61015275, -7227509.649486291, 5050108.61015275), ) assert_almost_equal( tuple(merged.rio.transform()), (300.0, 0.0, -7274009.649486291, 0.0, -300.0, 5050108.61015275, 0.0, 0.0, 1.0), ) assert_almost_equal(merged.attrs.pop("transform"), tuple(merged.rio.transform())[:6]) assert merged.rio.shape == (155, 155) assert merged.coords["band"].values == [1] assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"] assert merged.rio.crs == rds.rio.crs assert merged.attrs == rds.attrs assert_almost_equal(nansum(merged), 13556564)
def test_merge_datasets(): dem_test = os.path.join( TEST_INPUT_DATA_DIR, "MOD09GA.A2008296.h14v17.006.2015181011753.hdf" ) with open_rasterio(dem_test, group="MODIS_Grid_500m_2D") as rds: datasets = [ rds.isel(x=slice(600), y=slice(600)), rds.isel(x=slice(600, None), y=slice(600, None)), rds.isel(x=slice(600), y=slice(600, None)), rds.isel(x=slice(600, None), y=slice(600)), ] merged = merge_datasets(datasets) data_vars = sorted(merged.data_vars) assert data_vars == [ "QC_500m_1", "iobs_res_1", "num_observations_500m", "obscov_500m_1", "sur_refl_b01_1", "sur_refl_b02_1", "sur_refl_b03_1", "sur_refl_b04_1", "sur_refl_b05_1", "sur_refl_b06_1", "sur_refl_b07_1", ] data_var = data_vars[0] if RASTERIO_GE_125: assert_almost_equal( merged[data_var].rio.bounds(), (-4447802.078667, -10007554.677, -3335851.559, -8895604.157333), ) assert merged.rio.shape == (2400, 2400) assert_almost_equal(merged[data_var].sum(), 4539666606551516) else: assert_almost_equal( merged[data_var].rio.bounds(), (-4447802.078667, -10008017.989716524, -3335388.246283474, -8895604.157333), ) assert merged.rio.shape == (2401, 2401) assert_almost_equal(merged[data_var].sum(), 4543446965182987) assert_almost_equal( tuple(merged[data_var].rio.transform()), ( 463.3127165279158, 0.0, -4447802.078667, 0.0, -463.3127165279151, -8895604.157333, 0.0, 0.0, 1.0, ), ) assert merged.coords["band"].values == [1] assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"] assert merged.rio.crs == rds.rio.crs assert merged.attrs == rds.attrs assert merged.encoding["grid_mapping"] == "spatial_ref"
def test_rasterio_environment(): with create_tmp_geotiff() as (tmp_file, expected): # Should fail with error since suffix not allowed with pytest.raises(Exception): with rasterio.Env(GDAL_SKIP="GTiff"): with rioxarray.open_rasterio(tmp_file) as actual: assert_allclose(actual, expected)
def test_mask_and_scale(): with pytest.warns(SerializationWarning): with rioxarray.open_rasterio( os.path.join(TEST_INPUT_DATA_DIR, "tmmx_20190121.nc"), mask_and_scale=True ) as rds: assert np.nanmin(rds.air_temperature.values) == 248.7 assert np.nanmax(rds.air_temperature.values) == 302.1 assert rds.air_temperature.encoding == { "_Unsigned": "true", "add_offset": 220.0, "scale_factor": 0.1, "_FillValue": 32767.0, "missing_value": 32767, } attrs = rds.air_temperature.attrs attrs.pop("transform") assert attrs == { "coordinates": "day", "coordinate_system": "WGS84,EPSG:4326", "description": "Daily Maximum Temperature", "dimensions": "lon lat time", "grid_mapping": "spatial_ref", "long_name": "tmmx", "standard_name": "tmmx", "units": "K", }
def test_merge_arrays__res(): dem_test = os.path.join(TEST_INPUT_DATA_DIR, "MODIS_ARRAY.nc") with open_rasterio(dem_test, masked=True) as rds: rds.attrs = { "_FillValue": rds.rio.nodata, "crs": rds.attrs["crs"], } arrays = [ rds.isel(x=slice(100), y=slice(100)), rds.isel(x=slice(100, 200), y=slice(100, 200)), rds.isel(x=slice(100), y=slice(100, 200)), rds.isel(x=slice(100, 200), y=slice(100)), ] merged = merge_arrays(arrays, res=(300, 300)) assert_almost_equal( merged.rio.bounds(), (-7274009.6494863, 5003308.6101528, -7227209.6494863, 5050108.6101528), ) assert_almost_equal( tuple(merged.rio.transform()), (300.0, 0.0, -7274009.649486291, 0.0, -300.0, 5050108.61015275, 0.0, 0.0, 1.0), ) assert merged.rio._cached_transform() == merged.rio.transform() assert merged.rio.shape == (156, 156) assert merged.coords["band"].values == [1] assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"] assert merged.rio.crs == rds.rio.crs assert_almost_equal(merged.attrs.pop("_FillValue"), rds.attrs.pop("_FillValue")) compare_attrs = dict(rds.attrs) compare_attrs.pop("crs") assert merged.attrs == compare_attrs assert merged.encoding["grid_mapping"] == "spatial_ref" assert_almost_equal(nansum(merged), 13760565)
def test_if_damaged(self, file_path): import rioxarray try: result: xr.DataArray = rioxarray.open_rasterio(file_path) return False except Exception as err: return True
def test_merge_arrays(squeeze): dem_test = os.path.join(TEST_INPUT_DATA_DIR, "MODIS_ARRAY.nc") with open_rasterio(dem_test) as rds: rds.attrs = { "_FillValue": rds.rio.nodata, "crs": rds.attrs["crs"], } arrays = [ rds.isel(x=slice(100), y=slice(100)), rds.isel(x=slice(100, 200), y=slice(100, 200)), rds.isel(x=slice(100), y=slice(100, 200)), rds.isel(x=slice(100, 200), y=slice(100)), ] if squeeze: arrays = [array.squeeze() for array in arrays] merged = merge_arrays(arrays) if RASTERIO_GE_125: assert_almost_equal( merged.rio.bounds(), (-7274009.6494863, 5003777.3385, -7227678.3778335, 5050108.6101528), ) assert merged.rio.shape == (200, 200) assert_almost_equal(merged.sum(), 22865733) else: assert_almost_equal( merged.rio.bounds(), ( -7274009.649486291, 5003545.682141737, -7227446.721475236, 5050108.61015275, ), ) assert merged.rio.shape == (201, 201) assert_almost_equal(merged.sum(), 11368261) assert_almost_equal( tuple(merged.rio.transform()), ( 231.6563582639536, 0.0, -7274009.649486291, 0.0, -231.65635826374404, 5050108.61015275, 0.0, 0.0, 1.0, ), ) assert merged.rio._cached_transform() == merged.rio.transform() assert merged.coords["band"].values == [1] assert sorted(merged.coords) == ["band", "spatial_ref", "x", "y"] assert merged.rio.crs == rds.rio.crs assert merged.attrs == rds.attrs assert merged.encoding["grid_mapping"] == "spatial_ref"
def test_utm(): with create_tmp_geotiff() as (tmp_file, expected): with rioxarray.open_rasterio(tmp_file) as rioda: assert_allclose(rioda, expected) assert rioda.attrs["scale_factor"] == 1.0 assert rioda.attrs["add_offset"] == 0.0 assert rioda.attrs["long_name"] == ("d1", "d2", "d3") assert rioda.attrs["units"] == ("u1", "u2", "u3") assert rioda.rio.crs == expected.rio.crs assert_array_equal(rioda.rio.resolution(), expected.rio.resolution()) assert isinstance(rioda.rio._cached_transform(), Affine) assert rioda.rio.nodata is None # Check no parse coords with rioxarray.open_rasterio(tmp_file, parse_coordinates=False) as rioda: assert "x" not in rioda.coords assert "y" not in rioda.coords
def test_isel_window(): with rioxarray.open_rasterio( os.path.join(TEST_INPUT_DATA_DIR, "MODIS_ARRAY.nc")) as mda: assert (mda.rio.isel_window( Window.from_slices(slice(10, 12), slice(10, 12))) == mda.isel(x=slice(10, 12), y=slice(10, 12))).all()