使用R将SpatialPoints与SpatialLines匹配/合并 [英] Matching / Joining SpatialPoints with SpatialLines using R
问题描述
问题
我有一组shapefile 开罗的不同公交路线(每条路线有2趟).我想检查它们是否使用R:经过某些车站:
I have a set of shapefiles for different Bus Routes (Each route has 2 Trips) for Cairo. I wanted to check if they pass by certain stations using R:
Stops_Data <- readOGR("Stackoverflow Data/","Stops_Data")
Trips_Data <- readOGR("Stackoverflow Data/","Trips_Data")
plot(Trips_Data)
points(Stops_Data, col = "red")
我尝试使用over()来匹配它们.首先,我确保投影相同.
I try to match them using over(). First, I make sure the Projections are identical.
proj4string(Trips_Data) <- proj4string(Stops_Data)
over(Stops_Data, Trips_Data)
这只会给出NA.
所需的输出
目标是获得一个表格,该表格将每个行程与其经过的站点相匹配,并使用以下形式创建表格:
The target is to get a table that matches each trip with the Stops it passes by, and create a table using the following form:
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
可能的解决方案?
我不认为将点与线匹配甚至是不可能的,因为坐标几乎总是会稍微打勾.线没有多边形所具有的回旋余地.
I don't believe matching Points with Lines is even possible, as the coordinates will almost always be a slight tick off. Lines don't have leeway space the way Polygons do.
因此,我在想是否有办法将SpatialLinesDataFrame转换为多边形?考虑在每个宽度为100m的公交路线周围制作一个多边形,从而自然地包含所有Stops.
Hence, I'm thinking is there a way to convert SpatialLinesDataFrame into Polygons? Think making a polygon around each bus route that is 100m in width, and thus naturally encompasses all Stops.
推荐答案
如果"100m"值是near的定义,则可以使用rgeos::gDistance()
,但需要使用米与度的单位:
If the "100m" value is the definition of near, then we can use rgeos::gDistance()
but we need to re-project the spatial objects to a projection with meters vs degrees for units:
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以内:
Now, we'll see which points are within 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)
现在,我们只需要按路线查看点,所以我们进行一些数据整理,然后添加序列号(这可能是错误的,因为我不知道点是否按顺序排列,所以您可以滚动为此付出点时间):
Now, we just need to look at the points by route, so we do some data wrangling and then add in the sequence # (which may be wrong since I don't know if the points are in order so you can roll up your sleeves for this bit):
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
这篇关于使用R将SpatialPoints与SpatialLines匹配/合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!