【发布时间】:2016-08-27 07:07:40
【问题描述】:
有人问我 R 是否可以使用 shapefile - 我自己以前从未使用过 shapefile,但我敢肯定,其他人一定遇到过这种问题!
我有两个 shapefile: a) shapefile 1 (PolygonSamples.shp) 包含分布在德国各地的多边形列表(附件是样本)。多边形可能小于、等于或大于一个邮政编码多边形的多边形。
b) shapefile 2 列出了德国邮政编码,可从以下网址下载 https://blog.oraylis.de/2010/05/german-map-spatial-data-for-plz-postal-code-regions/
现在的问题是: 如何“匹配”这两个 shapefile 以获得一个数据框,其中列出 shapefile 1 中的哪个多边形与 shapefile 2 的哪些邮政编码匹配。理想情况下,结果如下所示
Polygon ID (shapefile 1) Postal Code (shapefile 2)
1 80995
2 80997
2 80999
3 81247
我发现的所有内容都与我的问题不符。 例如From a shapefile with polygons/areas, and points (lat,lon), figure out which polygon/area each point belongs to? In R 似乎很接近,但我无法获得所需的数据帧(或数据表)输出。
library(maps)
library(maptools)
# Polygons
tmp_dir <- "C:/Users/.../"
polygons <- readShapeSpatial(sprintf('%s/polygons.shp', tmp_dir)
plot(polygons)
# Postal codes
dir <- "C:/Users/..../"
postcode <- readShapeSpatial(sprintf('%s/post_pl.shp', dir)
plot(postcode)
缺失的代码片段会读到类似
result_table <- match(polygons_ID, postcode,
data1= polygon, data2 = postcode,
by = "coordinates in the shapefile"
shapefile (.shp) 中的多边形样本,包括。可以发送其他空间文件(.dbf、.prj、.qpj、.shx)。
非常感谢任何帮助!
PS:R 版本 3.2.3,64 位,Windows 7 上的 RStudio
【问题讨论】:
-
使用函数
over来自library(rgeos)
标签: r maps shapefile postal-code