Dear people of stackoverflow.
I am trying to calculate the area of each separate polygon of a SPDF. I am trying to make a function of it which allows me to put the data in and get a list of areas out. I am stuck with creating a for loop that returns all areas of my polygons. For the surface calculation of one polygon the following code works:
surfacefirstpolygon <- gArea(inputSPDF[1,1])
This code prints the i's one by one when I run the following code.
polys <- slot(inputSPDF,"polygons")
for(i in 1:length(polys)){
print(i)
}
Then I try to put these pieces of code together in a for loop by doing the following:
polys <- slot(inputSPDF,"polygons")
areasofpolygons <- for(i in 1:length(polys)){
gArea(inputSPDF[i,i])
}
This does not work and gives me the following error.
Error in is.projected(spgeom) :
error in evaluating the argument 'obj' in selecting a method for function 'is.projected': Error in [.data.frame
(x@data, i, j, ..., drop = FALSE) :
undefined columns selected.
Anybody know what is going wrong?
Result =
dd = dim(inputSPDF)
for(i in 1:dd[1]){
areasofpolygons[i] <- gArea(inputSPDF[i,1])
}