rterra

Converting spatvect to spatrast and filling polygons as 1


My goal here is to convert my DF which is built from KUD polygons into a raster where the polygon is filled with 1s and everything outside the polygon is 0. I keep getting only the points of the polygon (polygon boarder filled) as shown bellow.

enter image description here

str(KUD)
'data.frame':   1108 obs. of  13 variables:
 $ X      : num  2.12 2.13 2.13 2.13 2.13 ...
 $ long   : num  151 151 151 151 151 ...
 $ lat    : num  -33.3 -33.2 -33 -32.8 -32.8 ...
 $ order  : int  1 2 3 4 5 6 7 8 9 10 ...
 $ hole   : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
 $ piece  : int  1 1 1 1 1 1 1 1 1 1 ...
 $ id     : int  1 1 1 1 1 1 1 1 1 1 ...
 $ group  : num  1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 ...
 $ Contour: chr  "50%" "50%" "50%" "50%" ...
 $ Month  : chr  "Feb" "Feb" "Feb" "Feb" ...
 $ sex    : chr  "F" "F" "F" "F" ...
 $ data   : chr  "all" "all" "all" "all" ...
 $ pa     : num  1 1 1 1 1 1 1 1 1 1 ...

This is the original data which I have just split by sex and month and shortened to share here. I then converted to a spatvect

chosen_data_spat <- vect(chosen_data, geom = c("long", "lat"), crs = "EPSG:4326")

then

crs(chosen_data_spat) #my subsetted data
raster_res <- 0.1 #blank raster
r <- rast(ext(chosen_data_spat), resolution = raster_res, crs = crs(chosen_data_spat))
kud_raster <- rasterize(chosen_data_spat, r, field = 1)
plot(kud_raster) #see picture for outcome of this

Then I would have 1 and 0, 1 assigned to the converted polygons and 0 to the rest of the raster. Then I want to mask the raster of my KUD's to an extent to only have data that is found across both.

Data:

> long  lat Month   sex

152.0517255 -32.91524133    May M
152.0448803 -32.91124951    May M
151.8934239 -32.68456081    May M
151.8813066 -32.45787211    May M
151.94668   -32.23118341    May M
152.0517255 -32.06411389    May M
152.0901333 -32.0044947 May M
152.2784142 -31.78818822    May M
152.2940711 -31.777806  May M
152.4451709 -31.5511173 May M
152.491302  -31.3244286 May M
152.479521  -31.0977399 May M
152.4780232 -30.8710512 May M
152.5051029 -30.73423888    May M
152.5220439 -30.6443625 May M
152.6545947 -30.41767379    May M
152.7317916 -30.33063796    May M
152.9584803 -30.20623356    May M
153.185169  -30.23359474    May M
153.4118577 -30.37106937    May M
153.4510018 -30.41767379    May M
153.5645545 -30.6443625 May M
153.5817139 -30.8710512 May M
153.5207048 -31.0977399 May M
153.4118577 -31.32003691    May M
153.4092635 -31.3244286 May M
153.2147208 -31.5511173 May M
153.185169  -31.59897029    May M
153.0466397 -31.777806  May M
152.9675693 -32.0044947 May M
152.9584803 -32.03992273    May M
152.918596  -32.23118341    May M
152.8451358 -32.45787211    May M
152.7317916 -32.62148744    May M
152.678439  -32.68456081    May M
152.5051029 -32.83507694    May M
152.3305351 -32.91124951    May M
152.2784142 -32.93388139    May M
152.0517255 -32.91524133    May M
152.0517255 -32.91524133    May M
150.0115271 -35.69769436    May M
149.9614815 -35.63151393    May M
149.8595191 -35.40482523    May M
149.8447276 -35.17813653    May M
149.907311  -34.95144782    May M
150.0115271 -34.78947252    May M
150.0566646 -34.72475912    May M
150.2382159 -34.54806257    May M
150.3575987 -34.49807042    May M
150.4649046 -34.44182994    May M
150.6915933 -34.37434725    May M
150.918282  -34.398485  May M
151.0380429 -34.49807042    May M
151.1449707 -34.60098306    May M
151.2177159 -34.72475912    May M
151.2958613 -34.95144782    May M
151.3112997 -35.17813653    May M
151.2678176 -35.40482523    May M
151.1449707 -35.61824239    May M
151.1369441 -35.63151393    May M
150.918282  -35.84150026    May M
150.8745397 -35.85820263    May M
150.6915933 -35.93891579    May M
150.4649046 -35.96985953    May M
150.2382159 -35.89676207    May M
150.1809311 -35.85820263    May M
150.0115271 -35.69769436    May M
150.0115271 -35.69769436    May M

or https://pastebin.com/adQUpw45

Thanks in advance for your help!


Solution

  • Your example data

    f <- "https://pastebin.com/raw/adQUpw45"
    x <- read.table(f, header=T)
    

    You have 2 polygons (or 2 polygon parts), and you need to identify these.

    x$id <- c(rep(1, 40), rep(2, 28))
    

    Now you can do

    library(terra)
    v <- vect(as.matrix(x[, c("id", "long", "lat")]), crs = "EPSG:4326", 
                  type="polygons", atts=data.frame(id=1:2))
    

    And rasterize

    r <- rast(v, res=.01)
    r <- rasterize(v, r)
    
    plot(r)
    lines(v, lwd=4, col="gray")
    points(v, col="red")
    

    enter image description here