1

I have some GPS relocation data and I want to calculate the proportion of those points that fall within protected areas. I'm downloading the protected areas using the wdpar package which uses sf to deal with spatial data.

This question on counting points in polygons was helpful but I haven't been able to integrate the two packages. Hope you can help.

# load packages
library(wdpar)
library(dplyr)
library(ggmap)
library(sf)

# download protected area data 
# (excluding areas represented as point localities)
sa_raw_pa_data <- wdpa_fetch(c("South Africa"))
sa_raw_pa_data 

# reproject data
sa_pa_data <- st_transform(sa_raw_pa_data, 4326)

# plot it
plot(sa_pa_data)

# create some random data
set.seed(123); d = data.frame(long=runif(100,-180,180), lat=runif(100,-90,90))

# here's where I fall down 

# calculate the overlap with something like
    over(d,sa_pa_data$WDPAID)

# or
lengths(st_intersects(sa_pa_data$WDPAID, d))
  • Try spatialEco::point.in.poly as it supports sp and sf class objects. – Jeffrey Evans Dec 20 '19 at 0:11
1

Here's one way to solve it using st_intersects:

library (sf)

pts <- st_as_sf (d, coords = c ("long", "lat"), crs = 4326)
res <- st_intersects (pts, sa_pa_data)

length (unlist (res)) / nrow (pts) # fraction of intersecting points
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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