Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:39:48+00:00 2026-06-18T07:39:48+00:00

I have a raster stack with 27 rasters in it. I have 27 corresponding

  • 0

I have a raster stack with 27 rasters in it. I have 27 corresponding polygons in a spatial polygon data frame. I want to take polygon[i] overlay it on raster[i], extract and sum the values from raster [i], get a count of the number of cells within the polygon[i] and then divide the sum value by the # of cells. In other words, the raster is a utilization distribution or a kernel density of use. I want to know much use is occurring in the area of the polygon where it is overlapping the raster. I want to divide by the number of cells in the polygon to take into account the size of the polygon.

I have a script that was given to me that does this, only it was written with the intention of extracting data from 1 raster only by any number of spatial polygons in the data frame. It works, its ugly, and I now would like to convert it to something more stream line. I only wish I had someone around me who could help because this might take a while?

This is code Ive been given and my summary of what I think is going on:

msum99Kern07 = SpatialPolygonDataFrame (many polygons)
KERNWolfPIX07m = Raster (this is a single raster, I have 27 rasters I put into a stack

)

#Extracting value from raster to many polygons 
sRISK_Moose07m<- extract(KERNWolfPIX07m, msum99Kern07,df=FALSE,method='bilinear')

#Calculate THE SUM FOR EACH polygon#
sRISK_Moose07m<-unlist(lapply(sRISK_Moose07m, function(x) if (!is.null(x)) sum(x, na.rm=TRUE) else NA ))
sRISK_Moose07m<-as.data.frame(sRISK_Moose07m)

#Im not sure why these next commands are needed Im only guessing
#data.frame(levels) as there are many polygons creating a dataframe to put the info into
ID_SUM_07<-as.data.frame(levels(as.factor(msum07locs$ID2)))
#ADD ID TO THE risk data frame 
sRISK_Moose07m$ID<-ID_SUM_07[,1] 

#NUMBER OF CELLS WITHIN POLYGON EXTRACT CELLS/ POLYGON
NB_SUM2007m<-cellFromPolygon(KERNWolfPIX07m, msum99Kern07)
NB_SUM07m<-unlist(lapply(NB_SUM2007m, function(x) if (!is.null(x)) length(x) else NA ))

#####CONVERT TO DATA FRAME
NB_SUM07m<-as.data.frame(NB_SUM07m)

###ADD THE NB OF CELLS TO THE RISK_SUM FILE###
sRISK_Moose07m$NB_CELLS<-NB_SUM07m[,1]

###DIVIDING VALUE by NB CELLS##
sRISK_Moose07m$DIVID<-sRISK_Moose07m$sRISK_Moose07m/sRISK_Moose07m$NB_CELLS 

Now, I have my spatial polygon data frame with 27 polygons and my raster stack with 27 rasters. I want to select the raster[i] and polygon[i] and extract, sum, and calculate the kernel density of the overlapping area. One side thing to keep in mind, I may get an error because it is possible that the polygon and raster do not overlap…I don’t know how to check for this in R at all.

My script I have started:

moose99kern = spatial polygon data frame 27 moose
Rastwtrial = stack of 27 rasters having the same unique name as the ID in moose99kern

mkernID=unique(moose99kern$id)

