2012-08-30 7 views
14

패키지 sp은 서로 다른 공간 개념 (점, 선, 다각형)에 대해 다양한 클래스를 제공합니다. 일부 클래스의 경우 피쳐 좌표에 액세스하는 것이 간단합니다. . 모든 예제는 각 클래스 도움말 페이지에서 가져 왔습니다. SpatialPolygons를 들어SpatialPolygons 및 기타 sp 클래스의 피쳐 좌표 추출

l1 = cbind(c(1,2,3),c(3,2,2)) 
l1a = cbind(l1[,1]+.05,l1[,2]+.05) 
l2 = cbind(c(1,2,3),c(1,1.5,1)) 
Sl1 = Line(l1) 
Sl1a = Line(l1a) 
Sl2 = Line(l2) 
S1 = Lines(list(Sl1, Sl1a), ID="a") 
S2 = Lines(list(Sl2), ID="b") 
Sl = SpatialLines(list(S1,S2)) 
coordinates(Sl) 
# [prints a list of two with corresponding segments] 

, coordinates() 반환 다각형 센터 아래 입증한다.

Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2))) 
Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2))) 
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5))) 
Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE) 

Srs1 = Polygons(list(Sr1), "s1") 
Srs2 = Polygons(list(Sr2), "s2") 
Srs3 = Polygons(list(Sr3, Sr4), "s3/4") 
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3) 
coordinates(SpP) 
     [,1]  [,2] 
[1,] 2.696970 3.545455 
[2,] 3.666667 2.333333 
[3,] 6.133333 3.933333 

기능 좌표를 추출하는 공통 패키지에는 편리한 기능이 있습니까? 내가 SpatialPolygons에 대한 함수를 생각해 냈지만, 더 잘 테스트되었고 일관성이 있으며, 아마도 대부분의/모든 sp 클래스에 걸쳐있는 것을 찾고 있습니다. 내가 생각할 수있는

getEdges <- function(x) { 
    stopifnot(class(x) == "SpatialPolygons") 
    lapply([email protected], function(y) { 
       [email protected][[1]]@coords 
      }) 
} 
getEdges(SpP) 
# [returns a list of three, coordinates in a matrix] 
+1

내가 당신에게 아주 좋은 책 추천 : 세리에 "사용 R에"R과 응용 공간 데이터 분석 "귀하의 예제 데이터를 사용하여 ! " -> http://www.springerlink.com/content/978-0-387-78171-6#section=147788&page=1 – Pop

+0

일반적으로'@ property'를 사용하여 물건을 추출하는 것은 바람직하지 않습니다. 이것은 개체의 내부 조직이며 예고없이 변경 될 수 있습니다. –

답변

14

가장 좋은 방법은 ggplot2에서 fortify 기능을 사용하는 것입니다. fortify은 일반 R 객체 (예 : lm 등)를 data.frame으로 변환하는 방법이있는 제네릭 함수로 ggplot2은 플로팅에 사용할 수 있습니다. 전체 목록은 제공 : 당신은이 SpatialPolygons* 객체의 fortify 기능을 포함 볼 수 있습니다

> ggplot2:::fortify. 
ggplot2:::fortify.cld                 
ggplot2:::fortify.confint.glht               
ggplot2:::fortify.data.frame                
ggplot2:::fortify.default                
ggplot2:::fortify.glht                 
ggplot2:::fortify.Line 
ggplot2:::fortify.Lines 
ggplot2:::fortify.lm 
ggplot2:::fortify.map 
ggplot2:::fortify.NULL 
ggplot2:::fortify.Polygon 
ggplot2:::fortify.Polygons 
ggplot2:::fortify.SpatialLinesDataFrame 
ggplot2:::fortify.SpatialPolygons 
ggplot2:::fortify.SpatialPolygonsDataFrame 
ggplot2:::fortify.summary.glht 

. 결과

> obj = fortify(SpP) 
    long lat order hole piece group id 
1  2 2  1 FALSE  1 s1.1 s1 
2  1 4  2 FALSE  1 s1.1 s1 
3  4 5  3 FALSE  1 s1.1 s1 
4  4 3  4 FALSE  1 s1.1 s1 
5  2 2  5 FALSE  1 s1.1 s1 
6  5 2  1 FALSE  1 s2.1 s2 
7  2 2  2 FALSE  1 s2.1 s2 
8  4 3  3 FALSE  1 s2.1 s2 
9  5 2  4 FALSE  1 s2.1 s2 
10 4 5  1 FALSE  1 s3/4.1 s3/4 
11 10 5  2 FALSE  1 s3/4.1 s3/4 
12 5 2  3 FALSE  1 s3/4.1 s3/4 
13 4 3  4 FALSE  1 s3/4.1 s3/4 
14 4 5  5 FALSE  1 s3/4.1 s3/4 
15 5 4  6 TRUE  2 s3/4.2 s3/4 
16 5 3  7 TRUE  2 s3/4.2 s3/4 
17 6 3  8 TRUE  2 s3/4.2 s3/4 
18 6 4  9 TRUE  2 s3/4.2 s3/4 
19 5 4 10 TRUE  2 s3/4.2 s3/4 

및 플로팅 :

require(ggplot2); theme_set(theme_bw()) 
ggplot(aes(x = long, y = lat, group = group), data = obj) + geom_path() 

enter image description here