英文:
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
的邮政编码合并起来?
我看了一下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 "summarize"/"combine"/"merge" the outer border of those postcodes where I don't have data (where the value is `NA`).
# German postcode shapes
# Create temp files
temp <- tempfile()
temp2 <- tempfile()
# Download the zip file and save to 'temp'
URL <- "https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip"
download.file(URL, temp)
# Unzip the contents of the temp and save unzipped content in 'temp2'
unzip(zipfile = temp, exdir = temp2)
# Read shape file
library(rgdal)
GER_postcode <- readOGR(temp2)
head(GER_postcode@data$note)
# Create subsample
library(tidyverse)
GER_postcode@data$einwohner2 <- ifelse(substr(GER_postcode@data$plz, 1, 1) %in% c("0", "1", "7"), GER_postcode@data$einwohner, NA)
# Plot Subsample
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")
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)
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(dplyr)
library(leaflet)
URL <- "https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip"
# 使用GDAL虚拟文件系统从远程URL加载压缩的shapefile
GER_postcode <- paste0("/vsizip//vsicurl/", URL) %>% read_sf()
# 从giscoR获取国家轮廓
GER_outline <- giscoR::gisco_get_countries(country = "DE")
# 如果必须从输入形状构建它,我们可以使用sf::st_union()或切换到geos(R库),它在这种情况下表现得更好:
# GER_outline <- as_geos_geometry(GER_postcode) %>%
# geos_make_collection() %>%
# geos_unary_union() %>%
# st_as_sfc()
GER_postcode_subsample <- GER_postcode %>% filter(substr(plz, 1, 1) %in% c(0, 1, 7))
# 绘制子样本
qpal <- colorBin("Reds", GER_postcode_subsample$einwohner, bins=10)
leaflet(GER_postcode_subsample) %>%
addPolygons(data = GER_outline, smoothFactor = 0.5,
color="black", fillColor = "#808080", weight = 1) %>%
addPolygons(stroke = TRUE,opacity = 1,fillOpacity = 0.5, smoothFactor = 0.5,
color="black",fillColor = ~qpal(einwohner),weight = .2) %>%
# 将NA附加到值以包含在图例中
addLegend(values=c(~einwohner, NA),pal=qpal,title="Population")
<!-- -->
<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)
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(dplyr)
library(leaflet)
URL <- "https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip"
# use GDAL virtual file systems to load zipped shapefile from remote url
GER_postcode <- paste0("/vsizip//vsicurl/", URL) %>% read_sf()
# country outline from giscoR
GER_outline <- giscoR::gisco_get_countries(country = "DE")
# 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 <- as_geos_geometry(GER_postcode) %>%
# geos_make_collection() %>%
# geos_unary_union() %>%
# st_as_sfc()
GER_postcode_subsample <- GER_postcode %>% filter(substr(plz, 1, 1) %in% c(0, 1, 7))
# Plot Subsample
qpal <- colorBin("Reds", GER_postcode_subsample$einwohner, bins=10)
leaflet(GER_postcode_subsample) %>%
addPolygons(data = GER_outline, smoothFactor = 0.5,
color="black", fillColor = "#808080", weight = 1) %>%
addPolygons(stroke = TRUE,opacity = 1,fillOpacity = 0.5, smoothFactor = 0.5,
color="black",fillColor = ~qpal(einwohner),weight = .2) %>%
# append NA to values to include it in legend
addLegend(values=c(~einwohner, NA),pal=qpal,title="Population")
<!-- -->
<sup>Created on 2023-07-17 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论