我正在尝试获取定义多边形内 网格的一组点的坐标(我有一个 shapefile)。看起来最简单的事情就是创建一个点网格,然后将这些点过滤到仅多边形内的点。我看了https://gis.stackexchange.com/questions/133625/checking-if-points-fall-within-polygon-shapefile和 Convert 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 下载
最佳答案
如果我没猜错,你可以试试
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)
关于r - 查找多边形内网格点的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46061395/