【问题标题】:R: Match Polygons from Shapefile 1 to Area Codes in shapefile 2R:将 Shapefile 1 中的多边形与 shapefile 2 中的区号匹配
【发布时间】: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


【解决方案1】:

查看:https://gis.stackexchange.com/questions/140504/extracting-intersection-areas-in-r?newreg=033544fa0f5349bcb8167d78867c8073

它为您提供数据集 B 中的哪些 shapefile 与数据集 A 中的 shapefile 重叠,以及 B 的每个 shapefile 中有多少区域存在于目标 shapefile 中。

【讨论】:

    【解决方案2】:

    不幸的是,我在 R 中没有找到答案,但我可以弄清楚如何在 QGIS 中匹配两个独立的 shapefile。

    主要问题:自定义 shapefile 在 .prj 文件中使用地理编码 Google Mercator (EPSG = 900913),而下载的邮政编码 shapefile 使用 EPSG 4326。

    QGIS 不会自动将这些 .prj 文件识别为投影文件。必须手动设置。

    最重要的是:Google Mercator (EPSG = 900913) 已更改为 EPSG= 3857。因此,我必须手动设置自定义 shapefile! – WGS 84/Pseudo-Mercator EPSG = 3857 的 CRS。

    现在我可以右键单击自定义形状图层 -> 另存为...。并将 CRS 更改为 EPSG 4326。因此,新的自定义 shapefile 现在与下载的邮政编码 shapefile 具有相同的投影,并且可以按位置连接。

    (PS:虽然我有手动进行转换的解决方案,但我很想在 R 中执行此操作,因为我需要生成的文件进行分析。)

    【讨论】:

      猜你喜欢
      • 2014-01-30
      • 2012-12-05
      • 1970-01-01
      • 2013-04-19
      • 2021-08-15
      • 2018-04-15
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      相关资源
      最近更新 更多