8000 add short rioxarray example (#112) · qheuristics/xarray-tutorial@90b2c09 · GitHub
[go: up one dir, main page]

Skip to content

Commit 90b2c09

Browse files
authored
add short rioxarray example (xarray-contrib#112)
* add short rioxarray example * reset cache, typo
1 parent df8f83c commit 90b2c09

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

.github/workflows/main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
with:
2424
path: _build
2525
# NOTE: change key to "jupyterbook-DATE" to force rebuilding cache
26-
key: jupyterbook-20220617
26+
key: jupyterbook-20220627
2727

2828
- uses: ./.github/actions/setupconda
2929

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ dask-report.html
66
_build/
77
*.zarr
88
*.nc
9+
*.tiff
10+
*.tif
911
dask-worker-space/
1012

1113
# Byte-compiled / optimized / DLL files

fundamentals/01.1_io.ipynb

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@
4444
"outputs": [],
4545
"source": [
4646
"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)"
4851
]
4952
},
5053
{
@@ -194,7 +197,61 @@
194197
"id": "5b2acbba-ba4f-4819-8600-f89a6109e6df",
195198
"metadata": {},
196199
"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"
198255
]
199256
}
200257
],

0 commit comments

Comments
 (0)
0