Why is my fasterize function making a raster filled with 0s for my presence/absence raster?

157 Views Asked by At

I'm trying to produce a presence/absence raster from a set of coordinates. I've turned the coordinates into a polygon, but when I use fasterize it produces a copy of the template shape filled with 0s.

I think there's a problem with the line reading in the presence data (which is just a column of 1s named "yes") because there isn't an error and a raster is made, but I don't know how to fix it and I'm new to working with spatial data.

The code I'm using is:

paraster <- fasterize(paframe, cropbgEU,
                        field = "yes",
                        background = 0) %>% 
  mask(cropbgEU)

These are the details for the polygon paframe, and the template raster cropbgEU:

> paframe 

Simple feature collection with 2103 features and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 1.576116 ymin: 41.1864 xmax: 28.48484 ymax: 70.51157
Geodetic CRS:  WGS 84
First 10 features:
        yes                       geometry
1         1 POLYGON ((17.85235 59.86206...
2         1 POLYGON ((15.26105 59.28842...
3         1 POLYGON ((6.511416 52.38192...
4         1 POLYGON ((22.4303 60.42065,...
5         1 POLYGON ((20.73348 50.34674...
6         1 POLYGON ((21.05561 55.90442...
7         1 POLYGON ((25.47543 61.0417,...
8         1 POLYGON ((24.82326 60.19986...
9         1 POLYGON ((13.92114 53.08867...
10        1 POLYGON ((20.99356 56.31544...
> cropbgEU

class      : RasterLayer 
dimensions : 4260, 4620, 19681200  (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333  (x, y)
extent     : -10, 28.5, 36, 71.5  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : r_tmp_2023-06-04_002524_22764_76822.grd 
names      : layer 
values     : -11.09583, 19.84583  (min, max)

The extent and CRS line up as far as I can tell, which seemed to be the problem in a similar question where the raster was just NAs. There was a different question using the rasterize function, but I don't know how to apply that to fasterize: calling the data with field = paframe@data[,"yes"] returns the error:

Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'mask': trying to get slot "data" from an object (class "sf") that is not an S4 object 

I'm not sure if the problem could be further back when creating the polygon, but the code was:

paframe <- presence %>% 
  filter(yes %in% 1) %>% 
  st_as_sf(coords=c("decimalLongitude","decimalLatitude"),
           crs = 4326) %>% 
  # convert to an sf polygon object by buffering the points
  st_buffer(1) 

With the original presence dataframe looking like:

> head(presence)
    decimalLatitude decimalLongitude yes
26         59.86205         17.85235   1
31         59.28843         15.26104   1
95         52.38192          6.51143   1
128        60.42064         22.43029   1
189        50.34675         20.73349   1
190        55.90442         21.05563   1

For reference, I'm following the instructions in the blog post here by Amy Whitehead.

If anyone knows how to fix this, I would really appreciate it :)

0

There are 0 best solutions below