Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get crs and transform from xarray rio accessor in array_to_memory_file #679

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions examples/notebooks/89_image_array_viz.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,6 @@
"masked_array = xr.where(array < 2000, 0, 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create an in-memory raster dataset from the elevation class array and use the projection and extent of the DEM."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"image = leafmap.array_to_image(masked_array, source=dem)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -258,7 +242,7 @@
"source": [
"m = leafmap.Map()\n",
"m.add_raster(dem, colormap=\"terrain\", layer_name=\"DEM\")\n",
"m.add_raster(image, colormap=\"coolwarm\", layer_name=\"Classified DEM\")\n",
"m.add_raster(masked_array, colormap=\"coolwarm\", layer_name=\"Classified DEM\")\n",
"m"
]
}
Expand Down
13 changes: 11 additions & 2 deletions leafmap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10293,6 +10293,15 @@ def array_to_memory_file(
array = (
array.isel(time=0).rename({y_dim: "y", x_dim: "x"}).transpose("y", "x")
)
if hasattr(array, "rio"):
if hasattr(array.rio, "crs"):
crs = array.rio.crs
if hasattr(array.rio, "transform"):
transform = array.rio.transform()
elif source is None:
if hasattr(array, "encoding"):
if "source" in array.encoding:
source = array.encoding["source"]
array = array.values

if array.ndim == 3 and transpose:
Expand All @@ -10305,14 +10314,14 @@ def array_to_memory_file(
if compress is None:
compress = src.compression
else:
if cellsize is None:
raise ValueError("cellsize must be provided if source is not provided")
if crs is None:
raise ValueError(
"crs must be provided if source is not provided, such as EPSG:3857"
)

if transform is None:
if cellsize is None:
raise ValueError("cellsize must be provided if source is not provided")
# Define the geotransformation parameters
xmin, ymin, xmax, ymax = (
0,
Expand Down
Loading