使用 R 将 SpatialPoints 与 SpatialLines 匹配/连接

2023-12-27

Question

我有一个一组 shapefile https://www.dropbox.com/sh/ujoq4nb6tguv9kf/AAAzL-1-Mph-A4duvnKK33aDa?dl=0前往开罗的不同巴士路线(每条路线有 2 趟车次)。我想用 R 检查他们是否经过某些车站:

Stops_Data <- readOGR("Stackoverflow Data/","Stops_Data")
Trips_Data <- readOGR("Stackoverflow Data/","Trips_Data")

plot(Trips_Data)
points(Stops_Data,  col = "red")

我尝试使用 over() 来匹配它们。首先,我确保投影是相同的。

proj4string(Trips_Data) <- proj4string(Stops_Data)
over(Stops_Data, Trips_Data)

这仅给出 NA。


所需输出

目标是获取一个将每个行程与其经过的停靠点相匹配的表,并使用以下形式创建一个表:

      trip_id      stop_id stop_name stop_seque
45 CTA_1021_O PaM_1031_EMB    Embaba          1
46 CTA_1021_O PaM_1039_KKT   Kit Kat          2
47 CTA_1021_O PaM_1009_AGZ    Agouza          3
48 CTA_1021_O PaM_2004_ISA     Isaaf          4
49 CTA_1021_O PaM_1059_RAM    Ramses          5
50 CTA_1021_O PaM_1035_GMR    Ghamra          6

可能的解决方案?

我什至不相信将点与线匹配是可能的,因为坐标几乎总是会有轻微的偏差。线条不像多边形那样有余地。

因此,我在想有没有办法将 SpatialLinesDataFrame 转换为多边形?考虑围绕每条公交路线制作一个宽度为 100m 的多边形,从而自然地包含所有站点。


如果“100m”值是近的定义,那么我们可以使用rgeos::gDistance()但我们需要将空间对象重新投影到以米与度为单位的投影:

library(rgdal)
library(rgeos)
library(sp)
library(dplyr)
library(tidyr)

route_stops <- readOGR("Stops_Data.shp", "Stops_Data", stringsAsFactors=FALSE)
c <- readOGR("Trips_Data.shp", "Trips_Data", stringsAsFactors=FALSE)

# need meters vs degrees
prj <- "+proj=aea +lat_1=29.9 +lat_2=30.2 +lon_0=30.9"

route_stops <- SpatialPointsDataFrame(spTransform(route_stops, CRS(prj)), route_stops@data)
trips <- SpatialLinesDataFrame(spTransform(trips, CRS(prj)), trips@data)

# get a bird's eye view
plot(trips)
plot(route_stops, add=TRUE)

现在,我们将查看哪些点在 100m 之内:

near <- suppressWarnings(gDistance(route_stops, trips, byid=TRUE))
near <- apply(near, 1, `<=`, 100)

# make it easier to work with later on and include other data elements
colnames(near) <- trips$route_id
near <- data.frame(near)
near$stop_id <- route_stops$stop_id
near$stop_name <- route_stops$stop_name

# see what we found
plot(trips)
plot(route_stops[apply(near[,1:4], 1, any),], add=TRUE)

现在,我们只需要按路线查看点,因此我们进行一些数据整理,然后添加序列 # (这可能是错误的,因为我不知道点是否按顺序排列,因此您可以卷起袖子对于这一点):

gather(near, route, stop, -stop_id, -stop_name) %>% 
  group_by(route) %>% 
  filter(stop) %>% 
  mutate(stop_seq=1:n()) %>% 
  select(trip_id=route, stop_id, stop_name, stop_seq, -stop) %>% 
  ungroup() -> desired_output

print(desired_output, n=44)

