Is there a function to isolate rows of data based on the distance between the start and stop of their GPS coordinates?

47 views Asked by At

Budding ecologist here learning how to use R studio in data analysis. I'm research bison movements in the GYE to show where they cross highways - the data was stored on google looker studio after being transferred from old home-baked software. There are two types of sightings - static sightings and ones with a start and stop that show movement.

I need to isolate the rows that have start and stop data from the static ones in R, but the issue is that both static and start-stop rows have beginning and end linestring pathway data ( Ex: "LINESTRING(-111.097913 44.776748, -111.097813 44.776748)" ) - the static rows just have a negligible difference of 0.0001° for the longitude specifically, whereas the rows that show actual movement it's much greater. The latitude is consistent for static rows.

Is there a function that can separate row data based on the disparity between the two linestring longitude data points? I am a novice at R, taking lessons/exercises to build my skills so I'm not sure where to start for a function like this, but it seems possible. Any help is greatly appreciated!

I tried to create vectors with concatenated information based on the "LINESTRING" column, using subset(), but I couldn't find a way to isolate the data based on this kind of rule.

1

There are 1 answers

0
Grzegorz Sapijaszko On

I would use sf::st_distance() between start/stop points or sf::st_length() on linestrings itself, like:

data.frame(
  geom = c("LINESTRING(-111.097913 44.776748, -111.097813 44.776748)", 
           "LINESTRING(-111.097913 44.776748, -111.097813 44.776848)", 
           "LINESTRING(-111.097913 44.776748, -111.097813 44.716748)"),
  n = c(1, 2, 3)
  ) |>
  sf::st_as_sf(wkt = "geom", crs = "EPSG:4326") |>
  dplyr::mutate(length = sf::st_length(geom))
#> Simple feature collection with 3 features and 2 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -111.0979 ymin: 44.71675 xmax: -111.0978 ymax: 44.77685
#> Geodetic CRS:  WGS 84
#>   n                           geom          length
#> 1 1 LINESTRING (-111.0979 44.77...    7.893258 [m]
#> 2 2 LINESTRING (-111.0979 44.77...   13.636236 [m]
#> 3 3 LINESTRING (-111.0979 44.77... 6671.710745 [m]

and filter out those 'static' like:

[...]
  dplyr::mutate(length = sf::st_length(geom)) |>
  subset(length >= units::as_units(15, "m"))

#> Simple feature collection with 1 feature and 2 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -111.0979 ymin: 44.71675 xmax: -111.0978 ymax: 44.77675
#> Geodetic CRS:  WGS 84
#>   n                           geom       length
#> 3 3 LINESTRING (-111.0979 44.77... 6671.711 [m]

Created on 2024-03-04 with reprex v2.1.0