NewGraphEnvironment / fish_passage_peace_2022_reporting

https://newgraphenvironment.github.io/fish_passage_peace_2022_reporting/
Creative Commons Zero v1.0 Universal
0 stars 2 forks source link

extract photo metadata lat longs using gps track points #8

Closed NewGraphEnvironment closed 1 year ago

NewGraphEnvironment commented 1 year ago

Should be able to use sf to read in parsnip_2022_field_mw.GPX and use just the camera owner track_points (not mine) to do the match to camera photo metadata by nearest time. Really we need to just join two data frames 1. photo_metadata 2. track_points by the closest time (using indexes generated by iterating through the difference in times between each photo CreateDate and all the track_points.

Once the match is done we rename or mutate some columns so photo metadata can align with iphone columns.

Pretty sure the heavy lifting for what needs to happen is captured in portion of this script here. Obviously, the names of photo metadata columns below are likely different. You can see the gpx layers through sf::st_layers:


##add lon, lat to our track points
track_points2 <- dplyr::bind_cols(
  drake::readd(track_points),
  drake::readd(track_points) %>%
    st_coordinates() %>%
    as_tibble() %>%
    setNames(c("lon_gps_linked","lat_gps_linked"))
) %>%
  tibble::rownames_to_column() %>% 
  mutate(time = lubridate::as_datetime(time, tz="America/Vancouver")) ##put them in the same tz

#find the track point (named track_points2) that is closest in time to the CreateDate stamp of the photo..
#https://stackoverflow.com/questions/21607247/searching-for-nearest-date-in-data-frame
get_closest_line_in_history <- function(x, history){
  time_diffs <- difftime(x, history)
  time_diffs[time_diffs<0] <- NA

  res <- which.min(time_diffs)
  if (length(res) != 1){
    return(NA)  ##return a NA as in the post
  }else{
    return(res)
  }
}

###########try as a function, bind and add back
make_photo_metadata <- function(meta){
  indx_closest_point <- sapply(meta$createdate, 
                               get_closest_line_in_history, 
                               track_points2$time) %>% 
    as.character() %>% 
    as_tibble_col(column_name = "gps_idx")
  meta_w_index <- bind_cols(meta,
                            indx_closest_point)
  meta_joined_to_tracks <- left_join(meta_w_index,
            select(track_points2, rowname, time, ele, lon_gps_linked, lat_gps_linked), ##pulled out track name
            by = c("gps_idx" = "rowname"))
Mateo9569 commented 1 year ago

This is what I got so far. I've imported the gpx files successfully but I can't run the function that adds lon and lat to the track points. I get an error message saying "Error in drake::readd(track_points_mw) : cannot find drake cache."

# read in Mateo gpx file
gpx <- 'C:/Users/matwi/OneDrive/Projects/repo/fish_passage_peace_2022_reporting/data/gps/parsnip_2022_field_mw.GPX'

track_points_mw = read_sf(gpx, layer = "track_points")

# read in Al gpx file
gpx <- 'C:/Users/matwi/OneDrive/Projects/repo/fish_passage_peace_2022_reporting/data/gps/parsnip_2022_field_al.GPX'

track_points_ai = read_sf(gpx, layer = "track_points")

##add lon, lat to our track points
track_points2 <- dplyr::bind_cols(
  drake::readd(track_points_mw),
  drake::readd(track_points_mw) %>%
    st_coordinates() %>%
    as_tibble() %>%
    setNames(c("lon_gps_linked","lat_gps_linked"))
) %>%
  tibble::rownames_to_column() %>%
  mutate(time = lubridate::as_datetime(time, tz="America/Vancouver"))
NewGraphEnvironment commented 1 year ago

had written something about drake earlier but never ended up sending it. dang. We don't use drake anymore. that function is just calling that object. you can use the object you read in with sf::read_sf instead. You don't need to read in my track points because my photos are georeferenced. After joining iphone metadata to your metadata with the coordinates added it will be a case_when on a condition that there is no populated value for lat long already that will tell the script to sub in the new coordinates to the iphone columns for your photos only

Mateo9569 commented 1 year ago

Closed via commit here