{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 点密度度量——计数和核密度\n", "\n", "汇总运算对于聚合数据非常有用,无论是分析总体趋势还是可视化数据集。与简单地查看或显示地图上的点、线和多边形相比,汇总允许有效地分析和交流数据。本章将探讨两个强调数据集中的汇总运算:矩形或六边形网格中的计数点,或多边形和核密度的计数点。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd\n", "# import geoplot as gplt\n", "import math\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "import rasterio\n", "from rasterio.transform import Affine\n", "from scipy import stats\n", "from shapely.geometry import Polygon, box\n", "from sklearn.datasets import fetch_species_distributions\n", "from sklearn.neighbors import KernelDensity" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 计数点在矩形或六边形网格或多边形\n", "\n", "为了概述网格,创建新的多边形层,由网格和覆盖在另一个特征。可以在网格的每个单元格中总结该特性的一个方面。多边形层通常由渔网(矩形单元格)组成,但使用六边形作为网格正变得越来越普遍。\n", "\n", "让我们定义一个函数,它将创建一个由指定边长的矩形或六边形组成的网格。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def create_grid(feature, shape, side_length, proj):\n", " '''创建由矩形或六边形组成的网格,其指定的边长(side_length)覆盖了输入特征的范围。'''\n", "\n", " # 通过创建缓冲区,稍微扩展特征范围的最小值和最大值\n", " # 降低特征直接落在 cell 边界(两个 cells 之间)的可能性\n", " # 缓冲区是投影相关的(归于单位)\n", " feature = feature.buffer(20)\n", "\n", " # 获取缓冲输入特征的范围\n", " min_x, min_y, max_x, max_y = feature.total_bounds\n", "\n", " # 创建空列表来保存将组成网格的单个单元格\n", " cells_list = []\n", "\n", " # 如果指定,创建方格网格\n", " if shape in [\"square\", \"rectangle\", \"box\"]:\n", "\n", " # 改编自 https://james-brennan.github.io/posts/fast_gridding_geopandas/\n", " # 创建并遍历将定义具有指定边长的列位置的 x 值列表\n", " for x in np.arange(min_x - side_length, max_x + side_length, side_length):\n", " # 创建并遍历将定义具有指定边长的行位置的 y 值列表\n", " for y in np.arange(min_y - side_length, max_y + side_length, side_length):\n", " # 创建具有指定边长的方框并添加到列表中\n", " cells_list.append(box(x, y, x + side_length, y + side_length))\n", "\n", "\n", " # 否则,创建六边形网格\n", " elif shape == \"hexagon\":\n", " # 设置水平位移,它将定义具有指定边长(基于 normal hexagon)的列位置。\n", " x_step = 1.5 * side_length\n", "\n", " # 设置垂直位移,将定义具有指定边长的行位置(基于 normal hexagon)\n", " # 这是两个堆叠在一起的六边形的中心之间的距离(垂直)\n", " y_step = math.sqrt(3) * side_length\n", "\n", " # 求中点(边的中心点到中点的距离,基于 normal hexagon)\n", " apothem = (math.sqrt(3) * side_length / 2)\n", "\n", " # 设置列号\n", " column_number = 0\n", "\n", " # 创建并遍历将定义具有垂直位移的列位置的 x 值列表\n", " for x in np.arange(min_x, max_x + x_step, x_step):\n", " # 创建并遍历将定义具有水平位移的列位置的 y 值列表\n", " for y in np.arange(min_y, max_y + y_step, y_step):\n", " # 创建具有指定边长的六边形\n", " hexagon = [[x + math.cos(math.radians(angle)) * side_length, y + math.sin(math.radians(angle)) * side_length] for angle in range(0, 360, 60)]\n", " # 在列表中添加六边形\n", " cells_list.append(Polygon(hexagon))\n", "\n", " # 检查列数是否为偶数\n", " if column_number % 2 == 0:\n", " # 如果是偶数,则将 y 的最小值和最大值展开,\n", " # 以垂直取代下一行展开值,以避免遗漏任何靠近特征范围的特征\n", " min_y -= apothem\n", " max_y += apothem\n", "\n", " # E否则,为奇数\n", " else:\n", " # 将 y 的最小值和最大值恢复到原始值\n", " min_y += apothem\n", " max_y -= apothem\n", "\n", " # 将列数增加1\n", " column_number += 1\n", "\n", " # 否则,触发异常\n", " else:\n", " raise Exception(\"Specify a rectangle or hexagon as the grid shape.\")\n", "\n", " # 从单元格列表创建网格\n", " grid = gpd.GeoDataFrame(cells_list, columns = ['geometry'], crs = proj)\n", "\n", " # 创建一个列,为每个网格分配一个编号\n", " grid[\"Grid_ID\"] = np.arange(len(grid))\n", " return grid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "将通过计算网格中每个单元格中的井点数量来说明这种方法。可以通过两种不同的方式来实现这种方法,它们都有优点和缺点。\n", "\n", "首先,将为这两个示例设置一些全局参数。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# 设置网格中单元格的边长\n", "# 这取决于选择的投影,因为长度是在投影中指定的单位\n", "side_length = 5000\n", "\n", "# 设置网格形状\n", "shape = \"hexagon\"\n", "# shape = \"rectangle\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 分组\n", "\n", "此方法涉及使用空间连接将每个点分配到它所在的单元格。每个 cell 内的所有点随后被分组并计数。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }