GlobalFishingWatch / gfwr

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

add documentation for user geojson #107

Closed natemiller closed 8 months ago

natemiller commented 11 months ago

It can be hard to determine how to format a user geojson properly so that it works in the get_raster function. It would be useful add an example to the documentation (README or vignette) that outlines how to convert from an .shp to a .geojson with the correct format for the API.

The below is a rough example of converting a .shp (saved locally) to a .geojson using geojsonsf and then wrapping the result in a "{geojson: ... }" tag for use in with theget_raster` function.

library(sf)
library(geojsonsf)
library(ggplot2)

# load FAO Major Region 34 shapefile from file
fao_34_from_shp <- sf::read_sf('~/FAO_34_NOCOASTLINE.shp') 
# convert to GEOJSON using `geojsonsf` package
fao_34_geojson <- geojsonsf::sf_geojson(fao_34_from_shp)
# wrap geojson in geojson tag `{"geojson": ....}`
fao_34_tagged <- paste0('{"geojson":', fao_34_geojson,'}')

# get fishing effort data for FAO Major Region 34
fao_34_fishing_effort <-gfwr::get_raster(
  spatial_resolution = 'low',
  temporal_resolution = 'yearly',
  group_by = 'flag',
  date_range = '2021-01-01,2021-12-31',
  region = fao_34_tagged,
  region_source = 'user_json',
  key = gfwr::gfw_auth()
  )

# quick visual review
ggplot() +
  geom_tile(data = fao_34_fishing_effort,
            aes(Lon, Lat))