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)
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