2017-01-18 5 views
1

spsample()でシェイプファイルのリストから各シェイプファイルにランダムな点を配置する必要があります。いくつかの不規則なシェイプファイルについては、これは長いプロセスであることが証明されているので、私はspsample()のトラブルメーカである小さな遠隔ポリゴンをドロップすることによって単純にシェイプファイルを作成する必要があります。spsample()でランダムな点を配置するためのシェイプファイルを簡素化

これについては、各ポリゴンのサイズとそれ以外のすべてのポリゴンとの平均距離を知る必要があります。私はこの計算を高速化する方法を探しています、おそらくよりエレガントな(そしてより速い)方法で行うことができます。次のような試みは機能しますが、単純化アルゴリズムとしては時間がかかります。

#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メガバイトに〜100MBのシェープファイルを縮小してから採取するのにかかる時間を削減しました〜2.3秒〜0.11秒。

簡素化する場合は、あなたがbyid = TRUEを使用してgArea()gDistance()機能をvectoriseできるオプションではありません。

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

おかげフィ​​ル!私はsm_simplifyとベクトル化の両方を適用しました。 – Juta

+0

@juta本当に助けてうれしい! – Phil

関連する問題