Given a tibble s2map that contains street names and the geographical coordinates of points located on these streets,
head(s2map)
# A tibble: 6 × 13
# Groups: Street [1]
Street WayID Idx NodeId highway is_in…¹ railway addr.…² is_in publi…³ lat lon dista…⁴
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 Aleea Alexandru Tziga… 8421… 1 97961… reside… 0 0 0 0 0 44.4 26.1 NA
2 Aleea Alexandru Tziga… 8421… 2 44714… reside… 0 0 0 0 0 44.4 26.1 34.7
3 Aleea Alexandru Tziga… 8421… 3 44714… reside… 0 0 0 0 0 44.4 26.1 4.45
4 Aleea Alexandru Tziga… 8421… 4 44714… reside… 0 0 0 0 0 44.4 26.1 4.50
5 Aleea Alexandru Tziga… 8421… 5 97961… reside… 0 0 0 0 0 44.4 26.1 9.39
6 Aleea Alexandru Tziga… 8421… 6 97961… reside… 0 0 0 0 0 44.4 26.1 11.6
together with a data.table GPS_1 that contains a list of geographical coordinates
head(GPS_1)
Date Time Lat Lon Speed(km/h)
1: 2020-08-01 00:00:05 44.47336 26.14126 49
2: 2020-08-01 00:00:31 44.47613 26.13992 31
3: 2020-08-01 00:00:36 44.47630 26.13971 14
4: 2020-08-01 00:00:38 44.47629 26.13962 13
5: 2020-08-01 00:00:45 44.47623 26.13942 8
6: 2020-08-01 00:00:49 44.47628 26.13934 8
I want to determine on which street is located each point in GPS_1.
I have tried several things, given the examples found here. However, applying st_intersects returned no intersection. I thought the reason was the fact that I had two lists of points - so I tried creating sf_linestrings for each of the streets based on min and max coordinates. Here is the code I tried:
# create summarized obj for s2map
aux = s2map %>%
group_by(Street) %>%
summarise(lat_min = min(lat), lat_max=max(lat),
lon_min = min(lon), lon_max = max(lon),
total_distance = sum(distance[!is.na(distance)]))
Create list of simple feature geometries (linestrings)
begin.coord = data.frame(lon = aux$lon_min, lat = aux$lat_min)
end.coord = data.frame(lon= aux$lon_max, lat = aux$lat_max)
Create list of simple feature geometries (linestrings)
l_sf <- vector("list", nrow(begin.coord))
for (i in seq_along(l_sf)){
l_sf[[i]] <- st_linestring(as.matrix(rbind(begin.coord[i, ], end.coord[i,])))
}
Create simple feature geometry list column
l_sfc <- st_sfc(l_sf, crs = "+proj=longlat +datum=WGS84")
Convert to sp object if needed
lines_sp <- as(l_sfc, "Spatial")
- create a sf object from the sfc list of linestrings
lines_sf = st_sf(id = aux$Street, geometry = l_sfc)
pnts <- st_as_sf(GPS_1, coords=c("Lon","Lat"), crs=4326)
pnts_sf <- st_as_sf(pnts, coords=c("Lon","Lat"), crs=4326)
pnts_sf %>% mutate(
intersection = as.integer(st_intersects(geometry, lines_sf))
, area = if_else(is.na(intersection), '', lines_sf$id[intersection])
)
and the result:
Simple feature collection with 1236 features and 5 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 26.11976 ymin: 44.43174 xmax: 26.18438 ymax: 44.47812
Geodetic CRS: WGS 84
First 10 features:
Date Time Speed(km/h) geometry intersection area
1 2020-08-01 00:00:05 49 POINT (26.14126 44.47336) NA
2 2020-08-01 00:00:31 31 POINT (26.13992 44.47613) NA
3 2020-08-01 00:00:36 14 POINT (26.13971 44.4763) NA
4 2020-08-01 00:00:38 13 POINT (26.13962 44.47629) NA
5 2020-08-01 00:00:45 8 POINT (26.13942 44.47623) NA
6 2020-08-01 00:00:49 8 POINT (26.13934 44.47628) NA
7 2020-08-01 00:01:06 0 POINT (26.1393 44.47633) NA
8 2020-08-01 00:01:08 0 POINT (26.1393 44.47633) NA
9 2020-08-01 00:04:39 0 POINT (26.1393 44.47633) NA
10 2020-08-01 00:04:53 0 POINT (26.1393 44.47633) NA

sf::st_nearest_feature()to overcome this https://r-spatial.github.io/sf/reference/st_nearest_feature.html – Jindra Lacko Oct 13 '22 at 17:21s2mapandGPS_1viadput()to your original question so one has actual data to toy around with? – dimfalk Oct 14 '22 at 11:09