如何创建一个多边形并使其与R中的空间数据进行坐标

时间:2014-11-26 19:12:43

标签: r spatial shapefile

我想知道是否有办法在绘图空间数据图像中以交互方式设置多边形并获取它的坐标。我知道有locator()函数,但我想设置一个圆或正方形并得到它的坐标。

2 个答案:

答案 0 :(得分:0)

问题已经更新,所以也许我现在更好地了解目标。您想要绘制具有大量边界的区域,然后单击顶部的多边形?使用评论中提供的形状文件,您可以执行以下操作:

library(spatstat)
library(sp)
library(maptools)

obj <- readShapeSpatial("mun_lin.shp")
region <- unmark(as(obj, "psp"))
x11() # Necessary when using RStudio (at least on my machine)
plot(region, main = "")
x <- clickpoly(add = TRUE, col = 2, lwd = 2) # Start locator overlayed on region

然后,您只需左键单击所需的点,并在完成后单击中/右键。这些点存储为owin中的窗口(x)。您可以使用x,y坐标获得list

vertices(x)

答案 1 :(得分:0)

所以我使用了栅格库中的drawPoly(),这里是代码:

library(spatstat)
library(raster)
#load the shapefile
plot(shape)#plot it
points(data.coords,col="green",pch=4)#add data coordenates points
polygon<- drawPoly()#draw a polygon in the window
#take it limits
xlim<-polygon@bbox[1,]
ylim<-polygon@bbox[2,]
new_points<-ppp(data.coords[,1], data.coords[,2], xlim, ylim)#take points inside the polygon

new_points<- data.frame(new_points)#convert to data.frame

plot(shape)
points(new_points ,col="red",pch=4)#plot the selected points in the shape