Skip to content

Commit

Permalink
Fix #28 by using the json endpoint. Setup a list to hold results from…
Browse files Browse the repository at this point in the history
… the for loop and rbind results when done. We can't get more than 10K obs in one go, so this function now only gets up to 10K obs, but warns the user that this is the case.

Some minor formatting changes also.
  • Loading branch information
LDalby committed Jul 12, 2019
1 parent bba0183 commit 8c36dff
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 165 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Imports:
jsonlite,
ggplot2,
maps
RoxygenNote: 6.0.1
RoxygenNote: 6.1.1
Suggests: knitr, rmarkdown, testthat
VignetteBuilder: knitr
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Generated by roxygen2: do not edit by hand

export(get_inat_obs)
export(get_inat_obs_id)
Expand All @@ -11,4 +12,3 @@ import(httr)
import(jsonlite)
import(maps)
import(plyr)
importFrom("utils", "read.csv")
77 changes: 48 additions & 29 deletions R/get_inat_obs_project.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
#' Download observations or info from a project
#'@description retrieve observations from a particular iNaturalist project. This function can be used to get either observations or information from a project by project name or ID
#'@description retrieve observations from a particular iNaturalist project. This function can be used to get either observations or information from a project by project name or ID
#'@param grpid Name of the group as an iNaturalist slug or group id
#'@param type Either "observations" or "info" Observations returns all observations, and "info" returns project details similar to what you can find on a project webpage.
#'@param raw True or False. If TRUE and searching for project info, returns the raw output of parsed JSON for that project. Otherwise just some basic information is returned as a list
#'@param type character Either "observations" or "info" Observations returns all observations, and "info" returns project details similar to what you can find on a project webpage.
#'@param raw logical TRUE or FALSE. If TRUE and searching for project info, returns the raw output of parsed JSON for that project. Otherwise just some basic information is returned as a list
#'@details An iNaturalist slug is usually the project as single string with words seperated by hyphens. For instance, the project "State Flowers of the United States" has a slug of "state-flowers-of-the-united-states-eol-collection". This can be extracted from the URL for the project usually. The state flowers project has the following URL http://www.inaturalist.org/projects/state-flowers-of-the-united-states-eol-collection
#'
#'@examples \dontrun{
#' get_inat_obs_project(354, type = "observations")
#' get_inat_obs_project("crows-in-vermont", type="info",raw=FALSE)
#' get_inat_obs_project("crows-in-vermont", type="info",raw=FALSE)
#'}
#'@import httr jsonlite
#'@export

get_inat_obs_project <- function(grpid,type = c("observations","info"), raw = F){
get_inat_obs_project <- function(grpid, type = c("observations", "info"), raw = FALSE){
argstring = switch(match.arg(type),
observations = "obs",
info = "info")
url= paste("http://www.inaturalist.org/projects/",grpid,".json",sep="")
xx = fromJSON(content(GET(url),as="text"))
recs =xx$project_observations_count
observations = "obs",
info = "info")
url <- paste0("http://www.inaturalist.org/projects/",grpid,".json")
xx <- fromJSON(content(GET(url), as = "text"))
recs <- xx$project_observations_count

### Error handling for empty projects
dat = NULL
dat <- NULL
if(is.null(recs))(return(dat))
cat(paste(recs," Records\n0"))


cat(paste(recs," Records\n"))

if(argstring == "info"){
output <- list()
output[["title"]] <- xx$title
Expand All @@ -40,20 +39,40 @@ get_inat_obs_project <- function(grpid,type = c("observations","info"), raw = F)
if(raw){
output[["raw"]] <- xx
}


return(output)
} else if(argstring == "obs"){

if (recs %% 100 == 0){loopval<-recs %/% 100}
else{loopval <-(recs %/% 100)+1}
for(i in 1:loopval){
url1=paste("http://www.inaturalist.org/observations/project/",grpid,".csv?page=",i,"&per_page=100",sep="")
cat(paste("-",i*100,sep=""))
newdat=read.csv(url1,stringsAsFactors = FALSE)
dat=rbind(dat,newdat)
}
return(dat)
}

else if (argstring == "obs") {
per_page <- 200
if (recs %% per_page == 0) {
loopval <- recs %/% per_page
}
if (recs >= 10000) {
cat(
"Number of observations in project greater than current API limit\nreturning the first 10000\n"
)
loopval <- 10000 / per_page
}
else {
loopval <- (recs %/% per_page) + 1
}
obs_list <- vector("list", loopval)
for (i in 1:loopval) {
url1 <-
paste0(
"http://www.inaturalist.org/observations/project/", grpid,
".json?page=", i,
"&per_page=", per_page
)
if (i == 1) {
cat(paste0("0-", per_page))
}
if (i > 1) {
cat(paste0("-", i * per_page))
}
obs_list[[i]] <-
fromJSON(content(GET(url1), as = "text"), flatten = TRUE)
}
project_obs <- do.call("rbind", obs_list)
return(project_obs)
}
}
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ print(place_counts$most_species[1:10,])



## Mapping.
## Mapping

Basic maps can be created as well to quickly visualize search results. Maps can either be plotted automatically `plot = TRUE` or simply return a ggplot2 object with `plot = FALSE`. This works well with single species data, but more complicated plots are best made from scratch.

Expand Down
Loading

0 comments on commit 8c36dff

Please sign in to comment.