Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selecting multiple attributes in fasterize? #40

Open
lime-n opened this issue Mar 10, 2021 · 0 comments
Open

Selecting multiple attributes in fasterize? #40

lime-n opened this issue Mar 10, 2021 · 0 comments

Comments

@lime-n
Copy link

lime-n commented Mar 10, 2021

Are there capable options for selecting more than one attribute in the fasterize package? or
Is there an efficient method for selecting multiple fields when the raster used in fasterize does not have an attribute table, such that the attribute values are taken from the columns in the shapefile?

For example, when using rasterize, it automatically selects all the attributes, i.e.

r.2010 <- ebird_buff.2010 %>% 
  st_transform(crs = projection(r.2010)) %>% 
  rasterize(r.2010) %>% 
  # remove any empty cells at edges
  trim()

class      : RasterLayer 
dimensions : 1405, 1943, 2729915  (nrow, ncol, ncell)
resolution : 2316.564, 2316.564  (x, y)
extent     : -9738370, -5237287, 2741885, 5996656  (xmin, xmax, ymin, ymax)
crs        : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs 
source     : memory
names      : layer 
values     : 6, 28289  (min, max)
attributes :
          ID year locality_id species_observed
 from:     1 2010    L2228604                0
  to : 28289 2010    L1348755                0

Though, when using fasterize, it returns a field of 1:

class      : RasterLayer 
dimensions : 1409, 1946, 2741914  (nrow, ncol, ncell)
resolution : 2316.564, 2316.564  (x, y)
extent     : -9747636, -5239604, 2730302, 5994340  (xmin, xmax, ymin, ymax)
crs        : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs 
source     : memory
names      : layer 
values     : 1, 1  (min, max)

I've noticed that the attributes are credited to the raster if and only if attributes are currently present in the fasterized raster, though this is not necessarily true for rasterize.

i.e.

r
class      : RasterLayer 
dimensions : 1430, 2052, 2934360  (nrow, ncol, ncell)
resolution : 2316.564, 2316.564  (x, y)
extent     : -9793968, -5040379, 2716402, 6029088  (xmin, xmax, ymin, ymax)
crs        : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs 

Though, when r has an attribute table, then fasterize imitates it:

#This is the result when I use r.2010, the first example above, as the raster to be fasterized
class      : RasterLayer 
dimensions : 1405, 1943, 2729915  (nrow, ncol, ncell)
resolution : 2316.564, 2316.564  (x, y)
extent     : -9738370, -5237287, 2741885, 5996656  (xmin, xmax, ymin, ymax)
crs        : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +R=6371007.181 +units=m +no_defs 
source     : memory
names      : layer 
values     : 1, 1  (min, max)
attributes :
          ID year locality_id species_observed
 from:     1 2010    L2228604                0
  to : 28289 2010    L1348755                0

Though, if I wish to select a field, I can alternatively select individual fields likeso:

fasterize(r, field = "some_field")

Although, I cannot select multiple fields

fasterize(r, field = c("some_field_1", "some_field_2"))

Error in fasterize(., r.2019, field = c("some_field_1", "some_field_2")) :
Expecting a single string value: [type=character; extent=2].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant