From 6b768ec260fc65752c5396fbfe3a9b25e49ae2b1 Mon Sep 17 00:00:00 2001 From: RichardScottOZ <72196131+RichardScottOZ@users.noreply.github.com> Date: Sat, 9 Mar 2024 13:38:16 +1030 Subject: [PATCH] DOC: format conversion, driver and datatype examples for convert_to_raster notebook (#753) --- docs/examples/convert_to_raster.ipynb | 209 ++++++++++++++------------ 1 file changed, 110 insertions(+), 99 deletions(-) diff --git a/docs/examples/convert_to_raster.ipynb b/docs/examples/convert_to_raster.ipynb index 59f94363..b3c5aced 100644 --- a/docs/examples/convert_to_raster.ipynb +++ b/docs/examples/convert_to_raster.ipynb @@ -73,6 +73,7 @@ "}\n", "\n", "html[theme=dark],\n", + "body[data-theme=dark],\n", "body.vscode-dark {\n", " --xr-font-color0: rgba(255, 255, 255, 1);\n", " --xr-font-color2: rgba(255, 255, 255, 0.54);\n", @@ -302,6 +303,11 @@ " grid-column: 4;\n", "}\n", "\n", + ".xr-index-preview {\n", + " grid-column: 2 / 5;\n", + " color: var(--xr-font-color2);\n", + "}\n", + "\n", ".xr-var-name,\n", ".xr-var-dims,\n", ".xr-var-dtype,\n", @@ -323,14 +329,16 @@ "}\n", "\n", ".xr-var-attrs,\n", - ".xr-var-data {\n", + ".xr-var-data,\n", + ".xr-index-data {\n", " display: none;\n", " background-color: var(--xr-background-color) !important;\n", " padding-bottom: 5px !important;\n", "}\n", "\n", ".xr-var-attrs-in:checked ~ .xr-var-attrs,\n", - ".xr-var-data-in:checked ~ .xr-var-data {\n", + ".xr-var-data-in:checked ~ .xr-var-data,\n", + ".xr-index-data-in:checked ~ .xr-index-data {\n", " display: block;\n", "}\n", "\n", @@ -340,13 +348,16 @@ "\n", ".xr-var-name span,\n", ".xr-var-data,\n", + ".xr-index-name div,\n", + ".xr-index-data,\n", ".xr-attrs {\n", " padding-left: 25px !important;\n", "}\n", "\n", ".xr-attrs,\n", ".xr-var-attrs,\n", - ".xr-var-data {\n", + ".xr-var-data,\n", + ".xr-index-data {\n", " grid-column: 1 / -1;\n", "}\n", "\n", @@ -384,7 +395,8 @@ "}\n", "\n", ".xr-icon-database,\n", - ".xr-icon-file-text2 {\n", + ".xr-icon-file-text2,\n", + ".xr-no-icon {\n", " display: inline-block;\n", " vertical-align: middle;\n", " width: 1em;\n", @@ -394,110 +406,38 @@ " fill: currentColor;\n", "}\n", "
<xarray.Dataset>\n",
-       "Dimensions:      (y: 10, x: 10, time: 2)\n",
+       "Dimensions:      (time: 2, x: 10, y: 10)\n",
        "Coordinates:\n",
-       "  * y            (y) float64 8.085e+06 8.085e+06 ... 8.085e+06 8.085e+06\n",
-       "  * x            (x) float64 4.663e+05 4.663e+05 ... 4.663e+05 4.663e+05\n",
        "  * time         (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n",
-       "    spatial_ref  int64 0\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 6.611 5.581 0.3996 ... 3.491 5.056 3.368\n",
-       "    green        (time, y, x) float64 7.921 66.15 30.1 ... 21.76 27.29 18.41\n",
+       "    blue         (time, y, x) float64 ...\n",
+       "    green        (time, y, x) float64 ...\n",
        "Attributes:\n",
-       "    coordinates:  spatial_ref
  • coordinates :
    spatial_ref
  • " ], "text/plain": [ "\n", - "Dimensions: (y: 10, x: 10, time: 2)\n", + "Dimensions: (time: 2, x: 10, y: 10)\n", "Coordinates:\n", - " * y (y) float64 8.085e+06 8.085e+06 ... 8.085e+06 8.085e+06\n", - " * x (x) float64 4.663e+05 4.663e+05 ... 4.663e+05 4.663e+05\n", " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", - " spatial_ref int64 0\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", @@ -589,6 +529,77 @@ "!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": {}, @@ -654,7 +665,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.2" + "version": "3.9.16" } }, "nbformat": 4,