I have a code that calculates the area of the intersection between two polygons and for this I use lists to save the coordinates of the vertices of the polygons, however they are many polygons and it takes an average of 6h to run the whole code. Do you know any list operations that can help minimize the procedure?
My code
require(dplyr); require(rgeos); require(sp)
sim.polygons = function(objects, vertex){
polygons = NULL
for(i in 1:objects) polygons[[i]] = matrix(runif(vertex*2), ncol = 2)
return(polygons)
}
teste = function(lista1, lista2, progress = F){
lista1 = lapply(lista1, as, Class = "gpc.poly")
lista2 = lapply(lista2, as, Class = "gpc.poly")
res = matrix(0, nrow = length(lista2), ncol = length(lista1))
for(k in 1 : length(lista1)){
for(l in 1 : length(lista2)){
res[l, k] = area.poly(intersect(lista1[[k]], lista2[[l]])) #Gargalo do código
}
if(progress == T) print(k)
}
res
}
#exemplo
a = sim.polygons(50, 3) #no meu problema objects = 144 e vertex = 3
b = sim.polygons(100, 3) #objects = 114^2 e vertex = 3
teste(a, b, T)