在多边形内找到网格点的坐标

3
我正在尝试获取定义网格的一组点的坐标,这些点位于多边形内部(我有一个shapefile文件)。最简单的方法似乎是创建一个点网格,然后将这些点筛选为多边形内的点。我查看了https://gis.stackexchange.com/questions/133625/checking-if-points-fall-within-polygon-shapefileConvert a shapefile from polygons to points?,根据那里的答案,我尝试了以下操作:
library(rgdal)
city_bdry <- readOGR("Boundaries - City",
                     "geo_export_32ded882-2eab-4eaa-b9da-a18889600a40")
res <- 0.01
bb <- bbox(city_bdry)
gt <- GridTopology(cellcentre.offset = bb[,1], cellsize = c(res, res), 
                   cells.dim = c(diff(bb[,1]), diff(bb[2,])) / res + 1)
pts <- SpatialPoints(gt, proj4string = CRS(proj4string(city_bdry)))
ov <- over(pts, city_bdry)

然而,结果并不包括与多边形重叠的点的实际坐标,所以对我来说没有用。我怎样才能让这些信息包含在数据框中?或者,有没有更简单的方法来实现我想要做的事情?
我使用的shapefile可以从https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-City/ewy2-6yfk下载。

1
使用splancs::inout()。请参考此线程上的最后一个答案:https://dev59.com/h1cQ5IYBdhLWcg3wCfWS#45948442 我之前也遇到过这个问题,inout()是我找到的最简单的解决方案。 - Rich Pauloo
1
@RichPauloo 这解决了我的问题,谢谢。如果你把它作为答案,我会标记为已接受的。 - Empiromancer
很高兴它起作用了!这是一个常见的地理空间数据任务,这是一个例子,其中一个答案并不像我想象的那样容易在网上找到。提取轮廓然后使用inout()是一个两步过程,我想知道是否有人(也许在GIS SE上)在空间包之一中有更简单的一行解决方案。有人吗? - Rich Pauloo
3个回答

3

如果我理解正确,您可以尝试

library(rgdal)
download.file("https://data.cityofchicago.org/api/geospatial/ewy2-6yfk?method=export&format=Shapefile", tf<-tempfile(fileext = ".zip"), mode="wb")
unzip(tf, exdir=dir<-file.path(tempdir(), "Boundaries - City"))
city_bdry <- readOGR(dir, tools::file_path_sans_ext((list.files(dir)[1])))
res <- 0.01
bb <- bbox(city_bdry)
gt <- GridTopology(cellcentre.offset = bb[,1], cellsize = c(res, res), 
                   cells.dim = c(diff(bb[,1]), diff(bb[2,])) / res + 1)
pts <- SpatialPoints(gt, proj4string = CRS(proj4string(city_bdry)))

ov <- sp::over(pts, as(city_bdry, "SpatialPolygons"))
pts_over <- pts[!is.na(ov)]

plot(city_bdry)
points(pts_over)
coordinates(pts_over)

@Empiromancer 嗯,哪个点被重新排序了?我猜如果顺序改变了,plot(city_bdry);points(pts[!is.na(ov)])plot(city_bdry);points(pts[is.na(ov)]) 就不能正常工作了。 - lukeA
我不确定它是否重新排序任何内容,它似乎只是依赖于顺序被保留,并且我想知道这种行为是否由涉及的函数保证。 - Empiromancer
@Empiromancer 没有重新排序。它只是“在多边形内查找网格点的坐标”,正如标题所要求的那样。 - lukeA

2

1
你也可以通过简单地使用[子集数据来实现此目的,例如yourPoints[yourPolygons, ]
library(raster)
bra <- getData(country = "BRA", level = 1)

pts <- makegrid(bra, 100)
pts <- SpatialPoints(pts, proj4string = CRS(proj4string(bra)))

coordinates(pts[bra, ])

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