Skip to content

Commit

Permalink
added more occ_data and occ_Search tests for geometry,
Browse files Browse the repository at this point in the history
updaetd news
bumped to v0.9.3
  • Loading branch information
sckott committed Mar 29, 2016
1 parent 9ad333e commit 419a9a4
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Expand Up @@ -7,7 +7,7 @@ Description: A programmatic interface to the Web Service methods
taxonomic names, retrieving information on data providers,
getting species occurrence records, and getting counts of
occurrence records.
Version: 0.9.2.9760
Version: 0.9.3
License: MIT + file LICENSE
Authors@R: c(
person("Scott", "Chamberlain", role = c("aut", "cre"), email = "myrmecocystus@gmail.com"),
Expand Down
26 changes: 24 additions & 2 deletions NEWS.md
@@ -1,12 +1,34 @@
rgbif 0.9.3
===========

### NEW FEATURES

* `occ_data()` and `occ_search()` gain ability to more flexibly deal with inputs to the
`geometry` parameter. Previously, long WKT strings passed to `occ_search()` or
`occ_data()` would fail because URIs can only be so long. Another option is to use
the download API (see `?downloads`). This version adds the ability to choose what to
do with long WKT strings via the `geom_big` parameter: `asis` (same as previous version),
`bbox` which detects if a WKT sting is likely too long, and creates a bounding box from the
WKT string then once data is retrived, clips the result to the original WKT string; `axe`
uses the `geoaxe` package to chop up the input WKT polygon into many, with toggles in the
new parameters `geom_size` and `geom_n`. (#197) (#199)
* As part of this change, when >1 geometry value passed, or if `geom_big="axe"`, then
named elements of the output get names `geom1`, `geom2`, `geom3`, etc. instead of the
input WKT strings - this is because WKT strings can be very long, and make for very
awkward named access to elements. The original WKT strings can still be accessed via
`attr(result, "args")$geometry`

### MINOR IMPROVEMENTS

* xxx (#197)
* xxxx (#199)
* code tidying throughout the package

### BUG FIXES

* Fix parsing bug in `name_usage()` function, see commit [e88cf01cc11cb238d44222346eaeff001c0c637e](https://github.com/ropensci/rgbif/commit/e88cf01cc11cb238d44222346eaeff001c0c637e)
* Fix to tests to use new `testthat` fxn names, e.g., `expect_gt()`
instead of `expect_more_than()`
* Fix to `occ_download()` to parse error correctly when empty body passed from GBIF (#202)


rgbif 0.9.2
===========
Expand Down
2 changes: 1 addition & 1 deletion R/occ_data.R
Expand Up @@ -108,7 +108,7 @@ occ_data <- function(taxonKey=NULL, scientificName=NULL, country=NULL, publishin
out <- .get_occ_data()
} else {
out <- lapply(iter[[1]], .get_occ_data, itervar = names(iter))
names(out) <- iter[[1]]
names(out) <- transform_names(iter[[1]])
}

if (any(names(argscoll) %in% names(iter))) {
Expand Down
2 changes: 1 addition & 1 deletion R/occ_search.r
Expand Up @@ -148,7 +148,7 @@ occ_search <- function(taxonKey=NULL, scientificName=NULL, country=NULL, publish
out <- .get_occ_search()
} else {
out <- lapply(iter[[1]], .get_occ_search, itervar = names(iter))
names(out) <- iter[[1]]
names(out) <- transform_names(iter[[1]])
}

if (any(names(argscoll) %in% names(iter))) {
Expand Down
8 changes: 8 additions & 0 deletions R/zzz.r
Expand Up @@ -357,3 +357,11 @@ movecols <- function(x, cols){
}

c_utf8 <- function(x) content(x, "text", encoding = "UTF-8")

transform_names <- function(x) {
if (all(grepl("POLYGON", x)) && any(nchar(x) > 50)) {
paste0("geom", seq_along(x))
} else {
x
}
}
4 changes: 2 additions & 2 deletions man-roxygen/occ_data_egs.R
Expand Up @@ -111,7 +111,7 @@
#' #### Default option with large WKT string fails
#' # res <- occ_data(geometry = wkt)$data
#'
#' #### if WKT too long, with 'geom_handler=bbox': makes into bounding box
#' #### if WKT too long, with 'geom_big=bbox': makes into bounding box
#' res <- occ_data(geometry = wkt, geom_big = "bbox")$data
#' library("rgeos")
#' library("sp")
Expand All @@ -120,7 +120,7 @@
#' coordinates(res) <- ~decimalLongitude+decimalLatitude
#' points(res)
#'
#' #### Or, use 'geom_handler=axe'
#' #### Or, use 'geom_big=axe'
#' (res <- occ_data(geometry = wkt, geom_big = "axe"))
#' ##### manipulate essentially number of polygons that result, so number of requests
#' ###### default geom_size is 40
Expand Down
4 changes: 2 additions & 2 deletions man-roxygen/occ_search_egs.R
Expand Up @@ -126,7 +126,7 @@
#' #### Default option with large WKT string fails
#' # res <- occ_search(geometry = wkt)
#'
#' #### if WKT too long, with 'geom_handler=bbox': makes into bounding box
#' #### if WKT too long, with 'geom_big=bbox': makes into bounding box
#' res <- occ_search(geometry = wkt, geom_big = "bbox")$data
#' library("rgeos")
#' library("sp")
Expand All @@ -135,7 +135,7 @@
#' coordinates(res) <- ~decimalLongitude+decimalLatitude
#' points(res)
#'
#' #### Or, use 'geom_handler=axe'
#' #### Or, use 'geom_big=axe'
#' (res <- occ_search(geometry = wkt, geom_big = "axe"))
#' ##### manipulate essentially number of polygons that result, so number of requests
#' ###### default geom_size is 40
Expand Down
4 changes: 2 additions & 2 deletions man/occ_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/occ_search.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions tests/testthat/test-occ_data.R
Expand Up @@ -170,3 +170,83 @@ test_that("scientificName basic use works - no synonyms", {
expect_equal(attr(hh, "args")$scientificName, "Pipistrellus hesperus")
expect_equal(hh$data$name[1], "Pipistrellus hesperus")
})

######### geometry inputs work as expected
test_that("geometry inputs work as expected", {
skip_on_cran()

# in well known text format
aa <- occ_data(geometry='POLYGON((30.1 10.1, 10 20, 20 40, 40 40, 30.1 10.1))', limit=20)
expect_is(aa, "gbif_data")
expect_is(unclass(aa), "list")
expect_named(attr(aa, "args"), c('geometry', 'limit', 'offset'))
expect_gt(NROW(aa$data), 0)

# with a taxon key
key <- 3189815
bb <- occ_data(taxonKey=key, geometry='POLYGON((30.1 10.1, 10 20, 20 40, 40 40, 30.1 10.1))',
limit=20)
expect_is(bb, "gbif_data")
expect_is(unclass(bb), "list")
expect_named(attr(bb, "args"), c('taxonKey', 'geometry', 'limit', 'offset'))
expect_gt(NROW(bb$data), 0)
expect_lt(NROW(bb$data), NROW(aa$data))

# using bounding box, converted to WKT internally
cc <- occ_data(geometry=c(-125.0,38.4,-121.8,40.9), limit=20)
expect_is(cc, "gbif_data")
expect_is(unclass(cc), "list")
expect_named(attr(cc, "args"), c('geometry', 'limit', 'offset'))
expect_gt(NROW(cc$data), 0)
expect_equal(NROW(cc$data), NROW(aa$data))

# Search on a long WKT string - too long for a GBIF search API request
## internally convert WKT string to a bounding box
wkt <- "POLYGON((13.26349675655365 52.53991761181831,18.36115300655365 54.11445544219924,
21.87677800655365 53.80418956368524,24.68927800655365 54.217364774722455,28.20490300655365
54.320018299365124,30.49005925655365 52.85948216284084,34.70880925655365 52.753220564427814,
35.93927800655365 50.46131871049754,39.63068425655365 49.55761261299145,40.86115300655365
46.381388009130845,34.00568425655365 45.279102926537,33.30255925655365 48.636868465271846,
30.13849675655365 49.78513301801265,28.38068425655365 47.2236377039631,29.78693425655365
44.6572866068524,27.67755925655365 42.62220075124676,23.10724675655365 43.77542058000212,
24.51349675655365 47.10412345120368,26.79865300655365 49.55761261299145,23.98615300655365
52.00209943876426,23.63459050655365 49.44345313705238,19.41584050655365 47.580567827212114,
19.59162175655365 44.90682206053508,20.11896550655365 42.36297154876359,22.93146550655365
40.651849782081555,25.56818425655365 39.98171166226459,29.61115300655365 40.78507856230178,
32.95099675655365 40.38459278067577,32.95099675655365 37.37491910393631,26.27130925655365
33.65619609886799,22.05255925655365 36.814081996401605,18.71271550655365 36.1072176729021,
18.53693425655365 39.16878677351903,15.37287175655365 38.346355762190846,15.19709050655365
41.578843777436326,12.56037175655365 41.050735748143424,12.56037175655365 44.02872991212046,
15.19709050655365 45.52594200494078,16.42755925655365 48.05271546733352,17.48224675655365
48.86865641518059,10.62677800655365 47.817178329053135,9.57209050655365 44.154980365192,
8.16584050655365 40.51835445724746,6.05646550655365 36.53210972067291,0.9588092565536499
31.583640057148145,-5.54509699344635 35.68001485298146,-6.77556574344635 40.51835445724746,
-9.41228449344635 38.346355762190846,-12.40056574344635 35.10683619158607,-15.74040949344635
38.07010978950028,-14.68572199344635 41.31532459432774,-11.69744074344635 43.64836179231387,
-8.88494074344635 42.88035509418534,-4.31462824344635 43.52103366008421,-8.35759699344635
47.2236377039631,-8.18181574344635 50.12441989397795,-5.01775324344635 49.55761261299145,
-2.73259699344635 46.25998980446569,-1.67790949344635 44.154980365192,-1.32634699344635
39.30493590580802,2.18927800655365 41.44721797271696,4.47443425655365 43.26556960420879,
2.18927800655365 46.7439668697322,1.83771550655365 50.3492841273576,6.93537175655365
49.671505849335254,5.00177800655365 52.32557322466785,7.81427800655365 51.67627099802223,
7.81427800655365 54.5245591562317,10.97834050655365 51.89375191441792,10.97834050655365
55.43241335888528,13.26349675655365 52.53991761181831))"
wkt <- gsub("\n", " ", wkt)

# Default option with large WKT string fails
expect_error(occ_data(geometry = wkt),
"Client error: \\(413\\) Request Entity Too Large")

# if WKT too long, with 'geom_big=bbox': makes into bounding box
dd <- occ_data(geometry = wkt, geom_big = "bbox", limit = 30)
expect_lt(nchar(attr(dd, "args")$geometry), nchar(wkt))
expect_message(occ_data(geometry = wkt, geom_big = "bbox", limit = 1),
"geometry is big, querying BBOX, then pruning results to polygon")

# use 'geom_big=axe'
ee <- occ_data(geometry = wkt, geom_big = "axe", limit = 30)
## more calls
gg <- occ_data(geometry = wkt, geom_big = "axe", geom_size = 30, limit = 5)

expect_gt(length(names(gg)), length(names(ee)))
})
81 changes: 81 additions & 0 deletions tests/testthat/test-occ_search.r
Expand Up @@ -196,3 +196,84 @@ test_that("scientificName basic use works - no synonyms", {
expect_equal(attr(hh, "args")$scientificName, "Pipistrellus hesperus")
expect_equal(hh$data$name[1], "Pipistrellus hesperus")
})


######### geometry inputs work as expected
test_that("geometry inputs work as expected", {
skip_on_cran()

# in well known text format
aa <- occ_search(geometry='POLYGON((30.1 10.1, 10 20, 20 40, 40 40, 30.1 10.1))', limit=20)
expect_is(aa, "gbif")
expect_is(unclass(aa), "list")
expect_named(attr(aa, "args"), c('geometry', 'limit', 'offset', 'fields'))
expect_gt(NROW(aa$data), 0)

# with a taxon key
key <- 3189815
bb <- occ_search(taxonKey=key, geometry='POLYGON((30.1 10.1, 10 20, 20 40, 40 40, 30.1 10.1))',
limit=20)
expect_is(bb, "gbif")
expect_is(unclass(bb), "list")
expect_named(attr(bb, "args"), c('taxonKey', 'geometry', 'limit', 'offset', 'fields'))
expect_gt(NROW(bb$data), 0)
expect_lt(NROW(bb$data), NROW(aa$data))

# using bounding box, converted to WKT internally
cc <- occ_search(geometry=c(-125.0,38.4,-121.8,40.9), limit=20)
expect_is(cc, "gbif")
expect_is(unclass(cc), "list")
expect_named(attr(cc, "args"), c('geometry', 'limit', 'offset', 'fields'))
expect_gt(NROW(cc$data), 0)
expect_equal(NROW(cc$data), NROW(aa$data))

# Search on a long WKT string - too long for a GBIF search API request
## internally convert WKT string to a bounding box
wkt <- "POLYGON((13.26349675655365 52.53991761181831,18.36115300655365 54.11445544219924,
21.87677800655365 53.80418956368524,24.68927800655365 54.217364774722455,28.20490300655365
54.320018299365124,30.49005925655365 52.85948216284084,34.70880925655365 52.753220564427814,
35.93927800655365 50.46131871049754,39.63068425655365 49.55761261299145,40.86115300655365
46.381388009130845,34.00568425655365 45.279102926537,33.30255925655365 48.636868465271846,
30.13849675655365 49.78513301801265,28.38068425655365 47.2236377039631,29.78693425655365
44.6572866068524,27.67755925655365 42.62220075124676,23.10724675655365 43.77542058000212,
24.51349675655365 47.10412345120368,26.79865300655365 49.55761261299145,23.98615300655365
52.00209943876426,23.63459050655365 49.44345313705238,19.41584050655365 47.580567827212114,
19.59162175655365 44.90682206053508,20.11896550655365 42.36297154876359,22.93146550655365
40.651849782081555,25.56818425655365 39.98171166226459,29.61115300655365 40.78507856230178,
32.95099675655365 40.38459278067577,32.95099675655365 37.37491910393631,26.27130925655365
33.65619609886799,22.05255925655365 36.814081996401605,18.71271550655365 36.1072176729021,
18.53693425655365 39.16878677351903,15.37287175655365 38.346355762190846,15.19709050655365
41.578843777436326,12.56037175655365 41.050735748143424,12.56037175655365 44.02872991212046,
15.19709050655365 45.52594200494078,16.42755925655365 48.05271546733352,17.48224675655365
48.86865641518059,10.62677800655365 47.817178329053135,9.57209050655365 44.154980365192,
8.16584050655365 40.51835445724746,6.05646550655365 36.53210972067291,0.9588092565536499
31.583640057148145,-5.54509699344635 35.68001485298146,-6.77556574344635 40.51835445724746,
-9.41228449344635 38.346355762190846,-12.40056574344635 35.10683619158607,-15.74040949344635
38.07010978950028,-14.68572199344635 41.31532459432774,-11.69744074344635 43.64836179231387,
-8.88494074344635 42.88035509418534,-4.31462824344635 43.52103366008421,-8.35759699344635
47.2236377039631,-8.18181574344635 50.12441989397795,-5.01775324344635 49.55761261299145,
-2.73259699344635 46.25998980446569,-1.67790949344635 44.154980365192,-1.32634699344635
39.30493590580802,2.18927800655365 41.44721797271696,4.47443425655365 43.26556960420879,
2.18927800655365 46.7439668697322,1.83771550655365 50.3492841273576,6.93537175655365
49.671505849335254,5.00177800655365 52.32557322466785,7.81427800655365 51.67627099802223,
7.81427800655365 54.5245591562317,10.97834050655365 51.89375191441792,10.97834050655365
55.43241335888528,13.26349675655365 52.53991761181831))"
wkt <- gsub("\n", " ", wkt)

# Default option with large WKT string fails
expect_error(occ_search(geometry = wkt),
"Client error: \\(413\\) Request Entity Too Large")

# if WKT too long, with 'geom_big=bbox': makes into bounding box
dd <- occ_search(geometry = wkt, geom_big = "bbox", limit = 30)
expect_lt(nchar(attr(dd, "args")$geometry), nchar(wkt))
expect_message(occ_search(geometry = wkt, geom_big = "bbox", limit = 1),
"geometry is big, querying BBOX, then pruning results to polygon")

# use 'geom_big=axe'
ee <- occ_search(geometry = wkt, geom_big = "axe", limit = 30)
## more calls
gg <- occ_search(geometry = wkt, geom_big = "axe", geom_size = 30, limit = 5)

expect_gt(length(names(gg)), length(names(ee)))
})

0 comments on commit 419a9a4

Please sign in to comment.