0

I am trying to build a lines shapefile with the contour lines from the 20 meters isobath from along the East African coast.

This contour line of 20 meters has different segments and I have this information on a list, and I am trying to convert this list into a shapefile and merging the different segments of the line.

How could I create a loop to join the different lines of the contour line?

library(maptools)
library(sp)

# ctlns: contour lines
ctlns
[[1]]
[[1]]$level
[1] -20
[[1]]$x
[1] 39.11667 39.10909 39.11667 39.12754 39.11667
[[1]]$y
[1] -5.327083 -5.316667 -5.303509 -5.316667 -5.327083

[[2]]
[[2]]$level
[1] -20
[[2]]$x
 [1] 39.21667 39.20889 39.21667 39.21852 39.22667 39.23068 39.23333 39.24369 39.25000
[10] 39.25311 39.25173 39.25202 39.25000 39.23763 39.23333 39.22281 39.21667
[[2]]$y
 [1] -5.053763 -5.050000 -5.035417 -5.033333 -5.016667 -5.000000 -4.998276 -4.983333
 [9] -4.977907 -4.983333 -5.000000 -5.016667 -5.022381 -5.033333 -5.040000 -5.050000
[17] -5.053763

for(i in 1:length(ctlns))
{
  ctlns[[i]]
  isolines<- data.frame(ctlns[[i]])  
  iso <- cbind(isolines$x,isolines$y)
  L1 <-Line(iso)
  Ls1 = Lines(list(L1), ID = "a")
  iso_spatial<-mapply(cbind, list(L1))
}

I am new using spatial data on R.

1

Try this:

 iso_spatial <- ContourLines2SLDF(ctlns)

See the source of ?ContourLines2SLDF to see how it's done, and how you might restructure (if needed).

Also see ?rasterToContour in the raster package for a more convenient wrapping.

1

What is the class of the elements in ctlns? If they are SpatialLines you can use do.call, with rbind, to combine them into a single sp SpatialLines object. eg., do.call("rbind", ctlns)

However, I am assuming that this is the result of a specific function and a specific class object where "level" is the contour interval and x, y are vertices coordinates. You could loop through this data structure using lapply and build a list of SpatialLinesDataFrame (see ?SpatialLines and ?SpatialLinesDataFrame) objects and then use do.call, in the same way, to combine the contour lines. In this way you can maintain the contour interval as a column. This is likely what the maptools ContourLines2SLDF function that @mdsumner mentions does.

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.