如何将类从数据框更改为空间多边形? [英] How to change class from data frame to spatial polygon?

查看:81
本文介绍了如何将类从数据框更改为空间多边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在>如何将数据帧转换为空间坐标。但就我而言,我得到了非常大的数据。

I have found the same in here How to Convert data frame to spatial coordinates. But in my case, I got very large data.

exchange    longitude   latitude
AB  103.3281386 1.594218196
AB  103.3285929 1.593990735
AB  103.3312494 1.591424235
AB  103.3283736 1.594063254
AB  103.3536164 1.622771588
AB  103.3613242 1.627138676
AB  103.3560151 1.619455334
AB  103.3297071 1.593398614
AB  103.3269466 1.596574285
AB  103.3279517 1.593614052
AB  103.3281356 1.593848271
AB  103.3567136 1.620498495
AB  103.3668021 1.63456952
AB  103.359686  1.624821271
AB  103.3308963 1.585290892
AB  103.3319569 1.59104387
AB  103.3307149 1.592006748
AB  103.3283657 1.593675616
AB  103.3314873 1.591186363
AB  103.3319648 1.590585241
AB  103.3321508 1.590422594
AB  103.3318503 1.588685843
AB  103.3324507 1.594547225
AB  103.3442528 1.60909707
AB  103.3292733 1.593461728
AB  103.3288584 1.594312512
AB  103.329041  1.594135083
AB  103.3348961 1.59761749
AB  103.3500524 1.614224612

不可能像他们那样做:

mydf <- structure(list(longitude = c(128.6979, 153.0046, 104.3261, 124.9019, 
126.7328, 153.2439, 142.8673, 152.689), latitude = c(-7.4197, 
-4.7089, -6.7541, 4.7817, 2.1643, -5.65, 23.3882, -5.571)), .Names = c("longitude", 
"latitude"), class = "data.frame", row.names = c(NA, -8L))

所以有人可以帮助我将类从data.frame更改为空间多边形吗?

So anyone can help me to change class from data.frame to spatial polygon?

推荐答案


我的数据与另一篇文章相同。[...]
所以有人可以帮助我将class从data.frame更改为空间
多边形吗?

my data is just the same as the other post [...] So anyone can help me to change classfrom data.frame to spatial polygon?



library(sp)
sp <- SpatialPolygons(list(Polygons(list(Polygon(mydf[, -1])), ID=1)))
class(sp)
# [1] "SpatialPolygons"
# attr(,"package")
# [1] "sp"

,或者,如果您希望 exchange 作为多边形标识符:

or, if you want exchange to be the polygon identifier:

sp <- lapply(split(mydf[, -1], mydf[, 1]), Polygon)
sp <- SpatialPolygons(lapply(seq_along(sp), function(i) { 
  Polygons(list(sp[[i]]), ID = row.names(mydf[!duplicated(mydf[, 1]), ])[i] ) 
}))
# class(sp)
# [1] "SpatialPolygons"
# attr(,"package")
# [1] "sp"

数据:

mydf <- read.table(header=T, text="
exchange   longitude   latitude
AB  103.3281386 1.594218196
AB  103.3285929 1.593990735
AB  103.3312494 1.591424235
AB  103.3283736 1.594063254
AB  103.3536164 1.622771588
AB  103.3613242 1.627138676
AB  103.3560151 1.619455334
AB  103.3297071 1.593398614
AB  103.3269466 1.596574285
AB  103.3279517 1.593614052
AB  103.3281356 1.593848271
AB  103.3567136 1.620498495
AB  103.3668021 1.63456952
AB  103.359686  1.624821271
AB  103.3308963 1.585290892
AB  103.3319569 1.59104387
AB  103.3307149 1.592006748
AB  103.3283657 1.593675616
AB  103.3314873 1.591186363
AB  103.3319648 1.590585241
AB  103.3321508 1.590422594
AB  103.3318503 1.588685843
AB  103.3324507 1.594547225
AB  103.3442528 1.60909707
AB  103.3292733 1.593461728
AB  103.3288584 1.594312512
AB  103.329041  1.594135083
AB  103.3348961 1.59761749
AB  103.3500524 1.614224612")

这篇关于如何将类从数据框更改为空间多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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