【问题标题】:Convert a shapefile from polygons to points? [closed]将shapefile从多边形转换为点? [关闭]
【发布时间】:2013-01-24 11:42:46
【问题描述】:

我有一个基于非重叠多边形的shapefile (.shp),它具有很大的空间范围和许多相关属性。 shapefile 以 UTM 投影。我希望将多边形转换为点,这些点在30-m 分辨率网格中间隔开,其中每个点都将保留其所在多边形的属性。

输出只是一个点表:

X, Y, attribute1, attribute2, attribute 3,etc...

理想情况下,我希望在 R 中执行此操作,或者(不太理想)我可以在 Mac 上运行的其他一些免费程序。

【问题讨论】:

  • 只是一个想法,如何创建 30m 分辨率网格(作为点)来覆盖多边形并执行point.in.polygon 以仅拉出位于多边形线上的那些。如果您有任何属性,则可以将它们附加到单独的 df 中?
  • 你有没有尝试过自己做任何事情?

标签: r gis polygon point shapefile


【解决方案1】:

注意:我抛出这个部分是为了了解是否有更优雅的方式来做这些。所以,请空间类型,提出任何改进建议。

(特别是第 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

【讨论】:

  • 这应该已经用光栅包解决了,比如values(rasterize(myshp, raster(myshp), "attribute_name"))
猜你喜欢
  • 2018-06-13
  • 2011-11-11
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
  • 2013-10-03
  • 2013-04-21
相关资源
最近更新 更多