{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example - Convert dataset to raster (GeoTiff)\n", "\n", "Often, it is desirable to take a variable (band) out of your dataset and export it to a raster.\n", "This is possible with the `rio.to_raster()`method. It does most of the work for you so you don't\n", "have to.\n", "\n", "Note: The `rio.to_raster()` method only works on a 2-dimensional or 3-dimensional `xarray.DataArray` or a 2-dimensional `xarray.Dataset`.\n", "\n", "API Reference:\n", "\n", "- DataArray: [rio.to_raster()](../rioxarray.rst#rioxarray.raster_array.RasterArray.to_raster)\n", "- Dataset: [rio.to_raster()](../rioxarray.rst#rioxarray.raster_dataset.RasterDataset.to_raster)\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import rioxarray" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See docs for [rioxarray.open_rasterio](../rioxarray.rst#rioxarray-open-rasterio)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:      (time: 2, x: 10, y: 10)\n",
       "Coordinates:\n",
       "  * time         (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n",
       "  * x            (x) float64 4.663e+05 4.663e+05 ... 4.663e+05 4.663e+05\n",
       "  * y            (y) float64 8.085e+06 8.085e+06 ... 8.085e+06 8.085e+06\n",
       "    spatial_ref  int32 0\n",
       "Data variables:\n",
       "    blue         (time, y, x) float64 ...\n",
       "    green        (time, y, x) float64 ...\n",
       "Attributes:\n",
       "    coordinates:  spatial_ref
" ], "text/plain": [ "\n", "Dimensions: (time: 2, x: 10, y: 10)\n", "Coordinates:\n", " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", " * x (x) float64 4.663e+05 4.663e+05 ... 4.663e+05 4.663e+05\n", " * y (y) float64 8.085e+06 8.085e+06 ... 8.085e+06 8.085e+06\n", " spatial_ref int32 0\n", "Data variables:\n", " blue (time, y, x) float64 ...\n", " green (time, y, x) float64 ...\n", "Attributes:\n", " coordinates: spatial_ref" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rds = rioxarray.open_rasterio(\n", " \"../../test/test_data/input/PLANET_SCOPE_3D.nc\",\n", ")\n", "rds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Converting Dataset to raster\n", "\n", "Dataset: [rio.to_raster()](../rioxarray.rst#rioxarray.raster_dataset.RasterDataset.to_raster)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# note how one time slice was selected on export to make the dataset 2D\n", "rds.isel(time=0).rio.to_raster(\"planet_scope.tif\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"bounds\": [466266.0, 8084670.0, 466296.0, 8084700.0], \"colorinterp\": [\"gray\", \"undefined\"], \"count\": 2, \"crs\": \"EPSG:32722\", \"descriptions\": [\"blue\", \"green\"], \"driver\": \"GTiff\", \"dtype\": \"float64\", \"height\": 10, \"indexes\": [1, 2], \"interleave\": \"pixel\", \"lnglat\": [-51.31732641226951, -17.322997474192466], \"mask_flags\": [[\"nodata\"], [\"nodata\"]], \"nodata\": NaN, \"res\": [3.0, 3.0], \"shape\": [10, 10], \"tiled\": false, \"transform\": [3.0, 0.0, 466266.0, 0.0, -3.0, 8084700.0, 0.0, 0.0, 1.0], \"units\": [null, null], \"width\": 10}\n" ] } ], "source": [ "!rio info planet_scope.tif" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Converting DataArray to raster\n", "\n", "DataArray: [rio.to_raster()](../rioxarray.rst#rioxarray.raster_array.RasterArray.to_raster)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# note how selecting one variable allowed for multiple time steps in a single raster\n", "rds.green.rio.to_raster(\"planet_scope_green.tif\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"bounds\": [466266.0, 8084670.0, 466296.0, 8084700.0], \"colorinterp\": [\"gray\", \"undefined\"], \"count\": 2, \"crs\": \"EPSG:32722\", \"descriptions\": [\"green\", \"green\"], \"driver\": \"GTiff\", \"dtype\": \"float64\", \"height\": 10, \"indexes\": [1, 2], \"interleave\": \"pixel\", \"lnglat\": [-51.31732641226951, -17.322997474192466], \"mask_flags\": [[\"nodata\"], [\"nodata\"]], \"nodata\": NaN, \"res\": [3.0, 3.0], \"shape\": [10, 10], \"tiled\": false, \"transform\": [3.0, 0.0, 466266.0, 0.0, -3.0, 8084700.0, 0.0, 0.0, 1.0], \"units\": [null, null], \"width\": 10}\n" ] } ], "source": [ "!rio info planet_scope_green.tif" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Converting DataArray to raster in a different format\n", "Example here, an ER Mapper grid.\n", "Look at gdal for possible formats that you can use to write to." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# you will get a two file raster: the .ers file with the metdata and the data with no extension\n", "rds.blue.rio.to_raster(\"planet_scope_green.ers\", driver=\"ERS\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Change the compression of the raster and explicitly make it a Geotiff" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "rds.blue.rio.to_raster(\"planet_scope_green_LZW_compression.tif\", driver=\"GTiff\", compress=\"LZW\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Change the basic datatype of the raster (in this example, also saving space going to 32 bit)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rds.blue.dtype" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rds.blue.astype('float32').rio.to_raster(\"planet_scope_green_LZW_compression.tif\", driver=\"GTiff\", compress=\"LZW\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Memory efficient raster writing\n", "\n", "Useful for reading and writing larger rasters to disk.\n", "\n", "Note: This will increase the time it takes to generate the raster.\n", "\n", "Also see:\n", "\n", "- [Reading and Writing with Dask](dask_read_write.ipynb)\n", "- [Reading COGs in Parallel](read-locks.ipynb)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "rds = rioxarray.open_rasterio(\n", " \"../../test/test_data/input/PLANET_SCOPE_3D.nc\",\n", " lock=False, # disable internal caching\n", " cache=False, # don't keep data loaded in memory. pull from disk every time\n", ")\n", "\n", "rds.green.rio.to_raster(\n", " \"planet_scope_tiled.tif\",\n", " tiled=True, # GDAL: By default striped TIFF files are created. This option can be used to force creation of tiled TIFF files.\n", " windowed=True, # rioxarray: read & write one window at a time\n", ") " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"blockxsize\": 256, \"blockysize\": 256, \"bounds\": [466266.0, 8084670.0, 466296.0, 8084700.0], \"colorinterp\": [\"gray\", \"undefined\"], \"count\": 2, \"crs\": \"EPSG:32722\", \"descriptions\": [\"green\", \"green\"], \"driver\": \"GTiff\", \"dtype\": \"float64\", \"height\": 10, \"indexes\": [1, 2], \"interleave\": \"pixel\", \"lnglat\": [-51.31732641226951, -17.322997474192466], \"mask_flags\": [[\"nodata\"], [\"nodata\"]], \"nodata\": NaN, \"res\": [3.0, 3.0], \"shape\": [10, 10], \"tiled\": true, \"transform\": [3.0, 0.0, 466266.0, 0.0, -3.0, 8084700.0, 0.0, 0.0, 1.0], \"units\": [null, null], \"width\": 10}\n" ] } ], "source": [ "!rio info planet_scope_tiled.tif" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 4 }