Remove GPS coordinate outside a study area

103 Views Asked by At

I have a dataset of animals that were radio-tracked for 10 years. The columns I have are date, latitude, longitude, easting, northing and animal ID. Over the years, some coordinates were recorded wrong. As a result, some animal locations are recorded outside the study area (e.g., as wrong as fish on land). I want to identify those datapoints in my dataset and remove them without having to go over data rows individually. I came across the scrubr package but I was not very successful in getting it to work.

I have attached a dummy dataset here that deals with locations, but a similar logic applies (I think).

library(dplyr) 
library(tidyverse)
library(sp) 
library(ggmap) 
library(adehabitatLT) 
library(lubridate)
library(mapview)

Upload dummy dataset

starbucks =read.csv("https://raw.githubusercontent.com/libjohn/mapping-with-R/master/data/All_Starbucks_Locations_in_the_US_-_Map.csv")

Filter data

starbucksNC <- starbucks %>% filter(State == "NC")

Create the map

mapview(starbucksNC, xcol = "Longitude", ycol = "Latitude", crs = 4269, grid = FALSE)

#Describe study area boundary using four datapoints
-78.00149, 35.40030| -77.90917, 35.40869| -77.99672, 35.35299| -77.89235, 35.35523|

From here, I am not sure how to remove the GPS coordinates that lie outside of that boundary, from the original dataset.

1

There are 1 best solutions below

0
David Ranzolin On

Here's how you'd use the sf package to define a bounding box and isolate the points within it:

library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(mapview)

starbucks = read.csv("https://raw.githubusercontent.com/libjohn/mapping-with-R/master/data/All_Starbucks_Locations_in_the_US_-_Map.csv")
starbucks_sf <- st_as_sf(starbucks, coords = c("Longitude", "Latitude"), crs = 4326)

bb <- data.frame(
  longitude = c(-78.00149, -77.90917, -77.99672, -77.89235),
  latitude = c(35.40030, 35.40869, 35.35299, 35.35523)
  ) %>% 
  st_as_sf(coords = c("longitude", "latitude"), crs = 4326) %>% 
  st_bbox() %>% 
  st_as_sfc()

st_intersection(starbucks_sf, bb)
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries
#> Simple feature collection with 3 features and 21 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -77.9665 ymin: 35.37511 xmax: -77.93974 ymax: 35.39692
#> Geodetic CRS:  WGS 84
#>          Brand Store.Number                       Name Ownership.Type
#> 2236 Starbucks        14555 Wayne Memorial Dr & Hwy 70  Company Owned
#> 2237 Starbucks         9838                  Goldsboro  Company Owned
#> 2238 Starbucks        76570     Target Goldsboro T-964       Licensed
#>      Facility.ID     Features...Products    Features...Service
#> 2236       10619 Lunch, Oven-warmed Food Starbucks Card Mobile
#> 2237       10350 Lunch, Oven-warmed Food Starbucks Card Mobile
#> 2238       10150                         Starbucks Card Mobile
#>      Features...Stations Food.Region Venue.Type Phone.Number
#> 2236       Drive-Through        9999    Unknown 919-581-0993
#> 2237       Drive-Through        9999    Unknown 919-778-7866
#> 2238                            9999    Unknown 919-778-2150
#>                                                                             Location
#> 2236     2116-A Wayne Memorial Dr\nGoldsboro, NC 27534-1760\n(35.396923, -77.966504)
#> 2237      603 North Berkeley Blvd\nGoldsboro, NC 27534-3443\n(35.375111, -77.943958)
#> 2238 1204 Sunburst Dr\nGoldsboro, NC 27534-8202\n(35.383091731535, -77.939741537765)
#>                Street.Address            Street.Line.1 Street.Line.2      City
#> 2236 2116-A Wayne Memorial Dr 2116-A Wayne Memorial Dr               Goldsboro
#> 2237  603 North Berkeley Blvd  603 North Berkeley Blvd               Goldsboro
#> 2238         1204 Sunburst Dr         1204 Sunburst Dr               Goldsboro
#>      State        Zip Country                         Coordinates
#> 2236    NC 27534-1760      US             (35.396923, -77.966504)
#> 2237    NC 27534-3443      US             (35.375111, -77.943958)
#> 2238    NC 27534-8202      US (35.383091731535, -77.939741537765)
#>                 Insert.Date                   geometry
#> 2236 06/22/2012 06:32:13 PM  POINT (-77.9665 35.39692)
#> 2237 06/22/2012 06:32:13 PM POINT (-77.94396 35.37511)
#> 2238 06/22/2012 06:32:13 PM POINT (-77.93974 35.38309)

Created on 2023-05-16 with reprex v2.0.2