Spatial Data Analysis(三):点模式分析

Spatial Data Analysis(三):点模式分析

---- 1853年伦敦霍乱爆发

在此示例中,我将演示如何使用 John Snow 博士的经典霍乱地图在 Python 中执行 KDE 分析和距离函数。

感谢 Robin Wilson 将所有数据数字化并将其转换为友好的 GIS 格式。 原始数据从这里获得:
http://blog.rtwilson.com/john-snows-known-cholera-analysis-data-in-modern-gis-formats/

具体来说,我们需要这些包来进行分析:

  • rasterio:这是一个用于在 python 中处理栅格数据的包。 我们不使用栅格,但在这里使用它的原因是显示 GeoTiff 图像作为底图,该图像经过校正以与 GIS shapefile 对齐。
  • seaborn:这实际上是一个可视化包; 但是,它们确实具有可用于二维空间数据的 KDE 功能。
  • pointpats:这是一个用于进行 PPA 和最近邻分析的软件包。 我们需要用它来计算例如 G距离函数。

步骤1

导入所有需要的包

import geopandas as gpd
import rasterio
from rasterio.plot import show
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from pointpats import distance_statistics as stats
from pointpats import PointPattern, PoissonPointProcess

第2步

读入两个 shapefile:

  • SnowGIS/Cholera_Deaths.shp 是一个包含所有死亡和位置的点形状文件
  • SnowGIS/Pumps.shp 是包含所有泵的点形状文件

由于它们都是 shapefile,一旦我们使用“gpd.read_file()”将它们读入 python,我们就得到了 GeoDataFrame。

cases = gpd.read_file("https://raw.githubusercontent.com/Ziqi-Li/GEO4162C/main/data/SnowGIS/Cholera_Deaths.shp")pump = gpd.read_file("https://raw.githubusercontent.com/Ziqi-Li/GEO4162C/main/data/SnowGIS/Pumps.shp")

在我们进行下一步操作之前,最好先检查数据。 例如,您需要执行以下操作:

  • 1.查看数据表,了解GeoDataFrame中的属性
  • 2.检查几何图形,看看是否可以绘制它们
  • 3.检查您读入的shapefile是否具有相同的crs
cases.head() #This returns you the first 5 rows in the GeoDataFrame
IdCountgeometry
003POINT (529308.741 181031.352)
102POINT (529312.164 181025.172)
201POINT (529314.382 181020.294)
301POINT (529317.380 181014.259)
404POINT (529320.675 181007.872)
<script>const buttonEl =document.querySelector('#df-149da3d4-d8f5-4e15-82d9-0a5e55507ee2 button.colab-df-convert');buttonEl.style.display =google.colab.kernel.accessAllowed ? 'block' : 'none';async function convertToInteractive(key) {const element = document.querySelector('#df-149da3d4-d8f5-4e15-82d9-0a5e55507ee2');const dataTable =await google.colab.kernel.invokeFunction('convertToInteractive',[key], {});if (!dataTable) return;const docLinkHtml = 'Like what you see? Visit the ' +'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'+ ' to learn more about interactive tables.';element.innerHTML = '';dataTable['output_type'] = 'display_data';await google.colab.output.renderOutput(dataTable, element);const docLink = document.createElement('div');docLink.innerHTML = docLinkHtml;element.appendChild(docLink);}
</script>

<svg xmlns=“http://www.w3.org/2000/svg” height="24px"viewBox=“0 0 24 24”
width=“24px”>




cases.crs #This returns you the crs
<Projected CRS: EPSG:27700>
Name: OSGB36 / British National Grid
Axis Info [cartesian]:
- E[east]: Easting (metre)
- N[north]: Northing (metre)
Area of Use:
- name: United Kingdom (UK) - offshore to boundary of UKCS within 49°45'N to 61°N and 9°W to 2°E; onshore Great Britain (England, Wales and Scotland). Isle of Man onshore.
- bounds: (-9.01, 49.75, 2.01, 61.01)
Coordinate Operation:
- name: British National Grid
- method: Transverse Mercator
Datum: Ordnance Survey of Great Britain 1936
- Ellipsoid: Airy 1830
- Prime Meridian: Greenwich
cases.plot() #This returns you a map of all the cases
<Axes: >

