1

I have a dataset contains lines and I have imported them into R. I want to take a close look at the coordinates of them and define the identical first and last coordinate of each point if there is some -->(looking for polygon). Therefore, I am using Slot which makes me able to have a close look at the details of the desired object. My final goal is to define the number of identical point coordinates(First and last) for each line in order to discover the number of the potential polygon in my data.

How many of lines objects have potential to be a polygon?

To do so, I have done several steps: In the first step, I read my data into R. The second step, I have used slot to have a close look at the coordinate of each point(Sequence of points represent line object). the third step: I have tried to define the number of identical points but I have faced with an error says CRDs not found

at the following, you can tack a look at the codes

enter library(maptools)
#Read data directly from National Geophysical Data Center (NGDC) coastline 
#extractor. 
shorelinedat="http://www.asdar-book.org/RC1/datasets/auckland_mapgen.dat"
#Assign CRS
llCRS <- CRS("+proj=longlat +ellps=WGS84")
#Read data from mapgen into a SpatialLines object.
auck_shore <- MapGen2SL("auckland_mapgen.dat", llCRS)
#Required code to identify the lines.
lns <- slot(auck_shore, "lines")
table(sapply(lns, function(x) length(slot(x, "Lines"))))

Here is the code in which I faced with the error

#identifying the number of identical coordinates
islands_auck <- sapply(lns, function(x) {
+ crds <- slot(slot(x, "Lines")[[1]], "coords")
+ identical(crds[1, ], crds[nrow(crds), ])
+ })

This is the error

Error in +crds <- slot(slot(x, "Lines")[[1]], "coords") : 
object 'crds' not found
1

The problem I had was related to the syntax in the final code which I modified them in below

islands_auck <- sapply(lns, function(x) {
crds <- slot(slot(x, "Lines")[[1]], "coords")
identical(crds[1, ], crds[nrow(crds), ])
})
table(islands_auck)

so the final result will be

 islands_auck
 FALSE  TRUE 
 16    64 

16 lines without equal first and last coordinate (Closed polygon) 64 lines with closed polygon

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.