{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "c915fij0RD_z" }, "source": [ "# Read & visualize raster image using xarray" ] }, { "cell_type": "markdown", "metadata": { "id": "l0zyYYAxXeUQ" }, "source": [ "*Written by: Men Vuthy, 2022*" ] }, { "cell_type": "markdown", "metadata": { "id": "B2FXarKGzy2L" }, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": { "id": "nZSlx9c1ROyu" }, "source": [ "You can also run the code here in Google Colab. Try clicking button below:\n", "\n", "[](https://colab.research.google.com/drive/1vGTewC4kjxpQrVndB-WDv-Zep_L-hRDw#scrollTo=nZSlx9c1ROyu)" ] }, { "cell_type": "markdown", "metadata": { "id": "KHPUHilCZRA6" }, "source": [ "#### Objective\n", "\n", "* Read raster file using `xrray` module.\n", "* Visualize raster data in different style and color composites.\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "gmAWuVVWZ_XE" }, "source": [ "#### Code" ] }, { "cell_type": "markdown", "metadata": { "id": "cDnBEmWgQs19" }, "source": [ "Install necessary packages" ] }, { "cell_type": "markdown", "metadata": { "id": "M2cgaBLfIUWE" }, "source": [ "`!pip install rasterio`" ] }, { "cell_type": "markdown", "metadata": { "id": "sbD0DzKSQ762" }, "source": [ "Import necessary modules" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "ToqRwpboC220" }, "outputs": [], "source": [ "import numpy as np\n", "import rasterio\n", "import xarray as xr\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": { "id": "CyvWCq-hIvj7" }, "source": [ "Load and read raster data using `xr.open_rasterio`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "PRjLNwhnCzca" }, "outputs": [], "source": [ "# Load raster file\n", "raster = xr.open_rasterio(\"https://github.com/mapbox/rasterio/raw/1.2.1/tests/data/RGB.byte.tif\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 333 }, "id": "gRMTILJmJLqQ", "outputId": "e0d29aff-5839-40e8-94e9-6a0872ca834c" }, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray (band: 3, y: 718, x: 791)>\n",
"[1703814 values with dtype=uint8]\n",
"Coordinates:\n",
" * band (band) int64 1 2 3\n",
" * y (y) float64 2.827e+06 2.826e+06 2.826e+06 ... 2.612e+06 2.612e+06\n",
" * x (x) float64 1.021e+05 1.024e+05 1.027e+05 ... 3.389e+05 3.392e+05\n",
"Attributes:\n",
" transform: (300.0379266750948, 0.0, 101985.0, 0.0, -300.041782729805...\n",
" crs: +init=epsg:32618\n",
" res: (300.0379266750948, 300.041782729805)\n",
" is_tiled: 0\n",
" nodatavals: (0.0, 0.0, 0.0)\n",
" scales: (1.0, 1.0, 1.0)\n",
" offsets: (0.0, 0.0, 0.0)\n",
" AREA_OR_POINT: Area[1703814 values with dtype=uint8]
array([1, 2, 3])
array([2826764.979109, 2826464.937326, 2826164.895543, ..., 2612235.104457,\n",
" 2611935.062674, 2611635.020891])array([102135.018963, 102435.05689 , 102735.094817, ..., 338564.905183,\n",
" 338864.94311 , 339164.981037])