I have a spatial lines data frame which can be projected on to a map. The projection is defined as WGS84. I have a Spatial Points Data Frame which is also in the same location.

How can I isolate the streets/lines in the line data frame which contain, or are close to, a point in the points dataframe.

These are the points

These are the lines

I am not able to see the geometry of the lines described in the Spatial Lines Dataframe.

  • You can buffer the points into polygons according to the distance you need and then check which lines intersect with those polygons. The buffering can be done with rgeos:gBuffer and the intersection with rgeos:gintersection.
    – Jul
    Commented Oct 3, 2017 at 1:15

1 Answer 1


Get package sf, cast your sp objects to sf objects using y <- st_as_sf(x), and do something like


# some demo points
pts <- list(st_point(c(1,1)), st_point(c(2,1)), st_point(c(3.5,3.1)), 
            st_point(c(4.5,5)), st_point(c(3.6,3.1))) %>%
  st_sfc() %>% st_sf()

# and some demo nearby lines    
lns <- list(st_linestring(matrix(c(5,3, 3,3, 3,2), byrow = T, ncol = 2)),
            st_linestring(matrix(c(1.1, 1.1, 2.9, 2), byrow = T, ncol = 2)),
            st_linestring(matrix(c(4,5,5,5), byrow = T, ncol = 2))) %>% 
  st_sfc() %>%

# have a look
plot(pts, add = TRUE, col = 'grey50')

# return a sparse matrix of proximities
prox <- st_is_within_distance(pts, lns, dist = 0.11)

# or a dense one (rows = lns, cols = pts)
prox_d <- st_distance(lns, pts) <= 0.11

# subset the original lines with the sparse matrix
lns_w_nearby_pts <- lns[unique(unlist(prox)), ]

# or the dense one
lns_w_nearby_pts_d <- lns[apply(prox_d, MARGIN = 1, FUN = function(x) any(x == TRUE)), ]

identical(lns_w_nearby_pts, lns_w_nearby_pts_d) # == TRUE \o/

# check the results with e.g.
plot(lns_w_nearby_pts_d, add = TRUE, col = 'red')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.