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

rasterize with fun = length (and fun = "length") stopped working #1054

Closed
Nowosad opened this issue Mar 8, 2023 · 5 comments
Closed

rasterize with fun = length (and fun = "length") stopped working #1054

Nowosad opened this issue Mar 8, 2023 · 5 comments

Comments

@Nowosad
Copy link
Contributor

Nowosad commented Mar 8, 2023

# remotes::install_github("rspatial/terra")
library(terra)
#> terra 1.7.19
library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.2, PROJ 9.0.1; sf_use_s2() is TRUE
cycle_hire_osm = spData::cycle_hire_osm
cycle_hire_osm_projected = st_transform(cycle_hire_osm, "EPSG:27700")
raster_template = rast(ext(cycle_hire_osm_projected), resolution = 1000,
                       crs = st_crs(cycle_hire_osm_projected)$wkt)

ch_raster1 = rasterize(cycle_hire_osm_projected, raster_template, 
                       fun = length)
#> Error in FUN(X[[i]], ...): 2 arguments passed to 'length' which requires 1

ch_raster2 = rasterize(cycle_hire_osm_projected, raster_template, field = 1,
                       fun = length)
#> Error in FUN(X[[i]], ...): 2 arguments passed to 'length' which requires 1

Created on 2023-03-08 with reprex v2.0.2

@kadyb
Copy link
Contributor

kadyb commented Mar 8, 2023

FWIW: It seems that this function works if you use SpatVector instead of sf.

@rhijmans
Copy link
Member

rhijmans commented Mar 8, 2023

Thanks, fixed now. @kadyb thanks for pointing out the origin or the problem.

@bienflorencia
Copy link

bienflorencia commented Mar 27, 2023

Hello! I updated terra to the development version doing
install.packages('terra', repos='https://rspatial.r-universe.dev')
but the problem still persists:
Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'mask': 2 arguments passed to 'length' which requires 1

Has it been fixed? How can I run the rasterize function using fun = 'length'?

@kadyb
Copy link
Contributor

kadyb commented Mar 27, 2023

The function seems to work on @Nowosad example. Maybe you should give your example that doesn't work?

packageVersion("terra")
#> [1] ‘1.7.21’

@bienflorencia
Copy link

Thanks, @kadyb. I can run @Nowosad's example, but my code is not running. Not sure how to reproduce my example :/

> packageVersion("terra")
# [1] ‘1.7.21’

> packageVersion("sf")
# [1] ‘1.0.12’

> R.Version()
$platform
[1] "x86_64-w64-mingw32"

$arch
[1] "x86_64"

$os
[1] "mingw32"

$system
[1] "x86_64, mingw32"

$status
[1] ""

$major
[1] "4"

$minor
[1] "1.0"

$year
[1] "2021"

$month
[1] "05"

$day
[1] "18"

$`svn rev`
[1] "80317"

$language
[1] "R"

$version.string
[1] "R version 4.1.0 (2021-05-18)"

$nickname
[1] "Camp Pontanezen"

When running my code under the following system, the code runs with no issues:

> packageVersion('sf')
[1] ‘1.0.8’

> packageVersion('terra')
[1] ‘1.7.3’

> R.Version()
$platform
[1] "x86_64-w64-mingw32"

$arch
[1] "x86_64"

$os
[1] "mingw32"

$system
[1] "x86_64, mingw32"

$status
[1] ""

$major
[1] "4"

$minor
[1] "1.0"

$year
[1] "2021"

$month
[1] "05"

$day
[1] "18"

$`svn rev`
[1] "80317"

$language
[1] "R"

$version.string
[1] "R version 4.1.0 (2021-05-18)"

$nickname
[1] "Camp Pontanezen"

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

4 participants