-2

I'm interested in extracting count data from a list of linear features based on their spatial overlap with buffered point data. I have several thousand points, each of which I buffer with 100m radius. I then use st_intersects to identify the linear features that intersect each buffer. Next, I would like to use the feature ids to extract count data from a separate dataframe for each of these features. I want to sum them together so that I have the total counts for all features that intersect each buffered point.

St_intersects gives me a list, with a single row for every buffered point, and that list contains the index (or id) of the linear features that intersect the buffer. I'm not sure how to use this list to reference a separate dataframe and sum all count data associated with those linear features and store them in a column with the original point data.

points # sf object, point data 
linear_features # spatial line data
counts # dataframe with linear feature ids and count data based on use  

intersect_100 <- points %>% st_buffer(.,100) %>%  
  st_intersects(., linear_features) %>% # this gives index of overlapping linear features  
  lapply(., function(n){linear_features$id[n]}) # this gives list of ids that intersect with each buffer region  

I would now like to write a loop so that the 'points' dataframe has a new column called "total_counts" based on the counts of all linear features within 100m. These count data are stored in a separate dataframe called 'counts'. The 'linear_features' and 'counts' both have the same set of ids...

Thanks so much for any help!

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • 2
    Hi @jjmorgan. Your question is not very accessible in its current form. I recommend reviewing [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). In a nutshell: Less text. More code. Provide reproducible & minimal sample data that we can work with (as text, so we can copy & paste). Include a clear problem statement; and your desired/expected output. – Maurits Evers Jun 16 '22 at 23:40

1 Answers1

0

First, some reproducible example feel free to twink it!

First I simulate some lines and points. I used spastat.geom but sfheaders is also a good option!

library(sf)
library(spatstat.geom)
library(dplyr)

set.seed(2022)
#draw some lines
some_lines <- spatstat.geom::psp(runif(20), runif(20), runif(20), runif(20),  window = spatstat.geom::owin()) 
# convert in sf fromat and not spatstat
# [-1,] here is because spatsat creates also a bbox/windows
lines_sf <- sf::st_as_sf(some_lines)[-1,]
# some count data
lines_sf$count_data <- rbinom(20, 10, prob = 0.3)
# some points
some_points <- spatstat.geom::ppp(runif(20), runif(20), window = spatstat.geom::owin())
point_sf <- sf::st_as_sf(some_points)[-1,]
## add id for later
point_sf$id <- 1:20
# buffer the point, I needed to play a bit with the size 
point_sf_buffer <- sf::st_buffer(point_sf, 0.1)

# quick plot
plot(point_sf_buffer$geom, col = 2)
plot(lines_sf$geom,  add = TRUE)

Now we need to:

  1. do a spatial join between the two geomtries (we create more elements because some buffers match more than one lines)
  2. we sum the value that we get from lines to our buffered points:
# note instead of using a buffer st_is_within_distance can also used
joined <- sf::st_join(point_sf_buffer, lines_sf)
# counting count_data per buffer
joined_count  = joined |> 
    group_by(id) |> 
    summarize(sum_count = sum(count_data))

joined_count

Good resource: https://geocompr.robinlovelace.net/spatial-operations.html#non-overlapping-joins

defuneste
  • 376
  • 2
  • 7