for (i in length(mkernID)){
           r = Rastwtrial[Rastwtrial[[i]]== mkernID[i]] #pick frm Rasterstack the raster that has the same name
            mp = moose99kern[moose99kern$id == mkernID[i]] #pick from spatialpolygondataframe the polygon that has the same name 

            RISK_MooseTrial<- extract(r, mp, df=T, method'bilinear')
            risksum = (RISK_MooseTrial, function(x) if (!is.null(x)) sum(x, na.rm=TRUE) else NA )#sum all the values that were extracted from the raster

My script doesn’t even start to work because I don’t know how to index a raster stack. But even still, going through 1 raster/1polygon at a time, Im not sure what to do next in the code. If this is too much for StackOverflow I apologize. Im just seriously stuck and have no where to turn.
Here is test data with 2 individuals for polygons

 dput(mtestpoly) 
    new("SpatialPolygonsDataFrame"
        , data = structure(list(id = structure(1:2, .Label = c("F01001_1", "F07002_1"
    ), class = "factor"), area = c(1259.93082578125, 966.364499511719
    )), .Names = c("id", "area"), row.names = c("F01001_1", "F07002_1"
    ), class = "data.frame")
        , polygons = list(<S4 object of class structure("Polygons", package = "sp")>, 
        <S4 object of class structure("Polygons", package = "sp")>)
        , plotOrder = 1:2
        , bbox = structure(c(6619693.77161797, 1480549.31292137, 6625570.48348294, 
    1485861.5586371), .Dim = c(2L, 2L), .Dimnames = list(c("x", "y"
    ), c("min", "max")))
        , proj4string = new("CRS"
        , projargs = NA_character_

dput(Rastwtest)

new("RasterStack"
    , filename = ""
    , layers = list(<S4 object of class structure("RasterLayer", package = "raster")>, 
    <S4 object of class structure("RasterLayer", package = "raster")>)
    , title = character(0)
    , extent = new("Extent"
    , xmin = 1452505.6959799
    , xmax = 1515444.7110552
    , ymin = 6575235.1959799
    , ymax = 6646756.8040201
)
    , rotated = FALSE
    , rotation = new(".Rotation"
    , geotrans = numeric(0)
    , transfun = function () 
NULL
)
    , ncols = 176L
    , nrows = 200L
    , crs = new("CRS"
    , projargs = NA_character_
)
    , z = list()
    , layernames = "Do not use the layernames slot (it is obsolete and will be removed)\nUse function 'names'"
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:39:50+00:00Added an answer on June 18, 2026 at 7:39 am

    Maybe I miss something , but I think you over complicated the problem. For me you have :

    1. stack of raster : a list of raster : ss
    2. a list of polygons of the same size as ss : polys

    You need to apply extract for each pair(layer,poly) from (ss,polys)

    sapply(1:nlayers(ss), function(i) {
         m <- extract(ss[[i]],polys[i], method='bilinear', na.rm= T)[[1]]
         d <- ifelse (!is.null(m) , sum(m)/length(m), NA)
         d
    })
    

    Here an example of 2 legnths since you don’t give a reproducible example :

    ## generate some data
    library(raster)
    cds1 <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20))
    cds2 <- rbind(c(80,0), c(100,60), c(120,0), c(120,-55), c(80,0))
    ## In your case you need something like SpatialPolygons(moose99kern)
    polys <- SpatialPolygons(list(Polygons(list(Polygon(cds1)), 1), 
                                  Polygons(list(Polygon(cds2)), 2)))
    r   <- raster(ncol=36, nrow=18)
    r[] <- 1:ncell(r)
    r1   <- raster(ncol=36, nrow=18)
    r1[] <- seq(-1,-2,length.out=ncell(r1))
    ss <- stack(r,r1)
    ## density compute
    sapply(1:nlayers(ss), function(i) {
             ## sum of values of the cells of a Raster ss[[i]] covered by the poly polys[i]
             m <- extract(ss[[i]],polys[i], method='bilinear', na.rm= T)[[1]]
             d <- ifelse (!is.null(m) , sum(m)/length(m), NA)
    
    })
    
    [1] 387.815789  -1.494714
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several polygons & I like to extract mean values from several raster
Lets say I have some raster data I want to write to a file..
I have a raster file and I want to plot as a histogram ,i
I have the following data frame: id variable value ID1 1A 91.98473282 ID1 2A
I have a raster image (in Tiff format) and a polygon area in shapefile
I have a 2D raster of say 200pixels x 200pixels I want to subdivide
I have a SpatialPolygonsDataFrame with 120 Polygons and some associated data. Now I’d like
I have a game with a big raster map Now we are using jpeg
I have animal gps data with several locations for each day and regular gaps
I have a raster file (basically 2D array) with close to a million points.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.