r - 查找多边形内网格点的坐标

标签 r gis geospatial

我正在尝试获取定义多边形 网格的一组点的坐标(我有一个 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 下载

最佳答案

如果我没猜错,你可以试试

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/

相关文章:

xml - 获取 YouTube 视频的名称

r - 创建名称数量不断增加的随机数据框列表

php - 使用 MySQL 空间字段查找地理围栏(圆圈)内的记录

mysql - 匹配最近的经度/纬度

iphone - 为 MapKit 转换 "world file"GIS?

geometry - 如何最好地在一组纬度/经度坐标上执行走廊范围搜索

r - 散点图 : Error in FUN(X[[i]], ...):找不到对象 'Group'

r - 在 Shiny 中导入和访问大型数据文件

r - 使用 ggplot() 根据 R 中的 ISO3 代码为世界地图上的国家着色

javascript - 开放层 3 : "movestart" event on map