在R中将形状文件中的单元合并,同时保持其他单元粒度不变。

huangapple go评论62阅读模式
英文:

Combine units in a shapefile while keeping others granular in R

问题

我有一个包含德国5位邮政编码的形状文件(shapefile)。德国的大部分1位数字邮政编码与德国各州相似。我使用rgdal读取了这个形状文件数据,因此有了一个SpatialPolygonsDataFrame。我只有德国的某些部分的数据,也就是一些邮政编码。我想在5位数字级别上展示我有的数据。使用leaflet创建地图时,对我来说绘制近10,000个邮政编码需要很长时间。因此,我想要在那些没有数据的邮政编码外边界进行“汇总”/“合并”,其中数值为NA

# 德国邮政编码形状

# 创建临时文件
temp <- tempfile()
temp2 <- tempfile()

# 下载zip文件并保存到'temp'中
URL <- "https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip"
download.file(URL, temp)

# 解压缩临时文件的内容并保存解压后的内容到'temp2'中
unzip(zipfile = temp, exdir = temp2)

# 读取形状文件
library(rgdal)
GER_postcode <- readOGR(temp2)

head(GER_postcode@data$note)

# 创建子样本
library(tidyverse)

GER_postcode@data$einwohner2 <- ifelse(substr(GER_postcode@data$plz, 1, 1) %in% c("0", "1", "7"), GER_postcode@data$einwohner, NA)

# 绘制子样本
library(leaflet)

qpal <- colorBin("Reds", GER_postcode@data$einwohner2, bins = 10)

leaflet(GER_postcode) %>%
  addPolygons(stroke = TRUE, opacity = 1, fillOpacity = 0.5, smoothFactor = 0.5,
              color = "black", fillColor = ~qpal(einwohner2), weight = 1) %>%
  addLegend(values = ~einwohner2, pal = qpal, title = "Population")

如何使地图显示那些具有值的邮政编码形状并将所有其他值为NA的邮政编码合并起来?

在R中将形状文件中的单元合并,同时保持其他单元粒度不变。

我看了一下library(rgeos)gUnaryUnion(),它们可以将形状文件中的所有单位合并到外边界。尽管我只需要在子集上执行此操作。


<details>
<summary>英文:</summary>

 I have a 5-digit postcode shapefile for Germany. Big number 1-digits postcodes are similar to German states. I read shapefile data with `rgdal` thus having a `SpatialPolygonsDataFrame`. I have only data for some part of Germany, i.e. some postcodes. The data I have I like to show on a granular 5-digit level. Using `leaflet` to create a map, it makes very long time for me, to plot all of the almost 10.000 postcodes. Thus I like to &quot;summarize&quot;/&quot;combine&quot;/&quot;merge&quot; the outer border of those postcodes where I don&#39;t have data (where the value is `NA`). 

    # German postcode shapes 
    
    # Create temp files
    temp &lt;- tempfile()
    temp2 &lt;- tempfile()
    
    # Download the zip file and save to &#39;temp&#39; 
    URL &lt;- &quot;https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip&quot;
    download.file(URL, temp)
    
    # Unzip the contents of the temp and save unzipped content in &#39;temp2&#39;
    unzip(zipfile = temp, exdir = temp2)
    
    # Read shape file 
    library(rgdal)
    GER_postcode &lt;- readOGR(temp2)
    
    head(GER_postcode@data$note)
    
    # Create subsample 
    library(tidyverse)
    
    GER_postcode@data$einwohner2 &lt;- ifelse(substr(GER_postcode@data$plz, 1, 1) %in% c(&quot;0&quot;, &quot;1&quot;, &quot;7&quot;), GER_postcode@data$einwohner, NA)
    
    # Plot Subsample 
    library(leaflet)
    
    qpal &lt;- colorBin(&quot;Reds&quot;, GER_postcode@data$einwohner2, bins=10)
    
    leaflet(GER_postcode) %&gt;%
      addPolygons(stroke = TRUE,opacity = 1,fillOpacity = 0.5, smoothFactor = 0.5,
                  color=&quot;black&quot;,fillColor = ~qpal(einwohner2),weight = 1) %&gt;%
      addLegend(values=~einwohner2,pal=qpal,title=&quot;Population&quot;)

How can I make the map show those postcode shapes with values and combine all other where the value is `NA`? 

[![enter image description here][1]][1]

