Skip to content

Commit a750ec2

Browse files
authored
Get crs and transform from xarray rio accessor in array_to_memory_file (opengeos#679)
1 parent a3af097 commit a750ec2

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

examples/notebooks/89_image_array_viz.ipynb

+1-17
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,6 @@
227227
"masked_array = xr.where(array < 2000, 0, 1)"
228228
]
229229
},
230-
{
231-
"cell_type": "markdown",
232-
"metadata": {},
233-
"source": [
234-
"Create an in-memory raster dataset from the elevation class array and use the projection and extent of the DEM."
235-
]
236-
},
237-
{
238-
"cell_type": "code",
239-
"execution_count": null,
240-
"metadata": {},
241-
"outputs": [],
242-
"source": [
243-
"image = leafmap.array_to_image(masked_array, source=dem)"
244-
]
245-
},
246230
{
247231
"cell_type": "markdown",
248232
"metadata": {},
@@ -258,7 +242,7 @@
258242
"source": [
259243
"m = leafmap.Map()\n",
260244
"m.add_raster(dem, colormap=\"terrain\", layer_name=\"DEM\")\n",
261-
"m.add_raster(image, colormap=\"coolwarm\", layer_name=\"Classified DEM\")\n",
245+
"m.add_raster(masked_array, colormap=\"coolwarm\", layer_name=\"Classified DEM\")\n",
262246
"m"
263247
]
264248
}

leafmap/common.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -10293,6 +10293,15 @@ def array_to_memory_file(
1029310293
array = (
1029410294
array.isel(time=0).rename({y_dim: "y", x_dim: "x"}).transpose("y", "x")
1029510295
)
10296+
if hasattr(array, "rio"):
10297+
if hasattr(array.rio, "crs"):
10298+
crs = array.rio.crs
10299+
if hasattr(array.rio, "transform"):
10300+
transform = array.rio.transform()
10301+
elif source is None:
10302+
if hasattr(array, "encoding"):
10303+
if "source" in array.encoding:
10304+
source = array.encoding["source"]
1029610305
array = array.values
1029710306

1029810307
if array.ndim == 3 and transpose:
@@ -10305,14 +10314,14 @@ def array_to_memory_file(
1030510314
if compress is None:
1030610315
compress = src.compression
1030710316
else:
10308-
if cellsize is None:
10309-
raise ValueError("cellsize must be provided if source is not provided")
1031010317
if crs is None:
1031110318
raise ValueError(
1031210319
"crs must be provided if source is not provided, such as EPSG:3857"
1031310320
)
1031410321

1031510322
if transform is None:
10323+
if cellsize is None:
10324+
raise ValueError("cellsize must be provided if source is not provided")
1031610325
# Define the geotransformation parameters
1031710326
xmin, ymin, xmax, ymax = (
1031810327
0,

0 commit comments

Comments
 (0)