查找多边形内网格点的坐标
Find coordinates of grid points within polygon
我正在尝试获取定义网格 在多边形 内的一组点的坐标(我有一个 shapefile)。看起来最简单的事情就是创建一个点网格,然后将这些点过滤到仅多边形内的点。我查看了 https://gis.stackexchange.com/questions/133625/checking-if-points-fall-within-polygon-shapefile and 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)
使用splancs::inout()
。
1.获取多边形的轮廓
outline <- mySpatialPolygonsDataFrame@polygons[[2]]@Polygons[[1]]@coords
2。使用 inout() 查找轮廓内的点
library(splancs)
pts_in_polygon <- points[inout(points,outline), ]
您也可以通过简单地用 [
对数据进行子集化来实现这一点,例如 yourPoints[yourPolygons, ]
:
library(raster)
bra <- getData(country = "BRA", level = 1)
pts <- makegrid(bra, 100)
pts <- SpatialPoints(pts, proj4string = CRS(proj4string(bra)))
coordinates(pts[bra, ])
我正在尝试获取定义网格 在多边形 内的一组点的坐标(我有一个 shapefile)。看起来最简单的事情就是创建一个点网格,然后将这些点过滤到仅多边形内的点。我查看了 https://gis.stackexchange.com/questions/133625/checking-if-points-fall-within-polygon-shapefile and 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)
使用splancs::inout()
。
1.获取多边形的轮廓
outline <- mySpatialPolygonsDataFrame@polygons[[2]]@Polygons[[1]]@coords
2。使用 inout() 查找轮廓内的点
library(splancs)
pts_in_polygon <- points[inout(points,outline), ]
您也可以通过简单地用 [
对数据进行子集化来实现这一点,例如 yourPoints[yourPolygons, ]
:
library(raster)
bra <- getData(country = "BRA", level = 1)
pts <- makegrid(bra, 100)
pts <- SpatialPoints(pts, proj4string = CRS(proj4string(bra)))
coordinates(pts[bra, ])