ecohealthalliance / fasterize

High performance raster conversion for modern spatial data 🚀🌏▦
https://ecohealthalliance.github.io/fasterize/
Other
182 stars 15 forks source link

Error: Expecting a single string value: [type=closure; extent=1] #13

Closed fab4ap closed 6 years ago

fab4ap commented 6 years ago

Hello: Wanted to report this error. Hope the foll. is self-explanatory. Works with rasterize() but not in fasterize().

library(fasterize) library(sf) library(raster) riv129mprdsf = st_read(dsn=".", layer="prdall_riv129_20k129") Simple feature collection with 129084 features and 25 fields geometry type: POLYGON dimension: XY bbox: xmin: -4278174 ymin: 3802881 xmax: 3161826 ymax: 10742880 epsg (SRID): NA proj4string: +proj=lcc +lat_1=49 +lat_2=77 +lat_0=0 +lon_0=-95 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +nodefs

class(riv129mprdsf) [1] "sf" "data.frame"

canus1kgrd class : RasterLayer dimensions : 6940, 7440, 51633600 (nrow, ncol, ncell) resolution : 1000, 1000 (x, y) extent : -4282964, 3157036, 3808483, 10748483 (xmin, xmax, ymin, ymax) coord. ref. : +proj=lcc +lat_1=49 +lat_2=77 +lat_0=0 +lon_0=-95 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0 data source : in memory names : canus20kgrdsync values : 0, 128710 (min, max)

act = fasterize(riv129mprdsf, canus1kgrd, field="riv129_20k", fun=mean, background=0) Error in fasterize(riv129mprdsf, canus1kgrd, field = "riv129_20k", fun = mean) : Expecting a single string value: [type=closure; extent=1].

mdsumner commented 6 years ago

What the error is saying is that mean (the function, i.e. a closure) is not an allowed value for "fun" - it must be a

?fasterize
#character. The name of a function by which to combine overlapping polygons. 
# Currently takes "sum", "first", "last", "min", "max", "count", or "any".

You cannot apply a mean at all currently, but there's a workaround for using raster for this described in the help. Something like (untested ...)

x <- fasterize(riv129mprdsf, canus1kgrd, by = "riv129_20k", background=0)
act <- calc(x, mean)

Beware that if you have a lot of distinct values in that field you'll get a huge-number layered raster, which might be a pain so first try this, if it's more than "a few" you'll get a very large raster given your x/y dimensions here, so iterating over individual features and accumulating a sum with raster addition might be better than a big-calc in one go.

length(unique(riv129mprdsf$riv129_20k))

(Calling a function a closure shows a fun part of R's history as descendant from Lisp, and lexical scoping - it's explained here: http://adv-r.had.co.nz/Functional-programming.html#closures You also get a fun error like this from mean[1] )

noamross commented 6 years ago

I'm closing this as it is expected behavior, but opened #18, #19, and #20 in response.

fab4ap commented 6 years ago

OK. Thanks much! Prasad

On Fri, Mar 23, 2018 at 10:01 AM, Noam Ross notifications@github.com wrote:

I'm closing this as it is expected behavior, but opened #18 https://github.com/ecohealthalliance/fasterize/issues/18, #19 https://github.com/ecohealthalliance/fasterize/issues/19, and #20 https://github.com/ecohealthalliance/fasterize/issues/20 in response.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ecohealthalliance/fasterize/issues/13#issuecomment-375674203, or mute the thread https://github.com/notifications/unsubscribe-auth/AjQIhyDO9VRoUOrJQ8AnHPUpJ44ZpElfks5thQApgaJpZM4SjGdq .