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

时间:2017-09-05 18:40:16

标签: 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)

然而,结果并不包括与多边形重叠的点的实际坐标,因此对我来说没用。如何将这些信息包含在数据框中?或者,有没有更简单的方法来做我想做的事情?

我可以从https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-City/ewy2-6yfk

下载我使用的shapefile

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分:2)

使用splancs::inout()

<强> 1。获取多边形的轮廓

outline <- mySpatialPolygonsDataFrame@polygons[[2]]@Polygons[[1]]@coords

<强> 2。使用inout()查找大纲中的哪些点

library(splancs)
pts_in_polygon <- points[inout(points,outline), ]

Note: very similar to the answer I provide to create an irregularly shaped grid (especially for kriging.)

答案 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, ])