【问题标题】:Create polygon from set of points distributed从分布的点集创建多边形
【发布时间】:2014-11-23 03:07:25
【问题描述】:

我需要 R 语言方面的帮助

来自我的代码:

inter1= read.table("C:/inter.csv", header=TRUE)
inter1$xx<-inter1$long
inter1$yy<-inter1$lat
coordinates(inter1) = ~long + lat
#Plot the results:
plot(inter1)

我有这个情节:http://i.stack.imgur.com/98aTf.png

我现在正在寻找绘图上的每组点绘制一个多边形,我不知道我必须经过什么过程才能到达那里,谢谢你的帮助

inter.csv:

long    lat var1.pred
1   4.2 19  31.8216045615229
2   4.3 19  31.913824396486
3   4.4 19  32.0090783396173
4   4.5 19  32.1067681024233
5   4.6 19  32.2061094352961
6   4.7 19  32.3061148156713
7   4.8 19  32.4055837134796
8   4.9 19  32.503104196147
9   5   19  32.5970697606984
10  5.1 19  32.6857147918646
11  5.2 19  32.767170733855
12  5.3 19  32.8395428348418
13  5.4 19  32.9010042955024
14  5.5 19  32.9499012300441
15  5.6 19  32.9848587133105
16  5.7 19  33.004876178167
17  5.8 19  33.0094002932703
18  5.9 19  32.998365567474
19  6   19  32.9721970820907
20  6.1 19  32.9317751315546
21  6.2 19  32.8783669584517
22  6.3 19  32.8135349988031
23  6.4 19  32.7390332831422
24  6.5 19  32.6567036402505

【问题讨论】:

  • 您的表格是否包含标识每组点的列?
  • 不,观察都在一个表中,如下:我编辑了我的帖子

标签: r polygon point


【解决方案1】:

在您的情况下,一种解决方案是通过中间光栅化,然后将其多边形化。可以平滑多边形以获得更好的可视化效果。见下方代码

inter1= read.table("inter.csv", header=TRUE)

#add a category (required for later rasterizing/polygonizing)
inter1 <- cbind(inter1, cat = rep(1L, nrow(inter1)),stringsAsFactors = FALSE)

#convert to spatial points
coordinates(inter1) = ~long + lat

#gridify your set of points
gridded(inter1) <- TRUE

#convert to raster
r <- raster(inter1)

#convert raster to polygons
sp = rasterToPolygons(r, dissolve = T)

#addition transformation to distinguish well the set of polygons
polys <- slot(sp@polygons[[1]], "Polygons")
output <- SpatialPolygons(
  Srl = lapply(1:length(polys),
               function(x){
                 p <- polys[[x]]

                 #applying spline.poly function for smoothing polygon edges
                 px <- slot(polys[[x]], "coords")[,1]
                 py <- slot(polys[[x]], "coords")[,2]
                 bz <- spline.poly(slot(polys[[x]], "coords"),100, k=3)
                 bz <- rbind(bz, bz[1,])
                 slot(p, "coords") <- bz               

                 # create Polygons object
                 poly <- Polygons(list(p), ID = x)
                 return(poly)
               }),
  proj4string = CRS("+init=epsg:4326")
)

#plot
plot(sp, border = "gray", lwd = 2) #polygonize result
plot(output, border = "red",  add = TRUE) #smoothed polygons

注意:您有长/纬度坐标 (crs = EPSG:4326),所以我制作了这个示例,以便您可以在构建过程中查看在何处指定空间多边形的投影。如果此时没有指定proj4string,在创建output对象做proj4string(output) &lt;- CRS("+init=epsg:4326")之后仍然可以这样做

【讨论】:

  • 是的,我以为你会有一个专栏来聚集,但我后来看到了你的评论。不确定光栅化是否可行。我要检查集群是否有帮助。
  • 没有文件托管。您应该使用另一个(例如 pastebin、Dropbox)并粘贴链接。
  • 好的,从您的屏幕截图中,我看到您根据var1.pred 变量进行了选择,它与plot(inter1) 不对应,请使用您设置的正确过滤器进行更新。
  • 我已经编辑了我的答案,请检查。希望这会有所帮助
  • 我可以从rgeos 向您推荐gSimplify(请参阅上面的更新)。它们可能是其他工具(如果我看到更好的东西,我会不断更新)。如果解决了你的问题,请投票
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-25
  • 2021-09-04
  • 2011-12-05
  • 2019-08-10
  • 2019-04-03
  • 2020-06-07
相关资源
最近更新 更多