根据分布的点集创建多边形

3

我需要关于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

你的表中是否包含一个标识每组点的列? - eblondel
没有,所有的观察结果都在一个表格中,如下所示:我编辑了我的帖子。 - user26480
1个回答

9

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

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) <- CRS("+init=epsg:4326")


1
是的,我以为你会有一个用于聚类的列,但我在之后看到了你的评论。不确定光栅化是否可行。我将检查聚类是否有帮助。 - eblondel
1
没有文件托管。您应该使用另一个(例如pastebin,dropbox)并粘贴链接。 - eblondel
1
好的,从你的截图中我看到你基于“var1.pred”变量进行了选择,它与“plot(inter1)”不对应,请使用你设置的正确过滤器进行更新。 - eblondel
1
我编辑了我的回答,请查看。希望这有所帮助。 - eblondel
1
我已经更新了代码,依赖于在此处提供的贡献函数“spline.poly”http://gis.stackexchange.com/questions/24827/how-to-smooth-the-polygons-in-a-contour-map/24929#24929。这将替换基于Bezier的平滑处理。 - eblondel
显示剩余11条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接