0

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]) 
}
Zuenie
  • 963
  • 2
  • 11
  • 30
  • I figured it might be because list is not able in putting them underneath each other. So I tried the following which also does not work: test <- list() for(i in 1:length(polys)){ test[i] <- gArea(gem_ned_LAEA[i,i]) } – Zuenie Sep 03 '14 at 08:47
  • What is `data` and `gArea`? This is not reproducible! BTW: There is no list comprehension mechanism in R in that way you try to do. Have a look at the [apply functions](http://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r/) to realize such things. – Patrick Roocks Sep 03 '14 at 08:58
  • data is my input data and gArea is a function of the package rgeos which calculates the surface of a polygon. Thank you for the apply link. Will look at it now. Regarding the reproducibility: I am working with an SpatialPolygonDataFrame. It is not that easy to produce one from scratch. In another question that I asked, I linked my script to dropbox and let people download my shapefile from my dropbox. Then I got the comment that this could be unsafe. I do not know how to make it a better reproducible script. – Zuenie Sep 03 '14 at 09:01
  • What is the output of dim(inputSPDF) just before the 'for' loop? – rnso Sep 03 '14 at 10:15
  • Unfortunately your answer below is returning an empty areasofpolygons. When I run the following code: - dd = dim(inputdata) - Running dd returns: [1] 510 58 – Zuenie Sep 03 '14 at 11:14
  • So if i is going beyond 58, an error will be there. Try my answer below and see if it works. – rnso Sep 03 '14 at 11:17

1 Answers1

1

Try:

dd = dim(inputSPDF)
for(i in 1:dd[1]) for(j in 1:dd[2]){
      areasofpolygons[length(areasofpolygons)+1] = gArea(inputSPDF[i,j])
    }
areasofpolygons

The error mentioned in the question probably indicates that i is exceeding the dimensions ("undefined columns selected"). Also, only identical row and column numbers will be accessed if [i,i] is used.

rnso
  • 23,686
  • 25
  • 112
  • 234
  • 1
    To make it a good answer, please add some comments on what this code does and how. – Ilya Luzyanin Sep 03 '14 at 10:39
  • 1
    I have added some information. Feedback from @Zuenie will also be appreciated. – rnso Sep 03 '14 at 11:15
  • When I run these lines it runs pretty long (like 5 minutes) and then returns an empty areasofpolygons. It returns no error. But thank you already for helping me! I do not comprehend why the number of columns would be wrong also in my first script. – Zuenie Sep 03 '14 at 11:23
  • Your loop is not alloting the result. I have edited my code above. Try and let us know the result. – rnso Sep 03 '14 at 11:33
  • got it! Delete the j part and include areasofpolygons in the formula and it works. So firstline: dd = dim(inputSPDF) secondline: for(i in 1:dd[1]){ areasofpolygons[i] <- gArea(inputSPDF[i,1]) } – Zuenie Sep 03 '14 at 11:33
  • Your code actually is not giving the right result. It inspired me to come to my right answer so I accepted it as the right answer. When running your code I get more areas than I have polygons. And it takes a long time compared to my line which I added in the question under result. Thank you very much for helping me! – Zuenie Sep 03 '14 at 11:57