2

I have a shapefile witch contains several empty polygons. I´m extracting values from raster file based in some polygons of this shapefile. But my loop stops at every empty polygon. Simple solution would be to remove NAs with na.omit(shape@data) but this deletes some xy values as well.

Here is my code. The loop stops once it finds a empty polygon.

all.mun <- NULL
  for (j in mun.codes)
  {
    mun       <- mun.br[mun.br$CODIGO_MUN == j, ] # isolate municipality
    suit.mun  <- extract(suit, mun)               # extract suits from the municipality
    suit.mun  <- do.call("c", suit.mun)           # convert to data frame no matter how many lists the function extract::raster returns
    suit.mean <- mean(suit.mun)                   # mean suitabilitie of the site
    all.mun   <- rbind(all.mun, suit.mean)        # saving result by observation, i.e municipalitie
  }

mun.condes is a vector with a list of 6 digit municipalities codes. mun.br is the shapefile of the municipalities

> names(mun.br)
 [1] "ID"         "NOMEUF"     "REGIAO"     "CODMESO"    "NOMEMESO"   "CODMICRO"  
 [7] "NOMEMICRO"  "NOME_1"     "NOME_2"     "CODIGO_UF"  "CODIGO_MUN" "AREA_1997" 

Here is the error message.

Error in y[i, ] : 
  cannot get a slot ("Polygons") from an object of type "NULL"
1
  • Are you sure this is because of empty polygons (in mun.br, I assume)? Could it happen if there's no match to a CODIGO_MUN and so mun in the first line of the loop is NULL? Hard to tell without your data....
    – Spacedman
    Commented Sep 10, 2019 at 16:26

1 Answer 1

1

As mentioned by Spacedman, it is much easier to address your issue with reproducible data. To make sure you address both the possible concern of emptiness and not matching CODIGO_MUN, I would try

library(raster) # make sure next time that you specify which libraries you are using, I am assuming these suffice
library(rgdal)
all.mun <- NULL
  for (j in levels(as.factor(mun.br$CODIGO_MUN))) # make sure all 'j' match CODIGO_MUN's values
  {
     if (!identical(mun.br[mun.br$CODIGO_MUN == j, ]@polygons, NULL)) {  # if polygon is not NULL
     # if (length(mun.br[mun.br$CODIGO_MUN == j, ]@polygons)>0) { # alternative if conditional if the former does not work
        mun       <- mun.br[mun.br$CODIGO_MUN == j, ] # isolate municipality
        suit.mun  <- extract(suit, mun)               # extract suits from the municipality
        suit.mun  <- do.call("c", suit.mun)           # convert to data frame no matter how many lists the function extract::raster returns
        suit.mean <- mean(suit.mun)                   # mean suitabilitie of the site
        all.mun   <- rbind(all.mun, suit.mean)        # saving result by observation, i.e municipalitie
     }
  }

Note that I am assuming that an object without polygon has a polygon slot equivalent to NULL. You can try identical(...,NA) or identical(...,integer(0)) in case the former does not work, or the one I left commented.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.