0

I'm trying to create a raster using the following data frame (without NAs), with the latitude, longitude and values extracted from a HDF4 file generated by SeaDAS:

> dim(df)
[1] 10538622        3
> head(df)
          x         y         z
1 -61.38239 -30.91730 -0.015534
2 -61.38239 -30.91730 -0.015534
3 -61.38239 -30.91730 -0.015534
4 -61.38239 -30.91730 -0.015534
5 -61.37815 -30.91839 -0.015534
6 -61.37815 -30.91839 -0.015534

but I get the following error:

>   ras = rast(df, type = "xyz", crs = prj, digits = 6)
Error in matrix(NA, nrow = ncell(r), ncol = nlyr(r)) : 
  invalid 'nrow' value (too large or NA)
In addition: Warning message:
In matrix(NA, nrow = ncell(r), ncol = nlyr(r)) :
  NAs introduced by coercion to integer range

I already tried to reduce the size of the data frame using a bounding box, but even with only 23530 points I still get same error. There are a few points very close to each other (hence the identical points 1, 2, 3 and 4 in the df) and the grid is probably not regular. What could be the issue here?

Thanks, Matheus

  • Greetings. It would be great if you could share a reproducible dataset. You can share the `dput` of your data by following the steps in this video: https://youtu.be/3EID3P1oisg – Shawn Hemelstrand Feb 25 '22 at 18:54
  • Thank you Shawn. How can I share it here, considering it is a very large output file (a text file of 16533 lines, with a size of 1.2 mb)? – matheushtavares Feb 25 '22 at 19:40
  • You can subset or use the head of data, then use dput. For example: `df2 <- df[20:30, ]` and then `dput(df2)`. Then just copy and paste what it spits out here. – Shawn Hemelstrand Feb 26 '22 at 01:29
  • 1
    Thank you again, Shawn. I was able to solve it using Robert Hijmans's answer. – matheushtavares Feb 28 '22 at 17:28

1 Answers1

1

If the values are not on a regular grid, you should perhaps use rasterize instead. I am guessing that a regular grid is detected but at an extremely high spatial resolution. The development version of terra now should show a warning before the error.

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63