GlobalFishingWatch / gfwr

R package for accessing data from Global Fishing Watch APIs
https://globalfishingwatch.github.io/gfwr/
Apache License 2.0
59 stars 7 forks source link

`get_raster` not accepting custom `sf` object #166

Closed jamesgrecian closed 3 months ago

jamesgrecian commented 3 months ago

Hi gfwr team,

I'm new to using the gfwr package after previously using Juan Mayorga's excellent guide to using google big query.

I'm trying to pass a sf polygon to the get_raster function but the object is not being recognised. I can't find guidance on how best to generate this object.

Here's a reprex of what I'm trying:

require(tidyverse)
#> Loading required package: tidyverse
require(gfwr)
#> Loading required package: gfwr
require(sf)
#> Loading required package: sf
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE

CP <- sf::st_bbox(c(xmin = -180, xmax = 180, ymin = -30, ymax = -60), crs = 4326) |> sf::st_as_sfc()

gfw_dat <- get_raster(spatial_resolution = 'LOW',
                      temporal_resolution = 'YEARLY',
                      group_by = 'GEARTYPE',
                      start_date = '2021-01-01',
                      end_date = '2022-01-01',
                      region = CP,
                      region_source = 'USER_JSON',
                      key = gfw_auth())
#> Error in get_raster(spatial_resolution = "LOW", temporal_resolution = "YEARLY", : custom region is not an sf polygon

is(CP, "sfc_POLYGON")
#> [1] TRUE

Created on 2024-07-19 with reprex v2.1.1

VLucet commented 3 months ago

This line expect the custom region to be a sf object, not just an sfc, and for that object to use the name geometry for its geometry column, which is a convention but often a weak point of checking for sf objects

https://github.com/GlobalFishingWatch/gfwr/blob/0a6e9d0f6152781ce1edaa6d1080777090dcf3a3/R/get_raster.R#L94

This should fix it I think

CP <- sf::st_bbox(c(xmin = -180, xmax = 180, ymin = -30, ymax = -60), crs = 4326) |> 
    sf::st_as_sfc() |> 
    sf::st_as_sf() |> 
    dplyr::rename(geometry = x)

Although I am running in an API call error, but that is a different story, as I am not authentified with GFW. Adding it just for the record.

Error in `httr2::req_perform()`:
! HTTP 401 Unauthorized.
Run `rlang::last_trace()` to see where the error occurred.
jamesgrecian commented 3 months ago

This works with no errors on my machine, sessionInfo (abridged) below

> sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.5

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/London
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] sf_1.0-16       gfwr_2.0.0      lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4     purrr_1.0.2     readr_2.1.5     tidyr_1.3.1     tibble_3.2.1   
[11] ggplot2_3.5.1   tidyverse_2.0.0
VLucet commented 3 months ago

Great. The only comment I'd have for the devs is to not expect the region to have a geometry named column, or to error and let the user know explicitly when this column isn't present. Otherwise this can be closed.

AndreaSanchezTapia commented 3 months ago

Hi @jamesgrecian, Thank you so much for reaching out. And thanks @VLucet for responding. @VLucet you are right, we will remove the check using the geometry sfc name and check using only st_geometry(). Other than that your code chunk works.

We will push the small change to the code so that you don't need to rename the geometry column to "geometry" and let you know.

VLucet commented 3 months ago

Good point to use st_geometry()

AndreaSanchezTapia commented 3 months ago

I did the update, @jamesgrecian so this should work without the renaming line.

CP <- sf::st_bbox(c(xmin = -180, xmax = 180, ymin = -30, ymax = -60), crs = 4326) |> 
    sf::st_as_sfc() |> 
    sf::st_as_sf() 

Please note that I also renamed the option USER_JSON to USER_SHAPEFILE to be more precise, so in your case this should work:


gfw_dat <- get_raster(spatial_resolution = 'LOW',
                      temporal_resolution = 'YEARLY',
                      group_by = 'GEARTYPE',
                      start_date = '2021-01-01',
                      end_date = '2022-01-01',
                      region = CP,
                      region_source = 'USER_SHAPEFILE',
                      key = gfw_auth())
AndreaSanchezTapia commented 3 months ago

I'm closing this issue but feel free to reopen if anything is missing.