rangeMap.save: Save, retrieve and export maps.

Description Usage Arguments Details Value Note See Also Examples

View source: R/MapSave-methods.R

Description

Apply a chosen SQL or function at each grid cell, allowing for complex subsetting at both ID (e.g. species) and pixel (e.g. assemblage) levels.

Usage

1
2
rangeMap.save(CON, tableName, FUN, biotab, biotrait, subset = list(),
  path, overwrite = FALSE, ...)

Arguments

CON

an sqlite connection pointing to a valid rangeMapper project.

tableName

name of the table (quoted) to be added to the sqlite database. the prefix ‘MAP’ will be appended to tableName prior to saving.

FUN

the function to be applied to each pixel. If FUN is missing then species richness (species count) is computed.

biotab

character string identifying the ‘BIO’ table to use.

biotrait

character string identifying the ID of the ‘BIO’ table. see bio.save

subset

a named list. See details

path

path to the raster file(quoted) to be imported to the existing project. raster package is required at this step.

overwrite

if TRUE then the table is removed

...

when FUN is a function, ... denotes any extra arguments to be passed to it.

Details

The subset argument accepts a named list. Names refers to ‘BIO’, ‘MAP’ and ‘metadata_rages’ table names while the strings in the list are character strings containing the SQL WHERE clause. The subset can point to either one table type (e.g. list(MAP_species_richness = "species_richness > 500") ) or can point to several table types (e.g. list(BIO_lifeHistory = "clutch_size > 4", MAP_meanAltitude = "meanAltitude < 1000", metadata_ranges = "Area < 1000"))

Any valid SQL expression can be used to build up a subset. See http://www.sqlite.org/lang_expr.html

When using cl parameter you must load the given packages used in FUN by loading the packages inside the function, using '::' or initializing the cluster before calling rangeMap.save (e.g. clusterEvalQ(cl=cl, library(caper)))).

Value

TRUE when the MAP was created successfully.

Note

SQL aggregate functions are more efficient then their R counterparts. For simple aggregate functions like mean, median, sd, count it is advisable to use SQL functions rather then R functions.

See Also

metadata.update.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require(rangeMapper)
require(rgdal)
breding_ranges = readOGR(system.file(package = "rangeMapper",
     "extdata", "wrens", "vector_combined"), "wrens", verbose = FALSE)[1:50, ]
breding_ranges = spTransform(breding_ranges,
    CRS("+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0
        +ellps=WGS84 +units=m +no_defs") )
data(wrens)
d = subset(wrens, select = c('sci_name', 'body_size', 'body_mass', 'clutch_size') )

con = ramp("wrens.sqlite", gridSize = 500000, spdf = breding_ranges, biotab = d,
            ID = "sci_name", metadata = rangeTraits(),
            FUN = "median", overwrite = TRUE)


lmSlope = function(formula, data) {
    fm = try(lm(formula, data = data), silent = TRUE)
    if (inherits(fm, "try-error"))
        res = NA else res = coef(fm)[2]
    as.numeric(res)
}

# Subsetting by Species and Assembladge
rangeMap.save(con, FUN = lmSlope, biotab = "biotab", biotrait = "body_mass",
    tableName = "slope_bodyMass_clutchSize", formula = log(body_mass) ~ clutch_size,
    list(MAP_species_richness = "species_richness >= 5",
        BIO_biotab = "body_size > 15"
        ), overwrite = TRUE)

## Not run: 
# Import raster maps to the current project
r = system.file(package = "rangeMapper", "extdata", "etopo1", "etopo1_Americas.tif")
rangeMap.save(con, path = r, tableName = "meanAltitude", FUN = mean, overwrite = TRUE)
m = rangeMap.fetch(con, spatial = FALSE)
plot(m)


## End(Not run)

valcu/rangeMapper documentation built on Feb. 6, 2021, 8:20 p.m.