Skip to content

Commit

Permalink
Merge pull request #169 from GlobalFishingWatch/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
AndreaSanchezTapia authored Jul 22, 2024
2 parents 0a6e9d0 + d9e9630 commit a8f4041
Show file tree
Hide file tree
Showing 25 changed files with 373 additions and 334 deletions.
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if anything is new or missing.

## Endpoints

- Same endpoints as in v1.1.0
- Same endpoints as in `v1.1.0`
+ `get_raster()` communicates with the 4Wings API to get fishing effort
+ `get_vessel_info()` communicates with the Vessels API
+ `get_event()` communicates with the Events API
Expand Down Expand Up @@ -50,6 +50,9 @@ script and copy the string of the request when [filling an issue](https://github
+ The `region` argument now accepts `sf` polygons rather than a GeoJSON string
+ Parameters `start_date` and `end_date` replace `date_range` for consistency with other functions

### Additional changes

- Option `USER_JSON` has been renamed to `USER_SHAPEFILE` for clarity (addresses #161)


# gfwr 1.1.0
Expand Down
2 changes: 1 addition & 1 deletion R/get_endpoint.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#'
#' Function to get API dataset name for given event type
#'
#' @param dataset_type Type of dataset to get API dataset name for. It can be
#' @param dataset_type Type of dataset to get API dataset name for. It can be "raster",
#' "ENCOUNTER", "LOITERING", "FISHING", "PORT_VISIT", "GAP", "EEZ", "RFMO" or "MPA"
#' @param ... Other arguments that would depend on the dataset type.
#' @importFrom httr2 request
Expand Down
27 changes: 20 additions & 7 deletions R/get_event.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' @param confidences Confidence levels (1-4) of events (port visits only)
#' @param region sf shape to filter raster or GFW region code (such as an
#' EEZ code). See details about formatting the geojson
#' @param region_source source of the region ('EEZ','MPA', 'RFMO' or 'USER_JSON')
#' @param region_source source of the region ('EEZ','MPA', 'RFMO' or 'USER_SHAPEFILE')
#' @param gap_intentional_disabling Logical. Whether the Gap events are intentional,
#' according to Global Fishing Watch algorithms
#' @param key Authorization token. Can be obtained with gfw_auth() function
Expand Down Expand Up @@ -125,6 +125,18 @@
#' region_source = 'EEZ',
#' flags = 'CHN',
#' key = gfw_auth())
#'
#' # fishing events in user shapefile
#' test_polygon <- sf::st_bbox(c(xmin = -70, xmax = -40, ymin = -10, ymax = 5),
#' crs = 4326) |>
#' sf::st_as_sfc() |>
#' sf::st_as_sf()
#'get_event(event_type = 'FISHING',
#' start_date = "2020-10-01",
#' end_date = "2020-12-31",
#' region = test_polygon,
#' region_source = 'USER_SHAPEFILE',
#' key = gfw_auth())
#' }
#' @export

Expand Down Expand Up @@ -233,8 +245,9 @@ get_event <- function(event_type,
} else if (region_source == 'RFMO' & is.character(region)) {
region = rjson::toJSON(list(region = list(dataset = 'public-rfmo',
id = region)))
} else if (region_source == 'USER_JSON') {
if (methods::is(region, 'sf') & base::class(region$geometry)[1] %in% c("sfc_POLYGON","sfc_MULTIPOLYGON")) {
} else if (region_source == 'USER_SHAPEFILE') {
if (methods::is(region, 'sf') & any(base::class(sf::st_geometry(region)) %in% c("sfc_POLYGON","sfc_MULTIPOLYGON"))
) {
region <- sf_to_geojson(region, endpoint = 'event')
} else {
stop('custom region is not an sf polygon')
Expand All @@ -250,7 +263,7 @@ get_event <- function(event_type,
list(startDate = jsonlite::unbox(start)),
list(endDate = jsonlite::unbox(end))
))
} else if (region_source == 'USER_JSON') {
} else if (region_source == 'USER_SHAPEFILE') {

body_args <- jsonlite::toJSON(c(body_args,
list(startDate = jsonlite::unbox(start)), # removes from array
Expand Down Expand Up @@ -341,7 +354,7 @@ get_event <- function(event_type,
#' @param region_source Optional but mandatory if using the argument region.
#' Source of the region. If 'EEZ','MPA', 'RFMO',
#' then the value for the argument region must be the code for that region.
#' If 'USER_JSON', then region has to be an sf object
#' If 'USER_SHAPEFILE', then region has to be an sf object
#' @param region GFW region code (such as an EEZ, MPA or RFMO code) or a
#' formatted geojson shape. See Details about formatting the geojson.
#' @param duration duration, in minutes, of the event, ex. 30
Expand Down Expand Up @@ -492,7 +505,7 @@ get_event_stats <- function(event_type,
} else if (region_source == 'RFMO' & is.character(region)) {
region = rjson::toJSON(list(region = list(dataset = 'public-rfmo',
id = region)))
} else if (region_source == 'USER_JSON') {
} else if (region_source == 'USER_SHAPEFILE') {
if (methods::is(region, 'sf') & base::class(region$geometry)[1] %in% c("sfc_POLYGON","sfc_MULTIPOLYGON")) {
region <- sf_to_geojson(region, endpoint = 'event')
} else {
Expand All @@ -510,7 +523,7 @@ get_event_stats <- function(event_type,
list(endDate = jsonlite::unbox(end)),
list(timeseriesInterval = jsonlite::unbox(interval))
))
} else if (region_source == 'USER_JSON') {
} else if (region_source == 'USER_SHAPEFILE') {

body_args <- jsonlite::toJSON(c(body_args,
list(startDate = jsonlite::unbox(start)), # removes from array
Expand Down
10 changes: 5 additions & 5 deletions R/get_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @param end_date End of date range to search events, in YYYY-MM-DD format and excluding this date
#' @param region sf shape to filter raster or GFW region code (such as a
#' Marine Regions Geographic Identifier or EEZ code).
#' @param region_source source of the region ('EEZ','MPA', 'RFMO' or 'USER_JSON')
#' @param region_source source of the region ('EEZ','MPA', 'RFMO' or 'USER_SHAPEFILE')
#' @param key Authorization token. Can be obtained with `gfw_auth()` function
#' @param print_request Boolean. Whether to print the request, for debugging
#' purposes. When contacting the GFW team it will be useful to send this string
Expand Down Expand Up @@ -50,7 +50,7 @@
#' start_date = '2021-01-01',
#' end_date = '2021-10-01',
#' region = test_shape,
#' region_source = 'USER_JSON',
#' region_source = 'USER_SHAPEFILE',
#' key = gfw_auth(),
#' print_request = TRUE)
#' }
Expand Down Expand Up @@ -90,9 +90,9 @@ if (is.null(region_source)) stop("region_source and region params are required")
if (length(region) > 1) stop("only 1 RFMO region must be provided")
region <- rjson::toJSON(list(region = list(dataset = 'public-rfmo',
id = region)))
} else if (region_source == 'USER_JSON') {
if (methods::is(region, 'sf') & base::class(region$geometry)[1] %in% c("sfc_POLYGON","sfc_MULTIPOLYGON")
) {
} else if (region_source == 'USER_SHAPEFILE') {
if (methods::is(region, 'sf') & any(base::class(sf::st_geometry(region)) %in% c("sfc_POLYGON","sfc_MULTIPOLYGON"))
) {
region <- sf_to_geojson(region, endpoint = 'raster')
} else {
stop('custom region is not an sf polygon')
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ get_regions <- function(region_source = 'EEZ',
result <- get_endpoint(dataset_type = region_source) %>%
httr2::req_headers(Authorization = paste("Bearer", key, sep = " ")) %>%
httr2::req_user_agent(gfw_user_agent()) %>%
httr2::req_error(body = parse_response_error) %>%
# httr2::req_error(body = parse_response_error) %>%
httr2::req_perform(.) %>%
httr2::resp_body_json(.) %>%
dplyr::bind_rows()
Expand Down
6 changes: 3 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,12 @@ and converts the response to a data frame. In order to use it, you should specif
* The variable to group by: `FLAG`, `GEARTYPE`, `FLAGANDGEARTYPE`, `MMSI` or `VESSEL_ID`
* The date range `note: this must be 366 days or less`
* The region polygon in `sf` format or the region code (such as an EEZ code) to filter the raster
* The source for the specified region. Currently, `EEZ`, `MPA`, `RFMO` or `USER_JSON` (for `sf` shapefiles).
* The source for the specified region. Currently, `EEZ`, `MPA`, `RFMO` or `USER_SHAPEFILE` (for `sf` shapefiles).


### Examples

You can load a sample shapefile inside `gfwr` to see how `'USER_JSON'` works:
You can load a sample shapefile inside `gfwr` to see how `'USER_SHAPEFILE'` works:

```{r example_map_1}
data("test_shape")
Expand All @@ -307,7 +307,7 @@ get_raster(
start_date = '2021-01-01',
end_date = '2021-02-01',
region = test_shape,
region_source = 'USER_JSON',
region_source = 'USER_SHAPEFILE',
key = key
)
```
Expand Down
Loading

0 comments on commit a8f4041

Please sign in to comment.