从 ONS 下载 County Shapefile

Downloading County Shapefile from ONS

我正在尝试从 here 定义的 ONS 下载 Ultra Generalized Clipped Boundaries。我打算用它来显示带有 ggplot 的 Choropleth 地图。

但是,我在使用 readOGR 时收到以下消息。

ogrInfo 错误(dsn = dsn,layer = layer,encoding = encoding,use_iconv = use_iconv,: 无法打开图层

下载和解压缩似乎有效,它以 ESRI Shape 文档的形式出现。

我做错了什么?

library(tidyverse)
library(rgeos)
library(rgdal)
library(maptools)

dest_zip_file <- "Counties_December_2017_Ultra_Generalised_Clipped_Boundaries_in_England.zip"
shape_file_name <- "Counties_December_2017_Ultra_Generalised_Clipped_Boundaries_in_England"

download.file("http://geoportal1-ons.opendata.arcgis.com/datasets/c6404b30a373457e9d87f724dd57585c_4.zip?outSR={%22latestWkid%22:27700,%22wkid%22:27700}",
              dest_zip_file)
unzip(dest_zip_file, paste0(shape_file_name,".shp"))

county_shapes <- readOGR(dsn = ".",
                         layer = shape_file_name,
                         verbose = TRUE)

您正在将文件夹解压缩到一个文件 - .shp - 您实际上应该有几个文件作为 'shape file' 的一部分,包括 .shx 和 .dbf 文件。

您可以通过解压缩所有文件并读入来修复您的代码:

unzip(dest_zip_file)

county_shapes <- readOGR(dsn = ".",
                         layer = shape_file_name,
                         verbose = TRUE)

附带说明一下,如果您正在寻找英国的地理边界,我总是会推荐 geoportal,在那里您经常可以找到 link 以通过 API 下载 geojson。例如,我找到了 2017 年县界的 link 并使用 sf:

读入
library(sf)
library(ggplot2)
county <- read_sf('https://opendata.arcgis.com/datasets/c6404b30a373457e9d87f724dd57585c_2.geojson')
ggplot() +
  geom_sf(data = county)