2
votes

I hope the same question wasn't asked elsewhere, because, even if it is a basic exercise, I couldn't find it in other questions...

I've got a raster, derived from the rasterization of a vector; in this raster, pixels corresponding to the polygons were assigned a number (e.g. all the pixels belonging to polygon A were assigned the number 53; the pixels belonging to polygon H were assigned the number 102). Please notice that the polygons in the original vector do not have an ID code (so, polygon "A" or "H" is something I invented now). This is the structure of the raster I've got:

> structure(lodi_C00)
class       : RasterLayer 
dimensions  : 1994, 1932, 3852408  (nrow, ncol, ncell)
resolution  : 30, 30  (x, y)
extent      : 516000, 573960, 4990200, 5050020  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : C:\Users\Laura\Desktop\MSc thesis\Dati\3_Segmentation\Lodi_segmented\lodi_single_classes_rasterized\lodiC00.tif 
names       : lodiC00 
values      : 1, 152  (min, max)
attributes  :
        ID   category
 from:   0           
 to  : 152 M158200079

Now I need to select from this raster only the pixels corresponding to some polygons, so pixels with specific values. I've got a list of the pixel values I want to select (there are 106 values):

> C00_trainingrows
  [1] 152  62  74  40 102  36  14  78  79  31  35  12   9 137   7   8  43 101  52 133  59 123  23  28  49  93  11  63  72 125   1  69  86
 [34] 100 112 145 128 135  32  99  34  44  61  66  47  50 131 129  95 108  76  38 109  39  64  37  53 122  57  21  55 111 113  33  91  77
 [67] 132  51  88  10  13 107  24  65 105  60  87  71 147 149  17 139  25 120 124 114  27  45 103   6  84  29 144 141  22  26   5  16  75
[100]   2  41  42 126 118  54 110

So, I want to have a new raster in which only the pixels with the values "152", "62", etc are kept.

I know that when selecting only one value (e.g. 152), this works:

lodi_C00_training <- lodi_C00 == 152
writeRaster(lodi_C00_training, "lodi_C00_training", format="GTiff", progress="text", overwrite=TRUE)

However, I need to have all the 106 values. Any suggestion on how to do it?

1
@LeDYoM, this is not my homework. But even if it was, what's the problem? In any case, it's a problem I'm not able to solve by myself and for which I'm asking help to more experienced programmers.Laura Paladini
Of course, but please, be more concrete in your question and adjust the tags, so experts in the field can come and answerLeDYoM
@LeDYoM, I added the tags "spatial" and "geospatial", I hope this improves my post. I'm open to any other suggestion, I'm relatively new to this forum, so I may be missing important tags...Laura Paladini
Sorry, but the language or technology you are using would be a good start.LeDYoM
Wouldn't it be more efficient to filter your input polygons beforehanf to keep only the ones you're interested in , and then rasterize just those ?lbusett

1 Answers

2
votes

If your raster is not too large you can try this:

#Dummy data start
lodi_C00          <- raster(matrix(1:9))
C00_trainingrows  <- c(1,5,9)

#copy your raster
lodi_C00_training <- lodi_C00

#set all pixels that are not contained in your vector to NA
lodi_C00_training[!(lodi_C00[] %in% C00_trainingrows)] <- NA

EDIT after comment:

You can try something like this:

#define function 
#(setting C00_trainingsrows as a fixed paramater might not be the best practice)

selectPixels <- function(x) {
if(!is.na(x)) { 
  if(!(x %in% C00_trainingrows)){
    x <- NA
  }
} 
return(x)
}


 #Set up a cluster with two cores
 beginCluster(2)
 lodi_C00_training <- clusterR(lodi_C00, 
                                fun=calc,
                                args=list(fun=selectPixels),
                                export='C00_trainingrows')
 endCluster()