0

I have such data of points which could be stored with their radii.

cdf = rbind(matrix(c(100, 200, 10), ncol = 3), matrix(c(400, 300, 20), ncol = 3))
di = c("1", "2")

spcs <- list()
for (c in 1:dim(cdf)[1]) {
  spc <- spCircle(radius = cdf[c,3], centerPoint = c(x=cdf[c,1], y=cdf[c,2]), spID = di[c])
  spcs <- c(spcs, spc)
}

Now, how to add it as a polygon (as other "normal" polygons) to the whole processing of creation of shapefile layers?

I do with typical polygons this way:

sps <- SpatialPolygons(spcs)
sdf <- SpatialPolygonsDataFrame(sps, data.frame(x = rep(NA, length(sps)), row.names = di))

rgdal::writeOGR(obj = sdf, dsn = title, layer = name, 
                  driver = "ESRI Shapefile", overwrite_layer = FALSE, verbose = TRUE)

but here the format is not the same. For example:

Error in SpatialPolygons(spcs) : no slot of name "area" for this object of class "SpatialPolygons"

How to add it as polygon

2
  • Is this spCircle function from the sampSurf package?
    – Spacedman
    Commented Jun 4, 2019 at 22:15
  • Yes, but I wonder to use something else as well.
    – Peter.k
    Commented Jun 5, 2019 at 12:03

1 Answer 1

1

If you read the documentation for spCircle, specifically the Value section:

    Value:

         A list with the following components...

    spCircle : The "‘SpatialPolygons’" polygon object.

    location : The "‘SpatialPoints’" point object.

you'll see it returns a list with two components. It looks like you want the polygon object. In your loop make a list with just the spc$spCircle component instead of the entire spc object:

> spcs <- list()
> for (c in 1:dim(cdf)[1]) {
   spc <- spCircle(radius = cdf[c,3], centerPoint = c(x=cdf[c,1], y=cdf[c,2]), spID = di[c])
   spcs <- c(spcs, spc$spCircle)
 }

Now spcs a list of 2 SpatialPolygons objects each of length 1. To combine them into a SpatialPolygonsDataFrame with two rows you first have to row-bind them into a single SpatialPolygons object of length 2. Then you can SpatialPolygonsDataFrame them with some attributes:

> SpatialPolygonsDataFrame(do.call(rbind,spcs),data.frame(id=1:2,cdf))
class       : SpatialPolygonsDataFrame 
features    : 2 
extent      : 90.00503, 420, 190.0013, 319.9975  (xmin, xmax, ymin, ymax)
crs         : NA 
variables   : 4
names       : id,  X1,  X2, X3 
min values  :  1, 100, 200, 10 
max values  :  2, 400, 300, 20 
1
  • Thanks! It works this way!
    – Peter.k
    Commented Jun 5, 2019 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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