{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 光栅数据\n", "\n", "栅格数据模型使用单元格数组或像素来表示真实世界的对象。光栅数据集通常用于表示和管理图像、表面温度、数字高程模型和许多其他实体。栅格可以被认为是区域对象的一种特殊情况,其中区域被划分为规则的单元格网格。但是,有规律间隔的标记点数组可能是更好的类比,因为栅格存储为值数组,其中每个单元格在大多数 GIS 环境中由单个坐标对定义。在栅格数据模型中隐含着一个与每个单元格或像素相关的值。这与向量模型相反,向量模型可能具有或不具有与几何原语相关的值。\n", "\n", "为了处理光栅数据,将使用 `rasterio` 和稍后的 `geowombat`。幕后是 `numpy.ndarray` 做了所有繁重的工作。为了理解栅格是如何工作的,它有助于从零开始构建栅格。\n", "\n", "这里创建了两个 ndarray 对象,一个 X 跨越经度(longitude) $[-90°,90°]$,另一个 Y 跨越纬度(latitude) $[-90°,90°]$。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-90., -54., -18., 18., 54., 90.],\n", " [-90., -54., -18., 18., 54., 90.],\n", " [-90., -54., -18., 18., 54., 90.],\n", " [-90., -54., -18., 18., 54., 90.],\n", " [-90., -54., -18., 18., 54., 90.],\n", " [-90., -54., -18., 18., 54., 90.]])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "x = np.linspace(-90, 90, 6)\n", "y = np.linspace(-90, 90, 6)\n", "X, Y = np.meshgrid(x, y)\n", "X" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "生成一些表示温度的数据并将其存储在数组 `Z` 中:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZgAAAGzCAYAAAASUAGgAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAA9hAAAPY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LE7VG61e/vwXg8HhUUFGj79u3hdaFQSNu3b4+La9TxxHEclZeXa/Pmzfrzn/+sUaNGuT2Sa0KhkAKBgNtjmJk5c6b279+vxsbG8DJlyhQtWrRIjY2NcREXSWpra9PRo0c1bNgwt0fpVr8/g5Gk5cuXq6SkRFOmTNHUqVO1cuVKtbe3a/HixW6PZqatra3TdzXHjx9XY2OjBg8erLy8PBcns1NWVqb169frxRdflM/n05kzZyT953/fS01NdXk6OxUVFZo7d67y8vLU2tqq9evXa8eOHdq6davbo5nx+Xxd7q2lpaXpuuuu69f33O6//37Nnz9fI0aM0KlTp1RZWamkpCQtXLjQ7dG65/bL2KLlZz/7mZOXl+d4PB5n6tSpzu7du90eydSrr77qSOqylJSUuD2ame6OV5Kzdu1at0cz9c1vftMZMWKE4/F4nCFDhjgzZ850/vSnP7k9VtTFw8uUFyxY4AwbNszxeDzO9ddf7yxYsMA5cuSI22NdFv8fDADARL+/BwMAcAeBAQCYIDAAABMEBgBggsAAAEwQGACACQIDADBBYAAAJggMAMAEgQEAmCAwAAAT/w9MAI5HjiVeDQAAAABJRU5ErkJggg==", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from matplotlib import pyplot as plt\n", "\n", "Z1 = np.abs(((X - 10) ** 2 + (Y - 10) ** 2) / 1 ** 2)\n", "Z2 = np.abs(((X + 10) ** 2 + (Y + 10) ** 2) / 2.5 ** 2)\n", "Z = (Z1 - Z2)\n", "\n", "plt.imshow(Z)\n", "plt.title(\"Temperature\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "注意,`Z` 不包含关于其位置的数据。它只是数组,存储在 `x` 和 `y` 中的信息与它完全没有关联。这些位置数据通常存储在文件头文件中。为了在地图上“定位”数组,将使用仿射变换。\n", "\n", "## 为数组分配空间数据\n", "\n", "好的,我们有一个数据数组和每个单元格的一些坐标,但是我们如何从中创建一个空间数据集呢?为了做到这一点,我们需要三个组成部分:\n", "\n", "- 数据数组,通常是 xy 坐标\n", "- 一种坐标参考系统,它定义了我们所使用的坐标空间(例如度或米,本初子午线的位置等)\n", "- 定义左上角坐标和单元格分辨率(resolution)的变换\n", "\n", "一旦有了这些组件,就可以在几行代码中用 python 编写出工作的空间栅格数据集。只需要以 `rasterio` 能够理解的格式提供上面列出的信息。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from rasterio.transform import Affine\n", "import rasterio as rio\n", "\n", "res = (x[-1] - x[0]) / 240.0\n", "transform = Affine.translation(x[0] - res / 2, y[0] - res / 2) * Affine.scale(res, res)\n", "\n", "# open in 'write' mode, unpack profile info to dst\n", "with rio.open(\n", " \"./new_raster.tif\",\n", " \"w\",\n", " driver=\"GTiff\", # output file type\n", " height=Z.shape[0], # shape of array\n", " width=Z.shape[1],\n", " count=1, # number of bands\n", " dtype=Z.dtype, # output datatype\n", " crs=\"+proj=latlong\", # CRS\n", " transform=transform, # location and resolution of upper left cell\n", ") as dst:\n", " # check for number of bands\n", " if dst.count == 1:\n", " # write single band\n", " dst.write(Z, 1)\n", " else:\n", " # write each band individually\n", " for band in range(len(Z)):\n", " # write data, band # (starting from 1)\n", " dst.write(Z[band], band + 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "光栅数据通常是“多波段”(multiband)的(如红、绿、蓝),所以这里提供了多波段和单波段数据都适用的代码。\n", "\n", "如果存储的是多波段数据,则维度应该存储为 `(band, y, x )`。\n", "```" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.4 ('tvmx': conda)", "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.10.4" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "e579259ee6098e2b9319de590d145b4b096774fe457bdf04260e3ba5c171e887" } } }, "nbformat": 4, "nbformat_minor": 2 }