Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require(rgdal)
- require(rgeos)
- library(sp)
- library(raster)
- library(spatialEco)
- library(spdep)
- Shapefile <- readOGR(".","US_place_2000")
- Shapefile <- sp.na.omit(Shapefile, margin=1) # NAs are present for some reason?
- Touching_List <- gTouches(Shapefile, byid = TRUE, returnDense=FALSE)
- # also tried Touching_List <- poly2nb(Shapefile, queen = FALSE)
- all.length.list <- lapply(1:length(Touching_List), function(from) {
- if(is.null(Touching_List[[from]]==FALSE){
- lines <- rgeos::gIntersection(Shapefile[from,], Shapefile[Touching_List[[from]],], byid = TRUE)
- if (class(lines) == "SpatialCollections") {
- list.Lines <- lapply(1:length(Touching_List[[from]]), function(to) {
- line.single <- rgeos::gIntersection(Shapefile[from,], Shapefile[Touching_List[[from]][to],])
- if (class(line.single) == "SpatialPoints") {
- # Double the point to create a line
- L1 <- rbind(line.single@coords, line.single@coords)
- rownames(L1) <- letters[1:2]
- Sl1 <- Line(L1)
- Lines.single <- Lines(list(Sl1), ID = as.character(to))
- } else if (class(line.single) == "SpatialLines") {
- Lines.single <- line.single@lines[[1]]
- Lines.single@ID <- as.character(to)
- }
- Lines.single
- })
- lines <- SpatialLines(list.Lines)
- }
- l_lines <- sp::SpatialLinesLengths(lines)
- res <- data.frame(origin = from,
- perimeter = perimeters[from],
- touching = Touching_List[[from]],
- t.length = l_lines,
- t.pc = 100*l_lines/perimeters[from])
- res
- }
- })
- all.length.df <- do.call("rbind", all.length.list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement