Home > front end >  Filter one data frame by the extent of another data frame
Filter one data frame by the extent of another data frame

Time:10-19

I have two data frames with lat and long columns(x,y) called "wind_ras_data" and "vec". I want to filter the values in the "vec" data frame by the extent of the "wind_ras_data" data frame.

> extent(wind_ras_data)
class      : Extent 
xmin       : -13519474 
xmax       : -13030428 
ymin       : 3809653 
ymax       : 4129625 

> extent(vec)
class      : Extent 
xmin       : -13579579 
xmax       : -12995375 
ymin       : 3740029 
ymax       : 4168294


# Does not work

vec_within <- vec %>% 
  filter(extent(vec) %in% extent(wind_ras_data))

#workflow

wind_ras_data <- as.data.frame(rasterToPoints(final_ras_wind)) %>% 
  pivot_longer(!c(x,y), names_to = "date", values_to = "wind") 

coordinates(wind_ras_data) <- c('x', 'y')

#Determine the projection of the lat-long coordinates, by default it is EPSG:4326
proj4string(wind_ras_data) <- CRS(" init=epsg:4326")

#Convert the coordinates to the used metric system (EPSG:3857)
wind_ras_data<-spTransform(wind_ras_data,CRS(" init=EPSG:3857")) 




vec <- vec_data %>%
  mutate(angle = (270 - (atan2(u, v) * (180/pi))%60)) %>%  
  dplyr::select(x, everything()) %>% 
  dplyr::select(!c(u,v))

coordinates(vec) <- c('x', 'y')

#Determine the projection of the lat-long coordinates, by default it is EPSG:4326
proj4string(vec) <- CRS(" init=epsg:4326")

#Convert the coordinates to the used metric system (EPSG:3857)
vec<-spTransform(vec,CRS(" init=EPSG:3857"))

vec_within <- vec %>% 
  filter(gWithin(vec, wind_ras_data, byId = TRUE))

CodePudding user response:

You can try the gWithin from the rgeos package.

library(rgeos)

vec_within <- vec %>% 
  filter(gWithin(vec, wind_ras_data, byId = TRUE))

This assumes that both vec and wind_ras_data are spatial objects (i.e. spatial polygons).

CodePudding user response:

I ended up converting both to a dataframe then just filtering by the max and min of both extents.

wind_ras_data <- as.data.frame(rasterToPoints(final_ras_wind)) %>% 
  pivot_longer(!c(x,y), names_to = "date", values_to = "wind") 

# coordinates(wind_ras_data) <- c('x', 'y')
# 
# #Determine the projection of the lat-long coordinates, by default it is EPSG:4326
# proj4string(wind_ras_data) <- CRS(" init=epsg:4326")
# 
# #Convert the coordinates to the used metric system (EPSG:3857)
# wind_ras_data<-spTransform(wind_ras_data,CRS(" init=EPSG:3857")) %>% 
#   as.data.frame()




vec <- vec_data %>%
  mutate(angle = (270 - (atan2(u, v) * (180/pi))%60)) %>%  
  dplyr::select(x, everything()) %>% 
  dplyr::select(!c(u,v))

coordinates(vec) <- c('x', 'y')

#Determine the projection of the lat-long coordinates, by default it is EPSG:4326
proj4string(vec) <- CRS(" init=epsg:4326")

#Convert the coordinates to the used metric system (EPSG:3857)
vec<-spTransform(vec,CRS(" init=EPSG:3857")) %>% 
  as.data.frame()

vec_within <- vec %>% 
  filter(x > -13519474,
         x < -13030225,
         y > 3809671,
         y < 4129620)
  • Related