0

I have an Object of class 'SpatialPolygonsDataFrame' with the structure shown below.

What's the secret to getting to the @area slot? Ultimately, I'm trying to filter this file so that I'm only left with polygons >= 64m^2. The resultant shapefile will be imported to OCAD (orienteering mapping software), in an attempt to semi-automatically map a class of vegetation.

I thought I had some understanding of S4 classes, but clearly I have a gap.

among many similar failed attempts:

> xfile@polygons$Polygons@Polygons$Polygon
Error: trying to get slot "Polygons" from an object of a basic class ("NULL") with no slots
> xfile@polygons$Polygons@Polygons
Error: trying to get slot "Polygons" from an object of a basic class ("NULL") with no slots
> xfile@polygons@Polygons
Error: trying to get slot "Polygons" from an object of a basic class ("list") with no slots
> xfile@polygons$Polygons
NULL
> xfile@polygons$'Polygons'@Polygons$'Polygon'@area
Error: trying to get slot "Polygons" from an object of a basic class ("NULL") with no slots
> xfile@polygons$'Polygons'@Polygons
Error: trying to get slot "Polygons" from an object of a basic class ("NULL") with no slots
> xfile@polygons$'Polygons'
NULL
> xfile@polygons$area
NULL
> xfile$polygons
NULL
> xfile@polygons
( the whole file )
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 1 obs. of  1 variable:
  .. ..$ layer: num 1
  ..@ polygons   :List of 1
  .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
  .. .. .. ..@ Polygons :List of 191
  .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
  .. .. .. .. .. .. ..@ labpt  : num [1:2] 513021 4319805
  .. .. .. .. .. .. ..@ area   : num 9.64
  .. .. .. .. .. .. ..@ hole   : logi FALSE
  .. .. .. .. .. .. ..@ ringDir: int 1
  .. .. .. .. .. .. ..@ coords : num [1:19, 1:2] 513018 513025 513025 513024 513024 ...
  .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
  .. .. .. .. .. .. ..@ labpt  : num [1:2] 513053 4319805
  .. .. .. .. .. .. ..@ area   : num 1.85
  .. .. .. .. .. .. ..@ hole   : logi FALSE
  .. .. .. .. .. .. ..@ ringDir: int 1
  .. .. .. .. .. .. ..@ coords : num [1:5, 1:2] 513051 513054 513054 513051 513051 ...

......

.. .. .. .. .. [list output truncated]
  .. .. .. ..@ plotOrder: int [1:191] 18 118 101 168 110 148 61 135 166 90 ...
  .. .. .. ..@ labpt    : num [1:2] 513181 4319787
  .. .. .. ..@ ID       : chr "0"
  .. .. .. ..@ area     : num 38206
  ..@ plotOrder  : int 1
  ..@ bbox       : num [1:2, 1:2] 512793 4319369 513380 4319807
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+proj=utm +zone=13 +datum=WGS84 +units=m +no_defs"

3
  • Have you tried reading the shapefile with the package sf by using x <-st_read("myshapefile.shp")? Perhaps it might be easier that way. – saQuist Feb 3 at 9:25
  • @saQuist - Yes, I'd fiddled with that but did not see anything relating to 'area' in the resulting x, str(s), or attributes(x). I'd actually tried this with the subject file with the same no 'area' info results. My workaround was to write it to disk with st_write() and read it back with shapefile() and voilà, I could then see (but not maneuver to) 'area.' Any hints on how to navigate the shapefile() result as shown above, or how to locate the (hidden?) area slot/location in the result of st_read()? Thanks. – G. Moore Feb 3 at 12:53
  • You can use gArea from the rgeos package to return area for each polygon on a sp class object. – Jeffrey Evans Mar 24 at 2:09
0

Ref Hong Ooi's answer in https://stackoverflow.com/questions/42059378/cant-use-operator-on-s4-objects-stored-in-a-list-in-r, I've worked out that the area value for individual Polygons in this object can be accessed via:

> xfile@polygons[[1]]@Polygons[[1]]@area
[1] 9.642906
> xfile@polygons[[1]]@Polygons[[2]]@area
[1] 1.854405

Now I just need to figure out how to filter on area. I think I see a lapply() fcn in my future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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