繁体   English   中英

如何创建多边形并使其与R中的空间数据协调

[英]how to create a polygon and get it coordinates with spatial data in R

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

这个问题已经更新,所以也许我现在更好地了解了目标。 您想绘制一个边界很多的区域,然后单击顶部的多边形吗? 使用注释中提供的形状文件,您可以执行以下操作:

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

然后,只需单击鼠标左键,然后单击鼠标中键即可。 这些点在x中存储为窗口( owin )。 您可以获得带有x,y坐标的list

vertices(x)

所以我使用了来自栅格库的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM