-
|
Is there a way to create a rioxarray-enabled DataArray "from scratch", i.e., without having a file that can be opened via open_rasterio? Should I create a DataArray and then set somehow the crs and transform manually? I am currently saving a tiff file just to reopen it, like this (the height, width, crs, and transform params are known) with rasterio.open(
file,
"w",
driver="GTiff",
height=nrow,
width=ncol,
count=1,
dtype="uint8",
crs=epsg,
transform=transform,
) as dst:
dst.write(np.ones((nrow, ncol)), 1) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
xds = xarray.DataArray(
np.ones((nrow, ncol), dtype="uint8"),
dims=("y", "x"),
)
xds.rio.write_transform(transform, inplace=True)
xds.rio.write_crs("EPSG:4326", inplace=True)
xds.rio.write_coordinate_system(inplace=True) |
Beta Was this translation helpful? Give feedback.
-
|
Thanks, that put me on the right track but it doesn't have the coords set. This is what the DataArray looks like when I load it with <xarray.DataArray (band: 1, y: 6736, x: 4210)>
[28358560 values with dtype=uint8]
Coordinates:
* band (band) int64 1
* x (x) float64 2.601e+05 2.601e+05 ... 3.863e+05 3.863e+05
* y (y) float64 4.907e+06 4.907e+06 ... 4.705e+06 4.705e+06
spatial_ref int64 0
Attributes:
scale_factor: 1.0
add_offset: 0.0And this is what the one you suggested is <xarray.DataArray (y: 6735, x: 4210)>
array([[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1],
...,
[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1]], dtype=uint8)
Coordinates:
spatial_ref int64 0
Dimensions without coordinates: y, xBut I think this will work: # get the x and y coords (may be a simpler/better way to do this)
x, _ = rasterio.transform.xy(transform, np.arange(ncol), np.arange(ncol))
_, y = rasterio.transform.xy(transform, np.arange(nrow), np.arange(nrow))
da = xarray.DataArray(
np.ones((1,nrow, ncol), dtype="uint8"),
dims=("band", "y", "x"),
coords=dict(x=x, y=y, band=[1])
)
da.rio.write_transform(transform, inplace=True)
da.rio.write_crs(epsg, inplace=True)Which produces: <xarray.DataArray (band: 1, y: 6735, x: 4210)>
array([[[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1],
...,
[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1]]], dtype=uint8)
Coordinates:
* x (x) float64 2.601e+05 2.601e+05 ... 3.863e+05 3.863e+05
* y (y) float64 4.907e+06 4.907e+06 ... 4.705e+06 4.705e+06
* band (band) int64 1
spatial_ref int64 0I've just got to figure out if that once cell difference in the y-dimension means anything (I think it's a rasterio quirk which is noted in the docs) |
Beta Was this translation helpful? Give feedback.
Thanks, that put me on the right track but it doesn't have the coords set. This is what the DataArray looks like when I load it with
open_rasterio:And this is what the one you suggested is