在这里插入图片描述

pump.crs
<Projected CRS: EPSG:27700>
Name: OSGB36 / British National Grid
Axis Info [cartesian]:
- E[east]: Easting (metre)
- N[north]: Northing (metre)
Area of Use:
- name: United Kingdom (UK) - offshore to boundary of UKCS within 49°45'N to 61°N and 9°W to 2°E; onshore Great Britain (England, Wales and Scotland). Isle of Man onshore.
- bounds: (-9.01, 49.75, 2.01, 61.01)
Coordinate Operation:
- name: British National Grid
- method: Transverse Mercator
Datum: Ordnance Survey of Great Britain 1936
- Ellipsoid: Airy 1830
- Prime Meridian: Greenwich
pump.plot()
<Axes: >

在这里插入图片描述

步骤 3

制作一个简单的地图,覆盖带有泵的案例

ax = cases.plot() #First we need to create an axispump.plot(ax=ax,color="orange") #Then plot the pumps using thes previously created axis as a parameter in the `plot()`
<Axes: >

在这里插入图片描述

ax = cases.plot(markersize = 20)
#Check out here for differernt shapes of the markers:
#https://matplotlib.org/api/markers_api.htmlpump.plot(ax=ax,markersize=500,marker="*",color="red")
<Axes: >

在这里插入图片描述

步骤4

使用 rasterio 读取 TIF 底图“SnowGIS/SnowMap.tif”

basemap = rasterio.open('https://raw.githubusercontent.com/Ziqi-Li/GEO4162C/main/data/SnowGIS/SnowMap.tiff')show(basemap)

在这里插入图片描述

<Axes: >

步骤 5

将地图叠加在一起

#First we create a figure with a size of (10,10)
f, ax = plt.subplots(figsize=(10,10))#Plot each layer, with the same axis `ax`
show(basemap,ax=ax)cases.plot(ax=ax, markersize = 30, alpha=0.8)
pump.plot(ax=ax,markersize=1000, marker="*",color="red")
<Axes: >

在这里插入图片描述

步骤 6

执行核密度估计(KDE)

函数 sns.kdeplot() 有几个参数:

    1. GeoDataFrame cases
    1. x 和 y 坐标,我们可以从 GeoDataFrame 中提取为 cases.geometry.xcases.geometry.y
    1. bw_method 是 KDE 的带宽。
  • 4.fill参数表示你想要计数图还是填充色图
  • 5.cmap参数表示您要使用的颜色
    1. alpha参数表示透明度的级别。

基本上 4、5、6 是您可以更改的样式参数。

执行 KDE 的代码是:

sns.kdeplot(data=cases,x=cases.geometry.x,y=cases.geometry.y,bw_method=0.5,fill=True,cmap="coolwarm",alpha=0.8)
<Axes: >

在这里插入图片描述

让我们将 kde 覆盖在我们的地图上。 请注意,您需要指定要在与其余地图相同的轴上绘制的 kdeplot。 所以我们需要在sns.kdeplot(ax=ax,...)中添加ax=ax

f, ax = plt.subplots(figsize=(10,10))
show(basemap,ax=ax)
cases.plot(ax=ax, markersize = 50)
pump.plot(ax=ax,markersize=1000,marker="*",color="red")#4th layer
sns.kdeplot(ax=ax, data=cases,x=cases.geometry.x,y=cases.geometry.y,bw_method=0.5,fill=True,cmap="coolwarm",alpha=0.6)
<Axes: >

在这里插入图片描述

我们可以看到热点中心与中央泵对齐得很好!

sns.kdeplot() 中的另一个重要参数是有一个 weights 参数,您可以使用它根据每个位置的案例数量来权衡您的位置。

  • 首先让我们回顾一下 GeoDataFrame 的情况,我们发现有一列名为“Count”。
  • 然后,让我们使用这个“Count”作为 KDE 中的权重。 为此,请将“weights=cases.Count”添加到现有的 kde 函数中
