5
I have a code that calculates the area of the intersection between two polygons and for that I use lists to store the coordinates of the vertices of the polygons, however there are many polygons and it is taking on average 6h to run the whole code. Do you know any list operation 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)
I replaced the
area.poly
by a that one and the time has dropped enough, but I’ll wait a while to see if someone can decrease the team. If not, you take it. Thank you!– Wagner Jorge