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

Count (length) of points with rasterize #1127

Closed
skiptoniam opened this issue Apr 21, 2023 · 1 comment
Closed

Count (length) of points with rasterize #1127

skiptoniam opened this issue Apr 21, 2023 · 1 comment

Comments

@skiptoniam
Copy link

skiptoniam commented Apr 21, 2023

Dear Dev Team,

First of all hats off for the wonderful package. Love it!

I have an issue when trying to rasterize points and get a count of point per cell.
I've had tried a few different approaches but always end up with the same error:

test <- rasterize(sites,r,fun=function(x,...){length(x)})
=====================================Error: [writeValues] incorrect number of values (too few) for writing

Here is a reproducible example:

library(terra)
r <- rast(xmin=-180, xmax=180,
          ymin=-90, ymax=90,
          nrows=18000, ncols=36000)

x <- runif(100000,-180,180)
y <- runif(100000, -90,90)

sites <- cbind(x,y)

test <- rasterize(sites,r,fun=function(x,...){length(x)})

I have also tried with fun=length and the same error:

test <- rasterize(sites,r,fun=length)
======================================Error: [writeValues] incorrect number of values (too few) for writing

I'm using terra version 1.7.29 on linux

Any suggestions would be great. I did notice I could get the function work fine with the raster package.

Here is my session info:

sessionInfo()
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.1 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
[1] LC_CTYPE=en_AU.UTF-8 LC_NUMERIC=C LC_TIME=en_AU.UTF-8 LC_COLLATE=en_AU.UTF-8
[5] LC_MONETARY=en_AU.UTF-8 LC_MESSAGES=en_AU.UTF-8 LC_PAPER=en_AU.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] raster_3.6-14 sp_1.6-0 terra_1.7-29

loaded via a namespace (and not attached):
[1] Rcpp_1.0.10 spatstat.data_3.0-0 rstudioapi_0.14 magrittr_2.0.3 splines_4.2.2
[6] spatstat.utils_3.0-1 ppmFit_0.1.0 lattice_0.20-45 R6_2.5.1 rlang_1.0.6
[11] foreach_1.5.2 tools_4.2.2 parallel_4.2.2 rgdal_1.6-6 grid_4.2.2
[16] glmnet_4.1-6 cli_3.6.0 deldir_1.0-6 iterators_1.0.14 remotes_2.4.2
[21] survival_3.4-0 brio_1.1.3 Matrix_1.5-1 codetools_0.2-19 testthat_3.1.6
[26] spatstat.geom_3.0-6 shape_1.4.6 polyclip_1.10-4 compiler_4.2.2

@rhijmans
Copy link
Member

Thanks very much for reporting this bug. Fixed now.

With your version (and the current CRAN version) you should be able to get what you are after with

test <- rasterize(sites,r,fun="count"}

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

2 participants