I was looking at `library(rgeos)` and `gUnaryUnion()` that units all units in a shapefile to outer borders. Although I only need to do this on a subset.

  [1]: https://i.stack.imgur.com/Tkcx7.png

</details>


# 答案1
**得分**: 2

我个人会使用过滤器/子集来删除不相关的多边形,并将国家轮廓用作单独的背景层。轮廓可以通过将邮政编码形状联合起来来构建,虽然下载一个(或安装一些捆绑国家形状的软件包)可能更快。

由于“rgdal”和“regos”将在几个月内停用,下面的示例使用“sf”(以及可选的“geos”软件包)。

```r
library(sf)
#&gt; Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(dplyr)
library(leaflet)

URL &lt;- "https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip"

# 使用GDAL虚拟文件系统从远程URL加载压缩的shapefile
GER_postcode &lt;- paste0("/vsizip//vsicurl/", URL) %&gt;%  read_sf()

# 从giscoR获取国家轮廓
GER_outline &lt;- giscoR::gisco_get_countries(country = "DE")

# 如果必须从输入形状构建它,我们可以使用sf::st_union()或切换到geos(R库),它在这种情况下表现得更好:

# GER_outline &lt;- as_geos_geometry(GER_postcode) %&gt;% 
#   geos_make_collection() %&gt;% 
#   geos_unary_union() %&gt;% 
#   st_as_sfc()

GER_postcode_subsample &lt;- GER_postcode %&gt;% filter(substr(plz, 1, 1) %in% c(0, 1, 7))

# 绘制子样本
qpal &lt;- colorBin("Reds", GER_postcode_subsample$einwohner, bins=10)

leaflet(GER_postcode_subsample) %&gt;%
  addPolygons(data = GER_outline, smoothFactor = 0.5,
              color="black", fillColor = "#808080", weight = 1) %&gt;% 
  addPolygons(stroke = TRUE,opacity = 1,fillOpacity = 0.5, smoothFactor = 0.5,
              color="black",fillColor = ~qpal(einwohner),weight = .2) %&gt;%
  # 将NA附加到值以包含在图例中
  addLegend(values=c(~einwohner, NA),pal=qpal,title="Population")

在R中将形状文件中的单元合并,同时保持其他单元粒度不变。<!-- -->

<sup>创建于2023年7月17日,使用reprex v2.0.2</sup>

英文:

I personally would use filter / subset to drop irrelevant polygons and use country outline as a separate background layer. Outline can be built by making an union of postcode shapes, though it's likely faster to download one (or install some package that bundles country shapes)

As rgdal and regos will be retired in just few months, the example below uses sf (and optional geos package ) instead.

library(sf)
#&gt; Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(dplyr)
library(leaflet)

URL &lt;- &quot;https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip&quot;

# use GDAL virtual file systems to load zipped shapefile from remote url
GER_postcode &lt;- paste0(&quot;/vsizip//vsicurl/&quot;, URL) %&gt;%  read_sf()

# country outline from giscoR
GER_outline &lt;- giscoR::gisco_get_countries(country = &quot;DE&quot;)

# if it must be built from input shapes, we can use sf::st_union() or switch  
# to geos (the R library), it performs bit better in this case:

# GER_outline &lt;- as_geos_geometry(GER_postcode) %&gt;% 
#   geos_make_collection() %&gt;% 
#   geos_unary_union() %&gt;% 
#   st_as_sfc()

GER_postcode_subsample &lt;- GER_postcode %&gt;% filter(substr(plz, 1, 1) %in% c(0, 1, 7))

# Plot Subsample 
qpal &lt;- colorBin(&quot;Reds&quot;, GER_postcode_subsample$einwohner, bins=10)

leaflet(GER_postcode_subsample) %&gt;%
  addPolygons(data = GER_outline, smoothFactor = 0.5,
              color=&quot;black&quot;, fillColor = &quot;#808080&quot;, weight = 1) %&gt;% 
  addPolygons(stroke = TRUE,opacity = 1,fillOpacity = 0.5, smoothFactor = 0.5,
              color=&quot;black&quot;,fillColor = ~qpal(einwohner),weight = .2) %&gt;%
  # append NA to values to include it in legend
  addLegend(values=c(~einwohner, NA),pal=qpal,title=&quot;Population&quot;)

在R中将形状文件中的单元合并,同时保持其他单元粒度不变。<!-- -->

<sup>Created on 2023-07-17 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年7月17日 21:55:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定