Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Simple question: how best to do this? I've found one way that works, but this is very slow (when x is a SpatialPolygonsDataFrame):

y <- fortify(x) 
n.verts <- aggregate(y, by=list(y$id), FUN=length)[,2]
x@data$nverts <- n.verts 
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

If the SpatialPolygonsDataFramehas just one part for each polygon, you can use the following:

# test data from www.gadm.org: administrative boundaries Liechtenstein/Level 1
R> load("LIE_adm1.RData")

# check number of parts
R> sapply(gadm@polygons, function(x) length(x))
 [1] 1 1 1 1 1 1 1 1 1 1 1

# number of vertices
R> sapply(gadm@polygons, function(y) nrow(y@Polygons[[1]]@coords))
 [1]  307  169  383  675   64  572  431  617 1389  425  456
share|improve this answer
    
That solved it, many thanks! –  RobinLovelace Apr 16 '13 at 14:19
    
ps system.time(sapply(dshe@polygons, function(y) nrow(y@Polygons[[1]]@coords))) tells me your method is several orders of magnitude faster than mine. –  RobinLovelace Apr 16 '13 at 14:25
    
because fortify() handles the more general case. –  rcs Apr 16 '13 at 14:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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