0

Problem Outline:

I am attempting to do some species distribution modeling using the function bioclim(). My map of Sri Lanka (Image 1: see below) was extracted from GADM resources, which I understand is a vector object.

For the bioclim() function to work, you need to use this format:

Usage

bioclim(x, p.....)

Arguments:

x Raster*object or matrix

p two column matrix or SpatialPoints object

I am quite new in regards to producing maps and doing species distribution modeling in R. After spending a lot of hours researching, I have tried my very best figure out how to rasterize this GADM vector object into raster file format to be inputted into the bioclim() function. I have been following these species distribution exercises in order to construct a species distribution model for GPS points which constitute the latitude/longitude points for blue whales that were recorded during field surveys.

Aim

The code below was used to produce a map of Sri Lanka (see image 1) with associated GPS points, and my aim is to produce a species distribution model using the function bioclim() and then display the probability bar onto a separate map (see image 2).

The aim of the model is to illustrate the probability of detecting blue whales within particular locations in Sri Lankan waters. Image 2 is the desired output.

After many attempts, I have finally produced code that works, but I am experiencing problems rasterizing the GADM vector object into raster format using the function system.time() and running the bioclim() model. Please see the 2 error message below.

If anyone can help me solve this problem, I cannot express my gratitude.

I cannot publish the data but I have provided a mini data frame below:

Many thanks in advance.

Error messages:

##Error message when I run the system.time() function to rasterize the GADM vector object into a raster file

Error in .getPutVals(p, field, npol, mask) : invalid value for field
Timing stopped at: 0.003 0 0.004

##Error message when I run the bioclim() model

        Error in (function (classes, fdef, mtable)  : 
      unable to find an inherited method for function ‘bioclim’ for signature ‘"SpatialPolygonsDataFrame", "missing"’

R-code:

###Open Packages

library("sp")
library("rgdal")
library("raster")
library("maptools")
library("rgdal")
library("dismo")
library("spatialEco")
library("ggplot2")
library("dplyr")
library("maps")
library("ggspatial")
library("GADMTools")
library("maps")

##Mini dataframe for Blue.whale.new

Blue.whale.new <- data.frame(longitude = c(80.5, 80.5, 80.5, 80.5, 80.4, 80.4, 80.5, 80.5, 80.4),
                         latitude = c(5.84, 5.82, 5.85, 5.85, 5.89, 5.82, 5.82, 5.84, 5.83))

    ###Uploading the map from GADM resources

    ##Plotting the map of Sri Lanka
    dev.new()

    bioclim1.data <- getData('GADM', country='LKA', level=1)

    ####Get boudnign box of Sri Lanka shape file
    bb=bioclim1.data@bbox

    # Determine geographic extent of our data
    max.lat <- ceiling(max(Blue.whale$latitude))
    min.lat <- floor(min(Blue.whale$latitude))
    max.lon <- ceiling(max(Blue.whale$longitude))
    min.lon <- floor(min(Blue.whale$longitude))
    geographic.extent <- extent(x = c(min.lon, max.lon, min.lat, max.lat))

    #####Plot map
    dev.new()

    plot(bioclim1.data, 
         xlim = c(min(c(min.lon,bb[1,1])), max(c(max.lon,bb[1,2]))),
         ylim = c(min(c(min.lat,bb[2,1])), max(c(max.lat,bb[2,2]))),
         axes = TRUE, 
         col = "grey95")

    # Add the points for individual observation
    points(x = Blue.whale$longitude, 
           y = Blue.whale$latitude, 
           col = "olivedrab", 
           pch = 15, 
           cex = 0.50)

    ###Building a model and visualising results

    ##Crop bioclim data to geographic extent of blue whales
    bioclim.data.blue.whale<-crop(x=bioclim1.data, y=geographic.extent)

    ###Rasterize the GADM Formatinto raster format

    ##Define Rasterlayer object
    r.raster<-raster()

    ##Define raster layer extent
    extent(r.raster)<-extent(bioclim1.data)

    # Rasterize
      system.time(bioclim1.data <- rasterize(bioclim1.data, r.raster))

    Error in .getPutVals(p, field, npol, mask) : invalid value for field
Timing stopped at: 0.003 0 0.004

    ##Build species distribution model

    Blue.whale.model<-bioclim(x=bioclim1.data, y=Blue.whale_New)

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘bioclim’ for signature ‘"SpatialPolygonsDataFrame", "data.frame"’

R-code I plan to run once this error message issue is solved:

# Predict presence from model
predict.presence <- dismo::predict(object = Blue.whale.model, x = bioclim1.data, ext = geographic.extent)

# Plot base map
plot(bioclim1.data, 
     xlim = c(min(c(min.lon,bb[1,1])), max(c(max.lon,bb[1,2]))),
     ylim = c(min(c(min.lat,bb[2,1])), max(c(max.lat,bb[2,2]))),
     axes = TRUE, 
     col = "grey95")

# Add model probabilities
plot(predict.presence, add = TRUE)

# Redraw those country borders
plot(bioclim1.data, add = TRUE, border = "grey5")

# Add original observations
points(Blue.whale_New$longitude, Blue.whale_New$latitude, col = "olivedrab", pch = 20, cex = 0.75)
box()

Image 1

enter image description here

Image 2 (Example of desired output):

enter image description here

3
  • The code is failing as bioclim1.data doesn't have a variable called "NAME_2". It does have a variable called "NAME_1" but that will still fail as it is character data, not numeric. You need to reconsider your approach as you need data over the ocean, not land. You should build a rasterStack or matrix of ocean variables for variable x in the bioclim function, e.g. sea surface temp, salinity, bathymetry, currents, etc. Commented May 14, 2020 at 10:59
  • Right! I see! It uncanny you said this because I am creating general mixed linear models (GLM's) with 8 ecological predictors. The reason why I am doing this code above is because I want to produce pseudo-absence data of which we will find the values for our predictor variables and then add this data to the GLM's. I am not sure how to scale maps properly, but those GPS points are all in the sea. i do have this data in my data fame. So, I should build a raster stack with one of my predictor variables and this should then be applicable for the x in the bioclim() function. Thank you ! Commented May 14, 2020 at 14:47
  • I am feeling like a duck out of water but I am determined to reach my goal. Commented May 14, 2020 at 14:48

1 Answer 1

0

For this part

Blockquote

Error in .getPutVals(p, field, npol, mask) : invalid value for field Timing stopped at: 0.003 0 0.004

Blockquote

I had a similar problem trying to create a training dataset for a semantic socio-economic classification from Remotely sensed data, using this command

rp <- rasterize(poly, r, "Class_ID")

Error in .getPutVals(p, field, npol, mask) : invalid value for field

I later realized that I just made a mistake in the spelling of the field (CLASS and not Class)

rp <- rasterize(poly, r, "CLASS_ID")

That was my problem, I suggest you define the field to use from the vector data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.