|
44 | 44 | "outputs": [],
|
45 | 45 | "source": [
|
46 | 46 | "import numpy as np\n",
|
47 |
| - "import xarray as xr" |
| 47 | + "import xarray as xr\n", |
| 48 | + "\n", |
| 49 | + "# Ensure random arrays are the same each time\n", |
| 50 | + "np.random.seed(0)" |
48 | 51 | ]
|
49 | 52 | },
|
50 | 53 | {
|
|
194 | 197 | "id": "5b2acbba-ba4f-4819-8600-f89a6109e6df",
|
195 | 198 | "metadata": {},
|
196 | 199 | "source": [
|
197 |
| - "## Raster files using rioxarray" |
| 200 | + "## Raster files using rioxarray\n", |
| 201 | + "\n", |
| 202 | + "[rioxarray](https://corteva.github.io/rioxarray) is an *Xarray extension* that allows reading and writing a wide variety of geospatial image formats compatible with Geographic Information Systems (GIS), for example GeoTIFF.\n", |
| 203 | + "\n", |
| 204 | + "If rioxarray is installed your environment it will be automatically detected and give you access to the `.rio` accessor:" |
| 205 | + ] |
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "code", |
| 209 | + "execution_count": null, |
| 210 | + "id": "648d3d02-b322-468f-8bd5-41eaf3d2bc36", |
| 211 | + "metadata": {}, |
| 212 | + "outputs": [], |
| 213 | + "source": [ |
| 214 | + "da = xr.DataArray(\n", |
| 215 | + " data=ds1.a.data,\n", |
| 216 | + " coords={\n", |
| 217 | + " \"y\": np.linspace(47.5, 47.8, 4),\n", |
| 218 | + " \"x\": np.linspace(-122.9, -122.7, 2),\n", |
| 219 | + " },\n", |
| 220 | + ")\n", |
| 221 | + "\n", |
| 222 | + "# Add Geospatial Coordinate Reference https://epsg.io/4326\n", |
| 223 | + "# this is stored as a 'spatial_ref' coordinate\n", |
| 224 | + "da.rio.write_crs(\"epsg:4326\", inplace=True)\n", |
| 225 | + "da" |
| 226 | + ] |
| 227 | + }, |
| 228 | + { |
| 229 | + "cell_type": "code", |
| 230 | + "execution_count": null, |
| 231 | + "id": "87b540d6-33fd-4956-9a03-4d812a62f50e", |
| 232 | + "metadata": {}, |
| 233 | + "outputs": [], |
| 234 | + "source": [ |
| 235 | + "da.rio.to_raster('ds1_a.tiff')" |
| 236 | + ] |
| 237 | + }, |
| 238 | + { |
| 239 | + "cell_type": "markdown", |
| 240 | + "id": "d83ec937-8e01-488d-873b-16dd83edcd62", |
| 241 | + "metadata": {}, |
| 242 | + "source": [ |
| 243 | + "NOTE: you can now load this file into GIS tools like [QGIS](https://www.qgis.org)! Or open back into Xarray:" |
| 244 | + ] |
| 245 | + }, |
| 246 | + { |
| 247 | + "cell_type": "code", |
| 248 | + "execution_count": null, |
| 249 | + "id": "81c1881c-7c7f-48c6-8add-63886372357a", |
| 250 | + "metadata": {}, |
| 251 | + "outputs": [], |
| 252 | + "source": [ |
| 253 | + "DA = xr.open_dataarray('ds1_a.tiff', engine='rasterio')\n", |
| 254 | + "DA.rio.crs" |
198 | 255 | ]
|
199 | 256 | }
|
200 | 257 | ],
|
|
0 commit comments