cases.head()
IdCountgeometry
003POINT (529308.741 181031.352)
102POINT (529312.164 181025.172)
201POINT (529314.382 181020.294)
301POINT (529317.380 181014.259)
404POINT (529320.675 181007.872)
<script>const buttonEl =document.querySelector('#df-fc93c370-9178-4d6b-a535-1aca6b1bd379 button.colab-df-convert');buttonEl.style.display =google.colab.kernel.accessAllowed ? 'block' : 'none';async function convertToInteractive(key) {const element = document.querySelector('#df-fc93c370-9178-4d6b-a535-1aca6b1bd379');const dataTable =await google.colab.kernel.invokeFunction('convertToInteractive',[key], {});if (!dataTable) return;const docLinkHtml = 'Like what you see? Visit the ' +'<a target="_blank" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'+ ' to learn more about interactive tables.';element.innerHTML = '';dataTable['output_type'] = 'display_data';await google.colab.output.renderOutput(dataTable, element);const docLink = document.createElement('div');docLink.innerHTML = docLinkHtml;element.appendChild(docLink);}
</script>

<svg xmlns=“http://www.w3.org/2000/svg” height="24px"viewBox=“0 0 24 24”
width=“24px”>




f, ax = plt.subplots(figsize=(10,10))
show(basemap,ax=ax,adjust=None)
cases.plot(ax=ax, markersize = 50,column="Count")
pump.plot(ax=ax,markersize=1000,marker="*",color="red")sns.kdeplot(ax=ax,data=cases, x=cases.geometry.x, y=cases.geometry.y, bw_method=0.5, fill=True,cmap="coolwarm",alpha=0.8,weights=cases.Count)plt.tight_layout()plt.savefig('choloera.png',dpi=600)

在这里插入图片描述

你能找到什么? 基本上 KDE 最热点正好指向中央泵! 如果 John Snow 博士能够进行 KDE,他的生活会变得更轻松吗?

添加底图

在这里,我添加了一小节关于使用“contextily”包将底图添加到绘图中的内容,这在大多数情况下非常有用,当我们没有另一个底图作为参考/提供必要的上下文时。

contextily 在 Google Colab 中不可用,所以我们需要在这里安装它。

pip install -q contextily
import contextily as cx
f, ax = plt.subplots(figsize=(10,10))cases.plot(ax=ax, markersize = 50, figsize=(9,9))
pump.plot(ax=ax,markersize=1000,marker="*",color="red")
sns.kdeplot(ax=ax,data=cases, x=cases.geometry.x, y=cases.geometry.y, bw_method=0.5, fill=True,cmap="coolwarm",alpha=0.8,weights=cases.Count)#This is the new line of code:
#We need to put a crs into the function.
#The source defines the style of the base map: https://contextily.readthedocs.io/en/latest/providers_deepdive.html
#There are many options available.
cx.add_basemap(ax, source=cx.providers.Stamen.TonerLite, crs=cases.crs) # we need to put a crs into the function.

在这里插入图片描述

步骤 7

最近邻分析

下一节我们将使用距离函数和显着性检验进行最近邻分析。 这可以在很棒的“pointpats”包中轻松完成。

首先,让我们从“cases”GeoDataFrame 中提取 x 和 y 坐标,并将它们作为二维数组。

x = cases.geometry.x.values
y = cases.geometry.y.valuespoints = np.array(list(zip(x,y)))

其次,让我们使用点创建一个“PointPattern()”类

pp = PointPattern(points)
/usr/local/lib/python3.10/dist-packages/libpysal/cg/shapes.py:1492: FutureWarning: Objects based on the `Geometry` class will deprecated and removed in a future version of libpysal.warnings.warn(dep_msg, FutureWarning)
/usr/local/lib/python3.10/dist-packages/libpysal/cg/shapes.py:1208: FutureWarning: Objects based on the `Geometry` class will deprecated and removed in a future version of libpysal.warnings.warn(dep_msg, FutureWarning)
pp
<pointpats.pointpattern.PointPattern at 0x7d59d1a5f010>

“knn”函数将返回每个点的最近邻居以及到该邻居的距离。

接下来,让我们生成 100 个 CSR(每个都是泊松过程)作为我们的空基准。 请记住,这将是我们进行显着性检验的置信区间。

CSRs = PoissonPointProcess(pp.window, pp.n, 100, asPP=True) # simulate CSR 100 times
/usr/local/lib/python3.10/dist-packages/libpysal/cg/shapes.py:1923: FutureWarning: Objects based on the `Geometry` class will deprecated and removed in a future version of libpysal.warnings.warn(dep_msg, FutureWarning)
/usr/local/lib/python3.10/dist-packages/libpysal/cg/shapes.py:103: FutureWarning: Objects based on the `Geometry` class will deprecated and removed in a future version of libpysal.warnings.warn(dep_msg, FutureWarning)

运行 G 距离 stats.Genv() 函数并将其可视化。

它显示 G 曲线高于置信包络线(红色曲线),表明我们在霍乱图中观察到聚集模式。

genv = stats.Genv(pp, realizations=CSRs)genv.plot()

在这里插入图片描述

您还可以轻松执行 K 或 F 距离函数。 同样,K 曲线高于置信包络线(红色曲线),表明我们在霍乱地图中观察到聚集模式。

kenv = stats.Kenv(pp, realizations=CSRs) # call Fenv for F function
kenv.plot()

在这里插入图片描述

F 曲线低于置信包络线(红色曲线),表明我们在霍乱地图中观察到聚集模式。

import warnings
warnings.filterwarnings('ignore')
fenv = stats.Fenv(pp, realizations=CSRs) # call Fenv for F function
fenv.plot()

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/200521.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数字串最大乘积切分(动态规划)

不得不说&#xff0c;动态规划是真的骚 题解已经在图片里面了 代码如下&#xff1a; #include<stdio.h> long long gethnum(long long n);int main(void) {//定义变量并输入int N, M;long long dp[19][7] {0}, num[20][20] {0};scanf("%d%d", &N, &am…

Ext JS 之拖拽Grid(Drag and Drop Grid)

Ext JS 如何实现Grid的拖拽 Ext JS 提供了一个Grid的插件类 Ext.grid.plugin.DragDrop 实现Grid 的拖放功能, 该类的别名是 plugin.gridviewdragdrop。 该插件会将特定的Ext.dd.DragZone和Ext.dd.DropZone实例附加到了Grid的View 上,DropZone是放置的区块,接收来自具有相同…

Linux(统信UOS) 发布.Net Core,并开启Https,绑定证书

实际开发中&#xff0c;有时会需要为小程序或者需要使用https的应用提供API接口服务&#xff0c;这就需要为.Net Core 配置https&#xff0c;配置起来很简单&#xff0c;只需要在配置文件appsettings.json中添加下面的内容即可 "Kestrel": {"Endpoints": …

anaconda3的激活和Cvcode配置C++:报错:CondaIOError: Missing write permissions in:

报错&#xff1a;CondaIOError: Missing write permissions in: 原因&#xff1a;anaconda所在文件夹只有root 才有权限 查看用户名 whoamisudo chown -R 用户名 /home/anaconda3激活anaconda3 #激活 source activate #退出 source deactivate 配置Cvcode配置C 首先看g的…

代码随想Day28 | 93.复原IP地址、78.子集、90.子集II

93.复原IP地址 这道题首先要解决的一个问题是&#xff0c;判断某个字符串是合格的ip地址&#xff1a; 主要考虑到如下三点&#xff1a; 以0为开头的数字不合法里有非正整数字符不合法如果大于255了不合法 接下来&#xff0c;递归三部走&#xff1a; 参数&#xff1a;除了控制…

leetcode 1004. 最大连续1的个数 III(优质解法)

代码&#xff1a; class Solution {public int longestOnes(int[] nums, int k) {int lengthnums.length;int zero0; //计数器&#xff0c;计数翻转 0 的个数int max0; //记录当前获得的最长子数组长度for(int left0,right0;right<length;right){if(nums[right]0){zero;wh…

IBM Qiskit量子机器学习速成(三)

Torch连接器和混合量子神经网络 本章内容介绍如何将QNN作为一般的Torch神经层嵌入Torch框架的经典神经网络。首先我们会学习如何嵌入EstimatorQNN&#xff0c;最后我们会使用这一技术实现手写数字的识别。 一般步骤 首先创建特征映射电路(feature map)和假设(ansatz)分别作为…

tomcat结构目录有哪些