## Source: local data frame [44 x 4]
## 
##       trip_id      stop_id                    stop_name stop_seq
##         <chr>        <chr>                        <chr>    <int>
## 1    CTA_1021 PaM_1059_RAM                       Ramses        1
## 2    CTA_1021 PaM_1035_GMR                       Ghamra        2
## 3    CTA_1021 PaM_1064_ZAM                      Zamalek        3
## 4    CTA_1021 PaM_1054_MUN               Mustafa Nahhas        4
## 5    CTA_1021 PaM_1064_SSS               Salah Salem St        5
## 6    CTA_1021 PaM_1057_RAB             Rabaa El-Adaweya        6
## 7    CTA_1021 PaM_1030_TAY                    Eltayaran        7
## 8    CTA_1021 PaM_1004_EIT                 8th District        8
## 9    CTA_1021 PaM_1048_MNH                       Manhal        9
## 10   CTA_1021 PaM_1031_EMB                       Embaba       10
## 11   CTA_1021 PaM_1039_KKT                      Kit Kat       11
## 12   CTA_1021 PaM_1073_TAB                        Tabba       12
## 13   CTA_1021 PaM_1043_MAA                       Ma3rad       13
## 14   CTA_1021 PaM_1072_TAS                 Ta'men Sehhy       14
## 15   CTA_1021 PaM_2004_ISA                        Isaaf       15
## 16   CTA_1020 PaM_1042_LEB               Lebanon Square        1
## 17   CTA_1020 PaM_1059_RAM                       Ramses        2
## 18   CTA_1020 PaM_1007_ART Abdel el Monem Riad / Tahrir        3
## 19   CTA_1020 PaM_1070_SFX                Sphinx Square        4
## 20   CTA_1020 PaM_1009_AGZ                       Agouza        5
## 21   CTA_1020 PaM_2004_ISA                        Isaaf        6
## 22   CTA_1020 PaM_2005_AHM                  Ahmed Helmy        7
## 23 CTA_1021.1 PaM_1059_RAM                       Ramses        1
## 24 CTA_1021.1 PaM_1035_GMR                       Ghamra        2
## 25 CTA_1021.1 PaM_1064_ZAM                      Zamalek        3
## 26 CTA_1021.1 PaM_1054_MUN               Mustafa Nahhas        4
## 27 CTA_1021.1 PaM_1064_SSS               Salah Salem St        5
## 28 CTA_1021.1 PaM_1057_RAB             Rabaa El-Adaweya        6
## 29 CTA_1021.1 PaM_1030_TAY                    Eltayaran        7
## 30 CTA_1021.1 PaM_1004_EIT                 8th District        8
## 31 CTA_1021.1 PaM_1048_MNH                       Manhal        9
## 32 CTA_1021.1 PaM_1031_EMB                       Embaba       10
## 33 CTA_1021.1 PaM_1039_KKT                      Kit Kat       11
## 34 CTA_1021.1 PaM_1073_TAB                        Tabba       12
## 35 CTA_1021.1 PaM_1043_MAA                       Ma3rad       13
## 36 CTA_1021.1 PaM_1072_TAS                 Ta'men Sehhy       14
## 37 CTA_1021.1 PaM_2004_ISA                        Isaaf       15
## 38 CTA_1020.1 PaM_1042_LEB               Lebanon Square        1
## 39 CTA_1020.1 PaM_1059_RAM                       Ramses        2
## 40 CTA_1020.1 PaM_1007_ART Abdel el Monem Riad / Tahrir        3
## 41 CTA_1020.1 PaM_1070_SFX                Sphinx Square        4
## 42 CTA_1020.1 PaM_1009_AGZ                       Agouza        5
## 43 CTA_1020.1 PaM_2004_ISA                        Isaaf        6
## 44 CTA_1020.1 PaM_2005_AHM                  Ahmed Helmy        7
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 R 将 SpatialPoints 与 SpatialLines 匹配/连接 的相关文章

  • grep() 搜索数据框的列名

    有没有更清晰 更简单 更直接 更短的方法来做到这一点 其中 df1 是数据框 names df1 grep Yield names df1 我想返回任何包含单词 yield 的列名称 Thanks grep has a value应该适用于
  • 可以明确声明包依赖项的版本吗?

    我倾向于对我编写的代码进行明确而不是隐含的描述 因此 在成功创建自己的包之后 我立即想到的下一件事是如何最好地确保代码的健壮性和可靠性 其中一部分与我的包所依赖的包有关 实际问题 在这方面 是否可以明确声明需要 期望哪个版本的包依赖项 我正
  • 将密度曲线拟合到 R 中的直方图

    R中有没有可以将曲线拟合到直方图的函数 假设您有以下直方图 hist c rep 65 times 5 rep 25 times 5 rep 35 times 10 rep 45 times 4 看上去很正常 但其实是歪曲的 我想拟合一条倾
  • 如何调整ggplot直方图的时间刻度轴

    我正在使用一个数据框 其中一列包含POSIXct日期时间值 我正在尝试使用绘制这些时间戳的直方图ggplot2但我有两个问题 我不知道如何设置 binwidthgeom histogram 我想将每个垃圾箱设置为一天或一周 我尝试提供 di
  • 将summary()写入as.data.frame以在ggplot / R中使用

    请查找 af 数据样本t below 我正在使用以下方法进行竞争风险分析etmCIF来自etm package 产生以下结果 这很好 但需要更好的图形 曾经有一个ggtrans etm函数将数据导入ggplot 然而 这个功能显然被删除了
  • R 中的 NA 替换函数

    我正在尝试替换矩阵中的 NA mat 零 我在用着mat is na mat lt 0 当我有 18946 个变量的 94531 个观察值或更小的矩阵时 效果很好 但我在 22752 个变量的 112039 个观察值的矩阵上尝试它 R 显示
  • 在 mac (iMac OSX ) 终端中远程运行脚本(r 脚本)到其他计算机

    我有一个小示例脚本 script p r 如下所示 打算在终端中运行 usr bin Rscript sink output capture txt mn lt mean 1 10 and so on much longer list of
  • 使用底格里斯河从纬度/经度获取人口普查区

    我有相对较多的坐标 我想获取其人口普查区 除了 FIPS 代码 我知道我可以使用以下命令查找各个纬度 经度对call geolocator latlon 已完成here https stackoverflow com questions 5
  • 使用 dplyr:group_by 将数据帧分成多个子集?

    有没有办法根据 group by 组使用 dplyr 将一个数据帧拆分为数据帧的子集 mtcars gt group by cyl gear gt codes 非常感谢 好吧 并不是你真的想要 但你可以这样做tidyr 即nearly一样的
  • 错误:“rjags”的包或命名空间加载失败

    在终端的 conda 环境之一中 我能够成功安装包 rjags 但是 当我在该环境中运行 R 并运行库 rjags 时 出现以下错误 加载所需的包 coda 错误 rjags 的包或命名空间加载失败 rjags 的 loadNamespac
  • 从 R 环境中删除对象

    我正在阅读 Hadley 的 Advanced R 在第 8 章中 他说我们可以使用以下方法从环境中删除对象 rm 但是 移除该物体后我仍然可以看到该物体 这是我的代码 e lt new env e a lt 1 e b lt 2 e a
  • 如何更改 Shiny 中 navbarPage 折叠的断点

    我想用shiny navbarPage collapsible TRUE 当在小屏幕上查看我的 Shiny 应用程序时 将导航元素折叠到菜单中 默认情况下 当浏览器宽度小于 940 像素时会触发折叠 有什么方法可以改变这一点 以便在稍大的浏
  • fread 将空导入为 NA

    我正在尝试导入带有空白的 csv 读取为 不幸的是他们都读作 NA now 为了更好地演示问题 我还展示了如何NA NA and 都映射到同一事物 除了最底部的示例 这将妨碍简单的解决方法dt is na dt lt gt write cs
  • b'从 ANSI 1252 重新编码为 UTF-8 失败,并出现错误:“参数无效”。'巨熊猫 蟒蛇

    我正在尝试将 shapefile 读入 GeoDataFrame 通常我只是这样做并且它有效 import pandas as pd import geopandas as gpd from shapely geometry import
  • glmnet 未从 cv.glmnet 收敛 lambda.min

    我跑了20倍cv glmnet套索模型以获得 lambda 的 最佳 值 但是 当我尝试重现结果时glmnet 我收到一个错误 内容如下 Warning messages 1 from glmnet Fortran code error c
  • 使用 R 进行项目组织 [重复]

    这个问题在这里已经有答案了 可能的重复 统计分析和报告撰写的工作流程 https stackoverflow com questions 1429907 workflow for statistical analysis and repor
  • R 中的字符串作为函数参数

    数据框chocolates列出了糖果的类型以及每种糖果的一组评级 ID sweetness filling crash snickers 0 67 0 55 0 40 milky way 0 81 0 53 0 56 我正在编写一个函数 它
  • 表单提交时出现 rvest 错误

    我想从以下网页中抓取数据 https swgoh gg u zozo collection 180 emperor palpatine https swgoh gg u zozo collection 180 emperor palpati
  • R - 通过覆盖和递归合并列表

    假设我有两个带有名字的列表 a list a 1 b 2 c list d 1 e 2 d list a 1 b 2 b list a 2 c list e 1 f 2 d 3 e 2 我想递归地合并这些列表 如果第二个参数包含冲突的值 则
  • 如何将 ggrough 图表另存为 .png

    说我正在使用R包裹ggrough https xvrdm github io ggrough https xvrdm github io ggrough 我有这个代码 取自该网页 library ggplot2 library ggroug

随机推荐