我正在编写一个脚本,它可以获取在谷歌地球创建的输入文件KML,并在多边形内绘制坐标点网格。
到目前为止,我已经有了多边形输入和一个围绕多边形的点网格,但我希望只有多边形内的点。
我尝试使用over()
函数来实现这一点,但它没有起作用。是否有其他建议?
您可以在此处下载我的测试KML文件HERE。
library(rgdal)
library(sp)
library(maptools)
# ogrInfo() to find layer name... not as labelled in Google Earth?!
my.poly = readOGR(ds = "PolyNYC.kml", layer = "PolyNYC")
proj4string(my.poly) <- "+proj=longlat +datum=WGS84 +no_defs"
# Creating grid of points
grdpts <- makegrid(my.poly)
# Converting from df to spdf
coords = cbind(grdpts$x1, grdpts$x2)
sp = SpatialPoints(coords)
spdf = SpatialPointsDataFrame(coords, grdpts, proj4string = CRS(proj4string(my.poly)))
# Using over() to select only those points in the polygon
inPoly = over(spdf, my.poly)
# This is not working
# Plotting the polygon with the points overlaid.
plot(my.poly)
points(spdf, pch = 3, col = "red")
#kmlPoints(obj = spdf, kmlfile = "BBoxFromPoly.kml", kmlname = "Testing123")
cellsize
参数;但我不完全确定你需要的确切参数。 - SymbolixAU