简体   繁体   English

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

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

I Would like to know if there is a way to set a polygon interactively in a plot spatial data image and get It's coordinates. 我想知道是否有一种方法可以在绘图空间数据图像中交互式设置多边形并获取其坐标。 I know there is the locator() function, but I would like to set a circles or square and get it coordinates. 我知道有locator()函数,但我想设置一个圆形或正方形并获取其坐标。

The question has been updated so maybe I better understand the aim now. 这个问题已经更新,所以也许我现在更好地了解了目标。 You want to plot a region with a lot of boundaries and then click a polygon on top? 您想绘制一个边界很多的区域,然后单击顶部的多边形吗? With the shape file provided in the comment you can do: 使用注释中提供的形状文件,您可以执行以下操作:

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

Then you just left click the points you need and middle/right click when you are done. 然后,只需单击鼠标左键,然后单击鼠标中键即可。 The points are stored as a window ( owin ) in x . 这些点在x中存储为窗口( owin )。 You can get a list with the x,y coordinates: 您可以获得带有x,y坐标的list

vertices(x)

So I had used drawPoly() from the raster library, here is the code: 所以我使用了来自栅格库的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