Luk*_*uke 2 gis r point polygon shapefile
我有一个基于非重叠面的多边形shapefile (.shp)
,具有较大的空间范围和许多关联的属性。shapefile投影在UTM中。我想将多边形转换为在30-m
分辨率网格中间隔开的点,其中每个点将保留其位于其中的多边形的属性。
输出将只是要点表:
X, Y, attribute1, attribute2, attribute 3,etc...
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想在R或(较不理想)我可以在Mac上运行的其他一些免费程序中执行此操作。
注意:我将对此进行部分介绍,以了解是否存在一种更优雅的方式来执行任何此操作。因此,请根据空间类型提出改进建议。
(尤其是在步骤2中,它设置了一个"SpatialPoints"
将要提取值的点的网格,这在我看来总是很低级。)
这用于over()
从为此目的构造"SpatialPolygonDataFrame"
的"SpatialPoints"
对象中包含的坐标处提取属性。
library(rgdal)
## (1) Read in an example shapefile
dsn <- system.file("vectors", package = "rgdal")[1]
scot_BNG <- readOGR(dsn=dsn, layer="scot_BNG")
scot_BNG <- scot_BNG[1:5,] # Let's just use part of it
## (2) Set up a SpatialPoints object with the grid of points
## for which you want to extract values
res <- 10000 ## Distance between grid points (30 in OP's question)
BB <- bbox(scot_BNG)
BB <- res*round(BB/res) ## Pretty up the bounding box
GT <- GridTopology(cellcentre.offset = BB[,1],
cellsize = c(res, res),
cells.dim = (c(diff(BB[1,]), diff(BB[2,]))/res) + 1)
SP <- SpatialPoints(GT, proj4string = CRS(proj4string(scot_BNG)))
## (3) Extract the values
vals <- over(SP, scot_BNG)
res <- cbind(coordinates(SP), vals)
## Finally, have a look at a few of the points.
x <- res[!is.na(res$SP_ID),]
rbind(head(x,3), tail(x,3))[1:10]
# x y SP_ID NAME ID_x COUNT SMR LONG LAT PY
# 4 230000 970000 0 Sutherland 12 5 279.3 58.06 4.64 37521
# 5 240000 970000 0 Sutherland 12 5 279.3 58.06 4.64 37521
# 25 220000 960000 0 Sutherland 12 5 279.3 58.06 4.64 37521
# 425 260000 780000 4 Bedenoch 17 2 186.9 57.06 4.09 27075
# 426 270000 780000 4 Bedenoch 17 2 186.9 57.06 4.09 27075
# 427 280000 780000 4 Bedenoch 17 2 186.9 57.06 4.09 27075
Run Code Online (Sandbox Code Playgroud)