2017-08-16 14 views
2

ボロノイポリゴンを作成するためのヘルプページを完全に理解しているかどうか分かりません。単純な機能を持つボロノイポリゴンの作成R

library(sf) 

# function to get polygon from boundary box 
bbox_polygon <- function(x) { 
    bb <- sf::st_bbox(x) 

    p <- matrix(
    c(bb["xmin"], bb["ymin"], 
     bb["xmin"], bb["ymax"], 
     bb["xmax"], bb["ymax"], 
     bb["xmax"], bb["ymin"], 
     bb["xmin"], bb["ymin"]), 
    ncol = 2, byrow = T 
) 

    sf::st_polygon(list(p)) 
} 

nc <- st_centroid(st_read(system.file("shape/nc.shp", package="sf")))["BIR79"] 
box <- st_sfc(bbox_polygon(nc)) 
v <- st_voronoi(nc, box) 

plot(v) 

output

それを修正するために、任意のアイデア?

+2

1つの機能のVoronoiポリゴンが意味をなしませんか? – loki

答えて

2

sf doc pagesの例を出発点として、st_voronoi()は、ポイントで構成されるsfオブジェクトでは機能しないようです。

library(sf) 

# function to get polygon from boundary box 

bbox_polygon <- function(x) { 
    bb <- sf::st_bbox(x) 

    p <- matrix(
    c(bb["xmin"], bb["ymin"], 
     bb["xmin"], bb["ymax"], 
     bb["xmax"], bb["ymax"], 
     bb["xmax"], bb["ymin"], 
     bb["xmin"], bb["ymin"]), 
    ncol = 2, byrow = T 
) 

    sf::st_polygon(list(p)) 
} 

nc <- st_read(system.file("shape/nc.shp", package="sf"))["BIR79"] 
nc_centroids <- st_centroid(nc) 
box <- st_sfc(bbox_polygon(nc_centroids)) 

head(nc_centroids) 

各ポイントには別々のジオメトリエントリがあります。

Simple feature collection with 6 features and 1 field 
geometry type: POINT 
dimension:  XY 
bbox:   xmin: -81.49826 ymin: 36.36145 xmax: -76.0275 ymax: 36.49101 
epsg (SRID): 4267 
proj4string: +proj=longlat +datum=NAD27 +no_defs 
    BIR79      geometry 
1 1364 POINT(-81.4982613405682 36.... 
2 542 POINT(-81.125145134236 36.4... 
3 3616 POINT(-80.6857465738484 36.... 
4 830 POINT(-76.0275025784544 36.... 
5 1606 POINT(-77.4105635619488 36.... 
6 1838 POINT(-76.9947769754215 36.... 

これはマルチポイントジオメトリにポイントを兼ね備え:

head(st_union(nc_centroids)) 

出力:ポイントの労働組合の代わりに、元sfオブジェクトの動作を使用して

Geometry set for 1 feature 
geometry type: MULTIPOINT 
dimension:  XY 
bbox:   xmin: -84.05976 ymin: 34.07663 xmax: -75.80982 ymax: 36.49101 
epsg (SRID): 4267 
proj4string: +proj=longlat +datum=NAD27 +no_defs 
MULTIPOINT(-84.0597597853139 35.131067104959, -... 

v <- st_voronoi(st_union(nc_centroids), box) 
plot(v, col = 0) 

enter image description here

元の境界ボックスの代わりに正しい状態境界線を取得する方法は次のとおりです。

plot(st_intersection(st_cast(v), st_union(nc)), col = 0) # clip to smaller box 
plot(nc_centroids, add = TRUE) 

enter image description here

私はマークされたポイントと似た何かをしようとしていると私は、得られたタイルのためのポイントの属性を保存する必要があります。それはまだ分かっていない。

+0

あなたは属性を保持することができましたか?私は同じ問題を抱えている。 – CPhil

+1

私はVoronoiタイルと元のポイントデータの後ろに 'st_join'を使って別の空間結合を行わなければなりませんでした。https://github.com/andybega/r-misc/blob/master/spatial/marked-ポイントツーポリゴン.R;結果はhttps://github.com/andybega/r-misc/blob/master/spatial/marked-points-to-polygons.mdのように表示されます – andybega

関連する問題