Search code examples
rextract

Extract gbif occurrences within a geographic administrative area with rgbif


I would like to extract all occurrences for a Canadian province for a given taxa with rgbif. I am following this tutorial : https://docs.ropensci.org/rgbif/articles/getting_occurrence_data.html

It mentions that we can use the gadm instead of the polygonal boundaries but I can't figure out the code. My real area that I am want should be CAN11_1. I am trying to simplify the code from here and adapt to my taxa. This simplified code works:

occ_download(
  pred("taxonKey", 2436775),
 pred("gadm","ETH"),
  format = "SIMPLE_CSV")

However, this code using phylumKey instead of taxonKey does not work. The error message says that the problem is the pred("gadm", "ETH").

occ_download(
  pred("phylumKey", 35),
 pred("gadm","ETH"),

  format = "SIMPLE_CSV")

This code works but is not the correct polygon.

occ_download(
  pred_in("phylumKey", c(35, 9, 13)), # important to use pred_in
  pred_within("POLYGON((-70 47,-69 47,-69 49,-70 49,-70 47))"),
  format = "SIMPLE_CSV"
)

What am I missing???


Solution

  • You can use pred_and() with two separate pred() calls. Two separate unwrapped pred() calls e.g. not using pred_in() should work too.

    When that error was returned, it seems like for some reason pred_in() was expecting a list object, although it does work with a single value.

    library(rgbif)
    
    # Request data (omit user, pwd, and email if already added to .Renviron)
    occ_download(pred_and(pred("phylumKey", 35), pred("gadm","ETH")),
                 format = "SIMPLE_CSV",
                 user = "your_gbif_username",
                 pwd = "your_gbif_password",
                 email = "your_gbif_email")
    
    # Check download status using occurrence number,
    # see result in console from previous step
    occ_download_wait("download occurrence number")
    
    # Load data
    df <- occ_download_get("download occurrence number") %>%
      occ_download_import()
    
    head(df)
    # # A tibble: 6 × 50
    #     gbifID datasetKey    occurrenceID kingdom phylum class order family genus species
    #    <int64> <chr>         <chr>        <chr>   <chr>  <chr> <chr> <chr>  <chr> <chr>  
    # 1 92657932 85042096-f76… ""           Plantae Bryop… Spha… Spha… Sphag… Spha… Sphagn…
    # 2 92643197 85042096-f76… ""           Plantae Bryop… Bryo… Brya… Mniac… Pohl… Pohlia…
    # 3 92630747 85042096-f76… ""           Plantae Bryop… Bryo… Dicr… Dicra… Para… Parale…
    # 4 92629842 85042096-f76… ""           Plantae Bryop… Bryo… Bart… Bartr… Phil… Philon…
    # 5 92622427 85042096-f76… ""           Plantae Bryop… Bryo… Brya… Mniac… Mnium Mnium …
    # 6 92622418 85042096-f76… ""           Plantae Bryop… Bryo… Brya… Mniac… Plag… Plagio…
    # # ℹ 40 more variables: infraspecificEpithet <chr>, taxonRank <chr>,
    # #   scientificName <chr>, verbatimScientificName <chr>,
    # #   verbatimScientificNameAuthorship <chr>, countryCode <chr>, locality <chr>,
    # #   stateProvince <chr>, occurrenceStatus <chr>, individualCount <int>,
    # #   publishingOrgKey <chr>, decimalLatitude <dbl>, decimalLongitude <dbl>,
    # #   coordinateUncertaintyInMeters <dbl>, coordinatePrecision <dbl>, elevation <dbl>,
    # #   elevationAccuracy <dbl>, depth <lgl>, depthAccuracy <lgl>, eventDate <chr>, …