将R中的SpatialPointsDataFrame转换为SpatialLinesDataFrame [英] Convert SpatialPointsDataFrame to SpatialLinesDataFrame in R

查看:11
本文介绍了将R中的SpatialPointsDataFrame转换为SpatialLinesDataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HURDAT数据集绘制飓风轨迹。 我目前已经在R中生成了一个SpatialPointsDataFrame对象,该对象类似于2004年。

    > str(cluster.2004.sdf)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 2693 obs. of  4 variables:
  .. ..$ Sid      : int [1:2693] 1331 1331 1331 1331 1331 1331 1331 1331 1331 1331 ...
  .. ..$ clusterid: num [1:2693] 2 2 2 2 2 2 2 2 2 2 ...
  .. ..$ name     : Factor w/ 269 levels "","ABBY      ",..: 6 6 6 6 6 6 6 6 6 6 ...
  .. ..$ WmaxS    : num [1:2693] 78.9 82.8 80.9 70.9 76.9 ...
  ..@ coords.nrs : num(0) 
  ..@ coords     : num [1:2693, 1:2] 754377 612852 684956 991386 819565 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "lon" "lat"
  ..@ bbox       : num [1:2, 1:2] -3195788 1362537 4495870 9082812
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "lon" "lat"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
  .. .. ..@ projargs: chr "+proj=lcc +lat_1=60 +lat_2=30 +lon_0=-60 +ellps=WGS84"

    > summary(cluster.2004.sdf)
Object of class SpatialPointsDataFrame
Coordinates:
         min     max
lon -3195788 4495870
lat  1362537 9082812
Is projected: TRUE 
proj4string :
[+proj=lcc +lat_1=60 +lat_2=30 +lon_0=-60 +ellps=WGS84]
Number of points: 2693
Data attributes:
      Sid         clusterid             name         WmaxS       
 Min.   :1331   Min.   :1.000   IVAN      :517   Min.   : 14.83  
 1st Qu.:1334   1st Qu.:2.000   FRANCES   :403   1st Qu.: 31.35  
 Median :1337   Median :3.000   JEANNE    :379   Median : 50.04  
 Mean   :1337   Mean   :2.898   KARL      :283   Mean   : 61.66  
 3rd Qu.:1339   3rd Qu.:4.000   DANIELLE  :271   3rd Qu.: 90.40  
 Max.   :1341   Max.   :4.000   BONNIE    :253   Max.   :142.52  
                                (Other)   :587 
每个风暴都有唯一的风暴ID引用,标记为"SID"。 我要按"SID"对SpatialPointsDataFrame进行分组,并将所有点转换为一条直线。

我尝试了plyr包中的ddply,但坦率地说,我不知道我在做什么。 我知道我可以通过循环数据框中的每一行并将坐标附加到列表,然后使用SP包中的Lines函数转换该列表来实现这一点。

然而,我更喜欢一种更具R色彩的转换方式。 谢谢 理查德

推荐答案

mdSumner解决方案的问题是结果数据。Frame必须为每行一行,但在他的代码中,每个点都有一行。更正后的代码为:

## example data
d <- data.frame(x=runif(7), y=runif(7), id = c(rep("a", 3), rep("b", 4)))

library(sp)    
coordinates(d) <- ~x+y

## list of Lines per id, each with one Line in a list
x <- lapply(split(d, d$id), function(x) Lines(list(Line(coordinates(x))), x$id[1L]))

# the corrected part goes here:
lines <- SpatialLines(x)
data <- data.frame(id = unique(d$id))
rownames(data) <- data$id
l <- SpatialLinesDataFrame(lines, data)

因此,基本上问题是您必须为这些行创建data.frame,按id分组(每行一行)。在上面的情况下,除了id没有数据时,这是非常容易的。但是,如果您需要对原始SpatialPointDataFrame中的一些其他数据进行分组,则必须使用一些分组函数,如tapplyaggregate或使用My Favorite-sqldf

data <- sqldf('
select id, max(something), sum(something_else)
from d
group by id
')

这篇关于将R中的SpatialPointsDataFrame转换为SpatialLinesDataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