Giter VIP home page Giter VIP logo

Comments (2)

AaronPlex avatar AaronPlex commented on August 31, 2024

@krishnakeshav
Please try the code below where we can download information directly from GBIF and then convert that info into raster files of a desired resolution. In this case, myTaxon asks for the scientific name of the host (plant) instead of the common name: say, "Persea americana" instead of "avocado". Let me know if it works on your side.
Also, the user needs to create an account in GBIF and then use his/her credentials. (For the moment, I trust that people in this repo can see my credentials). lol

library(rgbif)

# fill in your gbif.org credentials
username <- "plexaaron"  
password <- "Oxysarcodexiaaurea7.."
email <- "[email protected]"

# User provide a taxon name and R generates the taxon key to search in GBIF
# Or user provide the taxon key directly
myTaxon <- c("Persea americana")
taxonkey <- name_backbone(myTaxon)$usageKey

# Downloading info from GBIF
downloadID<-occ_download(
  pred_in("taxonKey", taxonkey),
  format = "SIMPLE_CSV",
  user = username, pwd = password, email = email
)

hostOccGBIF<-occ_download_get(downloadID[1]) %>%
  occ_download_import()

# Cleaning dataset
hostOccGBIF<-hostOccGBIF[hostOccGBIF$countryCode != "",]
hostOccGBIF<-hostOccGBIF[hostOccGBIF$countryCode != "ZZ",]
hostOccGBIF<-hostOccGBIF[hostOccGBIF$occurrenceStatus == "PRESENT",]
hostOccGBIF$Lon <- as.numeric(hostOccGBIF$decimalLongitude)
hostOccGBIF$Lat <- as.numeric(hostOccGBIF$decimalLatitude)
hostOccGBIF<-hostOccGBIF[,colnames(hostOccGBIF) %in% 
                           c("species", "Lon", "Lat")]
hostOccGBIF<-hostOccGBIF[is.na(hostOccGBIF$Lon)==FALSE, ]
hostOccGBIF<-hostOccGBIF[is.na(hostOccGBIF$Lat)==FALSE, ]
length(hostOccGBIF$species)

library(terra)
vectorHost<-vect(hostOccGBIF, crs="+proj=longlat", geom=c("Lon","Lat"))
e <- ext(-180, 180, -60, 90) #left, right, bottom, top
vectorHost<-crop(vectorHost, e)
r <- rast(res=0.5, ext=e) # res=1 here is equivalent to resolution=12 in geohabnet
rasterHost<-rasterize(vectorHost, r, fun=length)
plot(rasterHost)
# Optional: Applying the spatial operation named focal to mitigate sampling bias
frasterHost<-focal(rasterHost, 3, mean, na.policy="all", na.rm=TRUE)
plot(frasterHost)

from habitatconnectivity.

krishnakeshav avatar krishnakeshav commented on August 31, 2024

@AaronPlex @GarrettLab I am moving GBIF dev to separate issue - #38

from habitatconnectivity.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.