2017-01-18 6 views
1

spsample()을 사용하여 shapefile 목록에서 각 shapefile에 임의의 점을 배치해야합니다. 불규칙한 쉐이프 파일의 경우에는 긴 프로세스로 판명되었으므로 spsample()이 문제가되는 작고 먼 폴리곤을 삭제하여 간단히 쉐이프 파일을 작성해야합니다.spsample()을 사용하여 임의의 점을 배치하기위한 shapefile을 단순화합니다.

각 폴리곤의 크기와 다른 모든 다각형과의 평균 거리를 알아야합니다. 이 계산 속도를 높이는 방법을 찾고 있는데 아마도 좀 더 우아하고 빠른 방법으로 할 수있을 것입니다. 다음과 같은 시도가 있지만 알고리즘을 간소화하는 데 너무 많은 시간이 걸립니다.

#program tries to place random points on shapefile shapes[[i]] if it fails after 300 seconds it goes though to simplifying part and swaps the old shapefile with a simplified version. 

d <- shapes[[i]] 
Fdist <- list() 

for(m in 1:dim(d)[1]) { 
     pDist <- vector() 
     for(n in 1:dim(d)[1]) { 
     pDist <- append(pDist, gDistance(d[m,],d[n,])) 
     } 

     Fdist[[m]] <- pDist 
     [email protected]$mean[m]<-mean(Fdist[[m]]) 
     [email protected]$gArea[m]<-gArea(d[m,]) 
    } 

#drop small and remote polygons 

d.1<-d[[email protected]$gArea>=quantile([email protected]$gArea, prob = seq(0, 1, length=11), type=5)[[1]] & ([email protected]$mean<=quantile([email protected]$mean, prob = seq(0, 1, length=11), type=5)[[10]]),] 

#replace with simplified polygon 

shapes[[i]]<-d.1 

나는 모든 제안에 감사드립니다.

답변

2

먼저 폴리곤을 단순화하려고합니다. rmapshaper 패키지 ms_simplify 크게 미끄러 져 다각형 또는 간격 도입하지 않고, 당신의 다각형을 단순화 할 수 있습니다 : 나는 손을 한 모양 파일로

library("rgdal") 
library("rmapshaper") 

big <- readOGR(dsn = ".", "unsimplified_shapefile") 
big_sample <- spsample(big, 1000, type = "stratified") 

small <- rmapshaper::ms_simplify(big, keep = 0.01) 
small_sample <- spsample(small, 1000, type = "stratified") 

을, 나는 ~ 2메가바이트에 ~ 메가 바이트 Shape 파일을 감소에서 샘플링하는 데 걸리는 시간을 감소 ~ 2.3s ~ ~ 0.11s.

단순화 경우는 byid = TRUE를 사용하여 gArea()gDistance() 기능을 벡터화 할 수있는 옵션이 아닙니다 :

library("rgeos") 
[email protected]$area <- gArea(big, byid = TRUE) 
[email protected]$dist <- gDistance(big, byid = TRUE) 
+0

감사 필! 나는 sm_simplify와 vectorization을 모두 적용했고, ** 빨리 ** 많이 실행되었습니다! – Juta

+0

@juta 정말 도움이 된 것을 기쁘게 생각합니다! – Phil