Search code examples
rr-sp

NA outcome when running over() between a SpatialPoints and SpatialPolygonsDataFrame


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? Thank you in advance


Solution

  • library(dismo) # I add to find this one! 
    library(sp)
    library(sf)
    tardigrade <- dismo::gbif("Eutardigrada", sp = T, removeZeros = T, download = T)
    tardigrade_sp <- as(tardigrade, "SpatialPoints")
    tardigrade_sp
    

    I get more point I assume you used a particular extent with ext = e that I could not reproduce.

    here I didn't get a CRS. In your post it looked like 3035 but my return looked more like 4326 so after testing it I stayed with 4326 (I assume this was your problem)

    # sp::proj4string(tardigrade_sp) <- sp::CRS("+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs")
    sp::proj4string(tardigrade_sp) <- sp::CRS("+init=epsg:4326")
    
    # I will go with in sf object
    tardi_sf <- sf::st_as_sf(tardigrade_sp)
    tardi_3035 <- sf::st_transform(tardi_sf, 3035) # it seems to me that you want this CRS
    
    tardi_3035_sp <- as(tardi_3035, "Spatial") # going back to sp
    biogeo <- sf::st_read("BiogeoRegions2016_shapefile/BiogeoRegions2016.shp")
    # CRS 3035 ETRS89_LAEA_Europe
    
    biogeo_3035 <- sf::st_transform(biogeo, 3035) # formating of CRS was weird 
    # so I dide a transform just to be exactly the same 
    
    biogeo_sp <- as(biogeo, "Spatial") 
    
    # a quick plot, I used a subset because this shape is quite heavy
    plot(biogeo$geometry[biogeo$code == "Continental"])
    plot(tardi_3035 , add = TRUE)
    
    # this seems to works now !
    sp::over(tardi_3035_sp, biogeo_sp, fn = NULL)
    

    you shoud read about sf and probably check https://geocompr.robinlovelace.net/