0

I am having a problem when over() doesn't give a value but keep giving meNA for every field in the outcome data.frame. The aim is to have the counts of tardigrade occurrences in each biogeoregion. Here is my data set and its compositions.

#extention=europe area
> tardigrade <- <- gbif("Eutardigrada",ext=e, sp=T, removeZeros=T, download=T)

> tardigrade_sp <- as(tardigrade, "SpatialPoints")

tardigrade_sp

class       : SpatialPoints 
features    : 2033 
extent      : -8.250667, 32.14889, 34.66667, 66.28333  (xmin, xmax, ymin, ymax)
crs         : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs 

biogeo # can be find at https://www.eea.europa.eu/data-and-maps/data/biogeographical-regions-europe-3

class       : SpatialPolygonsDataFrame 
features    : 12 
extent      : 943611, 8500000, 6e+05, 7800000  (xmin, xmax, ymin, ymax)
crs         : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs 
variables   : 4
names       : short_name, pre_2012,    code,                           name 
min values  :     alpine,      ALP,  Alpine, Alpine Bio-geographical Region 
max values  :    steppic,      STE, Steppic,                 outside Europe 


> over (biogeo, tardigrade_sp)
1  2  3  4  5  6  7  8  9 10 11 12 
NA NA NA NA NA NA NA NA NA NA NA NA

Can anyone tell me what did I do wrong and what should I do?

6
  • Just by watching the extents it appears they're not the same CRS, you assigned that projection to tardigrade sp instead of trnaforming from latlong
    – Elio Diaz
    Dec 30, 2021 at 18:07
  • 1
    Its definitley something wrong with coordinate systems - we don't know what your e is so its hard to exactly replicate your tardigrade data. But it does look like the numbers are lat-long but the coordinate system string definitely isnt. Please edit with the full details and code you used...
    – Spacedman
    Dec 30, 2021 at 18:28
  • I believe that somewhere, in code that we are not privy to, the tardigrade_sp object is having a Lambert azimuthal equal-area (laea) projection assigned to it where it is likely in a lat/long wgs84. If this is the projection comming in on import, it is incorrect. You need to use the correct projection definition and then transform it to a laea projection using spTransform. Dec 30, 2021 at 19:29
  • Where does the gbif function come from? You don't say. I just tried installing rgbif and that didn't have it. Maybe its spocc? Nope. OK I give up. We shouldn't have to waste our time trying to guess - please specify all the packages you are using!
    – Spacedman
    Dec 30, 2021 at 22:56
  • Your first line isn't valid R code. You've written tardigrade <- <- gbif( with two <- in it.
    – Spacedman
    Dec 31, 2021 at 16:04

1 Answer 1

2

The most important part you missed was setting the projection on the first tardigrade object and then converting either tardigrade or biogeo object to the CRS of the other object using spTransform. It is relevant for answers to include the libraries that were used.

It is also recommendable to use library sf for the sake of speed and logic when using spatial data. After the over function, I included a sf version of the code.

library(dismo)
library(sp)
library(rgdal)

# extent from original post
e = extent(-8.250667, 32.14889, 34.66667, 66.28333)
tardigrade = gbif("Eutardigrada", ext = e, sp = T, removeZeros = T, download = T)
# you missed this important step; object tardigrade was not having CRS:
proj4string(tardigrade) <- CRS("+init=epsg:4326")

# download and unzip at print(getwd())
download.file("https://www.eea.europa.eu/data-and-maps/data/biogeographical-regions-europe-3/zipped-shapefile-format-vector-polygon/zipped-shapefile-format-vector-polygon/at_download/file",
              "biogeo.zip")
unzip("biogeo.zip")

biogeo = readOGR("BiogeoRegions2016.shp")
# transform to epsg:4326 (long-lat)
biogeo = spTransform(biogeo, CRS("+init=epsg:4326"))

over(tardigrade, biogeo)
# PK_UID  short_name pre_2012        code                                name
# 1        4    atlantic      ATL    Atlantic    Atlantic Bio-geographical Region
# 2        6      boreal      BOR      Boreal      Boreal Bio-geographical Region
# 3        6      boreal      BOR      Boreal      Boreal Bio-geographical Region
# 4        6      boreal      BOR      Boreal      Boreal Bio-geographical Region
# 5        7 continental      CON Continental Continental Bio-geographical Region

# SF code ---------------
e = extent(-8.250667, 32.14889, 34.66667, 66.28333)
tardigrade = gbif("Eutardigrada", ext = e, sp = T, removeZeros = T, download = T)
tardigrade = st_as_sf(tardigrade) %>%
  st_set_crs(4326) 

# the file is already downloaded
biogeo = read_sf("BiogeoRegions2016.shp")
# transform to epsg:4326 (long-lat)
biogeo = st_transform(biogeo, 4326)


st_join(tardigrade, biogeo) %>%
  dplyr::filter(!is.na(PK_UID)) %>%
  dplyr::select(-1)
# PK_UID  short_name pre_2012        code                                name
# 1       4    atlantic      ATL    Atlantic    Atlantic Bio-geographical Region
# 2       6      boreal      BOR      Boreal      Boreal Bio-geographical Region
# 3       6      boreal      BOR      Boreal      Boreal Bio-geographical Region
# 4       6      boreal      BOR      Boreal      Boreal Bio-geographical Region
# 5       7 continental      CON Continental Continental Bio-geographical Region

enter image description here

2
  • 1
    May I ask how long did it take you to plot the biogeo polygons? (it takes me awfully long even with sf) Thank you so much.
    – Uyen Bui
    Jan 3, 2022 at 8:09
  • Sure, as sp it took 33 seconds; as sf 9 seconds; if you pass the following code after reading biogeo layer biogeo = st_simplify(biogeo, dTolerance = 1000 ) plotting takes 0.285 seconds (on a ryzen 7@3 GHz, with 16 GB ram)
    – Elio Diaz
    Jan 3, 2022 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.