Tomcat服务器结构目录包括以下内容&#xff1a; conf目录&#xff1a;存放Tomcat的配置信息&#xff0c;包括server.xml、context.xml、web.xml等核心配置文件。bin目录&#xff1a;存放Tomcat启动和关闭脚本&#xff0c;如catalina.sh、setenv.sh&#xff08;可选&#xff09…

深信服行为管理AC设置用户定时注销

PS&#xff1a;设置用户无流量注销及每天定时注销 AC版本&#xff1a;AC13.0.62.001 Build20221107 官方通告&#xff1a; 截止标准版本AC12.0.80和AC13.0.80&#xff0c;暂不支持指定周期时间内注销一次所有用户&#xff0c;仅支持每天的固定时间注销所有用户&#xff0c;每…

基于web的ssm网络在线考试系统源码和论文

摘要 随着Internet的发展&#xff0c;人们的日常生活已经离不开网络。未来人们的生活与工作将变得越来越数字化&#xff0c;网络化和电子化。网上管理&#xff0c;它将是直接管理网络在线考试系统的最新形式。本论文是以构建网络在线考试系统为目标&#xff0c;使用 java技术制…

软件测试方法之等价类测试

01 等价类划分法 1、应用场合 有数据输入的地方&#xff0c;可以使用等价类划分法。 从大量数据中挑选少量代表数据进行测试。 2、测试思想 穷举测试&#xff1a;把所有可能的数据全部测试一遍叫穷举测试。穷举测试是最全面的测试&#xff0c;但是在实际工作中不能采用&am…

vue3安装markdown-it

记录一个坑 vue ts使用markdown-it需要使用以下命令安装才行 npm install --save markdown-it vue/compiler-sfc来源https://juejin.cn/s/vue3%20markdown-it

Android View.inflate 和 LayoutInflater.from(this).inflate的区别

前言 两个都是布局加载器&#xff0c;而View.inflate是对 LayoutInflater.from(context).inflate的封装&#xff0c;功能相同&#xff0c;案例使用了dataBinding。 View.inflate(context, layoutResId, root) LayoutInflater.from(context).inflate(layoutResId, root, fals…

C++包管理利器CPM

C包管理利器CPM 一、介绍 CPM.cmake is a cross-platform CMake script that adds dependency management capabilities to CMake. It’s built as a thin wrapper around CMake’s FetchContent module that adds version control, caching, a simple API and more. CPM.cma…

CENTOS 7 添加黑名单禁止IP访问服务器

一、通过 firewall 添加单个黑名单 只需要把ip添加到 /etc/hosts.deny 文件即可&#xff0c;格式 sshd:$IP:deny vim /etc/hosts.deny# 禁止访问sshd:*.*.*.*:deny# 允许的访问sshd:.*.*.*:allowsshd:.*.*.*:allow 二、多次失败登录即封掉IP&#xff0c;防止暴力破解的脚本…

IBM 刚刚发布了首个 1000 量子比特的量子芯片

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

Python继承技法揭示,代码更具扩展性

更多Python学习内容&#xff1a;ipengtao.com 大家好&#xff0c;我是彭涛&#xff0c;今天为大家分享 Python继承技法揭示&#xff0c;代码更具扩展性&#xff0c;全文4000字&#xff0c;阅读大约11分钟。 继承是面向对象编程中的核心概念之一&#xff0c;它允许创建一个新的类…

spring 框架的 AOP

AOP依赖导入 <!-- AOP依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency>

Matlab 镜像变换(2D)

文章目录 一、简介二、实现代码三、实现效果参考资料一、简介 镜像变换是一个非常有趣的过程,它有着一个通用的套路(以2D为例):一个点围绕一个给定对称轴的镜像可以通过平移对称轴上一点,然后旋转它,使对称轴与x轴对齐,之后我们将旋转后的点的y坐标置为负,最后再将对称…

如何购买华为云服务器

华为云是华为推出的云计算服务平台&#xff0c;旨在为企业和个人提供全面的云端解决方案。它提供了包括计算、存储、数据库、人工智能、大数据、安全等多种云服务&#xff0c;覆盖了基础设施、平台和软件级别的需求。华为云致力于构建安全可信赖的云计算基础设施&#xff0c;以…